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 f338729
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 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
26 changes: 16 additions & 10 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { VToken } from '../../generated/templates/VToken/VToken';
import {
exponentToBigDecimal,
getExchangeRateBigDecimal,
valueOrBigIntZeroIfReverted,
valueOrNotAvailableIntIfReverted,
} from '../utilities';
import { getTokenPriceInUsd } from '../utilities';
import { getOrCreateMarket } from './getOrCreate';
Expand Down Expand Up @@ -184,31 +184,37 @@ export const updateMarket = (
);
market.underlyingPriceUsd = tokenPriceUsd.truncate(market.underlyingDecimals);

market.accrualBlockNumber = marketContract.accrualBlockNumber().toI32();
market.accrualBlockNumber = valueOrNotAvailableIntIfReverted(
marketContract.try_accrualBlockNumber(),
).toI32();
market.blockTimestamp = blockTimestamp;

market.exchangeRateMantissa = valueOrBigIntZeroIfReverted(
market.exchangeRateMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_exchangeRateStored(),
);

market.borrowIndexMantissa = valueOrBigIntZeroIfReverted(marketContract.try_borrowIndex());
market.borrowIndexMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_borrowIndex());

market.reservesMantissa = valueOrBigIntZeroIfReverted(marketContract.try_totalReserves());
market.reservesMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_totalReserves());

const cashBigInt = valueOrBigIntZeroIfReverted(marketContract.try_getCash());
const cashBigInt = valueOrNotAvailableIntIfReverted(marketContract.try_getCash());
market.cash = cashBigInt
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);

// calling supplyRatePerBlock & borrowRatePerBlock can fail due to external reasons, so we fall back to 0 in case of an error
market.borrowRateMantissa = valueOrBigIntZeroIfReverted(marketContract.try_borrowRatePerBlock());
market.supplyRateMantissa = valueOrBigIntZeroIfReverted(marketContract.try_supplyRatePerBlock());
market.borrowRateMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_borrowRatePerBlock(),
);
market.supplyRateMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_supplyRatePerBlock(),
);

market.treasuryTotalBorrowsMantissa = valueOrBigIntZeroIfReverted(
market.treasuryTotalBorrowsMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_totalBorrows(),
);
market.treasuryTotalSupplyMantissa = valueOrBigIntZeroIfReverted(
market.treasuryTotalSupplyMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_totalSupply(),
);

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 f338729

Please sign in to comment.