Skip to content

Commit

Permalink
refactor: vToken amounts as mantissa
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Nov 9, 2023
1 parent 80741a6 commit efc1ba5
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 72 deletions.
4 changes: 2 additions & 2 deletions subgraphs/isolated-pools/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}

"""
Expand Down
2 changes: 0 additions & 2 deletions subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ export function handleTransfer(event: Transfer): void {
event.logIndex,
event.params.amount,
market.exchangeRateMantissa,
market.underlyingDecimals,
market.vTokenDecimals,
);
}

Expand Down
6 changes: 3 additions & 3 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};
Expand Down
17 changes: 2 additions & 15 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
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,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';
10 changes: 5 additions & 5 deletions subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -116,19 +116,19 @@ 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"
totalUnderlyingRedeemedMantissa: BigInt!
"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!
}

"""
Expand Down
5 changes: 1 addition & 4 deletions subgraphs/venus/src/mappings/comptroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}
};
Expand Down
48 changes: 17 additions & 31 deletions subgraphs/venus/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down
16 changes: 7 additions & 9 deletions subgraphs/venus/src/operations/create.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/venus/tests/Comptroller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit efc1ba5

Please sign in to comment.