From 9971401ee6fb3850545796cf4e0bcd16c7632cc4 Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Wed, 27 Nov 2024 13:21:04 -0300 Subject: [PATCH] refactor: filter out zero borrow repay --- .../isolated-pools/src/mappings/vToken.ts | 29 ++++++++++--------- .../isolated-pools/src/operations/update.ts | 20 ------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/subgraphs/isolated-pools/src/mappings/vToken.ts b/subgraphs/isolated-pools/src/mappings/vToken.ts index 0cbda84a..2031bdce 100644 --- a/subgraphs/isolated-pools/src/mappings/vToken.ts +++ b/subgraphs/isolated-pools/src/mappings/vToken.ts @@ -31,10 +31,10 @@ import { createTransferTransaction, } from '../operations/create'; import { getMarket } from '../operations/get'; +import { getAccountVToken } from '../operations/get'; import { getOrCreateAccount, getOrCreateAccountVToken } from '../operations/getOrCreate'; import { updateAccountVTokenBorrow, - updateAccountVTokenRepayBorrow, updateAccountVTokenSupply, updateMarket, } from '../operations/update'; @@ -163,21 +163,24 @@ export function handleRepayBorrow(event: RepayBorrow): void { const market = getMarket(vTokenAddress)!; market.totalBorrowsMantissa = event.params.totalBorrows; + const vTokenAccount = getAccountVToken(vTokenAddress, event.params.borrower); + // Its possible to call repayborrow was called without having previously borrowed + if (vTokenAccount && vTokenAccount.storedBorrowBalanceMantissa.gt(zeroBigInt32)) { + updateAccountVTokenBorrow( + event.params.borrower, + Address.fromBytes(market.pool), + vTokenAddress, + event.block.number, + event.params.accountBorrows, + ); - updateAccountVTokenRepayBorrow( - event.params.borrower, - Address.fromBytes(market.pool), - vTokenAddress, - event.block.number, - event.params.accountBorrows, - ); - - createRepayBorrowTransaction(event); + createRepayBorrowTransaction(event); - if (event.params.accountBorrows.equals(zeroBigInt32)) { - market.borrowerCount = market.borrowerCount.minus(oneBigInt); + if (event.params.accountBorrows.equals(zeroBigInt32)) { + market.borrowerCount = market.borrowerCount.minus(oneBigInt); + } + market.save(); } - market.save(); } /* diff --git a/subgraphs/isolated-pools/src/operations/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index 070bf797..bd4cd835 100644 --- a/subgraphs/isolated-pools/src/operations/update.ts +++ b/subgraphs/isolated-pools/src/operations/update.ts @@ -59,26 +59,6 @@ export const updateAccountVTokenBorrow = ( return accountVToken as AccountVToken; }; -export const updateAccountVTokenRepayBorrow = ( - accountAddress: Address, - poolAddress: Address, - marketAddress: Address, - blockNumber: BigInt, - accountBorrows: BigInt, -): AccountVToken => { - const accountVToken = updateAccountVTokenAccrualBlockNumber( - accountAddress, - poolAddress, - marketAddress, - blockNumber, - ); - accountVToken.storedBorrowBalanceMantissa = accountBorrows; - const vTokenContract = VToken.bind(marketAddress); - accountVToken.borrowIndex = vTokenContract.borrowIndex(); - accountVToken.save(); - return accountVToken as AccountVToken; -}; - export const updateAccountVTokenTransferFrom = ( accountAddress: Address, poolAddress: Address,