diff --git a/subgraphs/venus/src/mappings/vToken.ts b/subgraphs/venus/src/mappings/vToken.ts index 431b3187..82f77863 100644 --- a/subgraphs/venus/src/mappings/vToken.ts +++ b/subgraphs/venus/src/mappings/vToken.ts @@ -276,8 +276,6 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void { // the underwater borrower. So we must get that address from the event, and // the repay token is the event.address createLiquidationEvent(event); - const collateralMarket = getOrCreateMarket(event.params.vTokenCollateral, event); - const borrowMarket = getOrCreateMarket(event.address, event); const borrowedVTokenContract = VToken.bind(event.address); const collateralContract = VToken.bind(event.params.vTokenCollateral); @@ -299,30 +297,19 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void { ); const borrowerCollateralAccountVToken = borrowerSupplyAccountVTokenResult.entity; - borrowerCollateralAccountVToken.vTokenBalanceMantissa = - borrowerCollateralAccountVToken.vTokenBalanceMantissa.minus(event.params.seizeTokens); - borrowerCollateralAccountVToken.save(); - const collateralBalance = collateralContract.balanceOf(event.params.borrower); - // Check if borrower is still supplying liquidated asset - if (collateralBalance.equals(zeroBigInt32)) { - collateralMarket.supplierCount = borrowMarket.supplierCount.minus(oneBigInt); - collateralMarket.save(); - } + borrowerCollateralAccountVToken.vTokenBalanceMantissa = collateralBalance; + borrowerCollateralAccountVToken.save(); - // Check if liquidator is new supplier const resultLiquidatorAccountVToken = getOrCreateAccountVToken( event.params.vTokenCollateral, event.params.liquidator, ); const liquidatorAccountVToken = resultLiquidatorAccountVToken.entity; - liquidatorAccountVToken.vTokenBalanceMantissa.plus(event.params.seizeTokens); + liquidatorAccountVToken.vTokenBalanceMantissa = collateralContract.balanceOf( + event.params.liquidator, + ); liquidatorAccountVToken.save(); - - if (liquidatorAccountVToken.vTokenBalanceMantissa.equals(event.params.seizeTokens)) { - collateralMarket.supplierCount = collateralMarket.supplierCount.plus(oneBigInt); - collateralMarket.save(); - } } /* Transferring of vTokens @@ -340,6 +327,7 @@ export function handleTransfer(event: Transfer): void { // with normal transfers, since mint, redeem, and seize transfers will already run updateMarket() let accountFromAddress = event.params.from; let accountToAddress = event.params.to; + const vTokenContract = VToken.bind(event.address); // Checking if the event is FROM the vToken contract or null (i.e. this will not run when minting) // Checking if the event is TO the vToken contract (i.e. this will not run when redeeming) // @TODO Edge case where someone who accidentally sends vTokens to a vToken contract, where it will not get recorded. @@ -351,20 +339,30 @@ export function handleTransfer(event: Transfer): void { getOrCreateAccount(accountFromAddress); const resultFrom = getOrCreateAccountVToken(event.address, accountFromAddress); const accountFromVToken = resultFrom.entity; - - // Creation updates balance - accountFromVToken.vTokenBalanceMantissa = accountFromVToken.vTokenBalanceMantissa.minus( - event.params.amount, - ); + const fromBalance = vTokenContract.balanceOf(accountFromAddress); + accountFromVToken.vTokenBalanceMantissa = fromBalance; accountFromVToken.save(); + + // Decrease if no longer minter + if (fromBalance.equals(zeroBigInt32)) { + const market = getMarket(event.address)!; + market.supplierCount = market.supplierCount.minus(oneBigInt); + market.save(); + } + getOrCreateAccount(accountToAddress); const resultTo = getOrCreateAccountVToken(event.address, accountToAddress); const accountToVToken = resultTo.entity; - - accountToVToken.vTokenBalanceMantissa = accountToVToken.vTokenBalanceMantissa.plus( - event.params.amount, - ); + const toBalance = vTokenContract.balanceOf(accountToAddress); + accountToVToken.vTokenBalanceMantissa = toBalance; accountToVToken.save(); + + // Increase balance if now minter + if (toBalance.equals(event.params.amount)) { + const market = getMarket(event.address)!; + market.supplierCount = market.supplierCount.plus(oneBigInt); + market.save(); + } } createTransferEvent(event); const market = getMarket(event.address)!; @@ -427,9 +425,6 @@ export function handleMintV1(event: MintV1): void { const result = getOrCreateAccountVToken(marketAddress, event.params.minter); const accountVToken = result.entity; - if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) { - market.supplierCount = market.supplierCount.plus(oneBigInt); - } // we'll first update the cash value of the market and then the rates, since they depend on it updateMarketCashMantissa(market, vTokenContract); @@ -443,10 +438,13 @@ export function handleMintV1(event: MintV1): void { getOrCreateAccount(event.params.minter); // Creation updates balance - accountVToken.vTokenBalanceMantissa = accountVToken.vTokenBalanceMantissa.plus( - event.params.mintTokens, - ); + accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(event.params.minter); accountVToken.save(); + + if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) { + market.supplierCount = market.supplierCount.plus(oneBigInt); + market.save(); + } } export function handleMintBehalfV1(event: MintBehalfV1): void { @@ -459,9 +457,6 @@ export function handleMintBehalfV1(event: MintBehalfV1): void { const result = getOrCreateAccountVToken(marketAddress, event.params.receiver); const accountVToken = result.entity; - if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) { - market.supplierCount = market.supplierCount.plus(oneBigInt); - } // we'll first update the cash value of the market updateMarketCashMantissa(market, vTokenContract); @@ -475,10 +470,13 @@ export function handleMintBehalfV1(event: MintBehalfV1): void { getOrCreateAccount(event.params.receiver); // Creation updates balance - accountVToken.vTokenBalanceMantissa = accountVToken.vTokenBalanceMantissa.plus( - event.params.mintTokens, - ); + accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(event.params.receiver); accountVToken.save(); + + if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) { + market.supplierCount = market.supplierCount.plus(oneBigInt); + market.save(); + } } export function handleRedeemV1(event: RedeemV1): void { @@ -503,9 +501,7 @@ export function handleRedeemV1(event: RedeemV1): void { const result = getOrCreateAccountVToken(marketAddress, event.params.redeemer); const accountVToken = result.entity; - accountVToken.vTokenBalanceMantissa = accountVToken.vTokenBalanceMantissa.minus( - event.params.redeemTokens, - ); + accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(event.params.redeemer); accountVToken.totalUnderlyingRedeemedMantissa = accountVToken.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount); accountVToken.save(); diff --git a/subgraphs/venus/tests/VToken/index.test.ts b/subgraphs/venus/tests/VToken/index.test.ts index 2957437a..ee2e1ad1 100644 --- a/subgraphs/venus/tests/VToken/index.test.ts +++ b/subgraphs/venus/tests/VToken/index.test.ts @@ -382,18 +382,9 @@ describe('VToken', () => { /** Setup test */ const transferEvent = createTransferEvent(aaaTokenAddress, from, to, amount); - createMockedFunction( - aaaTokenAddress, - 'getAccountSnapshot', - 'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)', - ) - .withArgs([ethereum.Value.fromAddress(from)]) - .returns([ - ethereum.Value.fromSignedBigInt(zeroBigInt32), - ethereum.Value.fromSignedBigInt(balanceOf), - ethereum.Value.fromSignedBigInt(zeroBigInt32), - ethereum.Value.fromSignedBigInt(oneBigInt), - ]); + + createAccountVTokenBalanceOfMock(aaaTokenAddress, from, balanceOf.minus(amount)); + createAccountVTokenBalanceOfMock(aaaTokenAddress, to, balanceOf.plus(amount)); const accountVTokenId = getAccountVTokenId(aaaTokenAddress, from).toHexString(); /** AccountVToken */ @@ -412,7 +403,7 @@ describe('VToken', () => { 'AccountVToken', accountVTokenId, 'vTokenBalanceMantissa', - mintTokens.minus(amount).toString(), + balanceOf.minus(amount).toString(), ); }); @@ -425,19 +416,9 @@ describe('VToken', () => { /** Setup test */ const transferEvent = createTransferEvent(aaaTokenAddress, from, to, amount); - createAccountVTokenBalanceOfMock(aaaTokenAddress, to, amount.plus(balanceOf)); - createMockedFunction( - aaaTokenAddress, - 'getAccountSnapshot', - 'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)', - ) - .withArgs([ethereum.Value.fromAddress(to)]) - .returns([ - ethereum.Value.fromSignedBigInt(zeroBigInt32), - ethereum.Value.fromSignedBigInt(balanceOf), - ethereum.Value.fromSignedBigInt(zeroBigInt32), - ethereum.Value.fromSignedBigInt(oneBigInt), - ]); + + createAccountVTokenBalanceOfMock(aaaTokenAddress, from, balanceOf.minus(amount)); + createAccountVTokenBalanceOfMock(aaaTokenAddress, to, amount); /** Fire Event */ handleTransfer(transferEvent); @@ -449,8 +430,9 @@ describe('VToken', () => { 'AccountVToken', accountVTokenId, 'vTokenBalanceMantissa', - amount.plus(balanceOf).toString(), + amount.toString(), ); + assert.fieldEquals('Market', aaaTokenAddress.toHexString(), 'supplierCount', '1'); }); test('registers new interest rate model', () => { @@ -484,7 +466,6 @@ describe('VToken', () => { const actualMintAmount = BigInt.fromI64(12); const halfActualMintAmount = actualMintAmount.div(BigInt.fromI64(2)); const mintTokens = BigInt.fromI64(10); - const accountBalance = mintTokens; const halfMintTokens = mintTokens.div(BigInt.fromI64(2)); const supplier01 = user1Address; @@ -493,7 +474,7 @@ describe('VToken', () => { supplier01, actualMintAmount, mintTokens, - accountBalance, + mintTokens, ); handleMint(mintEvent); @@ -507,6 +488,7 @@ describe('VToken', () => { actualMintAmount, mintTokens, ); + createAccountVTokenBalanceOfMock(aaaTokenAddress, supplier02, mintTokens); handleMintV1(mintEventV1); @@ -519,7 +501,7 @@ describe('VToken', () => { supplier03, actualMintAmount, mintTokens, - accountBalance, + mintTokens, ); createAccountVTokenBalanceOfMock(aaaTokenAddress, supplier03, mintTokens);