From 8435df88473abdc4ac51956fd3967658f8decb80 Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Thu, 2 Nov 2023 14:37:26 -0300 Subject: [PATCH] refactor: vToken amounts as mantissa --- subgraphs/isolated-pools/schema.graphql | 4 +- .../isolated-pools/src/mappings/vToken.ts | 2 - .../src/operations/getOrCreate.ts | 6 +-- .../isolated-pools/src/operations/update.ts | 17 +------ .../isolated-pools/src/utilities/index.ts | 1 + .../isolated-pools/tests/VToken/index.test.ts | 2 +- subgraphs/venus/schema.graphql | 10 ++-- subgraphs/venus/src/mappings/comptroller.ts | 5 +- subgraphs/venus/src/mappings/vToken.ts | 48 +++++++------------ subgraphs/venus/src/operations/create.ts | 16 +++---- subgraphs/venus/tests/Comptroller.test.ts | 2 +- subgraphs/venus/tests/VToken/index.test.ts | 2 +- 12 files changed, 41 insertions(+), 74 deletions(-) diff --git a/subgraphs/isolated-pools/schema.graphql b/subgraphs/isolated-pools/schema.graphql index af84bec5..52b1deb8 100644 --- a/subgraphs/isolated-pools/schema.graphql +++ b/subgraphs/isolated-pools/schema.graphql @@ -184,11 +184,11 @@ type AccountVToken @entity { enteredMarket: Boolean! "Total amount of underling redeemed" - totalUnderlyingRedeemedMantissa: BigDecimal! + totalUnderlyingRedeemedMantissa: BigInt! "The value of the borrow index upon users last interaction" accountBorrowIndexMantissa: BigInt! "Total amount underlying repaid" - totalUnderlyingRepaidMantissa: BigDecimal! + totalUnderlyingRepaidMantissa: BigInt! } """ diff --git a/subgraphs/isolated-pools/src/mappings/vToken.ts b/subgraphs/isolated-pools/src/mappings/vToken.ts index db16ba13..1b6ba2fe 100644 --- a/subgraphs/isolated-pools/src/mappings/vToken.ts +++ b/subgraphs/isolated-pools/src/mappings/vToken.ts @@ -258,8 +258,6 @@ export function handleTransfer(event: Transfer): void { event.logIndex, event.params.amount, market.exchangeRateMantissa, - market.underlyingDecimals, - market.vTokenDecimals, ); } diff --git a/subgraphs/isolated-pools/src/operations/getOrCreate.ts b/subgraphs/isolated-pools/src/operations/getOrCreate.ts index b8772029..47b375b8 100644 --- a/subgraphs/isolated-pools/src/operations/getOrCreate.ts +++ b/subgraphs/isolated-pools/src/operations/getOrCreate.ts @@ -13,7 +13,7 @@ import { } from '../../generated/schema'; import { Comptroller } from '../../generated/templates/Pool/Comptroller'; import { RewardsDistributor as RewardDistributorContract } from '../../generated/templates/RewardsDistributor/RewardsDistributor'; -import { zeroBigDecimal, zeroBigInt32 } from '../constants'; +import { zeroBigInt32 } from '../constants'; import { getAccountVTokenId, getAccountVTokenTransactionId, @@ -108,9 +108,9 @@ export const getOrCreateAccountVToken = ( accountVToken.accountSupplyBalanceMantissa = suppliedAmountMantissa; accountVToken.accountBorrowBalanceMantissa = borrowedAmountMantissa; - accountVToken.totalUnderlyingRedeemedMantissa = zeroBigDecimal; + accountVToken.totalUnderlyingRedeemedMantissa = zeroBigInt32; accountVToken.accountBorrowIndexMantissa = zeroBigInt32; - accountVToken.totalUnderlyingRepaidMantissa = zeroBigDecimal; + accountVToken.totalUnderlyingRepaidMantissa = zeroBigInt32; } return accountVToken; }; diff --git a/subgraphs/isolated-pools/src/operations/update.ts b/subgraphs/isolated-pools/src/operations/update.ts index 746e4bcd..e48c6673 100644 --- a/subgraphs/isolated-pools/src/operations/update.ts +++ b/subgraphs/isolated-pools/src/operations/update.ts @@ -3,11 +3,7 @@ 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 { - exponentToBigDecimal, - getExchangeRateBigDecimal, - valueOrNotAvailableIntIfReverted, -} from '../utilities'; +import { exponentToBigInt, valueOrNotAvailableIntIfReverted } from '../utilities'; import { getTokenPriceInCents } from '../utilities'; import { getOrCreateMarket } from './getOrCreate'; import { @@ -118,17 +114,8 @@ export const updateAccountVTokenTransferFrom = ( logIndex: BigInt, amount: BigInt, exchangeRate: BigInt, - underlyingDecimals: i32, - vTokenDecimals: i32, ): AccountVToken => { - const exchangeRateBigDecimal = getExchangeRateBigDecimal( - exchangeRate, - underlyingDecimals, - vTokenDecimals, - ); - const amountUnderlyingMantissa = exchangeRateBigDecimal - .times(exponentToBigDecimal(underlyingDecimals)) - .times(amount.toBigDecimal()); + const amountUnderlyingMantissa = exchangeRate.div(exponentToBigInt(18)).times(amount); const accountVToken = updateAccountVToken( marketAddress, diff --git a/subgraphs/isolated-pools/src/utilities/index.ts b/subgraphs/isolated-pools/src/utilities/index.ts index 2a14a150..c454360c 100644 --- a/subgraphs/isolated-pools/src/utilities/index.ts +++ b/subgraphs/isolated-pools/src/utilities/index.ts @@ -1,4 +1,5 @@ export { default as getExchangeRateBigDecimal } from './getExchangeRateBigDecimal'; export { default as getTokenPriceInCents } from './getTokenPriceInCents'; export { default as exponentToBigDecimal } from './exponentToBigDecimal'; +export { default as exponentToBigInt } from './exponentToBigInt'; export { default as valueOrNotAvailableIntIfReverted } from './valueOrNotAvailableIntIfReverted'; diff --git a/subgraphs/isolated-pools/tests/VToken/index.test.ts b/subgraphs/isolated-pools/tests/VToken/index.test.ts index b468dbc2..5321ef02 100644 --- a/subgraphs/isolated-pools/tests/VToken/index.test.ts +++ b/subgraphs/isolated-pools/tests/VToken/index.test.ts @@ -661,7 +661,7 @@ describe('VToken', () => { 'AccountVToken', accountVTokenId, 'totalUnderlyingRedeemedMantissa', - '53371670178204461670107500000000000000', + '53371549778058610525', ); }); diff --git a/subgraphs/venus/schema.graphql b/subgraphs/venus/schema.graphql index 7dadc7f8..82444b7d 100644 --- a/subgraphs/venus/schema.graphql +++ b/subgraphs/venus/schema.graphql @@ -24,7 +24,7 @@ type Market @entity { "The vToken contract balance of BEP20 or BNB" cashMantissa: BigInt! "Collateral factor determining how much one can borrow" - collateralFactor: BigDecimal! + collateralFactorMantissa: BigInt! "Exchange rate of tokens / vTokens" exchangeRateMantissa: BigInt! "Address of the interest rate model" @@ -116,7 +116,7 @@ type AccountVToken @entity { enteredMarket: Boolean! "VToken balance of the user" - vTokenBalance: BigDecimal! + vTokenBalanceMantissa: BigInt! "Total amount of underlying supplied" totalUnderlyingSuppliedMantissa: BigInt! "Total amount of underling redeemed" @@ -124,11 +124,11 @@ type AccountVToken @entity { "The value of the borrow index upon users last interaction" accountBorrowIndexMantissa: BigInt! "Total amount underlying borrowed, exclusive of interest" - totalUnderlyingBorrowed: BigDecimal! + totalUnderlyingBorrowedMantissa: BigInt! "Total amount underlying repaid" - totalUnderlyingRepaid: BigDecimal! + totalUnderlyingRepaidMantissa: BigInt! "Current borrow balance stored in contract (exclusive of interest since accrualBlockNumber)" - storedBorrowBalance: BigDecimal! + storedBorrowBalanceMantissa: BigInt! } """ diff --git a/subgraphs/venus/src/mappings/comptroller.ts b/subgraphs/venus/src/mappings/comptroller.ts index 3c471609..f599f61a 100644 --- a/subgraphs/venus/src/mappings/comptroller.ts +++ b/subgraphs/venus/src/mappings/comptroller.ts @@ -14,7 +14,6 @@ import { } from '../../generated/Comptroller/Comptroller'; import { Account, Market } from '../../generated/schema'; import { VToken } from '../../generated/templates'; -import { mantissaFactorBigDecimal } from '../constants'; import { createAccount, createMarket } from '../operations/create'; import { getOrCreateComptroller } from '../operations/getOrCreate'; import { updateCommonVTokenStats } from '../operations/update'; @@ -110,9 +109,7 @@ export const handleNewCollateralFactor = (event: NewCollateralFactor): void => { // sources can source from the contract creation block and not the time the // comptroller adds the market, we can avoid this altogether if (market != null) { - market.collateralFactor = event.params.newCollateralFactorMantissa - .toBigDecimal() - .div(mantissaFactorBigDecimal); + market.collateralFactorMantissa = event.params.newCollateralFactorMantissa; market.save(); } }; diff --git a/subgraphs/venus/src/mappings/vToken.ts b/subgraphs/venus/src/mappings/vToken.ts index e58f814a..391eb3a7 100644 --- a/subgraphs/venus/src/mappings/vToken.ts +++ b/subgraphs/venus/src/mappings/vToken.ts @@ -39,7 +39,7 @@ import { } from '../operations/create'; import { updateCommonVTokenStats } from '../operations/update'; import { updateMarket } from '../operations/update'; -import { exponentToBigDecimal } from '../utilities/exponentToBigDecimal'; +import { exponentToBigInt } from '../utilities/exponentToBigInt'; import { getMarketId, getTransactionId } from '../utilities/ids'; /* Account supplies assets into market and receives vTokens in exchange @@ -144,17 +144,12 @@ export const handleBorrow = (event: Borrow): void => { event.logIndex, ); - let borrowAmountBD = event.params.borrowAmount - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)); - - vTokenStats.storedBorrowBalance = event.params.accountBorrows - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)) - .truncate(market.underlyingDecimals); + vTokenStats.storedBorrowBalanceMantissa = event.params.accountBorrows; vTokenStats.accountBorrowIndexMantissa = market.borrowIndexMantissa; - vTokenStats.totalUnderlyingBorrowed = vTokenStats.totalUnderlyingBorrowed.plus(borrowAmountBD); + vTokenStats.totalUnderlyingBorrowedMantissa = vTokenStats.totalUnderlyingBorrowedMantissa.plus( + event.params.borrowAmount, + ); vTokenStats.save(); let borrowID = event.transaction.hash @@ -216,17 +211,12 @@ export const handleRepayBorrow = (event: RepayBorrow): void => { event.logIndex, ); - let repayAmountBD = event.params.repayAmount - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)); - - vTokenStats.storedBorrowBalance = event.params.accountBorrows - .toBigDecimal() - .div(exponentToBigDecimal(market.underlyingDecimals)) - .truncate(market.underlyingDecimals); + vTokenStats.storedBorrowBalanceMantissa = event.params.accountBorrows; vTokenStats.accountBorrowIndexMantissa = market.borrowIndexMantissa; - vTokenStats.totalUnderlyingRepaid = vTokenStats.totalUnderlyingRepaid.plus(repayAmountBD); + vTokenStats.totalUnderlyingRepaidMantissa = vTokenStats.totalUnderlyingRepaidMantissa.plus( + event.params.repayAmount, + ); vTokenStats.save(); let repayID = event.transaction.hash @@ -344,8 +334,10 @@ export const handleTransfer = (event: Transfer): void => { if (market.accrualBlockNumber != event.block.number.toI32()) { market = updateMarket(event.address, event.block.number.toI32(), event.block.timestamp.toI32()); } - let vTokenDecimals = market.vTokenDecimals; - let amountUnderlying = market.exchangeRateMantissa.times(event.params.amount); + + let amountUnderlying = market.exchangeRateMantissa + .times(event.params.amount) + .div(exponentToBigInt(18)); // Checking if the tx is FROM the vToken contract (i.e. this will not run when minting) // If so, it is a mint, and we don't need to run these calculations @@ -368,11 +360,8 @@ export const handleTransfer = (event: Transfer): void => { event.logIndex, ); - vTokenStatsFrom.vTokenBalance = vTokenStatsFrom.vTokenBalance.minus( - event.params.amount - .toBigDecimal() - .div(exponentToBigDecimal(vTokenDecimals)) - .truncate(vTokenDecimals), + vTokenStatsFrom.vTokenBalanceMantissa = vTokenStatsFrom.vTokenBalanceMantissa.minus( + event.params.amount, ); vTokenStatsFrom.totalUnderlyingRedeemedMantissa = @@ -403,11 +392,8 @@ export const handleTransfer = (event: Transfer): void => { event.logIndex, ); - vTokenStatsTo.vTokenBalance = vTokenStatsTo.vTokenBalance.plus( - event.params.amount - .toBigDecimal() - .div(exponentToBigDecimal(vTokenDecimals)) - .truncate(vTokenDecimals), + vTokenStatsTo.vTokenBalanceMantissa = vTokenStatsTo.vTokenBalanceMantissa.plus( + event.params.amount, ); vTokenStatsTo.totalUnderlyingSuppliedMantissa = diff --git a/subgraphs/venus/src/operations/create.ts b/subgraphs/venus/src/operations/create.ts index 30a0c6f4..5c79e661 100644 --- a/subgraphs/venus/src/operations/create.ts +++ b/subgraphs/venus/src/operations/create.ts @@ -1,10 +1,10 @@ -import { Address, BigDecimal, BigInt, log } from '@graphprotocol/graph-ts'; +import { Address, BigInt, log } from '@graphprotocol/graph-ts'; import { Account, AccountVToken, Market, MintEvent, RedeemEvent } from '../../generated/schema'; import { BEP20 } from '../../generated/templates/VToken/BEP20'; import { VBep20Storage } from '../../generated/templates/VToken/VBep20Storage'; import { VToken } from '../../generated/templates/VToken/VToken'; -import { zeroBigDecimal, zeroBigInt32 } from '../constants'; +import { zeroBigInt32 } from '../constants'; import { nullAddress, vBnbAddress } from '../constants/addresses'; import { getUnderlyingPrice } from '../utilities/getUnderlyingPrice'; import { getTransactionId } from '../utilities/ids'; @@ -22,16 +22,14 @@ export function createAccountVToken( accountVToken.accrualBlockNumber = BigInt.fromI32(0); // we need to set an initial real onchain value to this otherwise it will never be accurate const vTokenContract = BEP20.bind(Address.fromString(marketId)); - accountVToken.vTokenBalance = new BigDecimal( - vTokenContract.balanceOf(Address.fromString(account)), - ); + accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(Address.fromString(account)); accountVToken.totalUnderlyingSuppliedMantissa = zeroBigInt32; accountVToken.totalUnderlyingRedeemedMantissa = zeroBigInt32; accountVToken.accountBorrowIndexMantissa = zeroBigInt32; - accountVToken.totalUnderlyingBorrowed = zeroBigDecimal; - accountVToken.totalUnderlyingRepaid = zeroBigDecimal; - accountVToken.storedBorrowBalance = zeroBigDecimal; + accountVToken.totalUnderlyingBorrowedMantissa = zeroBigInt32; + accountVToken.totalUnderlyingRepaidMantissa = zeroBigInt32; + accountVToken.storedBorrowBalanceMantissa = zeroBigInt32; accountVToken.enteredMarket = false; return accountVToken; } @@ -83,7 +81,7 @@ export function createMarket(marketAddress: string): Market { market.borrowRateMantissa = zeroBigInt32; market.cashMantissa = zeroBigInt32; - market.collateralFactor = zeroBigDecimal; + market.collateralFactorMantissa = zeroBigInt32; market.exchangeRateMantissa = zeroBigInt32; market.interestRateModelAddress = interestRateModelAddress.reverted ? nullAddress diff --git a/subgraphs/venus/tests/Comptroller.test.ts b/subgraphs/venus/tests/Comptroller.test.ts index 15d9e624..85b2d82d 100644 --- a/subgraphs/venus/tests/Comptroller.test.ts +++ b/subgraphs/venus/tests/Comptroller.test.ts @@ -57,7 +57,7 @@ describe('handleMarketListing', () => { assertMarketDocument('underlyingPriceCents', '0'); assertMarketDocument('borrowRateMantissa', '0'); assertMarketDocument('cashMantissa', '0'); - assertMarketDocument('collateralFactor', '0'); + assertMarketDocument('collateralFactorMantissa', '0'); assertMarketDocument('exchangeRateMantissa', '0'); assertMarketDocument('interestRateModelAddress', interestRateModelAddress.toHex()); assertMarketDocument('name', 'Venus BNB'); diff --git a/subgraphs/venus/tests/VToken/index.test.ts b/subgraphs/venus/tests/VToken/index.test.ts index 0dc3b0e7..e3da492d 100644 --- a/subgraphs/venus/tests/VToken/index.test.ts +++ b/subgraphs/venus/tests/VToken/index.test.ts @@ -374,7 +374,7 @@ describe('VToken', () => { 'AccountVToken', accountVTokenId, 'totalUnderlyingRedeemedMantissa', - '53371670178204461670107500000000000000', + '53371670178204461670', ); });