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/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..b3528ed9 100644 --- a/subgraphs/venus/src/mappings/vToken.ts +++ b/subgraphs/venus/src/mappings/vToken.ts @@ -39,7 +39,6 @@ import { } from '../operations/create'; import { updateCommonVTokenStats } from '../operations/update'; import { updateMarket } from '../operations/update'; -import { exponentToBigDecimal } from '../utilities/exponentToBigDecimal'; import { getMarketId, getTransactionId } from '../utilities/ids'; /* Account supplies assets into market and receives vTokens in exchange @@ -144,17 +143,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 +210,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,7 +333,7 @@ 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); // Checking if the tx is FROM the vToken contract (i.e. this will not run when minting) @@ -368,11 +357,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 +389,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');