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 2, 2023
1 parent 80741a6 commit 4f9a6fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
8 changes: 4 additions & 4 deletions subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
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
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

0 comments on commit 4f9a6fa

Please sign in to comment.