Skip to content

Commit

Permalink
feat: support v1 and v2 mint and redeem events
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Nov 1, 2023
1 parent 34df4fb commit ccda02b
Show file tree
Hide file tree
Showing 10 changed files with 693 additions and 389 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@venusprotocol/governance-contracts": "^1.3.0",
"@venusprotocol/isolated-pools": "2.1.0-dev.2",
"@venusprotocol/oracle": "^1.7.3-dev.1",
"@venusprotocol/venus-protocol": "3.1.0",
"assemblyscript": "0.19.23",
"chai": "^4.3.6",
"eslint": "^8.25.0",
Expand Down
4 changes: 4 additions & 0 deletions subgraphs/venus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"prepare:bsc": "npx mustache config/bsc.json template.yaml > subgraph.yaml && npx mustache config/bsc.json src/constants/config-template > src/constants/config.ts",
"test": "graph test",
"test:integration": "true"
},
"dependencies": {
"@venusprotocol/venus-protocol": "3.1.0",
"@venusprotocol/venus-protocol-orig-events": "npm:@venusprotocol/[email protected]"
}
}
16 changes: 8 additions & 8 deletions subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ type MintEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
amount: BigDecimal!
amountMantissa: BigInt!
"Account that received tokens (minter)"
to: Bytes!
"Account that sent tokens (VToken contract)"
Expand All @@ -207,10 +207,10 @@ type MintEvent implements VTokenTransfer @entity {
blockNumber: Int!
"Block time"
blockTime: Int!
"Symbol of the vToken transferred"
vTokenSymbol: String!
"Address of the vToken transferred"
vTokenAddress: Bytes!
"Underlying token amount transferred"
underlyingAmount: BigDecimal
underlyingAmountMantissa: BigInt!
}

"""
Expand All @@ -221,7 +221,7 @@ type RedeemEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
amount: BigDecimal!
amountMantissa: BigInt!
"Account that received tokens (VToken contract)"
to: Bytes!
"Account that sent tokens (redeemer)"
Expand All @@ -230,10 +230,10 @@ type RedeemEvent implements VTokenTransfer @entity {
blockNumber: Int!
"Block time"
blockTime: Int!
"Symbol of the vToken transferred"
vTokenSymbol: String!
"Address of the vToken transferred"
vTokenAddress: Bytes!
"Underlying token amount transferred"
underlyingAmount: BigDecimal
underlyingAmountMantissa: BigInt!
}

"""
Expand Down
18 changes: 9 additions & 9 deletions subgraphs/venus/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export const zeroBigInt32 = BigInt.fromString('0');
export const oneBigInt = BigInt.fromString('1');

export const Actions = [
MINT,
REDEEM,
BORROW,
REPAY,
SEIZE,
LIQUIDATE,
TRANSFER,
ENTER_MARKET,
EXIT_MARKET,
'MINT',
'REDEEM',
'BORROW',
'REPAY',
'SEIZE',
'LIQUIDATE',
'TRANSFER',
'ENTER_MARKET',
'EXIT_MARKET',
];
130 changes: 82 additions & 48 deletions subgraphs/venus/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,36 @@ import {
BorrowEvent,
LiquidationEvent,
Market,
MintEvent,
RedeemEvent,
RepayEvent,
TransferEvent,
} from '../../generated/schema';
import {
AccrueInterest,
Borrow,
LiquidateBorrow,
Mint,
MintBehalf as MintBehalfV1,
Mint as MintV1,
NewMarketInterestRateModel,
NewReserveFactor,
Redeem,
Redeem as RedeemV1,
RepayBorrow,
Transfer,
} from '../../generated/templates/VToken/VToken';
import { VToken as VTokenContract } from '../../generated/templates/VToken/VToken';
import {
Mint,
MintBehalf,
Redeem,
} from '../../generated/templates/VTokenUpdatedEvents/VTokenUpdatedEvents';
import { oneBigInt, zeroBigInt32 } from '../constants';
import { nullAddress } from '../constants/addresses';
import { createAccount } from '../operations/create';
import { createMarket } from '../operations/create';
import {
createAccount,
createMarket,
createMintBehalfEvent,
createMintEvent,
createRedeemEvent,
} from '../operations/create';
import { updateCommonVTokenStats } from '../operations/update';
import { updateMarket } from '../operations/update';
import { exponentToBigDecimal } from '../utilities/exponentToBigDecimal';
Expand All @@ -50,31 +60,26 @@ export const handleMint = (event: Mint): void => {
if (!market) {
market = createMarket(event.address.toHexString());
}
let mintId = getTransactionId(event.transaction.hash, event.transactionLogIndex);

const vTokenDecimals = market.vTokenDecimals;
createMintEvent<Mint>(event);

let vTokenAmount = event.params.mintTokens
.toBigDecimal()
.div(exponentToBigDecimal(vTokenDecimals))
.truncate(vTokenDecimals);
let underlyingAmount = event.params.mintAmount
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);
if (event.params.mintTokens.equals(event.params.totalSupply)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
};

let mint = new MintEvent(mintId);
mint.amount = vTokenAmount;
mint.to = event.params.minter;
mint.from = event.address;
mint.blockNumber = event.block.number.toI32();
mint.blockTime = event.block.timestamp.toI32();
mint.vTokenSymbol = market.symbol;
mint.underlyingAmount = underlyingAmount;
mint.save();
export const handleMintBehalf = (event: MintBehalf): void => {
let market = Market.load(event.address.toHexString());
if (!market) {
market = createMarket(event.address.toHexString());
}

createMintBehalfEvent<MintBehalf>(event);

if (event.params.mintTokens.equals(event.params.totalSupply)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
};

Expand All @@ -95,30 +100,8 @@ export const handleRedeem = (event: Redeem): void => {
if (!market) {
market = createMarket(event.address.toHexString());
}
let vTokenDecimals = market.vTokenDecimals;
let redeemID = event.transaction.hash
.toHexString()
.concat('-')
.concat(event.transactionLogIndex.toString());

let vTokenAmount = event.params.redeemTokens
.toBigDecimal()
.div(exponentToBigDecimal(vTokenDecimals))
.truncate(vTokenDecimals);
let underlyingAmount = event.params.redeemAmount
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);

let redeem = new RedeemEvent(redeemID);
redeem.amount = vTokenAmount;
redeem.to = event.address;
redeem.from = event.params.redeemer;
redeem.blockNumber = event.block.number.toI32();
redeem.blockTime = event.block.timestamp.toI32();
redeem.vTokenSymbol = market.symbol;
redeem.underlyingAmount = underlyingAmount;
redeem.save();
createRedeemEvent<Redeem>(event);

if (event.params.totalSupply.equals(zeroBigInt32)) {
// if the current balance is 0 then the user has withdrawn all their assets from this market
Expand Down Expand Up @@ -496,3 +479,54 @@ export function handleNewMarketInterestRateModel(event: NewMarketInterestRateMod
market.interestRateModelAddress = event.params.newInterestRateModel;
market.save();
}

export function handleMintV1(event: MintV1): void {
let market = Market.load(event.address.toHexString());
if (!market) {
market = createMarket(event.address.toHexString());
}

createMintEvent<MintV1>(event);

const vTokenContract = VTokenContract.bind(event.address);
let totalSupply = vTokenContract.balanceOf(event.params.minter);

if (event.params.mintTokens.equals(totalSupply)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
}

export function handleMintBehalfV1(event: MintBehalfV1): void {
let market = Market.load(event.address.toHexString());
if (!market) {
market = createMarket(event.address.toHexString());
}

createMintBehalfEvent<MintBehalfV1>(event);

const vTokenContract = VTokenContract.bind(event.address);
let totalSupply = vTokenContract.balanceOf(event.params.receiver);

if (event.params.mintTokens.equals(totalSupply)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
}

export function handleRedeemV1(event: RedeemV1): void {
let market = Market.load(event.address.toHexString());
if (!market) {
market = createMarket(event.address.toHexString());
}
createRedeemEvent<RedeemV1>(event);

const vTokenContract = VTokenContract.bind(event.address);
let totalSupply = vTokenContract.balanceOf(event.params.redeemer);

if (totalSupply.equals(zeroBigInt32)) {
// if the current balance is 0 then the user has withdrawn all their assets from this market
market.supplierCount = market.supplierCount.minus(oneBigInt);
market.save();
}
}
45 changes: 44 additions & 1 deletion subgraphs/venus/src/operations/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Address, BigDecimal, BigInt, log } from '@graphprotocol/graph-ts';

import { Account, AccountVToken, Market } from '../../generated/schema';
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 { nullAddress, vBnbAddress } from '../constants/addresses';
import { getUnderlyingPrice } from '../utilities/getUnderlyingPrice';
import { getTransactionId } from '../utilities/ids';

export function createAccountVToken(
accountVTokenId: string,
Expand Down Expand Up @@ -106,3 +107,45 @@ export function createMarket(marketAddress: string): Market {

return market;
}

export function createMintEvent<E>(event: E): void {
const mintId = getTransactionId(event.transaction.hash, event.transactionLogIndex);

const mint = new MintEvent(mintId);
mint.amountMantissa = event.params.mintTokens;
mint.to = event.params.minter;
mint.from = event.address;
mint.blockNumber = event.block.number.toI32();
mint.blockTime = event.block.timestamp.toI32();
mint.vTokenAddress = event.address;
mint.underlyingAmountMantissa = event.params.mintAmount;
mint.save();
}

export function createMintBehalfEvent<E>(event: E): void {
const mintId = getTransactionId(event.transaction.hash, event.transactionLogIndex);

const mint = new MintEvent(mintId);
mint.amountMantissa = event.params.mintTokens;
mint.to = event.params.receiver;
mint.from = event.address;
mint.blockNumber = event.block.number.toI32();
mint.blockTime = event.block.timestamp.toI32();
mint.vTokenAddress = event.address;
mint.underlyingAmountMantissa = event.params.mintAmount;
mint.save();
}

export function createRedeemEvent<E>(event: E): void {
const redeemId = getTransactionId(event.transaction.hash, event.transactionLogIndex);

const redeem = new RedeemEvent(redeemId);
redeem.amountMantissa = event.params.redeemTokens;
redeem.to = event.address;
redeem.from = event.params.redeemer;
redeem.blockNumber = event.block.number.toI32();
redeem.blockTime = event.block.timestamp.toI32();
redeem.vTokenAddress = event.address;
redeem.underlyingAmountMantissa = event.params.redeemAmount;
redeem.save();
}
40 changes: 34 additions & 6 deletions subgraphs/venus/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,22 @@ templates:
- VTokenInfo
abis:
- name: VToken
file: ../../node_modules/@venusprotocol/venus-protocol/artifacts/contracts/Tokens/VTokens/VBep20.sol/VBep20.json
file: ../../node_modules/@venusprotocol/venus-protocol-orig-events/artifacts/contracts/Tokens/VTokens/VBep20.sol/VBep20.json
- name: VBep20Storage
file: ../../node_modules/@venusprotocol/venus-protocol/artifacts/contracts/Tokens/VTokens/VTokenInterfaces.sol/VBep20Storage.json
file: ../../node_modules/@venusprotocol/venus-protocol-orig-events/artifacts/contracts/Tokens/VTokens/VTokenInterfaces.sol/VBep20Storage.json
- name: PriceOracle
file: ../../node_modules/@venusprotocol/oracle/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json
- name: BEP20
file: ../../node_modules/@venusprotocol/venus-protocol/artifacts/contracts/Tokens/BEP20Interface.sol/BEP20Interface.json
- name: Comptroller
file: ../../node_modules/@venusprotocol/venus-protocol/artifacts/contracts/Comptroller/Comptroller.sol/Comptroller.json
eventHandlers:
- event: Mint(address,uint256,uint256,uint256)
handler: handleMint
- event: Redeem(address,uint256,uint256,uint256)
handler: handleRedeem
- event: Mint(address,uint256,uint256)
handler: handleMintV1
- event: MintBehalf(address,address,uint256,uint256)
handler: handleMintV1
- event: Redeem(address,uint256,uint256)
handler: handleRedeemV1
- event: Borrow(address,uint256,uint256,uint256)
handler: handleBorrow
- event: RepayBorrow(address,address,uint256,uint256,uint256)
Expand All @@ -92,3 +94,29 @@ templates:
handler: handleTransfer
- event: NewMarketInterestRateModel(address,address)
handler: handleNewMarketInterestRateModel
- name: VTokenUpdatedEvents
kind: ethereum/contract
network: {{ network }}
source:
abi: VTokenUpdatedEvents
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
file: ./src/mappings/vToken.ts
entities:
- User
- Market
- VTokenInfo
abis:
- name: VTokenUpdatedEvents
file: ../../node_modules/@venusprotocol/venus-protocol/artifacts/contracts/Tokens/VTokens/VBep20.sol/VBep20.json
- name: VBep20Storage
file: ../../node_modules/@venusprotocol/venus-protocol/artifacts/contracts/Tokens/VTokens/VTokenInterfaces.sol/VBep20Storage.json
eventHandlers:
- event: Mint(address,uint256,uint256,uint256)
handler: handleMint
- event: MintBehalf(address,address,uint256,uint256,uint256)
handler: handleMint
- event: Redeem(address,uint256,uint256,uint256)
handler: handleRedeem
Loading

0 comments on commit ccda02b

Please sign in to comment.