From 7a67b172badaf3a5feaadb5ab469082770e9703d Mon Sep 17 00:00:00 2001 From: Corey Rice <coreyarice@gmail.com> Date: Wed, 20 Sep 2023 19:13:16 -0300 Subject: [PATCH] refactor: cash mantissa --- subgraphs/isolated-pools/schema.graphql | 2 +- subgraphs/isolated-pools/src/operations/create.ts | 6 +----- subgraphs/isolated-pools/src/operations/update.ts | 7 ++----- .../src/utilities/exponentToBigInt.ts | 2 +- .../src/utilities/getTokenPriceInCents.ts | 4 ++-- subgraphs/isolated-pools/tests/VToken/index.test.ts | 2 +- subgraphs/isolated-pools/tests/integration/pool.ts | 2 +- .../integration/queries/marketByIdQuery.graphql | 2 +- .../tests/integration/queries/marketsQuery.graphql | 2 +- subgraphs/venus/schema.graphql | 2 +- subgraphs/venus/src/operations/create.ts | 8 ++++---- subgraphs/venus/src/operations/update.ts | 13 ++----------- subgraphs/venus/src/utilities/getUnderlyingPrice.ts | 4 ++-- subgraphs/venus/src/utilities/index.ts | 2 +- subgraphs/venus/tests/Comptroller.test.ts | 2 +- subgraphs/venus/tests/VToken/index.test.ts | 2 +- 16 files changed, 23 insertions(+), 39 deletions(-) diff --git a/subgraphs/isolated-pools/schema.graphql b/subgraphs/isolated-pools/schema.graphql index 4ae17a37..7793fabf 100644 --- a/subgraphs/isolated-pools/schema.graphql +++ b/subgraphs/isolated-pools/schema.graphql @@ -78,7 +78,7 @@ type Market @entity { "Borrow rate per block" borrowRateMantissa: BigInt! "The vToken contract balance of BEP20 or BNB" - cash: BigDecimal! + cashMantissa: BigInt! "Collateral factor determining how much one can borrow" collateralFactorMantissa: BigInt! "Exchange rate of tokens / vTokens" diff --git a/subgraphs/isolated-pools/src/operations/create.ts b/subgraphs/isolated-pools/src/operations/create.ts index 3336f175..567b770f 100644 --- a/subgraphs/isolated-pools/src/operations/create.ts +++ b/subgraphs/isolated-pools/src/operations/create.ts @@ -110,11 +110,7 @@ export function createMarket( market.borrowRateMantissa = vTokenContract.borrowRatePerBlock(); - market.cash = vTokenContract - .getCash() - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)) - .truncate(market.underlyingDecimals); + market.cashMantissa = vTokenContract.getCash(); market.exchangeRateMantissa = vTokenContract.exchangeRateStored(); diff --git a/subgraphs/isolated-pools/src/operations/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index 73bac358..d709809a 100644 --- a/subgraphs/isolated-pools/src/operations/update.ts +++ b/subgraphs/isolated-pools/src/operations/update.ts @@ -176,7 +176,7 @@ export const updateMarket = ( vTokenAddress, market.underlyingDecimals, ); - market.underlyingPriceCents = tokenPriceCents.truncate(market.underlyingDecimals); + market.underlyingPriceCents = tokenPriceCents; market.accrualBlockNumber = valueOrNotAvailableIntIfReverted( marketContract.try_accrualBlockNumber(), @@ -192,10 +192,7 @@ export const updateMarket = ( market.reservesMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_totalReserves()); const cashBigInt = valueOrNotAvailableIntIfReverted(marketContract.try_getCash()); - market.cash = cashBigInt - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)) - .truncate(market.underlyingDecimals); + market.cashMantissa = cashBigInt; // calling supplyRatePerBlock & borrowRatePerBlock can fail due to external reasons, so we fall back to 0 in case of an error market.borrowRateMantissa = valueOrNotAvailableIntIfReverted( diff --git a/subgraphs/isolated-pools/src/utilities/exponentToBigInt.ts b/subgraphs/isolated-pools/src/utilities/exponentToBigInt.ts index fbecec9f..dd63f053 100644 --- a/subgraphs/isolated-pools/src/utilities/exponentToBigInt.ts +++ b/subgraphs/isolated-pools/src/utilities/exponentToBigInt.ts @@ -1,6 +1,6 @@ import { BigInt } from '@graphprotocol/graph-ts'; -function exponentToBigInt(decimals: i32): i32 { +function exponentToBigInt(decimals: i32): BigInt { let bd = BigInt.fromString('1'); for (let i = 0; i < decimals; i++) { bd = bd.times(BigInt.fromString('10')); diff --git a/subgraphs/isolated-pools/src/utilities/getTokenPriceInCents.ts b/subgraphs/isolated-pools/src/utilities/getTokenPriceInCents.ts index 5bdae62c..67761bf1 100644 --- a/subgraphs/isolated-pools/src/utilities/getTokenPriceInCents.ts +++ b/subgraphs/isolated-pools/src/utilities/getTokenPriceInCents.ts @@ -1,4 +1,4 @@ -import { Address } from '@graphprotocol/graph-ts'; +import { Address, BigInt } from '@graphprotocol/graph-ts'; import { PriceOracle } from '../../generated/templates/VToken/PriceOracle'; import { NOT_AVAILABLE_BIG_INT } from '../constants'; @@ -11,7 +11,7 @@ const getTokenPriceInCents = ( poolAddress: Address, eventAddress: Address, underlyingDecimals: i32, -): i32 => { +): BigInt => { const pool = getPool(poolAddress); // will return NOT_AVAILABLE if the price cannot be fetched let underlyingPrice = NOT_AVAILABLE_BIG_INT; diff --git a/subgraphs/isolated-pools/tests/VToken/index.test.ts b/subgraphs/isolated-pools/tests/VToken/index.test.ts index f37936fa..b77af785 100644 --- a/subgraphs/isolated-pools/tests/VToken/index.test.ts +++ b/subgraphs/isolated-pools/tests/VToken/index.test.ts @@ -482,7 +482,7 @@ describe('VToken', () => { assertMarketDocument('borrowIndexMantissa', '300000000000000000000'); assertMarketDocument('reservesMantissa', '5128924555022289393'); assertMarketDocument('totalBorrowsMantissa', '2641234234636158123'); - assertMarketDocument('cash', '1.418171344423412457'); + assertMarketDocument('cashMantissa', '1418171344423412457'); assertMarketDocument('borrowRateMantissa', '12678493'); assertMarketDocument('supplyRateMantissa', '12678493'); }); diff --git a/subgraphs/isolated-pools/tests/integration/pool.ts b/subgraphs/isolated-pools/tests/integration/pool.ts index db161cf3..68b6fd18 100644 --- a/subgraphs/isolated-pools/tests/integration/pool.ts +++ b/subgraphs/isolated-pools/tests/integration/pool.ts @@ -135,7 +135,7 @@ describe('Pools', function () { markets.forEach((m, idx) => { expect(m.pool.id).to.equal(pool.id); expect(m.borrowRateMantissa).to.equal('0'); - expect(m.cash).to.equal('1'); + expect(m.cashMantissa).to.equal('1'); expect(m.collateralFactorMantissa).to.equal('700000000000000000'); expect(m.exchangeRateMantissa).to.equal('10000000000000000000000000000'); expect(m.interestRateModelAddress).to.equal(interestRateModelAddresses[idx]); diff --git a/subgraphs/isolated-pools/tests/integration/queries/marketByIdQuery.graphql b/subgraphs/isolated-pools/tests/integration/queries/marketByIdQuery.graphql index c3c008b6..7f7ebc53 100644 --- a/subgraphs/isolated-pools/tests/integration/queries/marketByIdQuery.graphql +++ b/subgraphs/isolated-pools/tests/integration/queries/marketByIdQuery.graphql @@ -6,7 +6,7 @@ query MarketById($id: ID!) { } badDebtMantissa borrowRateMantissa - cash + cashMantissa collateralFactorMantissa exchangeRateMantissa interestRateModelAddress diff --git a/subgraphs/isolated-pools/tests/integration/queries/marketsQuery.graphql b/subgraphs/isolated-pools/tests/integration/queries/marketsQuery.graphql index d07b7f8b..f9436083 100644 --- a/subgraphs/isolated-pools/tests/integration/queries/marketsQuery.graphql +++ b/subgraphs/isolated-pools/tests/integration/queries/marketsQuery.graphql @@ -6,7 +6,7 @@ query Markets { } badDebtMantissa borrowRateMantissa - cash + cashMantissa collateralFactorMantissa exchangeRateMantissa interestRateModelAddress diff --git a/subgraphs/venus/schema.graphql b/subgraphs/venus/schema.graphql index 6143fe6b..ca95039c 100644 --- a/subgraphs/venus/schema.graphql +++ b/subgraphs/venus/schema.graphql @@ -22,7 +22,7 @@ type Market @entity { "Borrow rate per block" borrowRateMantissa: BigInt! "The vToken contract balance of BEP20 or BNB" - cash: BigDecimal! + cashMantissa: BigInt! "Collateral factor determining how much one can borrow" collateralFactor: BigDecimal! "Exchange rate of tokens / vTokens" diff --git a/subgraphs/venus/src/operations/create.ts b/subgraphs/venus/src/operations/create.ts index 45024a92..f975b5be 100644 --- a/subgraphs/venus/src/operations/create.ts +++ b/subgraphs/venus/src/operations/create.ts @@ -58,7 +58,7 @@ export function createMarket(marketAddress: string): Market { market.underlyingDecimals = 18; market.underlyingName = 'Binance Coin'; market.underlyingSymbol = 'BNB'; - market.underlyingPriceCents = zeroBigDecimal; + market.underlyingPriceCents = zeroBigInt32; // It is all other VBEP20 contracts } else { market = new Market(marketAddress); @@ -71,8 +71,8 @@ export function createMarket(marketAddress: string): Market { market.underlyingName = underlyingContract.name(); market.underlyingSymbol = underlyingContract.symbol(); - const underlyingValue = getUnderlyingPrice(market.id, market.underlyingDecimals); - market.underlyingPriceCents = underlyingValue.underlyingPriceUsd; + const underlyingPriceCents = getUnderlyingPrice(market.id, market.underlyingDecimals); + market.underlyingPriceCents = underlyingPriceCents; } market.vTokenDecimals = contract.decimals(); @@ -81,7 +81,7 @@ export function createMarket(marketAddress: string): Market { const reserveFactor = contract.try_reserveFactorMantissa(); market.borrowRateMantissa = zeroBigInt32; - market.cash = zeroBigDecimal; + market.cashMantissa = zeroBigInt32; market.collateralFactor = zeroBigDecimal; market.exchangeRateMantissa = zeroBigInt32; market.interestRateModelAddress = interestRateModelAddress.reverted diff --git a/subgraphs/venus/src/operations/update.ts b/subgraphs/venus/src/operations/update.ts index 55464f5a..59047fcf 100644 --- a/subgraphs/venus/src/operations/update.ts +++ b/subgraphs/venus/src/operations/update.ts @@ -4,7 +4,6 @@ import { AccountVToken, Market } from '../../generated/schema'; import { VToken } from '../../generated/templates/VToken/VToken'; import { zeroBigInt32 } from '../constants'; import { createMarket } from '../operations/create'; -import { exponentToBigDecimal } from '../utilities/exponentToBigDecimal'; import { getUnderlyingPrice } from '../utilities/getUnderlyingPrice'; import { createAccountVToken } from './create'; import { getOrCreateAccountVTokenTransaction } from './getOrCreate'; @@ -43,17 +42,13 @@ export const updateMarket = ( if (market.accrualBlockNumber != blockNumber) { const contractAddress = Address.fromString(market.id); const contract = VToken.bind(contractAddress); - const vTokenDecimals = market.vTokenDecimals; market.accrualBlockNumber = contract.accrualBlockNumber().toI32(); market.blockTimestamp = blockTimestamp; const underlyingPriceCents = getUnderlyingPrice(market.id, market.underlyingDecimals); market.underlyingPriceCents = underlyingPriceCents; - market.totalSupplyMantissa = contract - .totalSupply() - .toBigDecimal() - .div(exponentToBigDecimal(vTokenDecimals)); + market.totalSupplyMantissa = contract.totalSupply(); /* Exchange rate explanation In Practice @@ -77,11 +72,7 @@ export const updateMarket = ( market.reservesMantissa = contract.totalReserves(); market.totalBorrowsMantissa = contract.totalBorrows(); - market.cash = contract - .getCash() - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)) - .truncate(market.underlyingDecimals); + market.cashMantissa = contract.getCash(); // Must convert to BigDecimal, and remove 10^18 that is used for Exp in Venus Solidity const borrowRatePerBlock = contract.try_borrowRatePerBlock(); diff --git a/subgraphs/venus/src/utilities/getUnderlyingPrice.ts b/subgraphs/venus/src/utilities/getUnderlyingPrice.ts index f2d652e4..0485b75b 100644 --- a/subgraphs/venus/src/utilities/getUnderlyingPrice.ts +++ b/subgraphs/venus/src/utilities/getUnderlyingPrice.ts @@ -1,8 +1,8 @@ -import { Address } from '@graphprotocol/graph-ts'; +import { Address, BigInt } from '@graphprotocol/graph-ts'; import { getTokenPriceCents } from './getTokenPriceCents'; -export function getUnderlyingPrice(address: string, underlyingDecimals: i32): i32 { +export function getUnderlyingPrice(address: string, underlyingDecimals: i32): BigInt { const contractAddress = Address.fromString(address); const underlyingPriceCents = getTokenPriceCents(contractAddress, underlyingDecimals); diff --git a/subgraphs/venus/src/utilities/index.ts b/subgraphs/venus/src/utilities/index.ts index 687ddf4f..d409e6a1 100644 --- a/subgraphs/venus/src/utilities/index.ts +++ b/subgraphs/venus/src/utilities/index.ts @@ -1,5 +1,5 @@ export { getBnbPriceInUsd } from './getBnbPriceInUsd'; -export { getTokenPrice } from './getTokenPrice'; +export { getTokenPriceCents } from './getTokenPriceCents'; export { exponentToBigDecimal } from './exponentToBigDecimal'; export { ensureComptrollerSynced } from './ensureComptrollerSynced'; export { getUnderlyingPrice } from './getUnderlyingPrice'; diff --git a/subgraphs/venus/tests/Comptroller.test.ts b/subgraphs/venus/tests/Comptroller.test.ts index c7f470fa..15d9e624 100644 --- a/subgraphs/venus/tests/Comptroller.test.ts +++ b/subgraphs/venus/tests/Comptroller.test.ts @@ -56,7 +56,7 @@ describe('handleMarketListing', () => { assertMarketDocument('underlyingSymbol', 'BNB'); assertMarketDocument('underlyingPriceCents', '0'); assertMarketDocument('borrowRateMantissa', '0'); - assertMarketDocument('cash', '0'); + assertMarketDocument('cashMantissa', '0'); assertMarketDocument('collateralFactor', '0'); assertMarketDocument('exchangeRateMantissa', '0'); assertMarketDocument('interestRateModelAddress', interestRateModelAddress.toHex()); diff --git a/subgraphs/venus/tests/VToken/index.test.ts b/subgraphs/venus/tests/VToken/index.test.ts index 674442ca..8a704b78 100644 --- a/subgraphs/venus/tests/VToken/index.test.ts +++ b/subgraphs/venus/tests/VToken/index.test.ts @@ -298,7 +298,7 @@ describe('VToken', () => { assertMarketDocument('borrowIndexMantissa', '300000000000000000000'); assertMarketDocument('reservesMantissa', '5128924555022289393'); assertMarketDocument('totalBorrowsMantissa', '2641234234636158123'); - assertMarketDocument('cash', '1.418171344423412457'); + assertMarketDocument('cashMantissa', '1418171344423412457'); assertMarketDocument('borrowRateMantissa', '12678493'); assertMarketDocument('supplyRateMantissa', '12678493'); });