Skip to content

Commit

Permalink
refactor: use function valueOrBigIntZeroIfReverted
Browse files Browse the repository at this point in the history
  • Loading branch information
gleiser-oliveira committed Jul 17, 2023
1 parent a845999 commit ff5ed22
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
22 changes: 7 additions & 15 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions subgraphs/isolated-pools/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BigInt, ethereum } from '@graphprotocol/graph-ts';

import { zeroBigInt32 } from '../constants';

function valueOrBigIntZeroIfReverted(callResult: ethereum.CallResult<BigInt>): BigInt {
return callResult.reverted ? zeroBigInt32 : callResult.value;
}

export default valueOrBigIntZeroIfReverted;

0 comments on commit ff5ed22

Please sign in to comment.