Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/builders dao contributions #138

Merged
merged 14 commits into from
Jan 29, 2024
Merged
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
},
"devDependencies": {
"@graphprotocol/client-cli": "^3.0.0",
"@graphprotocol/graph-cli": "0.56.0",
"@graphprotocol/graph-ts": "0.31.0",
"@graphprotocol/graph-cli": "^0.67.1",
"@graphprotocol/graph-ts": "^0.32.0",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.3",
"@nomicfoundation/hardhat-network-helpers": "^1.0.4",
"@nomicfoundation/hardhat-toolbox": "^1.0.2",
Expand Down
14 changes: 7 additions & 7 deletions subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type AccountVToken @entity {
"""
Auxiliary entity for AccountVToken
"""
type AccountVTokenTransaction @entity {
type AccountVTokenTransaction @entity(immutable: true) {
id: ID!

account: AccountVToken!
Expand Down Expand Up @@ -171,7 +171,7 @@ interface VTokenTransfer {
TransferEvent will be stored for every mint, redeem, liquidation, and any normal
transfer between two accounts.
"""
type TransferEvent implements VTokenTransfer @entity {
type TransferEvent implements VTokenTransfer @entity(immutable: true) {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
Expand All @@ -192,7 +192,7 @@ type TransferEvent implements VTokenTransfer @entity {
MintEvent stores information for mints. From address will always be a vToken
market address
"""
type MintEvent implements VTokenTransfer @entity {
type MintEvent implements VTokenTransfer @entity(immutable: true) {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
Expand All @@ -215,7 +215,7 @@ type MintEvent implements VTokenTransfer @entity {
RedeemEvent stores information for redeems. To address will always be a
vToken market address
"""
type RedeemEvent implements VTokenTransfer @entity {
type RedeemEvent implements VTokenTransfer @entity(immutable: true) {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
Expand All @@ -238,7 +238,7 @@ type RedeemEvent implements VTokenTransfer @entity {
LiquidationEvent stores information for liquidations. The event is emitted from
the vToken market address.
"""
type LiquidationEvent implements VTokenTransfer @entity {
type LiquidationEvent implements VTokenTransfer @entity(immutable: true) {
"Transaction hash concatenated with log index"
id: ID!
"vTokens seized"
Expand Down Expand Up @@ -283,7 +283,7 @@ interface UnderlyingTransfer {
"""
BorrowEvent stores information for borrows
"""
type BorrowEvent implements UnderlyingTransfer @entity {
type BorrowEvent implements UnderlyingTransfer @entity(immutable: true) {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying borrowed"
Expand All @@ -304,7 +304,7 @@ type BorrowEvent implements UnderlyingTransfer @entity {
RepayEvent stores information for repays. Payer is not always the same as
borrower, such as in the event of a Liquidation
"""
type RepayEvent implements UnderlyingTransfer @entity {
type RepayEvent implements UnderlyingTransfer @entity(immutable: true) {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying repaid"
Expand Down
115 changes: 28 additions & 87 deletions subgraphs/venus/src/mappings/comptroller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable prefer-const */
// to satisfy AS compiler
import { log } from '@graphprotocol/graph-ts';

import {
DistributedSupplierVenus,
MarketEntered,
Expand All @@ -12,86 +10,36 @@ import {
NewLiquidationIncentive,
NewPriceOracle,
} from '../../generated/Comptroller/Comptroller';
import { Account, Market } from '../../generated/schema';
import { VToken, VTokenUpdatedEvents } from '../../generated/templates';
import { createAccount, createMarket } from '../operations/create';
import { getOrCreateComptroller } from '../operations/getOrCreate';
import { updateCommonVTokenStats } from '../operations/update';
import {
getOrCreateAccount,
getOrCreateAccountVToken,
getOrCreateAccountVTokenTransaction,
getOrCreateComptroller,
getOrCreateMarket,
} from '../operations/getOrCreate';

export function handleMarketListed(event: MarketListed): void {
// Dynamically index all new listed tokens
VToken.create(event.params.vToken);
VTokenUpdatedEvents.create(event.params.vToken);
// Create the market for this token, since it's now been listed.
createMarket(event.params.vToken.toHexString());
getOrCreateMarket(event.params.vToken, event);
}

export function handleMarketEntered(event: MarketEntered): void {
let market = Market.load(event.params.vToken.toHexString());
// Null check needed to avoid crashing on a new market added. Ideally when dynamic data
// sources can source from the contract creation block and not the time the
// comptroller adds the market, we can avoid this altogether
if (!market) {
log.debug('[handleMarketEntered] market null: {}', [event.params.vToken.toHexString()]);
market = Market.load(event.params.vToken.toHexString());
}

if (!market) {
log.debug('[handleMarketEntered] market still null, return...', []);
return;
}
const market = getOrCreateMarket(event.params.vToken, event);
const account = getOrCreateAccount(event.params.account.toHex());
const accountVToken = getOrCreateAccountVToken(market.id, market.symbol, account.id, event);
accountVToken.enteredMarket = true;
accountVToken.save();

let accountId = event.params.account.toHex();
let account = Account.load(accountId);
if (account == null) {
createAccount(accountId);
}

let vTokenStats = updateCommonVTokenStats(
market.id,
market.symbol,
accountId,
event.transaction.hash,
event.block.timestamp,
event.block.number,
event.logIndex,
);
vTokenStats.enteredMarket = true;
vTokenStats.save();
getOrCreateAccountVTokenTransaction(accountVToken.id, event);
}

export function handleMarketExited(event: MarketExited): void {
let market = Market.load(event.params.vToken.toHexString());
// Null check needed to avoid crashing on a new market added. Ideally when dynamic data
// sources can source from the contract creation block and not the time the
// comptroller adds the market, we can avoid this altogether
if (!market) {
log.debug('[handleMarketExited] market null: {}', [event.params.vToken.toHexString()]);
market = Market.load(event.params.vToken.toHexString());
}
const market = getOrCreateMarket(event.params.vToken, event);
const account = getOrCreateAccount(event.params.account.toHex());
const accountVToken = getOrCreateAccountVToken(market.id, market.symbol, account.id, event);
accountVToken.enteredMarket = false;
accountVToken.save();

if (!market) {
log.debug('[handleMarketExited] market still null, return...', []);
return;
}

let accountID = event.params.account.toHex();
let account = Account.load(accountID);
if (account == null) {
createAccount(accountID);
}

let vTokenStats = updateCommonVTokenStats(
market.id,
market.symbol,
accountID,
event.transaction.hash,
event.block.timestamp,
event.block.number,
event.logIndex,
);
vTokenStats.enteredMarket = false;
vTokenStats.save();
getOrCreateAccountVTokenTransaction(accountVToken.id, event);
}

export function handleNewCloseFactor(event: NewCloseFactor): void {
Expand All @@ -101,14 +49,9 @@ export function handleNewCloseFactor(event: NewCloseFactor): void {
}

export function handleNewCollateralFactor(event: NewCollateralFactor): void {
let market = Market.load(event.params.vToken.toHexString());
// Null check needed to avoid crashing on a new market added. Ideally when dynamic data
// sources can source from the contract creation block and not the time the
// comptroller adds the market, we can avoid this altogether
if (market != null) {
market.collateralFactorMantissa = event.params.newCollateralFactorMantissa;
market.save();
}
const market = getOrCreateMarket(event.params.vToken, event);
market.collateralFactorMantissa = event.params.newCollateralFactorMantissa;
market.save();
}

// This should be the first event acccording to bscscan but it isn't.... price oracle is. weird
Expand All @@ -126,11 +69,9 @@ export function handleNewPriceOracle(event: NewPriceOracle): void {

// Also handles DistributedBorrowerVenus with same signature
export function handleXvsDistributed(event: DistributedSupplierVenus): void {
let vTokenAddress = event.params.vToken.toHex();
const venusDelta = event.params.venusDelta;
let market = Market.load(vTokenAddress);
if (market == null) {
market = createMarket(vTokenAddress);
}
market.totalXvsDistributedMantissa = market.totalXvsDistributedMantissa.plus(venusDelta);
const market = getOrCreateMarket(event.params.vToken, event);
market.totalXvsDistributedMantissa = market.totalXvsDistributedMantissa.plus(
event.params.venusDelta,
);
market.save();
}
Loading
Loading