Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor code for deposit [SF-962] #74

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -141,3 +136,10 @@ type Transfer @entity {
blockNumber: BigInt!
txHash: Bytes!
}

type Deposit @entity {
id: ID!
user: User!
currency: Bytes!
amount: BigInt!
}
32 changes: 12 additions & 20 deletions src/helper/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
User,
Liquidation,
Transfer,
Deposit,
} from '../../generated/schema';
import { getDailyVolumeEntityId } from '../utils/id-generation';
import { buildLendingMarketId } from '../utils/string';
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
};
44 changes: 27 additions & 17 deletions test/token-vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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()
);
});
});