From 3e6d7ce56155b57bfc9331f2c24d9de93feecb61 Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Wed, 18 Dec 2024 13:27:14 -0300 Subject: [PATCH] refactor: skip recording vbifi positions --- subgraphs/isolated-pools/src/mappings/pool.ts | 8 +- .../isolated-pools/src/mappings/vToken.ts | 156 +++++++++++------- .../src/operations/getOrCreate.ts | 56 ++++--- .../isolated-pools/src/operations/update.ts | 124 ++++++++------ .../src/operations/updateOrCreate.ts | 36 ++-- 5 files changed, 220 insertions(+), 160 deletions(-) diff --git a/subgraphs/isolated-pools/src/mappings/pool.ts b/subgraphs/isolated-pools/src/mappings/pool.ts index 30f97b3a..4c4c908e 100644 --- a/subgraphs/isolated-pools/src/mappings/pool.ts +++ b/subgraphs/isolated-pools/src/mappings/pool.ts @@ -41,9 +41,11 @@ export function handleMarketSupported(event: MarketSupported): void { } export function handleMarketUnlisted(event: MarketUnlisted): void { - const market = getMarket(event.params.vToken)!; - market.isListed = false; - market.save(); + const market = getMarket(event.params.vToken); + if (market) { + market.isListed = false; + market.save(); + } } export function handleMarketEntered(event: MarketEntered): void { diff --git a/subgraphs/isolated-pools/src/mappings/vToken.ts b/subgraphs/isolated-pools/src/mappings/vToken.ts index 577e39c7..38911c95 100644 --- a/subgraphs/isolated-pools/src/mappings/vToken.ts +++ b/subgraphs/isolated-pools/src/mappings/vToken.ts @@ -60,9 +60,11 @@ export function handleMint(event: Mint): void { // and finally we update the market total supply const vTokenContract = VTokenContract.bind(vTokenAddress); - const market = getMarket(vTokenAddress)!; - market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); + market.save(); + } } /* Account supplies vTokens into market and receives underlying asset in exchange @@ -86,13 +88,17 @@ export function handleRedeem(event: Redeem): void { event.block.number, currentBalance, ); - marketPosition.totalUnderlyingRedeemedMantissa = - marketPosition.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount); - // and finally we update the market total supply - const vTokenContract = VTokenContract.bind(vTokenAddress); - const market = getMarket(vTokenAddress)!; - market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); - market.save(); + if (marketPosition) { + marketPosition.totalUnderlyingRedeemedMantissa = + marketPosition.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount); + // and finally we update the market total supply + const vTokenContract = VTokenContract.bind(vTokenAddress); + const market = getMarket(vTokenAddress); + if (market) { + market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); + market.save(); + } + } } /* Borrow assets from the protocol. All values either BNB or BEP20 @@ -116,10 +122,11 @@ export function handleBorrow(event: Borrow): void { createBorrowTransaction(event); - const market = getMarket(vTokenAddress)!; - - market.totalBorrowsMantissa = event.params.totalBorrows; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.totalBorrowsMantissa = event.params.totalBorrows; + market.save(); + } } /* Repay some amount borrowed. Anyone can repay anyones balance @@ -145,14 +152,15 @@ export function handleRepayBorrow(event: RepayBorrow): void { event.block.number, event.params.accountBorrows, ); - const market = getMarket(vTokenAddress)!; - market.totalBorrowsMantissa = event.params.totalBorrows; - market.save(); + const market = getMarket(vTokenAddress); + if (market && marketPosition) { + market.totalBorrowsMantissa = event.params.totalBorrows; + market.save(); - marketPosition.totalUnderlyingRepaidMantissa = marketPosition.totalUnderlyingRepaidMantissa.plus( - event.params.repayAmount, - ); - createRepayBorrowTransaction(event); + marketPosition.totalUnderlyingRepaidMantissa = + marketPosition.totalUnderlyingRepaidMantissa.plus(event.params.repayAmount); + createRepayBorrowTransaction(event); + } } /* @@ -173,42 +181,50 @@ export function handleRepayBorrow(event: RepayBorrow): void { */ export function handleLiquidateBorrow(event: LiquidateBorrow): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - const vTokenContract = VTokenContract.bind(vTokenAddress); - const liquidator = getOrCreateAccount(event.params.liquidator); - liquidator.countLiquidator = liquidator.countLiquidator + 1; - liquidator.save(); + const market = getMarket(vTokenAddress); + if (market) { + const vTokenContract = VTokenContract.bind(vTokenAddress); + const liquidator = getOrCreateAccount(event.params.liquidator); + liquidator.countLiquidator = liquidator.countLiquidator + 1; + liquidator.save(); - const borrower = getOrCreateAccount(event.params.borrower); - borrower.countLiquidated = borrower.countLiquidated + 1; - borrower.save(); + const borrower = getOrCreateAccount(event.params.borrower); + borrower.countLiquidated = borrower.countLiquidated + 1; + borrower.save(); - market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); - market.save(); + market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); + market.save(); - createLiquidateBorrowTransaction(event); + createLiquidateBorrowTransaction(event); + } } export function handleProtocolSeize(event: ProtocolSeize): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - const vTokenContract = VTokenContract.bind(vTokenAddress); - market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + const vTokenContract = VTokenContract.bind(vTokenAddress); + market.totalSupplyVTokenMantissa = vTokenContract.totalSupply(); + market.save(); + } } export function handleAccrueInterest(event: AccrueInterest): void { const market = updateMarket(event.address, event.block.number); - market.totalBorrowsMantissa = event.params.totalBorrows; - market.borrowIndex = event.params.borrowIndex; - market.save(); + if (market) { + market.totalBorrowsMantissa = event.params.totalBorrows; + market.borrowIndex = event.params.borrowIndex; + market.save(); + } } export function handleNewReserveFactor(event: NewReserveFactor): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.reserveFactorMantissa = event.params.newReserveFactorMantissa; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.reserveFactorMantissa = event.params.newReserveFactorMantissa; + market.save(); + } } /* Transferring of vTokens @@ -272,46 +288,58 @@ export function handleTransfer(event: Transfer): void { export function handleNewMarketInterestRateModel(event: NewMarketInterestRateModel): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.interestRateModelAddress = event.params.newInterestRateModel; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.interestRateModelAddress = event.params.newInterestRateModel; + market.save(); + } } export function handleBadDebtIncreased(event: BadDebtIncreased): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.badDebtMantissa = event.params.badDebtNew; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.badDebtMantissa = event.params.badDebtNew; + market.save(); + } createMarketPositionBadDebt(vTokenAddress, event); } export function handleBadDebtRecovered(event: BadDebtRecovered): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.badDebtMantissa = event.params.badDebtNew; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.badDebtMantissa = event.params.badDebtNew; + market.save(); + } } export function handleNewAccessControlManager(event: NewAccessControlManager): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.accessControlManagerAddress = event.params.newAccessControlManager; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.accessControlManagerAddress = event.params.newAccessControlManager; + market.save(); + } } export function handleReservesAdded(event: ReservesAdded): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.reservesMantissa = event.params.newTotalReserves; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.reservesMantissa = event.params.newTotalReserves; + market.save(); + } } export function handleSpreadReservesReduced(event: SpreadReservesReduced): void { const vTokenAddress = event.address; - const market = getMarket(vTokenAddress)!; - market.reservesMantissa = event.params.newTotalReserves; - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.reservesMantissa = event.params.newTotalReserves; + market.save(); + } } export function handleHealBorrow(event: HealBorrow): void { @@ -323,7 +351,9 @@ export function handleHealBorrow(event: HealBorrow): void { zeroBigInt32, ); const vTokenContract = VTokenContract.bind(vTokenAddress); - const market = getMarket(vTokenAddress)!; - market.totalBorrowsMantissa = vTokenContract.totalBorrows(); - market.save(); + const market = getMarket(vTokenAddress); + if (market) { + market.totalBorrowsMantissa = vTokenContract.totalBorrows(); + market.save(); + } } diff --git a/subgraphs/isolated-pools/src/operations/getOrCreate.ts b/subgraphs/isolated-pools/src/operations/getOrCreate.ts index 94e33981..4e0c2c8f 100644 --- a/subgraphs/isolated-pools/src/operations/getOrCreate.ts +++ b/subgraphs/isolated-pools/src/operations/getOrCreate.ts @@ -29,6 +29,7 @@ import { createRewardDistributor, } from './create'; import { getMarketPosition, getMarket } from './get'; +import { vBifiAddress } from '../constants/addresses'; // BIFI was delisted before it was listed. Creation ignores this market. export const getOrCreateMarket = ( @@ -81,31 +82,34 @@ export const getOrCreateMarketPosition = ( marketAddress: Address, poolAddress: Address, enteredMarket: boolean = false, // eslint-disable-line @typescript-eslint/no-inferrable-types -): GetOrCreateMarketPositionReturn => { - let marketPosition = getMarketPosition(accountAddress, marketAddress); - let created = false; - if (!marketPosition) { - created = true; - const marketPositionId = getMarketPositionId(accountAddress, marketAddress); - marketPosition = new MarketPosition(marketPositionId); - marketPosition.account = accountAddress; - marketPosition.accountPool = getOrCreateAccountPool(accountAddress, poolAddress).id; - marketPosition.market = marketAddress; - marketPosition.enteredMarket = enteredMarket; - marketPosition.accrualBlockNumber = zeroBigInt32; - - const vTokenContract = VTokenContract.bind(marketAddress); - - marketPosition.vTokenBalanceMantissa = zeroBigInt32; - marketPosition.storedBorrowBalanceMantissa = zeroBigInt32; - marketPosition.borrowIndex = vTokenContract.borrowIndex(); - - marketPosition.totalUnderlyingRedeemedMantissa = zeroBigInt32; - marketPosition.totalUnderlyingRepaidMantissa = zeroBigInt32; - marketPosition.enteredMarket = false; - marketPosition.save(); +): GetOrCreateMarketPositionReturn | null => { + if (marketAddress.notEqual(vBifiAddress)) { + let marketPosition = getMarketPosition(accountAddress, marketAddress); + let created = false; + if (!marketPosition) { + created = true; + const marketPositionId = getMarketPositionId(accountAddress, marketAddress); + marketPosition = new MarketPosition(marketPositionId); + marketPosition.account = accountAddress; + marketPosition.accountPool = getOrCreateAccountPool(accountAddress, poolAddress).id; + marketPosition.market = marketAddress; + marketPosition.enteredMarket = enteredMarket; + marketPosition.accrualBlockNumber = zeroBigInt32; + + const vTokenContract = VTokenContract.bind(marketAddress); + + marketPosition.vTokenBalanceMantissa = zeroBigInt32; + marketPosition.storedBorrowBalanceMantissa = zeroBigInt32; + marketPosition.borrowIndex = vTokenContract.borrowIndex(); + + marketPosition.totalUnderlyingRedeemedMantissa = zeroBigInt32; + marketPosition.totalUnderlyingRepaidMantissa = zeroBigInt32; + marketPosition.enteredMarket = false; + marketPosition.save(); + } + return { entity: marketPosition, created }; } - return { entity: marketPosition, created }; + return null; }; export function getOrCreateMarketReward( @@ -153,8 +157,8 @@ export function getOrCreateAnkrStakedBNBToken(): Token { if (!tokenEntity) { tokenEntity = new Token(getTokenId(underlyingTokenAddress)); tokenEntity.address = underlyingTokenAddress; - tokenEntity.name = 'Ankr Staked BNB'; - tokenEntity.symbol = 'ankrBNB '; + tokenEntity.name = 'Ankr Staked BNB '; + tokenEntity.symbol = 'ankrBNB'; tokenEntity.decimals = 18; tokenEntity.save(); } diff --git a/subgraphs/isolated-pools/src/operations/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index 962e329e..0cc8ba76 100644 --- a/subgraphs/isolated-pools/src/operations/update.ts +++ b/subgraphs/isolated-pools/src/operations/update.ts @@ -19,7 +19,7 @@ export const updateMarketPositionAccrualBlockNumber = ( marketAddress: Address, poolAddress: Address, blockNumber: BigInt, -): MarketPosition => { +): MarketPosition | null => { getOrCreateAccount(accountAddress); const marketPosition = getOrCreateMarketPosition( accountAddress, @@ -27,9 +27,12 @@ export const updateMarketPositionAccrualBlockNumber = ( poolAddress, false, ); - marketPosition.entity.accrualBlockNumber = blockNumber; - marketPosition.entity.save(); - return marketPosition.entity as MarketPosition; + if (marketPosition) { + marketPosition.entity.accrualBlockNumber = blockNumber; + marketPosition.entity.save(); + return marketPosition.entity; + } + return null; }; export const updateMarketPositionSupply = ( @@ -37,31 +40,36 @@ export const updateMarketPositionSupply = ( marketAddress: Address, blockNumber: BigInt, accountSupplyBalanceMantissa: BigInt, -): MarketPosition => { - const market = getMarket(marketAddress)!; - const marketPosition = updateMarketPositionAccrualBlockNumber( - accountAddress, - marketAddress, - Address.fromBytes(market.pool), - blockNumber, - ); - const _vTokenBalanceMantissa = marketPosition.vTokenBalanceMantissa; - marketPosition.vTokenBalanceMantissa = accountSupplyBalanceMantissa; - marketPosition.save(); +): MarketPosition | null => { + const market = getMarket(marketAddress); + if (market) { + const marketPosition = updateMarketPositionAccrualBlockNumber( + accountAddress, + marketAddress, + Address.fromBytes(market.pool), + blockNumber, + ); + if (marketPosition) { + const _vTokenBalanceMantissa = marketPosition.vTokenBalanceMantissa; + marketPosition.vTokenBalanceMantissa = accountSupplyBalanceMantissa; + marketPosition.save(); - if ( - _vTokenBalanceMantissa.equals(zeroBigInt32) && - accountSupplyBalanceMantissa.notEqual(zeroBigInt32) - ) { - market.supplierCount = market.supplierCount.plus(oneBigInt); - } else if ( - accountSupplyBalanceMantissa.equals(zeroBigInt32) && - _vTokenBalanceMantissa.notEqual(zeroBigInt32) - ) { - market.supplierCount = market.supplierCount.minus(oneBigInt); + if ( + _vTokenBalanceMantissa.equals(zeroBigInt32) && + accountSupplyBalanceMantissa.notEqual(zeroBigInt32) + ) { + market.supplierCount = market.supplierCount.plus(oneBigInt); + } else if ( + accountSupplyBalanceMantissa.equals(zeroBigInt32) && + _vTokenBalanceMantissa.notEqual(zeroBigInt32) + ) { + market.supplierCount = market.supplierCount.minus(oneBigInt); + } + market.save(); + } + return marketPosition; } - market.save(); - return marketPosition as MarketPosition; + return null; }; export const updateMarketPositionBorrow = ( @@ -69,35 +77,45 @@ export const updateMarketPositionBorrow = ( marketAddress: Address, blockNumber: BigInt, accountBorrows: BigInt, -): MarketPosition => { - const market = getMarket(marketAddress)!; - const marketPosition = updateMarketPositionAccrualBlockNumber( - accountAddress, - marketAddress, - Address.fromBytes(market.pool), - blockNumber, - ); - const _storedBorrowBalanceMantissa = marketPosition.storedBorrowBalanceMantissa; - marketPosition.storedBorrowBalanceMantissa = accountBorrows; - const vTokenContract = VToken.bind(marketAddress); - marketPosition.borrowIndex = vTokenContract.borrowIndex(); - marketPosition.save(); +): MarketPosition | null => { + const market = getMarket(marketAddress); + if (market) { + const marketPosition = updateMarketPositionAccrualBlockNumber( + accountAddress, + marketAddress, + Address.fromBytes(market.pool), + blockNumber, + ); + if (marketPosition) { + const _storedBorrowBalanceMantissa = marketPosition.storedBorrowBalanceMantissa; + marketPosition.storedBorrowBalanceMantissa = accountBorrows; + const vTokenContract = VToken.bind(marketAddress); + marketPosition.borrowIndex = vTokenContract.borrowIndex(); + marketPosition.save(); - if (_storedBorrowBalanceMantissa.equals(zeroBigInt32) && accountBorrows.notEqual(zeroBigInt32)) { - market.borrowerCount = market.borrowerCount.plus(oneBigInt); - } else if ( - accountBorrows.equals(zeroBigInt32) && - _storedBorrowBalanceMantissa.notEqual(zeroBigInt32) - ) { - market.borrowerCount = market.borrowerCount.minus(oneBigInt); + if ( + _storedBorrowBalanceMantissa.equals(zeroBigInt32) && + accountBorrows.notEqual(zeroBigInt32) + ) { + market.borrowerCount = market.borrowerCount.plus(oneBigInt); + } else if ( + accountBorrows.equals(zeroBigInt32) && + _storedBorrowBalanceMantissa.notEqual(zeroBigInt32) + ) { + market.borrowerCount = market.borrowerCount.minus(oneBigInt); + } + market.save(); + return marketPosition; + } } - market.save(); - return marketPosition as MarketPosition; + return null; }; -export const updateMarket = (vTokenAddress: Address, blockNumber: BigInt): Market => { - const market = getMarket(vTokenAddress)!; - +export const updateMarket = (vTokenAddress: Address, blockNumber: BigInt): Market | null => { + const market = getMarket(vTokenAddress); + if (!market) { + return null; + } // Only updateMarket if it has not been updated this block if (market.accrualBlockNumber.equals(blockNumber)) { return market as Market; @@ -135,7 +153,7 @@ export const updateMarket = (vTokenAddress: Address, blockNumber: BigInt): Marke ); market.save(); - return market as Market; + return market; }; export function updatePoolMetadata( diff --git a/subgraphs/isolated-pools/src/operations/updateOrCreate.ts b/subgraphs/isolated-pools/src/operations/updateOrCreate.ts index 56d55edd..bae8f864 100644 --- a/subgraphs/isolated-pools/src/operations/updateOrCreate.ts +++ b/subgraphs/isolated-pools/src/operations/updateOrCreate.ts @@ -5,6 +5,7 @@ import { Actions } from '../constants'; import Box from '../utilities/box'; import { getMarketActionId } from '../utilities/ids'; import { getOrCreateMarketPosition } from './getOrCreate'; +import { vBifiAddress } from '../constants/addresses'; export const updateOrCreateMarketPosition = ( accountAddress: Address, @@ -12,7 +13,7 @@ export const updateOrCreateMarketPosition = ( poolAddress: Address, blockNumber: BigInt, enteredMarket: Box | null = null, -): MarketPosition => { +): MarketPosition | null => { let enteredMarketBool = false; if (enteredMarket !== null) { enteredMarketBool = enteredMarket.value; @@ -24,24 +25,29 @@ export const updateOrCreateMarketPosition = ( poolAddress, enteredMarketBool, ); - const marketPosition = result.entity; - marketPosition.enteredMarket = enteredMarketBool; - marketPosition.accrualBlockNumber = blockNumber; - marketPosition.save(); - - return marketPosition; + if (result) { + const marketPosition = result.entity; + marketPosition.enteredMarket = enteredMarketBool; + marketPosition.accrualBlockNumber = blockNumber; + marketPosition.save(); + return marketPosition; + } + return null; }; export const updateOrCreateMarketAction = ( vTokenAddress: Address, action: i32, pauseState: boolean, -): MarketAction => { - const id = getMarketActionId(vTokenAddress, action); - const marketAction = new MarketAction(id); - marketAction.market = vTokenAddress; - marketAction.action = Actions[action]; - marketAction.pauseState = pauseState; - marketAction.save(); - return marketAction; +): MarketAction | null => { + if (vTokenAddress.notEqual(vBifiAddress)) { + const id = getMarketActionId(vTokenAddress, action); + const marketAction = new MarketAction(id); + marketAction.market = vTokenAddress; + marketAction.action = Actions[action]; + marketAction.pauseState = pauseState; + marketAction.save(); + return marketAction; + } + return null; };