From f523748e7b784713844c99d70e61b6d7cbf4d3a4 Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Mon, 18 Sep 2023 22:58:18 -0300 Subject: [PATCH] refactor: handle supply event --- subgraphs/isolated-pools/schema.graphql | 2 - subgraphs/isolated-pools/src/mappings/pool.ts | 19 +--- .../isolated-pools/src/mappings/vToken.ts | 23 ++++- .../src/operations/getOrCreate.ts | 2 - .../isolated-pools/src/operations/update.ts | 38 +++++--- .../src/operations/updateOrCreate.ts | 8 +- .../isolated-pools/tests/VToken/index.test.ts | 95 ++++++++++++++++++- .../queries/accountVTokenByAccount.graphql | 1 - .../accountVTokenByAccountAndMarket.graphql | 1 - .../queries/accountVTokens.graphql | 1 - 10 files changed, 139 insertions(+), 51 deletions(-) diff --git a/subgraphs/isolated-pools/schema.graphql b/subgraphs/isolated-pools/schema.graphql index abe70f5e..32b10573 100644 --- a/subgraphs/isolated-pools/schema.graphql +++ b/subgraphs/isolated-pools/schema.graphql @@ -167,8 +167,6 @@ type AccountVToken @entity { id: ID! "Relation to market" market: Market! - "Symbol of the vToken" - symbol: String! "Relation to user" account: Account! "Transactions data" diff --git a/subgraphs/isolated-pools/src/mappings/pool.ts b/subgraphs/isolated-pools/src/mappings/pool.ts index ac174a07..32999fc0 100644 --- a/subgraphs/isolated-pools/src/mappings/pool.ts +++ b/subgraphs/isolated-pools/src/mappings/pool.ts @@ -27,20 +27,12 @@ import { import Box from '../utilities/box'; export function handleMarketEntered(event: MarketEntered): void { - const poolAddress = event.address; const vTokenAddress = event.params.vToken; const accountAddress = event.params.account; - const market = getOrCreateMarket(vTokenAddress, poolAddress); getOrCreateAccount(accountAddress); - updateOrCreateAccountVToken( - accountAddress, - vTokenAddress, - market.symbol, - event.block.number, - new Box(true), - ); + updateOrCreateAccountVToken(accountAddress, vTokenAddress, event.block.number, new Box(true)); getOrCreateAccountVTokenTransaction( accountAddress, event.transaction.hash, @@ -55,16 +47,9 @@ export function handleMarketExited(event: MarketExited): void { const vTokenAddress = event.params.vToken; const accountAddress = event.params.account; - const market = getOrCreateMarket(vTokenAddress); getOrCreateAccount(accountAddress); - updateOrCreateAccountVToken( - accountAddress, - vTokenAddress, - market.symbol, - event.block.number, - new Box(false), - ); + updateOrCreateAccountVToken(accountAddress, vTokenAddress, event.block.number, new Box(false)); getOrCreateAccountVTokenTransaction( accountAddress, event.transaction.hash, diff --git a/subgraphs/isolated-pools/src/mappings/vToken.ts b/subgraphs/isolated-pools/src/mappings/vToken.ts index 5815eaef..db16ba13 100644 --- a/subgraphs/isolated-pools/src/mappings/vToken.ts +++ b/subgraphs/isolated-pools/src/mappings/vToken.ts @@ -28,6 +28,7 @@ import { getOrCreateAccount, getOrCreateMarket } from '../operations/getOrCreate import { updateAccountVTokenBorrow, updateAccountVTokenRepayBorrow, + updateAccountVTokenSupply, updateAccountVTokenTransferFrom, updateAccountVTokenTransferTo, updateMarket, @@ -54,6 +55,15 @@ export function handleMint(event: Mint): void { // we read the current total amount of supplied tokens by this account in the market const suppliedTotal = event.params.accountBalance; + updateAccountVTokenSupply( + vTokenAddress, + event.params.minter, + event.transaction.hash, + event.block.timestamp, + event.block.number, + event.logIndex, + suppliedTotal, + ); if (suppliedTotal == event.params.mintTokens) { // and if they are the same, it means it's a new supplier market.supplierCount = market.supplierCount.plus(oneBigInt); @@ -80,6 +90,15 @@ export function handleRedeem(event: Redeem): void { // we read the account's balance and... const currentBalance = event.params.accountBalance; + updateAccountVTokenSupply( + vTokenAddress, + event.params.redeemer, + event.transaction.hash, + event.block.timestamp, + event.block.number, + event.logIndex, + currentBalance, + ); if (currentBalance == zeroBigInt32) { // if the current balance is 0 then the user has withdrawn all their assets from this market market.supplierCount = market.supplierCount.minus(oneBigInt); @@ -102,7 +121,6 @@ export function handleBorrow(event: Borrow): void { updateAccountVTokenBorrow( vTokenAddress, - market.symbol, event.params.borrower, event.transaction.hash, event.block.timestamp, @@ -141,7 +159,6 @@ export function handleRepayBorrow(event: RepayBorrow): void { updateAccountVTokenRepayBorrow( vTokenAddress, - market.symbol, event.params.borrower, event.transaction.hash, event.block.timestamp, @@ -234,7 +251,6 @@ export function handleTransfer(event: Transfer): void { updateAccountVTokenTransferFrom( vTokenAddress, - market.symbol, accountFromAddress, event.transaction.hash, event.block.timestamp, @@ -256,7 +272,6 @@ export function handleTransfer(event: Transfer): void { updateAccountVTokenTransferTo( vTokenAddress, - market.symbol, accountToAddress, event.transaction.hash, event.block.timestamp, diff --git a/subgraphs/isolated-pools/src/operations/getOrCreate.ts b/subgraphs/isolated-pools/src/operations/getOrCreate.ts index 99a583dc..b8772029 100644 --- a/subgraphs/isolated-pools/src/operations/getOrCreate.ts +++ b/subgraphs/isolated-pools/src/operations/getOrCreate.ts @@ -86,7 +86,6 @@ export const getOrCreateAccountVTokenTransaction = ( }; export const getOrCreateAccountVToken = ( - marketSymbol: string, accountAddress: Address, marketAddress: Address, enteredMarket: boolean = false, // eslint-disable-line @typescript-eslint/no-inferrable-types @@ -95,7 +94,6 @@ export const getOrCreateAccountVToken = ( let accountVToken = AccountVToken.load(accountVTokenId); if (!accountVToken) { accountVToken = new AccountVToken(accountVTokenId); - accountVToken.symbol = marketSymbol; accountVToken.account = accountAddress.toHexString(); accountVToken.market = marketAddress.toHexString(); accountVToken.enteredMarket = enteredMarket; diff --git a/subgraphs/isolated-pools/src/operations/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index 8ddae88a..66a01798 100644 --- a/subgraphs/isolated-pools/src/operations/update.ts +++ b/subgraphs/isolated-pools/src/operations/update.ts @@ -19,7 +19,6 @@ import { getOrCreatePool } from './getOrCreate'; const updateAccountVToken = ( marketAddress: Address, - marketSymbol: string, accountAddress: Address, txHash: Bytes, timestamp: BigInt, @@ -27,12 +26,7 @@ const updateAccountVToken = ( logIndex: BigInt, ): AccountVToken => { getOrCreateAccount(accountAddress); - const accountVToken = getOrCreateAccountVToken( - marketSymbol, - accountAddress, - marketAddress, - false, - ); + const accountVToken = getOrCreateAccountVToken(accountAddress, marketAddress, false); getOrCreateAccountVTokenTransaction( accountAddress, txHash, @@ -45,9 +39,30 @@ const updateAccountVToken = ( return accountVToken as AccountVToken; }; +export const updateAccountVTokenSupply = ( + marketAddress: Address, + accountAddress: Address, + txHash: Bytes, + timestamp: BigInt, + blockNumber: BigInt, + logIndex: BigInt, + accountSupplyBalanceMantissa: BigInt, +): AccountVToken => { + const accountVToken = updateAccountVToken( + marketAddress, + accountAddress, + txHash, + timestamp, + blockNumber, + logIndex, + ); + accountVToken.accountSupplyBalanceMantissa = accountSupplyBalanceMantissa; + accountVToken.save(); + return accountVToken as AccountVToken; +}; + export const updateAccountVTokenBorrow = ( marketAddress: Address, - marketSymbol: string, accountAddress: Address, txHash: Bytes, timestamp: BigInt, @@ -58,7 +73,6 @@ export const updateAccountVTokenBorrow = ( ): AccountVToken => { const accountVToken = updateAccountVToken( marketAddress, - marketSymbol, accountAddress, txHash, timestamp, @@ -73,7 +87,6 @@ export const updateAccountVTokenBorrow = ( export const updateAccountVTokenRepayBorrow = ( marketAddress: Address, - marketSymbol: string, accountAddress: Address, txHash: Bytes, timestamp: BigInt, @@ -84,7 +97,6 @@ export const updateAccountVTokenRepayBorrow = ( ): AccountVToken => { const accountVToken = updateAccountVToken( marketAddress, - marketSymbol, accountAddress, txHash, timestamp, @@ -99,7 +111,6 @@ export const updateAccountVTokenRepayBorrow = ( export const updateAccountVTokenTransferFrom = ( marketAddress: Address, - marketSymbol: string, accountAddress: Address, txHash: Bytes, timestamp: BigInt, @@ -121,7 +132,6 @@ export const updateAccountVTokenTransferFrom = ( const accountVToken = updateAccountVToken( marketAddress, - marketSymbol, accountAddress, txHash, timestamp, @@ -137,7 +147,6 @@ export const updateAccountVTokenTransferFrom = ( export const updateAccountVTokenTransferTo = ( marketAddress: Address, - marketSymbol: string, accountAddress: Address, txHash: Bytes, timestamp: BigInt, @@ -146,7 +155,6 @@ export const updateAccountVTokenTransferTo = ( ): AccountVToken => { const accountVToken = updateAccountVToken( marketAddress, - marketSymbol, accountAddress, txHash, timestamp, diff --git a/subgraphs/isolated-pools/src/operations/updateOrCreate.ts b/subgraphs/isolated-pools/src/operations/updateOrCreate.ts index 53fa35b4..02ba2fcf 100644 --- a/subgraphs/isolated-pools/src/operations/updateOrCreate.ts +++ b/subgraphs/isolated-pools/src/operations/updateOrCreate.ts @@ -9,7 +9,6 @@ import { getOrCreateAccountVToken } from './getOrCreate'; export const updateOrCreateAccountVToken = ( accountAddress: Address, marketAddress: Address, - marketSymbol: string, blockNumber: BigInt, enteredMarket: Box | null = null, ): AccountVToken => { @@ -18,12 +17,7 @@ export const updateOrCreateAccountVToken = ( enteredMarketBool = enteredMarket.value; } - const accountVToken = getOrCreateAccountVToken( - marketSymbol, - accountAddress, - marketAddress, - enteredMarketBool, - ); + const accountVToken = getOrCreateAccountVToken(accountAddress, marketAddress, enteredMarketBool); accountVToken.enteredMarket = enteredMarketBool; accountVToken.accrualBlockNumber = blockNumber; accountVToken.save(); diff --git a/subgraphs/isolated-pools/tests/VToken/index.test.ts b/subgraphs/isolated-pools/tests/VToken/index.test.ts index ff3b86e8..f61b6c3f 100644 --- a/subgraphs/isolated-pools/tests/VToken/index.test.ts +++ b/subgraphs/isolated-pools/tests/VToken/index.test.ts @@ -137,6 +137,19 @@ describe('VToken', () => { mintTokens, accountBalance, ); + createMockedFunction( + aaaTokenAddress, + 'getAccountSnapshot', + 'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)', + ) + .withArgs([ethereum.Value.fromAddress(minter)]) + .returns([ + ethereum.Value.fromSignedBigInt(zeroBigInt32), + ethereum.Value.fromSignedBigInt(mintTokens), + ethereum.Value.fromSignedBigInt(zeroBigInt32), + ethereum.Value.fromSignedBigInt(oneBigInt), + ]); + const market = getMarket(aaaTokenAddress); assert.assertNotNull(market); if (!market) { @@ -168,13 +181,47 @@ describe('VToken', () => { .truncate(market.underlyingDecimals) .toString(), ); + // AccountVToken + const accountVTokenId = getAccountVTokenId(aaaTokenAddress, minter); + assert.fieldEquals('AccountVToken', accountVTokenId, 'account', minter.toHexString()); + assert.fieldEquals('AccountVToken', accountVTokenId, 'market', aaaTokenAddress.toHexString()); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accrualBlockNumber', + oneBigInt.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accountSupplyBalanceMantissa', + accountBalance.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accountBorrowBalanceMantissa', + zeroBigInt32.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'totalUnderlyingRedeemedMantissa', + zeroBigInt32.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accountBorrowIndexMantissa', + zeroBigInt32.toString(), + ); }); test('registers redeem event', () => { const redeemer = user2Address; const actualRedeemAmount = BigInt.fromString('124620530798726345'); const redeemTokens = BigInt.fromString('37035970026454'); - const accountBalance = redeemTokens; + const accountBalance = zeroBigInt32; const redeemEvent = createRedeemEvent( aaaTokenAddress, redeemer, @@ -182,6 +229,18 @@ describe('VToken', () => { redeemTokens, accountBalance, ); + createMockedFunction( + aaaTokenAddress, + 'getAccountSnapshot', + 'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)', + ) + .withArgs([ethereum.Value.fromAddress(redeemer)]) + .returns([ + ethereum.Value.fromSignedBigInt(zeroBigInt32), + ethereum.Value.fromSignedBigInt(zeroBigInt32), + ethereum.Value.fromSignedBigInt(zeroBigInt32), + ethereum.Value.fromSignedBigInt(oneBigInt), + ]); const market = getMarket(aaaTokenAddress); assert.assertNotNull(market); if (!market) { @@ -213,6 +272,40 @@ describe('VToken', () => { .truncate(market.underlyingDecimals) .toString(), ); + // AccountVToken + const accountVTokenId = getAccountVTokenId(aaaTokenAddress, redeemer); + assert.fieldEquals('AccountVToken', accountVTokenId, 'account', redeemer.toHexString()); + assert.fieldEquals('AccountVToken', accountVTokenId, 'market', aaaTokenAddress.toHexString()); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accrualBlockNumber', + oneBigInt.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accountSupplyBalanceMantissa', + zeroBigInt32.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accountBorrowBalanceMantissa', + zeroBigInt32.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'totalUnderlyingRedeemedMantissa', + zeroBigInt32.toString(), + ); + assert.fieldEquals( + 'AccountVToken', + accountVTokenId, + 'accountBorrowIndexMantissa', + zeroBigInt32.toString(), + ); }); test('registers borrow event', () => { diff --git a/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccount.graphql b/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccount.graphql index 2efdaaca..05fd48db 100644 --- a/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccount.graphql +++ b/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccount.graphql @@ -4,7 +4,6 @@ query AccountVTokenByAccountId($accountId: String!) { market { id } - symbol account { id } diff --git a/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccountAndMarket.graphql b/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccountAndMarket.graphql index d26921c0..ec23d6c2 100644 --- a/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccountAndMarket.graphql +++ b/subgraphs/isolated-pools/tests/integration/queries/accountVTokenByAccountAndMarket.graphql @@ -4,7 +4,6 @@ query AccountVTokenByAccountAndMarketQuery($accountId: String!, $marketId: Strin market { id } - symbol account { id } diff --git a/subgraphs/isolated-pools/tests/integration/queries/accountVTokens.graphql b/subgraphs/isolated-pools/tests/integration/queries/accountVTokens.graphql index f3a30bcd..a4719ed7 100644 --- a/subgraphs/isolated-pools/tests/integration/queries/accountVTokens.graphql +++ b/subgraphs/isolated-pools/tests/integration/queries/accountVTokens.graphql @@ -4,7 +4,6 @@ query AccountVTokens { market { id } - symbol account { id }