Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
505 changes: 292 additions & 213 deletions subgraphs/isolated-pools/schema.graphql

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ReservesAdded,
SpreadReservesReduced,
Transfer,
VToken,
} from '../../generated/PoolRegistry/VToken';
import { VToken as VTokenContract } from '../../generated/PoolRegistry/VToken';
import { nullAddress } from '../constants/addresses';
Expand All @@ -31,12 +32,14 @@ import {
createTransferTransaction,
} from '../operations/create';
import { getMarket } from '../operations/get';
import { getOrCreateAccount } from '../operations/getOrCreate';
import { getOrCreateAccount, getOrCreateToken } from '../operations/getOrCreate';
import {
updateMarketPositionBorrow,
updateMarketPositionSupply,
updateMarket,
} from '../operations/update';
import { updateMarketRates } from '../operations/updateMarketRates';
import getTokenPriceInCents from '../utilities/getTokenPriceInCents';

/* Account supplies assets into market and receives vTokens in exchange
*
Expand Down Expand Up @@ -329,6 +332,16 @@ export function handleReservesAdded(event: ReservesAdded): void {
const market = getMarket(vTokenAddress);
if (market) {
market.reservesMantissa = event.params.newTotalReserves;

const marketContract = VToken.bind(vTokenAddress);
const underlyingToken = getOrCreateToken(Address.fromBytes(market.underlyingToken));
const underlyingPriceCents = getTokenPriceInCents(
Address.fromBytes(market.pool),
vTokenAddress,
underlyingToken.decimals,
);
updateMarketRates(market, marketContract, underlyingPriceCents, event.block.number);

market.save();
}
}
Expand All @@ -338,6 +351,16 @@ export function handleSpreadReservesReduced(event: SpreadReservesReduced): void
const market = getMarket(vTokenAddress);
if (market) {
market.reservesMantissa = event.params.newTotalReserves;

const marketContract = VToken.bind(vTokenAddress);
const underlyingToken = getOrCreateToken(Address.fromBytes(market.underlyingToken));
const underlyingPriceCents = getTokenPriceInCents(
Address.fromBytes(market.pool),
vTokenAddress,
underlyingToken.decimals,
);
updateMarketRates(market, marketContract, underlyingPriceCents, event.block.number);

market.save();
}
}
Expand Down
49 changes: 49 additions & 0 deletions subgraphs/isolated-pools/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
Pool,
RewardsDistributor,
Transaction,
MarketRatesData,
MarketSupplyPositionData,
MarketBorrowPositionData,
} from '../../generated/schema';
import { Comptroller } from '../../generated/templates/Pool/Comptroller';
import { RewardsDistributor as RewardDistributorContract } from '../../generated/templates/RewardsDistributor/RewardsDistributor';
Expand Down Expand Up @@ -341,3 +344,49 @@ export function createRewardDistributor(
}
return rewardsDistributor;
}

export function createMarketRatesData(
marketAddress: Address,
blockNumber: BigInt,
exchangeRateMantissa: BigInt,
supplyRateMantissa: BigInt,
borrowRateMantissa: BigInt,
underlyingPriceCents: BigInt,
): void {
const marketRatesEntity = new MarketRatesData('auto');
marketRatesEntity.market = marketAddress;
marketRatesEntity.exchangeRateMantissa = exchangeRateMantissa;
marketRatesEntity.supplyRateMantissa = supplyRateMantissa;
marketRatesEntity.borrowRateMantissa = borrowRateMantissa;
marketRatesEntity.blockNumber = blockNumber;
marketRatesEntity.underlyingPriceCents = underlyingPriceCents;
marketRatesEntity.save();
}

export function createMarketSupplyPositionData(
marketAddress: Address,
accountAddress: Address,
blockNumber: BigInt,
vTokenBalanceMantissa: BigInt,
): void {
const marketSupplyPositionEntity = new MarketSupplyPositionData('auto');
marketSupplyPositionEntity.market = marketAddress;
marketSupplyPositionEntity.account = accountAddress;
marketSupplyPositionEntity.blockNumber = blockNumber;
marketSupplyPositionEntity.vTokenBalanceMantissa = vTokenBalanceMantissa;
marketSupplyPositionEntity.save();
}

export function createMarketBorrowPositionData(
marketAddress: Address,
accountAddress: Address,
blockNumber: BigInt,
borrowBalanceMantissa: BigInt,
): void {
const marketBorrowPositionEntity = new MarketBorrowPositionData('auto');
marketBorrowPositionEntity.market = marketAddress;
marketBorrowPositionEntity.account = accountAddress;
marketBorrowPositionEntity.blockNumber = blockNumber;
marketBorrowPositionEntity.borrowBalanceMantissa = borrowBalanceMantissa;
marketBorrowPositionEntity.save();
}
26 changes: 14 additions & 12 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
getOrCreateToken,
} from './getOrCreate';
import { oneBigInt, zeroBigInt32 } from '../constants';
import { updateMarketRates } from './updateMarketRates';
import { createMarketSupplyPositionData } from './create';

export const updateMarketPositionAccrualBlockNumber = (
accountAddress: Address,
Expand Down Expand Up @@ -49,11 +51,19 @@ export const updateMarketPositionSupply = (
Address.fromBytes(market.pool),
blockNumber,
);

if (marketPosition) {
const _vTokenBalanceMantissa = marketPosition.vTokenBalanceMantissa;
marketPosition.vTokenBalanceMantissa = accountSupplyBalanceMantissa;
marketPosition.save();

createMarketSupplyPositionData(
marketAddress,
accountAddress,
blockNumber,
accountSupplyBalanceMantissa,
);

if (
_vTokenBalanceMantissa.equals(zeroBigInt32) &&
accountSupplyBalanceMantissa.notEqual(zeroBigInt32)
Expand Down Expand Up @@ -86,13 +96,16 @@ export const updateMarketPositionBorrow = (
Address.fromBytes(market.pool),
blockNumber,
);

if (marketPosition) {
const _storedBorrowBalanceMantissa = marketPosition.storedBorrowBalanceMantissa;
marketPosition.storedBorrowBalanceMantissa = accountBorrows;
const vTokenContract = VToken.bind(marketAddress);
marketPosition.borrowIndex = vTokenContract.borrowIndex();
marketPosition.save();

updateMarketPositionBorrow(accountAddress, marketAddress, blockNumber, accountBorrows);

if (
_storedBorrowBalanceMantissa.equals(zeroBigInt32) &&
accountBorrows.notEqual(zeroBigInt32)
Expand Down Expand Up @@ -134,23 +147,12 @@ export const updateMarket = (vTokenAddress: Address, blockNumber: BigInt): Marke
marketContract.try_accrualBlockNumber(),
);

const exchangeRateMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_exchangeRateStored(),
);
market.exchangeRateMantissa = exchangeRateMantissa;

market.reservesMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_totalReserves());

const cashBigInt = valueOrNotAvailableIntIfReverted(marketContract.try_getCash());
market.cashMantissa = cashBigInt;

// calling supplyRatePerBlock & borrowRatePerBlock can fail due to external reasons, so we fall back to 0 in case of an error
market.borrowRateMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_borrowRatePerBlock(),
);
market.supplyRateMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_supplyRatePerBlock(),
);
updateMarketRates(market, marketContract, tokenPriceCents, blockNumber);

market.save();
return market;
Expand Down
11 changes: 11 additions & 0 deletions subgraphs/isolated-pools/src/operations/updateMarketBorrowRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BigInt } from '@graphprotocol/graph-ts';
import { Market } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
import { valueOrNotAvailableIntIfReverted } from '../utilities';

export function updateMarketBorrowRate(market: Market, vTokenContract: VToken): BigInt {
market.borrowRateMantissa = valueOrNotAvailableIntIfReverted(
vTokenContract.try_borrowRatePerBlock(),
);
return market.borrowRateMantissa;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BigInt } from '@graphprotocol/graph-ts';
import { Market } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
import { valueOrNotAvailableIntIfReverted } from '../utilities';

export function updateMarketExchangeRate(market: Market, vTokenContract: VToken): BigInt {
market.exchangeRateMantissa = valueOrNotAvailableIntIfReverted(
vTokenContract.try_exchangeRateStored(),
);
return market.exchangeRateMantissa;
}
29 changes: 29 additions & 0 deletions subgraphs/isolated-pools/src/operations/updateMarketRates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Address, BigInt } from '@graphprotocol/graph-ts';
import { Market } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
import { createMarketRatesData } from './create';
import { updateMarketBorrowRate } from './updateMarketBorrowRate';
import { updateMarketExchangeRate } from './updateMarketExchangeRate';
import { updateMarketSupplyRate } from './updateMarketSupplyRate';

// if an event updated the market reserves, it means we need to update the market rates that depend on it:
// borrow rate, supply rate and the exchange rate
export function updateMarketRates(
market: Market,
vTokenContract: VToken,
underlyingPriceCents: BigInt,
blockNumber: BigInt,
): void {
const borrowRate = updateMarketBorrowRate(market, vTokenContract);
const supplyRate = updateMarketSupplyRate(market, vTokenContract);
const exchangeRate = updateMarketExchangeRate(market, vTokenContract);

createMarketRatesData(
Address.fromBytes(market.address),
blockNumber,
exchangeRate,
supplyRate,
borrowRate,
underlyingPriceCents,
);
}
12 changes: 12 additions & 0 deletions subgraphs/isolated-pools/src/operations/updateMarketSupplyRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BigInt } from '@graphprotocol/graph-ts';
import { Market } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
import { valueOrNotAvailableIntIfReverted } from '../utilities';

export function updateMarketSupplyRate(market: Market, vTokenContract: VToken): BigInt {
market.supplyRateMantissa = valueOrNotAvailableIntIfReverted(
vTokenContract.try_supplyRatePerBlock(),
);

return market.supplyRateMantissa;
}
Loading