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/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index 962e329e..24eeb889 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,11 @@ 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; }; export const updateMarketPositionSupply = ( @@ -37,31 +39,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 +76,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 +152,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..b6c612ca 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, @@ -36,12 +37,15 @@ 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; }; diff --git a/subgraphs/venus/src/operations/getOrCreate.ts b/subgraphs/venus/src/operations/getOrCreate.ts index 8cdda6df..995dcc48 100644 --- a/subgraphs/venus/src/operations/getOrCreate.ts +++ b/subgraphs/venus/src/operations/getOrCreate.ts @@ -24,6 +24,7 @@ import { getMarketPositionId, getTokenId } from '../utilities/ids'; import { getMarket } from './get'; import { updateMarketCashMantissa } from './updateMarketCashMantissa'; import { updateMarketRates } from './updateMarketRates'; +import { vBifiAddress } from '../constants/addresses'; export function getOrCreateMarket(marketAddress: Address, event: ethereum.Event): Market { let market = getMarket(marketAddress); @@ -134,7 +135,10 @@ export class GetOrCreateMarketPositionReturn { export function getOrCreateMarketPosition( accountId: Address, marketId: Address, -): GetOrCreateMarketPositionReturn { +): GetOrCreateMarketPositionReturn | null { + if (marketId.equals(vBifiAddress)) { + return null; + } const marketPositionId = getMarketPositionId(accountId, marketId); let marketPosition = MarketPosition.load(marketPositionId); let created = false;