Skip to content

Commit

Permalink
refactor: use valueOrNotAvailableIntIfReverted in updateMarket contra…
Browse files Browse the repository at this point in the history
…ct calls
  • Loading branch information
gleiser-oliveira committed Jul 17, 2023
1 parent db7f354 commit e8fc08a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
3 changes: 3 additions & 0 deletions subgraphs/isolated-pools/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
16 changes: 9 additions & 7 deletions subgraphs/isolated-pools/src/utilities/getTokenPriceInUsd.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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.
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -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';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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>): BigInt {
return callResult.reverted ? NOT_AVAILABLE_BIG_INT : callResult.value;
}

export default valueOrNotAvailableIntIfReverted;

0 comments on commit e8fc08a

Please sign in to comment.