diff --git a/subgraphs/isolated-pools/src/constants/index.ts b/subgraphs/isolated-pools/src/constants/index.ts index 996fa796..533cce0b 100644 --- a/subgraphs/isolated-pools/src/constants/index.ts +++ b/subgraphs/isolated-pools/src/constants/index.ts @@ -2,6 +2,9 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'; import exponentToBigDecimal from '../utilities/exponentToBigDecimal'; +export const NOT_AVAILABLE_BIG_INT = BigInt.fromString('-1'); +export const NOT_AVAILABLE_BIG_DECIMAL = BigDecimal.fromString('-1'); + export const zeroBigDecimal = BigDecimal.fromString('0'); export const zeroBigInt32 = BigInt.fromString('0'); export const oneBigInt = BigInt.fromString('1'); diff --git a/subgraphs/isolated-pools/src/utilities/getTokenPriceInUsd.ts b/subgraphs/isolated-pools/src/utilities/getTokenPriceInUsd.ts index 6d6c3eb1..a402936c 100644 --- a/subgraphs/isolated-pools/src/utilities/getTokenPriceInUsd.ts +++ b/subgraphs/isolated-pools/src/utilities/getTokenPriceInUsd.ts @@ -1,8 +1,10 @@ import { Address, BigDecimal } from '@graphprotocol/graph-ts'; import { PriceOracle } from '../../generated/templates/VToken/PriceOracle'; +import { NOT_AVAILABLE_BIG_DECIMAL } from '../constants'; import { getPool } from '../operations/get'; import exponentToBigDecimal from '../utilities/exponentToBigDecimal'; +import valueOrNotAvailableIntIfReverted from './valueOrNotAvailableIntIfReverted'; // Used for all vBEP20 contracts const getTokenPrice = ( @@ -11,7 +13,8 @@ const getTokenPrice = ( underlyingDecimals: i32, ): BigDecimal => { const pool = getPool(poolAddress); - let underlyingPrice = BigDecimal.zero(); + // will return NOT_AVAILABLE if the price cannot be fetched + let underlyingPrice = NOT_AVAILABLE_BIG_DECIMAL; if (pool && pool.priceOracleAddress) { const oracleAddress = Address.fromBytes(pool.priceOracleAddress); /* PriceOracle2 is used from starting of Comptroller. @@ -22,12 +25,11 @@ const getTokenPrice = ( */ const mantissaDecimalFactor = exponentToBigDecimal(36 - underlyingDecimals); const priceOracle = PriceOracle.bind(oracleAddress); - // Calling getUnderlyingPrice might revert if the pyth price pusher is unfunded - // On revert we will return 0 - const underlyingPriceResult = priceOracle.try_getUnderlyingPrice(eventAddress); - if (!underlyingPriceResult.reverted) { - underlyingPrice = underlyingPriceResult.value.toBigDecimal().div(mantissaDecimalFactor); - } + + const underlyingPriceBigInt = valueOrNotAvailableIntIfReverted( + priceOracle.try_getUnderlyingPrice(eventAddress), + ); + underlyingPrice = underlyingPriceBigInt.toBigDecimal().div(mantissaDecimalFactor); } return underlyingPrice; diff --git a/subgraphs/isolated-pools/src/utilities/index.ts b/subgraphs/isolated-pools/src/utilities/index.ts index 22131dc9..1689a0ff 100644 --- a/subgraphs/isolated-pools/src/utilities/index.ts +++ b/subgraphs/isolated-pools/src/utilities/index.ts @@ -1,4 +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'; +export { default as valueOrNotAvailableIntIfReverted } from './valueOrNotAvailableIntIfReverted'; diff --git a/subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts b/subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts deleted file mode 100644 index 8a3e2438..00000000 --- a/subgraphs/isolated-pools/src/utilities/valueOrBigIntZeroIfReverted.ts +++ /dev/null @@ -1,9 +0,0 @@ -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; diff --git a/subgraphs/isolated-pools/src/utilities/valueOrNotAvailableIntIfReverted.ts b/subgraphs/isolated-pools/src/utilities/valueOrNotAvailableIntIfReverted.ts new file mode 100644 index 00000000..b51b0c2c --- /dev/null +++ b/subgraphs/isolated-pools/src/utilities/valueOrNotAvailableIntIfReverted.ts @@ -0,0 +1,10 @@ +import { BigInt, ethereum } from '@graphprotocol/graph-ts'; + +import { NOT_AVAILABLE_BIG_INT } from '../constants'; + +// checks if a call reverted, in case it is we return -1 to indicate the wanted value is not available +function valueOrNotAvailableIntIfReverted(callResult: ethereum.CallResult): BigInt { + return callResult.reverted ? NOT_AVAILABLE_BIG_INT : callResult.value; +} + +export default valueOrNotAvailableIntIfReverted;