Skip to content

Commit 8224ba3

Browse files
authored
Fix: UserEMode user creation and relations (#33)
* fix: create user if not exists on eModeSwitch event * feat: add transaction data on user e mode * fix: make row of userEMode unique per protocol * chore: update user e mode docs
1 parent 0ff51c2 commit 8224ba3

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

relations.schema.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const protocolRelations = relations(protocol, ({ one, many }) => ({
3333
delegatesUpdated: many(delegateUpdated),
3434
delegates: many(userDelegation),
3535
eModes: many(eMode),
36+
userEModes: many(userEMode),
3637
}));
3738

3839
export const actionPausedRelations = relations(actionPaused, ({ one }) => ({
@@ -323,6 +324,14 @@ export const userEModeRelations = relations(userEMode, ({ one }) => ({
323324
fields: [userEMode.userId],
324325
references: [user.id],
325326
}),
327+
transaction: one(transaction, {
328+
fields: [userEMode.transactionId],
329+
references: [transaction.id],
330+
}),
331+
protocol: one(protocol, {
332+
fields: [userEMode.protocolId],
333+
references: [protocol.id],
334+
}),
326335
}));
327336

328337
export const priceSnapshotRelations = relations(priceSnapshot, ({ one }) => ({

src/riskEngine.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,28 @@ async function handleEModeSwitched({
439439
const protocolId = getAddressId(context.network.chainId, event.log.address);
440440
const eModeId = getEModeId(protocolId, event.args.newCategory);
441441
const chainId = BigInt(context.network.chainId);
442-
const userEModeId = getUserEModeId(event.args.account, eModeId);
442+
const transactionId = getTransactionId(event, context);
443+
const userEModeId = getUserEModeId(event.args.account, protocolId);
443444

444445
const params = {
445446
chainId,
446447
eModeId,
448+
transactionId,
447449
userId: event.args.account,
450+
protocolId,
448451
};
449452

450-
await context.db
451-
.insert(userEMode)
452-
.values({
453-
id: userEModeId,
454-
...params,
455-
})
456-
.onConflictDoUpdate(params);
453+
await Promise.all([
454+
context.db
455+
.insert(userEMode)
456+
.values({
457+
id: userEModeId,
458+
...params,
459+
})
460+
.onConflictDoUpdate(params),
461+
createIfNotExistsTransaction(event, context),
462+
createIfNotExistsUser(context, event.args.account),
463+
]);
457464
}
458465

459466
async function handleNewCloseFactor({

src/utils/docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const docs = extendWithBaseDefinitions({
137137

138138
...generateTypeDocSet('userEMode', 'user e-mode settings', {
139139
'userEMode.id':
140-
'Unique identifier for the user e-mode composed by <userAddress>-<eModeCategoryId>-<chainId>',
140+
'Unique identifier for the user e-mode composed by <userAddress>-<riskEngineAddress>-<chainId>',
141141
'userEMode.chainId': 'The blockchain network identifier',
142142
'userEMode.userId': 'User identifier',
143143
'userEMode.eModeId': 'E-mode identifier',

src/utils/id.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function getPTokenEModeId(pTokenId: string, eModeId: string) {
3636
return `${pTokenAddress}-${eModeId}`;
3737
}
3838

39-
export function getUserEModeId(userAddress: string, eModeId: string) {
40-
return `${userAddress}-${eModeId}`;
39+
export function getUserEModeId(userAddress: string, protocolId: string) {
40+
// With that we only saves the current eMode for each protocol
41+
return `${userAddress}-${protocolId}`;
4142
}

tables.schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ export const userEMode = onchainTable(
152152
chainId: t.bigint().notNull(),
153153
userId: t.text().notNull(),
154154
eModeId: t.text().notNull(),
155+
transactionId: t.text().notNull(),
156+
protocolId: t.text().notNull(),
155157
}),
156158
table => ({
157159
userIdx: index().on(table.userId),

0 commit comments

Comments
 (0)