Skip to content

Commit

Permalink
refactor: skip recording vbifi positions
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Dec 18, 2024
1 parent a9ba84e commit 3e6d7ce
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 160 deletions.
8 changes: 5 additions & 3 deletions subgraphs/isolated-pools/src/mappings/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export function handleMarketSupported(event: MarketSupported): void {
}

export function handleMarketUnlisted(event: MarketUnlisted): void {
const market = getMarket(event.params.vToken)!;
market.isListed = false;
market.save();
const market = getMarket(event.params.vToken);
if (market) {
market.isListed = false;
market.save();
}
}

export function handleMarketEntered(event: MarketEntered): void {
Expand Down
156 changes: 93 additions & 63 deletions subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ export function handleMint(event: Mint): void {

// and finally we update the market total supply
const vTokenContract = VTokenContract.bind(vTokenAddress);
const market = getMarket(vTokenAddress)!;
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
}
}

/* Account supplies vTokens into market and receives underlying asset in exchange
Expand All @@ -86,13 +88,17 @@ export function handleRedeem(event: Redeem): void {
event.block.number,
currentBalance,
);
marketPosition.totalUnderlyingRedeemedMantissa =
marketPosition.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount);
// and finally we update the market total supply
const vTokenContract = VTokenContract.bind(vTokenAddress);
const market = getMarket(vTokenAddress)!;
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
if (marketPosition) {
marketPosition.totalUnderlyingRedeemedMantissa =
marketPosition.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount);
// and finally we update the market total supply
const vTokenContract = VTokenContract.bind(vTokenAddress);
const market = getMarket(vTokenAddress);
if (market) {
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
}
}
}

/* Borrow assets from the protocol. All values either BNB or BEP20
Expand All @@ -116,10 +122,11 @@ export function handleBorrow(event: Borrow): void {

createBorrowTransaction(event);

const market = getMarket(vTokenAddress)!;

market.totalBorrowsMantissa = event.params.totalBorrows;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.totalBorrowsMantissa = event.params.totalBorrows;
market.save();
}
}

/* Repay some amount borrowed. Anyone can repay anyones balance
Expand All @@ -145,14 +152,15 @@ export function handleRepayBorrow(event: RepayBorrow): void {
event.block.number,
event.params.accountBorrows,
);
const market = getMarket(vTokenAddress)!;
market.totalBorrowsMantissa = event.params.totalBorrows;
market.save();
const market = getMarket(vTokenAddress);
if (market && marketPosition) {
market.totalBorrowsMantissa = event.params.totalBorrows;
market.save();

marketPosition.totalUnderlyingRepaidMantissa = marketPosition.totalUnderlyingRepaidMantissa.plus(
event.params.repayAmount,
);
createRepayBorrowTransaction(event);
marketPosition.totalUnderlyingRepaidMantissa =
marketPosition.totalUnderlyingRepaidMantissa.plus(event.params.repayAmount);
createRepayBorrowTransaction(event);
}
}

/*
Expand All @@ -173,42 +181,50 @@ export function handleRepayBorrow(event: RepayBorrow): void {
*/
export function handleLiquidateBorrow(event: LiquidateBorrow): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
const vTokenContract = VTokenContract.bind(vTokenAddress);
const liquidator = getOrCreateAccount(event.params.liquidator);
liquidator.countLiquidator = liquidator.countLiquidator + 1;
liquidator.save();
const market = getMarket(vTokenAddress);
if (market) {
const vTokenContract = VTokenContract.bind(vTokenAddress);
const liquidator = getOrCreateAccount(event.params.liquidator);
liquidator.countLiquidator = liquidator.countLiquidator + 1;
liquidator.save();

const borrower = getOrCreateAccount(event.params.borrower);
borrower.countLiquidated = borrower.countLiquidated + 1;
borrower.save();
const borrower = getOrCreateAccount(event.params.borrower);
borrower.countLiquidated = borrower.countLiquidated + 1;
borrower.save();

market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();

createLiquidateBorrowTransaction(event);
createLiquidateBorrowTransaction(event);
}
}

export function handleProtocolSeize(event: ProtocolSeize): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
const vTokenContract = VTokenContract.bind(vTokenAddress);
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
const market = getMarket(vTokenAddress);
if (market) {
const vTokenContract = VTokenContract.bind(vTokenAddress);
market.totalSupplyVTokenMantissa = vTokenContract.totalSupply();
market.save();
}
}

export function handleAccrueInterest(event: AccrueInterest): void {
const market = updateMarket(event.address, event.block.number);
market.totalBorrowsMantissa = event.params.totalBorrows;
market.borrowIndex = event.params.borrowIndex;
market.save();
if (market) {
market.totalBorrowsMantissa = event.params.totalBorrows;
market.borrowIndex = event.params.borrowIndex;
market.save();
}
}

export function handleNewReserveFactor(event: NewReserveFactor): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.reserveFactorMantissa = event.params.newReserveFactorMantissa;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.reserveFactorMantissa = event.params.newReserveFactorMantissa;
market.save();
}
}

/* Transferring of vTokens
Expand Down Expand Up @@ -272,46 +288,58 @@ export function handleTransfer(event: Transfer): void {

export function handleNewMarketInterestRateModel(event: NewMarketInterestRateModel): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.interestRateModelAddress = event.params.newInterestRateModel;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.interestRateModelAddress = event.params.newInterestRateModel;
market.save();
}
}

export function handleBadDebtIncreased(event: BadDebtIncreased): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.badDebtMantissa = event.params.badDebtNew;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.badDebtMantissa = event.params.badDebtNew;
market.save();
}

createMarketPositionBadDebt(vTokenAddress, event);
}

export function handleBadDebtRecovered(event: BadDebtRecovered): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.badDebtMantissa = event.params.badDebtNew;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.badDebtMantissa = event.params.badDebtNew;
market.save();
}
}

export function handleNewAccessControlManager(event: NewAccessControlManager): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.accessControlManagerAddress = event.params.newAccessControlManager;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.accessControlManagerAddress = event.params.newAccessControlManager;
market.save();
}
}

export function handleReservesAdded(event: ReservesAdded): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.reservesMantissa = event.params.newTotalReserves;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.reservesMantissa = event.params.newTotalReserves;
market.save();
}
}

export function handleSpreadReservesReduced(event: SpreadReservesReduced): void {
const vTokenAddress = event.address;
const market = getMarket(vTokenAddress)!;
market.reservesMantissa = event.params.newTotalReserves;
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.reservesMantissa = event.params.newTotalReserves;
market.save();
}
}

export function handleHealBorrow(event: HealBorrow): void {
Expand All @@ -323,7 +351,9 @@ export function handleHealBorrow(event: HealBorrow): void {
zeroBigInt32,
);
const vTokenContract = VTokenContract.bind(vTokenAddress);
const market = getMarket(vTokenAddress)!;
market.totalBorrowsMantissa = vTokenContract.totalBorrows();
market.save();
const market = getMarket(vTokenAddress);
if (market) {
market.totalBorrowsMantissa = vTokenContract.totalBorrows();
market.save();
}
}
56 changes: 30 additions & 26 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
createRewardDistributor,
} from './create';
import { getMarketPosition, getMarket } from './get';
import { vBifiAddress } from '../constants/addresses';

// BIFI was delisted before it was listed. Creation ignores this market.
export const getOrCreateMarket = (
Expand Down Expand Up @@ -81,31 +82,34 @@ export const getOrCreateMarketPosition = (
marketAddress: Address,
poolAddress: Address,
enteredMarket: boolean = false, // eslint-disable-line @typescript-eslint/no-inferrable-types
): GetOrCreateMarketPositionReturn => {
let marketPosition = getMarketPosition(accountAddress, marketAddress);
let created = false;
if (!marketPosition) {
created = true;
const marketPositionId = getMarketPositionId(accountAddress, marketAddress);
marketPosition = new MarketPosition(marketPositionId);
marketPosition.account = accountAddress;
marketPosition.accountPool = getOrCreateAccountPool(accountAddress, poolAddress).id;
marketPosition.market = marketAddress;
marketPosition.enteredMarket = enteredMarket;
marketPosition.accrualBlockNumber = zeroBigInt32;

const vTokenContract = VTokenContract.bind(marketAddress);

marketPosition.vTokenBalanceMantissa = zeroBigInt32;
marketPosition.storedBorrowBalanceMantissa = zeroBigInt32;
marketPosition.borrowIndex = vTokenContract.borrowIndex();

marketPosition.totalUnderlyingRedeemedMantissa = zeroBigInt32;
marketPosition.totalUnderlyingRepaidMantissa = zeroBigInt32;
marketPosition.enteredMarket = false;
marketPosition.save();
): GetOrCreateMarketPositionReturn | null => {
if (marketAddress.notEqual(vBifiAddress)) {
let marketPosition = getMarketPosition(accountAddress, marketAddress);
let created = false;
if (!marketPosition) {
created = true;
const marketPositionId = getMarketPositionId(accountAddress, marketAddress);
marketPosition = new MarketPosition(marketPositionId);
marketPosition.account = accountAddress;
marketPosition.accountPool = getOrCreateAccountPool(accountAddress, poolAddress).id;
marketPosition.market = marketAddress;
marketPosition.enteredMarket = enteredMarket;
marketPosition.accrualBlockNumber = zeroBigInt32;

const vTokenContract = VTokenContract.bind(marketAddress);

marketPosition.vTokenBalanceMantissa = zeroBigInt32;
marketPosition.storedBorrowBalanceMantissa = zeroBigInt32;
marketPosition.borrowIndex = vTokenContract.borrowIndex();

marketPosition.totalUnderlyingRedeemedMantissa = zeroBigInt32;
marketPosition.totalUnderlyingRepaidMantissa = zeroBigInt32;
marketPosition.enteredMarket = false;
marketPosition.save();
}
return { entity: marketPosition, created };
}
return { entity: marketPosition, created };
return null;
};

export function getOrCreateMarketReward(
Expand Down Expand Up @@ -153,8 +157,8 @@ export function getOrCreateAnkrStakedBNBToken(): Token {
if (!tokenEntity) {
tokenEntity = new Token(getTokenId(underlyingTokenAddress));
tokenEntity.address = underlyingTokenAddress;
tokenEntity.name = 'Ankr Staked BNB';
tokenEntity.symbol = 'ankrBNB ';
tokenEntity.name = 'Ankr Staked BNB ';
tokenEntity.symbol = 'ankrBNB';
tokenEntity.decimals = 18;
tokenEntity.save();
}
Expand Down
Loading

0 comments on commit 3e6d7ce

Please sign in to comment.