Skip to content

Commit

Permalink
refactor(isolated-pools): dont mix math and balance calls for the sam…
Browse files Browse the repository at this point in the history
…e value
  • Loading branch information
coreyar committed Oct 24, 2024
1 parent 4611c38 commit 3dd03f2
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 202 deletions.
80 changes: 36 additions & 44 deletions subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
NewAccessControlManager,
NewMarketInterestRateModel,
NewReserveFactor,
ProtocolSeize,
Redeem,
RepayBorrow,
ReservesAdded,
Expand All @@ -30,9 +29,7 @@ import {
} from '../operations/create';
import { getMarket } from '../operations/get';
import { getOrCreateAccount, getOrCreateAccountVToken } from '../operations/getOrCreate';
import { recordLiquidatorAsSupplier } from '../operations/recordLiquidatorAsSupplier';
import {
updateAccountVTokenAccrualBlockNumber,
updateAccountVTokenBorrow,
updateAccountVTokenRepayBorrow,
updateAccountVTokenSupply,
Expand Down Expand Up @@ -122,7 +119,6 @@ export function handleBorrow(event: Borrow): void {
vTokenAddress,
event.block.number,
event.params.accountBorrows,
market.borrowIndexMantissa,
);

createBorrowTransaction(event);
Expand Down Expand Up @@ -158,7 +154,6 @@ export function handleRepayBorrow(event: RepayBorrow): void {
vTokenAddress,
event.block.number,
event.params.accountBorrows,
market.borrowIndexMantissa,
);

createRepayBorrowTransaction(event);
Expand Down Expand Up @@ -214,23 +209,28 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void {
borrowedVTokenContract.borrowBalanceStored(event.params.borrower);
borrowerBorrowAccountVToken.save();

const borrowerSupplyAccountVTokenResult = getOrCreateAccountVToken(
const borrowerCollateralAccountVTokenResult = getOrCreateAccountVToken(
event.params.borrower,
Address.fromBytes(borrowMarket.pool),
event.params.vTokenCollateral,
);
const borrowerSupplyAccountVToken = borrowerSupplyAccountVTokenResult.entity;

borrowerSupplyAccountVToken.vTokenBalanceMantissa =
borrowerSupplyAccountVToken.vTokenBalanceMantissa.minus(event.params.seizeTokens);
borrowerSupplyAccountVToken.save();
const borrowerCollateralAccountVToken = borrowerCollateralAccountVTokenResult.entity;

const collateralBalance = collateralContract.balanceOf(event.params.borrower);
// Check if borrower is still supplying liquidated asset
if (collateralBalance.equals(zeroBigInt32)) {
collateralMarket.supplierCount = collateralMarket.supplierCount.minus(oneBigInt);
collateralMarket.save();
}
borrowerCollateralAccountVToken.vTokenBalanceMantissa = collateralBalance;
borrowerCollateralAccountVToken.save();

const liquidatorAccountVTokenResult = getOrCreateAccountVToken(
event.params.liquidator,
Address.fromBytes(collateralMarket.pool),
event.params.vTokenCollateral,
);
const liquidatorAccountVToken = liquidatorAccountVTokenResult.entity;

liquidatorAccountVToken.vTokenBalanceMantissa = collateralContract.balanceOf(
event.params.liquidator,
);
liquidatorAccountVToken.save();
}

export function handleAccrueInterest(event: AccrueInterest): void {
Expand All @@ -244,10 +244,6 @@ export function handleNewReserveFactor(event: NewReserveFactor): void {
market.save();
}

export function handleProtocolSeize(event: ProtocolSeize): void {
recordLiquidatorAsSupplier(event);
}

/* Transferring of vTokens
*
* event.params.from = sender of vTokens
Expand All @@ -273,6 +269,7 @@ export function handleTransfer(event: Transfer): void {
);
const accountFromAddress = event.params.from;
const accountToAddress = event.params.to;
const vTokenContract = VTokenContract.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.
Expand All @@ -288,40 +285,35 @@ export function handleTransfer(event: Transfer): void {
event.address,
);
const accountFromVToken = resultFrom.entity;

updateAccountVTokenAccrualBlockNumber(
accountFromAddress,
Address.fromBytes(market.pool),
event.address,
event.block.number,
);

// Creation updates balance
if (!resultFrom.created) {
accountFromVToken.vTokenBalanceMantissa = accountFromVToken.vTokenBalanceMantissa.minus(
event.params.amount,
);
accountFromVToken.save();
const fromBalance = vTokenContract.balanceOf(accountFromAddress);
accountFromVToken.vTokenBalanceMantissa = fromBalance;
accountFromVToken.save();

if (fromBalance.equals(zeroBigInt32)) {
// Decrease if no longer minter
const market = getMarket(event.address)!;
market.supplierCount = market.supplierCount.minus(oneBigInt);
market.save();
}

getOrCreateAccount(accountToAddress);

const resultTo = getOrCreateAccountVToken(
accountToAddress,
Address.fromBytes(market.pool),
event.address,
);
const accountToVToken = resultTo.entity;

updateAccountVTokenAccrualBlockNumber(
accountToAddress,
Address.fromBytes(market.pool),
event.address,
event.block.number,
);

accountToVToken.vTokenBalanceMantissa = accountToVToken.vTokenBalanceMantissa.plus(
event.params.amount,
);
const toBalance = vTokenContract.balanceOf(accountToAddress);
accountToVToken.vTokenBalanceMantissa = toBalance;
accountToVToken.save();
// Increase balance if now minter
if (toBalance.equals(event.params.amount)) {
const market = getMarket(event.address)!;
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
}

createTransferTransaction(event);
Expand Down
1 change: 0 additions & 1 deletion subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export const getOrCreateAccountVToken = (
accountVToken.borrowIndex = vTokenContract.borrowIndex();

accountVToken.totalUnderlyingRedeemedMantissa = zeroBigInt32;
accountVToken.borrowIndex = zeroBigInt32;
accountVToken.totalUnderlyingRepaidMantissa = zeroBigInt32;
accountVToken.enteredMarket = false;
accountVToken.save();
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const updateAccountVTokenBorrow = (
marketAddress: Address,
blockNumber: BigInt,
accountBorrows: BigInt,
borrowIndexMantissa: BigInt,
): AccountVToken => {
const accountVToken = updateAccountVTokenAccrualBlockNumber(
accountAddress,
Expand All @@ -54,7 +53,8 @@ export const updateAccountVTokenBorrow = (
blockNumber,
);
accountVToken.storedBorrowBalanceMantissa = accountBorrows;
accountVToken.borrowIndex = borrowIndexMantissa;
const vTokenContract = VToken.bind(marketAddress);
accountVToken.borrowIndex = vTokenContract.borrowIndex();
accountVToken.save();
return accountVToken as AccountVToken;
};
Expand All @@ -65,7 +65,6 @@ export const updateAccountVTokenRepayBorrow = (
marketAddress: Address,
blockNumber: BigInt,
accountBorrows: BigInt,
borrowIndexMantissa: BigInt,
): AccountVToken => {
const accountVToken = updateAccountVTokenAccrualBlockNumber(
accountAddress,
Expand All @@ -74,7 +73,8 @@ export const updateAccountVTokenRepayBorrow = (
blockNumber,
);
accountVToken.storedBorrowBalanceMantissa = accountBorrows;
accountVToken.borrowIndex = borrowIndexMantissa;
const vTokenContract = VToken.bind(marketAddress);
accountVToken.borrowIndex = vTokenContract.borrowIndex();
accountVToken.save();
return accountVToken as AccountVToken;
};
Expand Down
22 changes: 16 additions & 6 deletions subgraphs/isolated-pools/subgraph-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
AccountVTokensDocument,
AccountVTokensQuery,
AccountVTokensWithBorrowByMarketIdDocument,
AccountVTokensWithBorrowByMarketIdQuery,
AccountVTokensWithSupplyByMarketIdDocument,
AccountVTokensWithSupplyByMarketIdQuery,
MarketActionsDocument,
Expand Down Expand Up @@ -64,8 +65,17 @@ class SubgraphClient {
return result;
}

async getAccountVTokens(): Promise<AccountVTokensQuery> {
const result = await this.query(AccountVTokensDocument, {});
async getAccountVTokens({
first = 25,
skip = 0,
}: {
first: number;
skip: number;
}): Promise<AccountVTokensQuery> {
const result = await this.query(AccountVTokensDocument, { first, skip } as unknown as {
first: string;
skip: string;
});
return result.data;
}

Expand Down Expand Up @@ -93,7 +103,7 @@ class SubgraphClient {

async getAccountVTokensWithBorrowByMarketId(
marketId: string,
): Promise<AccountVTokensWithBorrowByMarketId> {
): Promise<AccountVTokensWithBorrowByMarketIdQuery> {
const result = await this.query(AccountVTokensWithBorrowByMarketIdDocument, { marketId });
return result.data;
}
Expand All @@ -117,6 +127,6 @@ class SubgraphClient {
}
}

export default new SubgraphClient(
'http://graph-node:8000/subgraphs/name/venusprotocol/venus-isolated-pools',
);
const createSubgraphClient = (url: string) => new SubgraphClient(url);

export default createSubgraphClient;
2 changes: 0 additions & 2 deletions subgraphs/isolated-pools/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ templates:
handler: handleRepayBorrow
- event: LiquidateBorrow(indexed address,indexed address,uint256,indexed address,uint256)
handler: handleLiquidateBorrow
- event: ProtocolSeize(indexed address,indexed address,uint256)
handler: handleProtocolSeize
- event: AccrueInterest(uint256,uint256,uint256,uint256)
handler: handleAccrueInterest
- event: NewReserveFactor(uint256,uint256)
Expand Down
Loading

0 comments on commit 3dd03f2

Please sign in to comment.