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 c05d642
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 52 deletions.
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
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
43 changes: 13 additions & 30 deletions subgraphs/venus/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 =
Expand Down Expand Up @@ -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 =
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 c05d642

Please sign in to comment.