Skip to content

Commit

Permalink
refactor: save vToken event amounts as mantissa
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Nov 2, 2023
1 parent ccda02b commit 80741a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 45 deletions.
18 changes: 9 additions & 9 deletions subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ type TransferEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
amount: BigDecimal!
amountMantissa: BigInt!
"Account that received tokens"
to: Bytes!
"Account that sent tokens"
Expand Down Expand Up @@ -244,7 +244,7 @@ type LiquidationEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens seized"
amount: BigDecimal!
amountMantissa: BigInt!
"Liquidator receiving tokens"
to: Bytes!
"Account being liquidated (borrower)"
Expand All @@ -258,7 +258,7 @@ type LiquidationEvent implements VTokenTransfer @entity {
"Symbol of the underlying asset repaid through liquidation"
underlyingSymbol: String!
"Underlying vToken amount that was repaid by liquidator"
underlyingRepayAmount: BigDecimal!
underlyingRepayAmountMantissa: BigInt!
}

"""
Expand All @@ -269,9 +269,9 @@ interface UnderlyingTransfer {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying borrowed"
amount: BigDecimal!
amountMantissa: BigInt!
"Total borrows of this asset the account has"
accountBorrows: BigDecimal!
accountBorrowsMantissa: BigInt!
"Account that borrowed the tokens"
borrower: Bytes!
"Block number"
Expand All @@ -289,9 +289,9 @@ type BorrowEvent implements UnderlyingTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying borrowed"
amount: BigDecimal!
amountMantissa: BigInt!
"Total borrows of this asset the account has"
accountBorrows: BigDecimal!
accountBorrowsMantissa: BigInt!
"Account that borrowed the tokens"
borrower: Bytes!
"Block number"
Expand All @@ -310,9 +310,9 @@ type RepayEvent implements UnderlyingTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying repaid"
amount: BigDecimal!
amountMantissa: BigInt!
"Total borrows of this asset the account has"
accountBorrows: BigDecimal!
accountBorrowsMantissa: BigInt!
"Account that borrowed the tokens"
borrower: Bytes!
"Block number"
Expand Down
43 changes: 7 additions & 36 deletions subgraphs/venus/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,9 @@ export const handleBorrow = (event: Borrow): void => {
.concat('-')
.concat(event.transactionLogIndex.toString());

let borrowAmount = event.params.borrowAmount
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);

let accountBorrows = event.params.accountBorrows
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);

let borrow = new BorrowEvent(borrowID);
borrow.amount = borrowAmount;
borrow.accountBorrows = accountBorrows;
borrow.amountMantissa = event.params.borrowAmount;
borrow.accountBorrowsMantissa = event.params.accountBorrows;
borrow.borrower = event.params.borrower;
borrow.blockNumber = event.block.number.toI32();
borrow.blockTime = event.block.timestamp.toI32();
Expand Down Expand Up @@ -244,19 +234,9 @@ export const handleRepayBorrow = (event: RepayBorrow): void => {
.concat('-')
.concat(event.transactionLogIndex.toString());

let repayAmount = event.params.repayAmount
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);

let accountBorrows = event.params.accountBorrows
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);

let repay = new RepayEvent(repayID);
repay.amount = repayAmount;
repay.accountBorrows = accountBorrows;
repay.amountMantissa = event.params.repayAmount;
repay.accountBorrowsMantissa = event.params.accountBorrows;
repay.borrower = event.params.borrower;
repay.blockNumber = event.block.number.toI32();
repay.blockTime = event.block.timestamp.toI32();
Expand Down Expand Up @@ -325,24 +305,15 @@ export const handleLiquidateBorrow = (event: LiquidateBorrow): void => {
.toHexString()
.concat('-')
.concat(event.transactionLogIndex.toString());
let vTokenDecimals = marketRepayToken.vTokenDecimals;
let vTokenAmount = event.params.seizeTokens
.toBigDecimal()
.div(exponentToBigDecimal(vTokenDecimals))
.truncate(vTokenDecimals);
let underlyingRepayAmount = event.params.repayAmount
.toBigDecimal()
.div(exponentToBigDecimal(marketRepayToken.underlyingDecimals))
.truncate(marketRepayToken.underlyingDecimals);

let liquidation = new LiquidationEvent(mintID);
liquidation.amount = vTokenAmount;
liquidation.amountMantissa = event.params.seizeTokens;
liquidation.to = event.params.liquidator;
liquidation.from = event.params.borrower;
liquidation.blockNumber = event.block.number.toI32();
liquidation.blockTime = event.block.timestamp.toI32();
liquidation.underlyingSymbol = marketRepayToken.underlyingSymbol;
liquidation.underlyingRepayAmount = underlyingRepayAmount;
liquidation.underlyingRepayAmountMantissa = event.params.repayAmount;
liquidation.vTokenSymbol = marketVTokenLiquidated.symbol;
liquidation.save();
};
Expand Down Expand Up @@ -447,7 +418,7 @@ export const handleTransfer = (event: Transfer): void => {
let transferId = getTransactionId(event.transaction.hash, event.transactionLogIndex);

let transfer = new TransferEvent(transferId);
transfer.amount = event.params.amount.toBigDecimal().div(exponentToBigDecimal(vTokenDecimals));
transfer.amountMantissa = event.params.amount;
transfer.to = event.params.to;
transfer.from = event.params.from;
transfer.blockNumber = event.block.number.toI32();
Expand Down

0 comments on commit 80741a6

Please sign in to comment.