Skip to content

Commit

Permalink
Merge pull request #185 from VenusProtocol/refactor-il-subgraph-with-…
Browse files Browse the repository at this point in the history
…tests

Refactor il subgraph with tests
  • Loading branch information
coreyar authored Oct 14, 2024
2 parents 294833d + a714aa8 commit 975d12d
Show file tree
Hide file tree
Showing 25 changed files with 489 additions and 345 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"graphql": "16.6.0",
"graphql-tag": "2.12.6",
"hardhat": "^2.16.1",
"hardhat-dependency-compiler": "^1.1.3",
"hardhat-dependency-compiler": "^1.2.1",
"hardhat-deploy": "^0.11.15",
"hardhat-deploy-ethers": "^0.3.0-beta.13",
"hardhat-gas-reporter": "^1.0.8",
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "isolated-pools-subgraph",
"version": "0.2.0",
"version": "0.3.0",
"license": "MIT",
"repository": {
"url": "https://github.com/VenusProtocol/subgraphs",
Expand Down
13 changes: 6 additions & 7 deletions subgraphs/isolated-pools/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Account is an BNB address, with a list of all vToken markets the account has
participated in, along with liquidation information.
"""
type Account @entity {
"User BNB address"
"User address"
id: Bytes!
"Pools the user is participating on"
pools: [AccountPool!]! @derivedFrom(field: "account")
Expand Down Expand Up @@ -172,17 +172,16 @@ type AccountVToken @entity {
badDebt: [AccountVTokenBadDebt!]! @derivedFrom(field:"account")
"Block number this asset was updated at in the contract"
accrualBlockNumber: BigInt!
"Borrow Index this position last accrued interest"
borrowIndex: BigInt!
"vToken balance representing underlying supplied to the market, underlying balance can be calculated with the exchange rate"
accountVTokenSupplyBalanceMantissa: BigInt!
"Current borrow balance stored in contract (exclusive of interest since accrualBlockNumber)"
accountBorrowBalanceMantissa: BigInt!
vTokenBalanceMantissa: BigInt!
"Stored borrow balance stored in contract (exclusive of interest since accrualBlockNumber)"
storedBorrowBalanceMantissa: BigInt!
"True if user is entered, false if they are exited"
enteredMarket: Boolean!

"Total amount of underlying redeemed"
totalUnderlyingRedeemedMantissa: BigInt!
"The value of the borrow index upon users last interaction"
accountBorrowIndexMantissa: BigInt!
"Total amount underlying repaid"
totalUnderlyingRepaidMantissa: BigInt!
}
Expand Down
4 changes: 1 addition & 3 deletions subgraphs/isolated-pools/src/mappings/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
NewRewardsDistributor,
NewSupplyCap,
} from '../../generated/PoolRegistry/Comptroller';
import { VToken as VTokenContract } from '../../generated/PoolRegistry/VToken';
import { RewardsDistributor as RewardsDistributorDataSource } from '../../generated/templates';
import { zeroBigInt32 } from '../constants';
import { getMarket } from '../operations/get';
Expand All @@ -31,8 +30,7 @@ import {
import Box from '../utilities/box';

export function handleMarketSupported(event: MarketSupported): void {
const vTokenContract = VTokenContract.bind(event.params.vToken);
const comptroller = vTokenContract.comptroller();
const comptroller = event.address;
const market = getOrCreateMarket(event.params.vToken, comptroller, event.block.timestamp);
market.isListed = true;
market.collateralFactorMantissa = zeroBigInt32;
Expand Down
31 changes: 15 additions & 16 deletions subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ import {
createTransferTransaction,
} from '../operations/create';
import { getMarket } from '../operations/get';
import {
getOrCreateAccount,
getOrCreateAccountVToken,
} from '../operations/getOrCreate';
import { getOrCreateAccount, getOrCreateAccountVToken } from '../operations/getOrCreate';
import { recordLiquidatorAsSupplier } from '../operations/recordLiquidatorAsSupplier';
import {
updateAccountVTokenAccrualBlockNumber,
Expand Down Expand Up @@ -68,7 +65,7 @@ export function handleMint(event: Mint): void {
event.block.number,
suppliedTotal,
);
if (suppliedTotal == event.params.mintTokens) {
if (suppliedTotal.equals(event.params.mintTokens)) {
// and if they are the same, it means it's a new supplier
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
Expand Down Expand Up @@ -99,7 +96,7 @@ export function handleRedeem(event: Redeem): void {
event.block.number,
currentBalance,
);
if (currentBalance == zeroBigInt32) {
if (currentBalance.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();
Expand Down Expand Up @@ -130,7 +127,7 @@ export function handleBorrow(event: Borrow): void {

createBorrowTransaction(event);

if (event.params.accountBorrows == event.params.borrowAmount) {
if (event.params.accountBorrows.equals(event.params.borrowAmount)) {
// if both the accountBorrows and the borrowAmount are the same, it means the account is a new borrower
market.borrowerCount = market.borrowerCount.plus(oneBigInt);
market.save();
Expand Down Expand Up @@ -166,7 +163,7 @@ export function handleRepayBorrow(event: RepayBorrow): void {

createRepayBorrowTransaction(event);

if (event.params.accountBorrows == zeroBigInt32) {
if (event.params.accountBorrows.equals(zeroBigInt32)) {
market.borrowerCount = market.borrowerCount.minus(oneBigInt);
market.save();
}
Expand Down Expand Up @@ -212,8 +209,8 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void {
const borrowerBorrowAccountVToken = borrowerBorrowAccountVTokenResult.entity;

// Creation updates balance
borrowerBorrowAccountVToken.accountBorrowIndexMantissa = borrowedVTokenContract.borrowIndex();
borrowerBorrowAccountVToken.accountBorrowBalanceMantissa =
borrowerBorrowAccountVToken.borrowIndex = borrowedVTokenContract.borrowIndex();
borrowerBorrowAccountVToken.storedBorrowBalanceMantissa =
borrowedVTokenContract.borrowBalanceStored(event.params.borrower);
borrowerBorrowAccountVToken.save();

Expand All @@ -224,8 +221,8 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void {
);
const borrowerSupplyAccountVToken = borrowerSupplyAccountVTokenResult.entity;

borrowerSupplyAccountVToken.accountVTokenSupplyBalanceMantissa =
borrowerSupplyAccountVToken.accountVTokenSupplyBalanceMantissa.minus(event.params.seizeTokens);
borrowerSupplyAccountVToken.vTokenBalanceMantissa =
borrowerSupplyAccountVToken.vTokenBalanceMantissa.minus(event.params.seizeTokens);
borrowerSupplyAccountVToken.save();

const collateralBalance = collateralContract.balanceOf(event.params.borrower);
Expand Down Expand Up @@ -301,8 +298,9 @@ export function handleTransfer(event: Transfer): void {

// Creation updates balance
if (!resultFrom.created) {
accountFromVToken.accountVTokenSupplyBalanceMantissa =
accountFromVToken.accountVTokenSupplyBalanceMantissa.minus(event.params.amount);
accountFromVToken.vTokenBalanceMantissa = accountFromVToken.vTokenBalanceMantissa.minus(
event.params.amount,
);
accountFromVToken.save();
}
getOrCreateAccount(accountToAddress);
Expand All @@ -322,8 +320,9 @@ export function handleTransfer(event: Transfer): void {

// Creation updates balance
if (!resultTo.created) {
accountToVToken.accountVTokenSupplyBalanceMantissa =
accountToVToken.accountVTokenSupplyBalanceMantissa.plus(event.params.amount);
accountToVToken.vTokenBalanceMantissa = accountToVToken.vTokenBalanceMantissa.plus(
event.params.amount,
);
accountToVToken.save();
}
}
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ export const getOrCreateAccountVToken = (
const suppliedAmountMantissa = accountSnapshot.value1;
const borrowedAmountMantissa = accountSnapshot.value2;

accountVToken.accountVTokenSupplyBalanceMantissa = suppliedAmountMantissa;
accountVToken.accountBorrowBalanceMantissa = borrowedAmountMantissa;
accountVToken.vTokenBalanceMantissa = suppliedAmountMantissa;
accountVToken.storedBorrowBalanceMantissa = borrowedAmountMantissa;
// @TODO
// accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(accountId);
// accountVToken.storedBorrowBalanceMantissa = zeroBigInt32;
// accountVToken.borrowIndex = vTokenContract.borrowIndex();

accountVToken.totalUnderlyingRedeemedMantissa = zeroBigInt32;
accountVToken.accountBorrowIndexMantissa = zeroBigInt32;
accountVToken.borrowIndex = zeroBigInt32;
accountVToken.totalUnderlyingRepaidMantissa = zeroBigInt32;
accountVToken.enteredMarket = false;
accountVToken.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const recordLiquidatorAsSupplier = (event: ProtocolSeize): void => {

// Creation updates balance
if (!liquidatorAccountVTokenResult.created) {
liquidatorAccountVToken.accountVTokenSupplyBalanceMantissa =
liquidatorAccountVToken.accountVTokenSupplyBalanceMantissa.plus(amount);
liquidatorAccountVToken.vTokenBalanceMantissa =
liquidatorAccountVToken.vTokenBalanceMantissa.plus(amount);
}
liquidatorAccountVToken.save();
// If the transfer amount equals balance it was a funding transfer
Expand Down
12 changes: 6 additions & 6 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const updateAccountVTokenSupply = (
marketAddress,
blockNumber,
);
accountVToken.accountVTokenSupplyBalanceMantissa = accountSupplyBalanceMantissa;
accountVToken.vTokenBalanceMantissa = accountSupplyBalanceMantissa;
accountVToken.save();
return accountVToken as AccountVToken;
};
Expand All @@ -53,8 +53,8 @@ export const updateAccountVTokenBorrow = (
marketAddress,
blockNumber,
);
accountVToken.accountBorrowBalanceMantissa = accountBorrows;
accountVToken.accountBorrowIndexMantissa = borrowIndexMantissa;
accountVToken.storedBorrowBalanceMantissa = accountBorrows;
accountVToken.borrowIndex = borrowIndexMantissa;
accountVToken.save();
return accountVToken as AccountVToken;
};
Expand All @@ -73,8 +73,8 @@ export const updateAccountVTokenRepayBorrow = (
marketAddress,
blockNumber,
);
accountVToken.accountBorrowBalanceMantissa = accountBorrows;
accountVToken.accountBorrowIndexMantissa = borrowIndexMantissa;
accountVToken.storedBorrowBalanceMantissa = accountBorrows;
accountVToken.borrowIndex = borrowIndexMantissa;
accountVToken.save();
return accountVToken as AccountVToken;
};
Expand Down Expand Up @@ -133,7 +133,7 @@ export const updateMarket = (
const marketContract = VToken.bind(vTokenAddress);

const tokenPriceCents = getTokenPriceInCents(
marketContract.comptroller(),
Address.fromBytes(market.pool),
vTokenAddress,
market.underlyingDecimals,
);
Expand Down
49 changes: 35 additions & 14 deletions subgraphs/isolated-pools/subgraph-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { DocumentNode } from 'graphql';
import { OperationResult, Client as UrqlClient, createClient } from 'urql/core';
import { Client as UrqlClient, createClient } from 'urql/core';

import {
AccountByIdDocument,
AccountFromMarketDocument,
AccountPositionsDocument,
AccountVTokenByAccountAndMarketQueryDocument,
AccountVTokenByAccountAndMarketQueryQuery,
AccountVTokenByAccountAndMarketDocument,
AccountVTokenByAccountAndMarketQuery,
AccountVTokenByAccountIdDocument,
AccountVTokensDocument,
AccountVTokensQuery,
AccountVTokensWithBorrowByMarketIdDocument,
AccountVTokensWithSupplyByMarketIdDocument,
AccountVTokensWithSupplyByMarketIdQuery,
MarketActionsDocument,
MarketByIdDocument,
MarketByIdQuery,
MarketsDocument,
PoolByIdDocument,
PoolsDocument,
Expand Down Expand Up @@ -49,19 +54,19 @@ class SubgraphClient {
return result;
}

async getMarketById(id: string) {
async getMarketById(id: string): Promise<MarketByIdQuery> {
const result = await this.query(MarketByIdDocument, { id });
return result;
return result.data;
}

async getAccountById(id: string) {
const result = await this.query(AccountByIdDocument, { id });
return result;
}

async getAccountVTokens() {
async getAccountVTokens(): Promise<AccountVTokensQuery> {
const result = await this.query(AccountVTokensDocument, {});
return result;
return result.data;
}

async getMarketActions() {
Expand All @@ -79,15 +84,31 @@ class SubgraphClient {
return result;
}

async getAccountVTokenByAccountAndMarket(
accountId: string,
async getAccountVTokensWithSupplyByMarketId(
marketId: string,
): Promise<AccountVTokensWithSupplyByMarketIdQuery> {
const result = await this.query(AccountVTokensWithSupplyByMarketIdDocument, { marketId });
return result.data;
}

async getAccountVTokensWithBorrowByMarketId(
marketId: string,
): Promise<OperationResult<AccountVTokenByAccountAndMarketQueryQuery>> {
const result = await this.query(AccountVTokenByAccountAndMarketQueryDocument, {
accountId,
marketId,
): Promise<AccountVTokensWithBorrowByMarketId> {
const result = await this.query(AccountVTokensWithBorrowByMarketIdDocument, { marketId });
return result.data;
}

async getAccountVTokenByAccountAndMarket({
accountId,
marketId,
}: {
accountId: string;
marketId: string;
}): Promise<AccountVTokenByAccountAndMarketQuery> {
const result = await this.query(AccountVTokenByAccountAndMarketDocument, {
id: `${marketId}${accountId.replace('0x', '')}`,
});
return result;
return result.data || { accountVToken: null };
}

async getAccountPositions(id: string) {
Expand Down
Loading

0 comments on commit 975d12d

Please sign in to comment.