From 90e5e94e649d628d7773a65ec18151ce711fd699 Mon Sep 17 00:00:00 2001 From: NetFreak26 Date: Mon, 18 Dec 2023 19:44:22 +0530 Subject: [PATCH] refactor: refactor code for deposit [SF-962] --- schema.graphql | 14 +++++++------ src/helper/initializer.ts | 32 +++++++++++----------------- test/token-vault.test.ts | 44 ++++++++++++++++++++++++--------------- 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/schema.graphql b/schema.graphql index 4edbbcd..9e1dd88 100644 --- a/schema.graphql +++ b/schema.graphql @@ -88,12 +88,7 @@ type User @entity { liquidations: [Liquidation!]! @derivedFrom(field: "user") transferCount: BigInt! transfers: [Transfer!]! @derivedFrom(field: "user") - depositETH: BigInt! - depositUSDC: BigInt! - depositWBTC: BigInt! - depositWFIL: BigInt! - depositAUSDC: BigInt! - depositAXLFIL: BigInt! + deposits: [Deposit!]! @derivedFrom(field: "user") } type DailyVolume @entity { @@ -141,3 +136,10 @@ type Transfer @entity { blockNumber: BigInt! txHash: Bytes! } + +type Deposit @entity { + id: ID! + user: User! + currency: Bytes! + amount: BigInt! +} diff --git a/src/helper/initializer.ts b/src/helper/initializer.ts index 831a9df..7004570 100644 --- a/src/helper/initializer.ts +++ b/src/helper/initializer.ts @@ -14,6 +14,7 @@ import { User, Liquidation, Transfer, + Deposit, } from '../../generated/schema'; import { getDailyVolumeEntityId } from '../utils/id-generation'; import { buildLendingMarketId } from '../utils/string'; @@ -63,12 +64,6 @@ export const getOrInitUser = (address: Bytes, createdAt: BigInt): User => { user.orderCount = BigInt.fromI32(0); user.liquidationCount = BigInt.fromI32(0); user.transferCount = BigInt.fromI32(0); - user.depositETH = BigInt.fromI32(0); - user.depositUSDC = BigInt.fromI32(0); - user.depositWBTC = BigInt.fromI32(0); - user.depositWFIL = BigInt.fromI32(0); - user.depositAUSDC = BigInt.fromI32(0); - user.depositAXLFIL = BigInt.fromI32(0); user.createdAt = createdAt; user.save(); @@ -244,22 +239,19 @@ export const initTransfer = ( transfer.save(); const currencyString = currency.toString(); - - if (currencyString == 'ETH') { - user.depositETH = user.depositETH.plus(amount); - } else if (currencyString == 'USDC') { - user.depositUSDC = user.depositUSDC.plus(amount); - } else if (currencyString == 'WBTC') { - user.depositWBTC = user.depositWBTC.plus(amount); - } else if (currencyString == 'WFIL') { - user.depositWFIL = user.depositWFIL.plus(amount); - } else if (currencyString == 'aUSDC') { - user.depositAUSDC = user.depositAUSDC.plus(amount); - } else if (currencyString == 'axlFIL') { - user.depositAXLFIL = user.depositAXLFIL.plus(amount); + const depositID = user.id + ':' + currencyString; + let deposit = Deposit.load(depositID); + if (!deposit) { + deposit = new Deposit(depositID); + deposit.user = user.id; + deposit.currency = currency; + deposit.amount = amount; + deposit.save(); } else { - log.error('Invalid currency: ', currencyString.split(',')); + deposit.amount = deposit.amount.plus(amount); + deposit.save(); } + user.transferCount = user.transferCount.plus(BigInt.fromI32(1)); user.save(); }; diff --git a/test/token-vault.test.ts b/test/token-vault.test.ts index 706a9b6..0d62a93 100644 --- a/test/token-vault.test.ts +++ b/test/token-vault.test.ts @@ -34,12 +34,9 @@ describe('Deposit & Withdraw', () => { assert.entityCount('User', 1); assert.fieldEquals('Protocol', 'ethereum', 'totalUsers', '1'); assert.fieldEquals('User', ALICE.toHexString(), 'transferCount', '1'); - assert.fieldEquals( - 'User', - ALICE.toHexString(), - 'depositETH', - amount.toString() - ); + + const depositID = ALICE.toHexString() + ':' + ccy.toString(); + assert.fieldEquals('Deposit', depositID, 'amount', amount.toString()); assert.entityCount('Transfer', 1); assert.fieldEquals('Transfer', id, 'user', ALICE.toHexString()); @@ -104,7 +101,7 @@ describe('Deposit & Withdraw', () => { assert.fieldEquals('Transfer', id, 'transferType', 'Withdraw'); }); - test('Should increment the deposit amount for each currency', () => { + test('Should create deposit for currency', () => { const amount1 = BigInt.fromI32(10000000); const amount2 = BigInt.fromI32(20000000); const amount3 = BigInt.fromI32(50000000); @@ -120,24 +117,37 @@ describe('Deposit & Withdraw', () => { handleDeposit(event2); handleDeposit(event3); - assert.fieldEquals('User', ALICE.toHexString(), 'depositETH', '0'); + assert.notInStore( + 'Deposit', + ALICE.toHexString() + ':' + ccy.toString() + ); assert.fieldEquals( - 'User', - ALICE.toHexString(), - 'depositWFIL', + 'Deposit', + ALICE.toHexString() + ':' + wfil.toString(), + 'amount', amount1.toString() ); assert.fieldEquals( - 'User', - ALICE.toHexString(), - 'depositUSDC', + 'Deposit', + ALICE.toHexString() + ':' + usdc.toString(), + 'amount', amount2.toString() ); assert.fieldEquals( - 'User', - ALICE.toHexString(), - 'depositAXLFIL', + 'Deposit', + ALICE.toHexString() + ':' + axlFIL.toString(), + 'amount', amount3.toString() ); + + const event4 = createDepositEvent(ALICE, axlFIL, amount2); + handleDeposit(event4); + + assert.fieldEquals( + 'Deposit', + ALICE.toHexString() + ':' + axlFIL.toString(), + 'amount', + (amount3 + amount2).toString() + ); }); });