Skip to content

Commit

Permalink
refactor: add isolated pool participant count and argument order refa…
Browse files Browse the repository at this point in the history
…ctoring to core pool
  • Loading branch information
coreyar committed Nov 29, 2024
1 parent 5aaaaeb commit df625de
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 422 deletions.
3 changes: 3 additions & 0 deletions subgraphs/venus/src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ import { Address } from '@graphprotocol/graph-ts';
import { comptrollerAddress as comptrollerAddressString } from './config';

export const comptrollerAddress = Address.fromString(comptrollerAddressString);

export const nullAddress = Address.fromString('0x0000000000000000000000000000000000000000');

export const nativeAddress = Address.fromString('0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE');
4 changes: 2 additions & 2 deletions subgraphs/venus/src/mappings/comptroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export function handleMarketUnlisted(event: MarketListed): void {

export function handleMarketEntered(event: MarketEntered): void {
const market = getOrCreateMarket(event.params.vToken, event);
const result = getOrCreateAccountVToken(Address.fromBytes(market.id), event.params.account);
const result = getOrCreateAccountVToken(event.params.account, Address.fromBytes(market.id));
const accountVToken = result.entity;
accountVToken.enteredMarket = true;
accountVToken.save();
}

export function handleMarketExited(event: MarketExited): void {
const market = getOrCreateMarket(event.params.vToken, event);
const result = getOrCreateAccountVToken(Address.fromBytes(market.id), event.params.account);
const result = getOrCreateAccountVToken(event.params.account, Address.fromBytes(market.id));
const accountVToken = result.entity;
accountVToken.enteredMarket = false;
accountVToken.save();
Expand Down
117 changes: 52 additions & 65 deletions subgraphs/venus/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable prefer-const */
// to satisfy AS compiler
import { Address, BigInt } from '@graphprotocol/graph-ts';

import {
AccrueInterest,
Borrow,
Expand All @@ -17,9 +15,8 @@ import {
Transfer,
VToken,
} from '../../generated/templates/VToken/VToken';
import { VToken as VTokenContract } from '../../generated/templates/VToken/VToken';
import { Mint, MintBehalf, Redeem } from '../../generated/templates/VTokenUpdatedEvents/VToken';
import { oneBigInt, zeroBigInt32 } from '../constants';
import { zeroBigInt32 } from '../constants';
import { nullAddress } from '../constants/addresses';
import {
createBorrowEvent,
Expand Down Expand Up @@ -53,6 +50,16 @@ import { getUnderlyingPrice } from '../utilities';
*/
export function handleMint(event: Mint): void {
const marketAddress = event.address;

const accountVToken = getOrCreateAccountVToken(event.params.minter, marketAddress);

updateAccountVTokenSupply(
event.params.minter,
marketAddress,
event.block.number,
accountVToken.entity.vTokenBalanceMantissa.plus(event.params.mintTokens),
);

const market = getOrCreateMarket(marketAddress, event);
const vTokenContract = VToken.bind(marketAddress);

Expand All @@ -64,17 +71,19 @@ export function handleMint(event: Mint): void {
market.save();

createMintEvent<Mint>(event);
}

export function handleMintBehalf(event: MintBehalf): void {
const marketAddress = event.address;

const accountVToken = getOrCreateAccountVToken(event.params.receiver, marketAddress);
updateAccountVTokenSupply(
event.params.minter,
event.params.receiver,
marketAddress,
event.block.number,
event.params.totalSupply,
accountVToken.entity.vTokenBalanceMantissa.plus(event.params.mintTokens),
);
}

export function handleMintBehalf(event: MintBehalf): void {
const marketAddress = event.address;
const market = getOrCreateMarket(marketAddress, event);
const vTokenContract = VToken.bind(marketAddress);

Expand All @@ -86,13 +95,6 @@ export function handleMintBehalf(event: MintBehalf): void {
market.save();

createMintBehalfEvent<MintBehalf>(event);

updateAccountVTokenSupply(
event.params.receiver,
marketAddress,
event.block.number,
event.params.totalSupply,
);
}

/* Account supplies vTokens into market and receives underlying asset in exchange
Expand All @@ -107,6 +109,18 @@ export function handleMintBehalf(event: MintBehalf): void {
*/
export function handleRedeem(event: Redeem): void {
const marketAddress = event.address;

const accountVToken = getOrCreateAccountVToken(event.params.redeemer, marketAddress);
updateAccountVTokenSupply(
event.params.redeemer,
marketAddress,
event.block.number,
accountVToken.entity.vTokenBalanceMantissa.minus(event.params.redeemTokens),
);
accountVToken.entity.totalUnderlyingRedeemedMantissa =
accountVToken.entity.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount);
accountVToken.entity.save();

const market = getOrCreateMarket(marketAddress, event);
const vTokenContract = VToken.bind(marketAddress);

Expand All @@ -120,16 +134,6 @@ export function handleRedeem(event: Redeem): void {
market.save();

createRedeemEvent<Redeem>(event);

const accountVToken = updateAccountVTokenSupply(
event.params.redeemer,
marketAddress,
event.block.number,
event.params.totalSupply,
);
accountVToken.totalUnderlyingRedeemedMantissa =
accountVToken.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount);
accountVToken.save();
}

/* Borrow assets from the protocol. All values either BNB or BEP20
Expand Down Expand Up @@ -182,9 +186,6 @@ export function handleRepayBorrow(event: RepayBorrow): void {
const market = getOrCreateMarket(marketAddress, event);
const vTokenContract = VToken.bind(marketAddress);

if (event.params.accountBorrows.equals(zeroBigInt32)) {
market.borrowerCount = market.borrowerCount.minus(oneBigInt);
}
market.totalBorrowsMantissa = event.params.totalBorrows;

// we'll update the cash value of the market
Expand Down Expand Up @@ -287,18 +288,18 @@ export function handleTransfer(event: Transfer): void {
// with normal transfers, since mint, redeem, and seize transfers will already run updateMarket()
let accountFromAddress = event.params.from;
let accountToAddress = event.params.to;
const vTokenContract = VToken.bind(event.address);

// Checking if the event is FROM the vToken contract or null (i.e. this will not run when minting)
// Checking if the event is TO the vToken contract (i.e. this will not run when redeeming)
// @TODO Edge case where someone who accidentally sends vTokens to a vToken contract, where it will not get recorded.
if (accountFromAddress.notEqual(nullAddress) && accountToAddress.notEqual(event.address)) {
getOrCreateAccount(accountFromAddress);
const accountVToken = getOrCreateAccountVToken(accountFromAddress, event.address);
updateAccountVTokenSupply(
accountFromAddress,
event.address,
event.block.number,
vTokenContract.balanceOf(accountFromAddress),
accountVToken.entity.vTokenBalanceMantissa.minus(event.params.amount),
);
}

Expand All @@ -310,11 +311,12 @@ export function handleTransfer(event: Transfer): void {
accountToAddress.notEqual(event.address)
) {
getOrCreateAccount(accountToAddress);
const accountVToken = getOrCreateAccountVToken(accountToAddress, event.address);
updateAccountVTokenSupply(
accountToAddress,
event.address,
event.block.number,
vTokenContract.balanceOf(accountToAddress),
accountVToken.entity.vTokenBalanceMantissa.plus(event.params.amount),
);
}

Expand Down Expand Up @@ -357,17 +359,16 @@ export function handleNewMarketInterestRateModel(event: NewMarketInterestRateMod
market.save();
}

function getVTokenBalance(vTokenAddress: Address, accountAddress: Address): BigInt {
const vTokenContract = VTokenContract.bind(vTokenAddress);
return vTokenContract.balanceOf(accountAddress);
}

export function handleMintV1(event: MintV1): void {
if (event.params.mintAmount.equals(zeroBigInt32)) {
return;
}

const marketAddress = event.address;
const accountVToken = getOrCreateAccountVToken(event.params.minter, marketAddress);
// Creation updates balance
updateAccountVTokenSupply(
event.params.minter,
event.address,
event.block.number,
accountVToken.entity.vTokenBalanceMantissa.plus(event.params.mintTokens),
);
const market = getOrCreateMarket(event.address, event);
const vTokenContract = VToken.bind(marketAddress);
// we'll first update the cash value of the market and then the rates, since they depend on it
Expand All @@ -378,22 +379,20 @@ export function handleMintV1(event: MintV1): void {

createMintEvent<MintV1>(event);

// Creation updates balance
updateAccountVTokenSupply(
event.params.minter,
event.address,
event.block.number,
vTokenContract.balanceOf(event.params.minter),
);

market.save();
}

export function handleMintBehalfV1(event: MintBehalfV1): void {
if (event.params.mintAmount.equals(zeroBigInt32)) {
return;
}
const marketAddress = event.address;

const accountVToken = getOrCreateAccountVToken(event.params.receiver, marketAddress);
// Creation updates balance
updateAccountVTokenSupply(
event.params.receiver,
event.address,
event.block.number,
accountVToken.entity.vTokenBalanceMantissa.plus(event.params.mintTokens),
);
const market = getOrCreateMarket(event.address, event);
const vTokenContract = VToken.bind(marketAddress);

Expand All @@ -406,24 +405,12 @@ export function handleMintBehalfV1(event: MintBehalfV1): void {
market.save();

createMintBehalfEvent<MintBehalfV1>(event);

// Creation updates balance
updateAccountVTokenSupply(
event.params.receiver,
event.address,
event.block.number,
vTokenContract.balanceOf(event.params.receiver),
);
}

export function handleRedeemV1(event: RedeemV1): void {
const marketAddress = event.address;
const market = getOrCreateMarket(event.address, event);
const vTokenContract = VToken.bind(marketAddress);
if (getVTokenBalance(event.address, event.params.redeemer).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);
}
// we'll first update the cash value of the market and then the rates, since they depend on it
updateMarketCashMantissa(market, vTokenContract);

Expand All @@ -435,7 +422,7 @@ export function handleRedeemV1(event: RedeemV1): void {

createRedeemEvent<RedeemV1>(event);

const result = getOrCreateAccountVToken(marketAddress, event.params.redeemer);
const result = getOrCreateAccountVToken(event.params.redeemer, marketAddress);
const accountVToken = result.entity;

updateAccountVTokenSupply(
Expand Down
19 changes: 13 additions & 6 deletions subgraphs/venus/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BEP20 } from '../../generated/templates/VToken/BEP20';
import { Comptroller } from '../../generated/templates/VToken/Comptroller';
import { VToken } from '../../generated/templates/VToken/VToken';
import { zeroBigInt32 } from '../constants';
import { nullAddress } from '../constants/addresses';
import { nativeAddress } from '../constants/addresses';
import {
getUnderlyingPrice,
valueOrNotAvailableAddressIfReverted,
Expand Down Expand Up @@ -48,7 +48,7 @@ export function getOrCreateMarket(marketAddress: Address, event: ethereum.Event)

// It is vBNB, which has a slightly different interface
if (market.symbol == 'vBNB') {
market.underlyingAddress = nullAddress;
market.underlyingAddress = nativeAddress;
market.underlyingDecimals = 18;
market.underlyingName = 'BNB';
market.underlyingSymbol = 'BNB';
Expand Down Expand Up @@ -80,7 +80,10 @@ export function getOrCreateMarket(marketAddress: Address, event: ethereum.Event)
updateMarketRates(market, vTokenContract);
updateMarketCashMantissa(market, vTokenContract);
market.totalSupplyVTokenMantissa = zeroBigInt32;
market.borrowIndex = vTokenContract.borrowIndex();
market.borrowIndex = valueOrNotAvailableIntIfReverted(
vTokenContract.try_borrowIndex(),
'vBEP20 try_borrowIndex()',
);
market.totalBorrowsMantissa = zeroBigInt32;
market.reservesMantissa = zeroBigInt32;

Expand Down Expand Up @@ -112,10 +115,10 @@ export class GetOrCreateAccountVTokenReturn {
}

export function getOrCreateAccountVToken(
marketId: Address,
accountId: Address,
marketId: Address,
): GetOrCreateAccountVTokenReturn {
const accountVTokenId = getAccountVTokenId(marketId, accountId);
const accountVTokenId = getAccountVTokenId(accountId, marketId);
let accountVToken = AccountVToken.load(accountVTokenId);
let created = false;
if (!accountVToken) {
Expand All @@ -132,7 +135,11 @@ export function getOrCreateAccountVToken(
accountVToken.totalUnderlyingRedeemedMantissa = zeroBigInt32;
accountVToken.totalUnderlyingRepaidMantissa = zeroBigInt32;
accountVToken.storedBorrowBalanceMantissa = zeroBigInt32;
accountVToken.borrowIndex = vTokenContract.borrowIndex();
accountVToken.accrualBlockNumber = zeroBigInt32;
accountVToken.borrowIndex = valueOrNotAvailableIntIfReverted(
vTokenContract.try_borrowIndex(),
'vBEP20 try_borrowIndex()',
);
accountVToken.enteredMarket = false;
accountVToken.save();
}
Expand Down
7 changes: 5 additions & 2 deletions subgraphs/venus/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address, BigInt } from '@graphprotocol/graph-ts';

import { AccountVToken } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
// import { valueOrNotAvailableIntIfReverted } from '../utilities';
import { valueOrNotAvailableIntIfReverted } from '../utilities';
// import { getTokenPriceInCents } from '../utilities';
import { getMarket } from './get';
import { getOrCreateAccount, getOrCreateAccountVToken } from './getOrCreate';
Expand Down Expand Up @@ -66,7 +66,10 @@ export const updateAccountVTokenBorrow = (
const _storedBorrowBalanceMantissa = accountVToken.storedBorrowBalanceMantissa;
accountVToken.storedBorrowBalanceMantissa = accountBorrows;
const vTokenContract = VToken.bind(marketAddress);
accountVToken.borrowIndex = vTokenContract.borrowIndex();
accountVToken.borrowIndex = valueOrNotAvailableIntIfReverted(
vTokenContract.try_borrowIndex(),
'vBEP20 try_borrowIndex()',
);
accountVToken.save();

if (_storedBorrowBalanceMantissa.equals(zeroBigInt32) && accountBorrows.notEqual(zeroBigInt32)) {
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/venus/src/utilities/ids.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, BigInt, Bytes } from '@graphprotocol/graph-ts';

export const getAccountVTokenId = (marketAddress: Address, accountAddress: Address): Bytes =>
export const getAccountVTokenId = (accountAddress: Address, marketAddress: Address): Bytes =>
marketAddress.concat(accountAddress);

export const getTransactionId = (transactionHash: Bytes, logIndex: BigInt): Bytes =>
Expand Down
Loading

0 comments on commit df625de

Please sign in to comment.