From ff5ed22badad896d5a9f2094ac3b91f173692281 Mon Sep 17 00:00:00 2001 From: Gleiser Oliveira Date: Mon, 17 Jul 2023 14:44:18 -0600 Subject: [PATCH] refactor: use function valueOrBigIntZeroIfReverted --- .../isolated-pools/src/operations/update.ts | 22 ++++++------------- .../isolated-pools/src/utilities/index.ts | 1 + .../utilities/valueOrBigIntZeroIfReverted.ts | 9 ++++++++ 3 files changed, 17 insertions(+), 15 deletions(-) create mode 100644 subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts diff --git a/subgraphs/isolated-pools/src/operations/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index a83da045..88ad49da 100644 --- a/subgraphs/isolated-pools/src/operations/update.ts +++ b/subgraphs/isolated-pools/src/operations/update.ts @@ -3,8 +3,11 @@ import { Address, BigInt, Bytes } from '@graphprotocol/graph-ts'; import { PoolMetadataUpdatedNewMetadataStruct } from '../../generated/PoolRegistry/PoolRegistry'; import { AccountVToken, Market } from '../../generated/schema'; import { VToken } from '../../generated/templates/VToken/VToken'; -import { zeroBigInt32 } from '../constants'; -import { exponentToBigDecimal, getExchangeRateBigDecimal } from '../utilities'; +import { + exponentToBigDecimal, + getExchangeRateBigDecimal, + valueOrBigIntZeroIfReverted, +} from '../utilities'; import { getTokenPriceInUsd } from '../utilities'; import { getOrCreateMarket } from './getOrCreate'; import { @@ -197,19 +200,8 @@ export const updateMarket = ( .truncate(market.underlyingDecimals); // calling supplyRatePerBlock & borrowRatePerBlock can fail due to external reasons, so we fall back to 0 in case of an error - const borrowRatePerBlockResult = marketContract.try_borrowRatePerBlock(); - if (borrowRatePerBlockResult.reverted) { - market.borrowRateMantissa = zeroBigInt32; - } else { - market.borrowRateMantissa = borrowRatePerBlockResult.value; - } - - const supplyRatePerBlockResult = marketContract.try_supplyRatePerBlock(); - if (supplyRatePerBlockResult.reverted) { - market.supplyRateMantissa = zeroBigInt32; - } else { - market.supplyRateMantissa = supplyRatePerBlockResult.value; - } + market.borrowRateMantissa = valueOrBigIntZeroIfReverted(marketContract.try_borrowRatePerBlock()); + market.supplyRateMantissa = valueOrBigIntZeroIfReverted(marketContract.try_supplyRatePerBlock()); market.treasuryTotalBorrowsMantissa = marketContract.totalBorrows(); market.treasuryTotalSupplyMantissa = marketContract.totalSupply(); diff --git a/subgraphs/isolated-pools/src/utilities/index.ts b/subgraphs/isolated-pools/src/utilities/index.ts index ac748124..22131dc9 100644 --- a/subgraphs/isolated-pools/src/utilities/index.ts +++ b/subgraphs/isolated-pools/src/utilities/index.ts @@ -1,3 +1,4 @@ export { default as getExchangeRateBigDecimal } from './getExchangeRateBigDecimal'; export { default as getTokenPriceInUsd } from './getTokenPriceInUsd'; export { default as exponentToBigDecimal } from './exponentToBigDecimal'; +export { default as valueOrBigIntZeroIfReverted } from './valueOrBigIntZeroIfReverted'; diff --git a/subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts b/subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts new file mode 100644 index 00000000..8a3e2438 --- /dev/null +++ b/subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts @@ -0,0 +1,9 @@ +import { BigInt, ethereum } from '@graphprotocol/graph-ts'; + +import { zeroBigInt32 } from '../constants'; + +function valueOrBigIntZeroIfReverted(callResult: ethereum.CallResult): BigInt { + return callResult.reverted ? zeroBigInt32 : callResult.value; +} + +export default valueOrBigIntZeroIfReverted;