Skip to content

Commit

Permalink
refactor: create token entity
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Dec 13, 2024
1 parent b63e178 commit 5175620
Show file tree
Hide file tree
Showing 26 changed files with 213 additions and 106 deletions.
31 changes: 21 additions & 10 deletions subgraphs/isolated-pools/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ type Pool @entity {
}

"""
ERC20 Token
"""
type Token @entity(immutable: true) {
"Address of the asset"
id: Bytes!
"Address of the asset"
address: Bytes!
"Name of the asset"
name: String!
"Symbol of the asset"
symbol: String!
"Decimals of the asset"
decimals: Int!
}

"""
MarketAction is an action that can be taken on a market
enum MarketPauseGuardianAction {
Mint = "Mint",
Borrow = "Borrow",
Expand All @@ -45,7 +62,7 @@ type MarketAction @entity {
"Concatentation of address and action"
id: Bytes!
"vToken Address affected"
vToken: Bytes!
market: Market!
"Action (Borrow, Mint)"
action: String!
"True if paused, otherwise False if active"
Expand Down Expand Up @@ -83,12 +100,8 @@ type Market @entity {
supplyRateMantissa: BigInt!
"VToken symbol"
symbol: String!
"Underlying token address"
underlyingAddress: Bytes!
"Underlying token name"
underlyingName: String!
"Underlying token symbol"
underlyingSymbol: String!
"Underlying Token"
underlyingToken: Token!
"Max token borrow amount allowed"
borrowCapMantissa: BigInt!
"Total borrowed underlying token"
Expand All @@ -106,8 +119,6 @@ type Market @entity {
lastUnderlyingPriceCents: BigInt!
"Block price was last updated"
lastUnderlyingPriceBlockNumber: BigInt!
"Underlying token decimal length"
underlyingDecimals: Int!
"vToken decimal length"
vTokenDecimals: Int!

Expand Down Expand Up @@ -247,7 +258,7 @@ type RewardsDistributor @entity {
"Address of the pool"
pool: Pool!
"Address of the reward token"
rewardTokenAddress: Bytes!
rewardToken: Token!
"Distribution rate for suppliers"
marketRewards: [MarketReward!]! @derivedFrom(field:"rewardsDistributor")
"Depending on the Chain, the rewards distributor is time based or block based"
Expand Down
37 changes: 20 additions & 17 deletions subgraphs/isolated-pools/src/operations/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigInt } from '@graphprotocol/graph-ts';
import { Address, BigInt, Bytes } from '@graphprotocol/graph-ts';

import { Comptroller as ComptrollerContract } from '../../generated/PoolRegistry/Comptroller';
import { PoolRegistry as PoolRegistryContract } from '../../generated/PoolRegistry/PoolRegistry';
Expand All @@ -23,7 +23,6 @@ import {
} from '../../generated/schema';
import { Comptroller } from '../../generated/templates/Pool/Comptroller';
import { RewardsDistributor as RewardDistributorContract } from '../../generated/templates/RewardsDistributor/RewardsDistributor';
import { BEP20 as BEP20Contract } from '../../generated/templates/VToken/BEP20';
import { VToken as VTokenContract } from '../../generated/templates/VToken/VToken';
import { BORROW, LIQUIDATE, MINT, REDEEM, REPAY, TRANSFER, zeroBigInt32 } from '../constants';
import {
Expand All @@ -37,7 +36,7 @@ import {
vWETHLiquidStakedETHAddress,
vWETHCoreAddress,
} from '../constants/addresses';
import { getOrCreateMarketReward } from './getOrCreate';
import { getOrCreateMarketReward, getOrCreateToken } from './getOrCreate';
import { getTokenPriceInCents, valueOrNotAvailableIntIfReverted } from '../utilities';
import {
getAccountId,
Expand Down Expand Up @@ -107,7 +106,7 @@ export function createMarket(
const vTokenContract = VTokenContract.bind(vTokenAddress);
const poolComptroller = Comptroller.bind(comptroller);
const underlyingAddress = vTokenContract.underlying();
const underlyingContract = BEP20Contract.bind(Address.fromBytes(underlyingAddress));

const market = new Market(vTokenAddress);

market.address = vTokenAddress;
Expand All @@ -118,14 +117,14 @@ export function createMarket(
market.interestRateModelAddress = vTokenContract.interestRateModel();
market.symbol = vTokenContract.symbol();
market.vTokenDecimals = vTokenContract.decimals();
const underlyingToken = getOrCreateToken(underlyingAddress);
market.underlyingToken = underlyingToken.id;

market.underlyingAddress = underlyingAddress;
market.underlyingName = underlyingContract.name();
market.underlyingSymbol = underlyingContract.symbol();
const underlyingDecimals = underlyingContract.decimals();
market.underlyingDecimals = underlyingDecimals;

const underlyingValue = getTokenPriceInCents(comptroller, vTokenAddress, underlyingDecimals);
const underlyingValue = getTokenPriceInCents(
comptroller,
vTokenAddress,
underlyingToken.decimals,
);
market.lastUnderlyingPriceCents = underlyingValue;
market.lastUnderlyingPriceBlockNumber = blockNumber;
market.accessControlManagerAddress = vTokenContract.accessControlManager();
Expand Down Expand Up @@ -178,15 +177,17 @@ export function createMarket(
}

if (vTokenAddress.equals(vankrBNBLiquidStakedBNBAddress)) {
market.underlyingAddress = Address.fromHexString('0x5269b7558D3d5E113010Ef1cFF0901c367849CC9');
market.underlyingToken = getOrCreateToken(
Address.fromBytes(Bytes.fromHexString('0x5269b7558D3d5E113010Ef1cFF0901c367849CC9')),
).id;
market.symbol = 'vankrBNB_LiquidStakedBNB';
market.underlyingName = 'Ankr Staked BNB ';
}

if (vTokenAddress.equals(vankrBNBDeFiAddress)) {
market.underlyingAddress = Address.fromHexString('0x5269b7558D3d5E113010Ef1cFF0901c367849CC9');
market.underlyingToken = getOrCreateToken(
Address.fromBytes(Bytes.fromHexString('0x5269b7558D3d5E113010Ef1cFF0901c367849CC9')),
).id;
market.symbol = 'vankrBNB_DeFi';
market.underlyingName = 'Ankr Staked BNB ';
}

if (vTokenAddress.equals(vSnBNBAddress)) {
Expand All @@ -195,7 +196,9 @@ export function createMarket(
}

if (vTokenAddress.equals(vWETHLiquidStakedETHAddress) || vTokenAddress.equals(vWETHCoreAddress)) {
market.underlyingAddress = Address.fromHexString('0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9');
market.underlyingToken = getOrCreateToken(
Address.fromBytes(Bytes.fromHexString('0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9')),
).id;
}

market.save();
Expand Down Expand Up @@ -311,7 +314,7 @@ export function createRewardDistributor(
const rewardsDistributor = new RewardsDistributor(id);
rewardsDistributor.address = rewardsDistributorAddress;
rewardsDistributor.pool = comptrollerAddress;
rewardsDistributor.rewardTokenAddress = rewardToken;
rewardsDistributor.rewardToken = getOrCreateToken(rewardToken).id;
rewardsDistributor.isTimeBased = valueOrFalseIfReverted(
rewardDistributorContract.try_isTimeBased(),
);
Expand Down
24 changes: 24 additions & 0 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Address, BigInt } from '@graphprotocol/graph-ts';

import { VToken as VTokenContract } from '../../generated/PoolRegistry/VToken';
import { BEP20 } from '../../generated/PoolRegistry/BEP20';
import {
Account,
AccountPool,
Expand All @@ -9,6 +10,7 @@ import {
Pool,
MarketReward,
RewardsDistributor,
Token,
} from '../../generated/schema';
import { zeroBigInt32 } from '../constants';
import {
Expand All @@ -17,6 +19,7 @@ import {
getPoolId,
getMarketRewardId,
getRewardsDistributorId,
getTokenId,
} from '../utilities/ids';
import {
createAccount,
Expand Down Expand Up @@ -140,3 +143,24 @@ export const getOrCreateRewardDistributor = (

return rewardsDistributor as RewardsDistributor;
};

/**
* Creates and Token object with symbol and address
*
* @param asset Address of the token
* @returns Token
*/
export function getOrCreateToken(asset: Address): Token {
let tokenEntity = Token.load(getTokenId(asset));

if (!tokenEntity) {
const erc20 = BEP20.bind(asset);
tokenEntity = new Token(getTokenId(asset));
tokenEntity.address = asset;
tokenEntity.name = erc20.name();
tokenEntity.symbol = erc20.symbol();
tokenEntity.decimals = erc20.decimals();
tokenEntity.save();
}
return tokenEntity;
}
11 changes: 8 additions & 3 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { VToken } from '../../generated/templates/VToken/VToken';
import { valueOrNotAvailableIntIfReverted } from '../utilities';
import { getTokenPriceInCents } from '../utilities';
import { getMarket } from './get';
import { getOrCreateAccount, getOrCreateMarketPosition, getOrCreatePool } from './getOrCreate';
import {
getOrCreateAccount,
getOrCreateMarketPosition,
getOrCreatePool,
getOrCreateToken,
} from './getOrCreate';
import { oneBigInt, zeroBigInt32 } from '../constants';

export const updateMarketPositionAccrualBlockNumber = (
Expand Down Expand Up @@ -98,11 +103,11 @@ export const updateMarket = (vTokenAddress: Address, blockNumber: BigInt): Marke
return market as Market;
}
const marketContract = VToken.bind(vTokenAddress);

const underlyingToken = getOrCreateToken(Address.fromBytes(market.underlyingToken));
const tokenPriceCents = getTokenPriceInCents(
Address.fromBytes(market.pool),
vTokenAddress,
market.underlyingDecimals,
underlyingToken.decimals,
);
market.lastUnderlyingPriceCents = tokenPriceCents;
market.lastUnderlyingPriceBlockNumber = blockNumber;
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/src/operations/updateOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const updateOrCreateMarketAction = (
): MarketAction => {
const id = getMarketActionId(vTokenAddress, action);
const marketAction = new MarketAction(id);
marketAction.vToken = vTokenAddress;
marketAction.market = vTokenAddress;
marketAction.action = Actions[action];
marketAction.pauseState = pauseState;
marketAction.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getExchangeRateBigDecimal = (
- If you call the vUSDC contract on bscscan it comes back (2.0 * 10^14)
- The real value is ~0.02. So vDAI is off by 10^28, and vUSDC 10^16
How to calculate for tokens with different decimals
- Must div by tokenDecimals, 10^market.underlyingDecimals
- Must div by tokenDecimals, 10^market.underlyingToken.decimals
- Must multiply by vtokenDecimals, 10^8
- Must div by mantissa, 10^18
*/
Expand Down
2 changes: 2 additions & 0 deletions subgraphs/isolated-pools/src/utilities/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export const getMarketRewardId = (

export const getAccountPoolId = (accountAddress: Address, poolAddress: Address): Bytes =>
accountAddress.concat(poolAddress);

export const getTokenId = (tokenAddress: Address): Bytes => tokenAddress;
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ query AccountPositions($id: ID!) {
id
liquidationThresholdMantissa
exchangeRateMantissa
underlyingAddress
underlyingToken {
address
decimals
}
vTokenDecimals
underlyingDecimals
}
}
borrows: tokens(where: { storedBorrowBalanceMantissa_gt: 0 }) {
Expand All @@ -28,9 +30,11 @@ query AccountPositions($id: ID!) {
borrowIndex
market {
id
underlyingAddress
underlyingToken {
address
decimals
}
vTokenDecimals
underlyingDecimals
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
query MarketActions {
marketActions {
id
vToken
market {
address
}
action
pauseState
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ query MarketById($id: ID!) {
reservesMantissa
supplyRateMantissa
symbol
underlyingAddress
underlyingName
underlyingToken {
address
name
symbol
decimals
}
lastUnderlyingPriceCents
lastUnderlyingPriceBlockNumber
underlyingSymbol
borrowCapMantissa
supplyCapMantissa
accrualBlockNumber
borrowIndex
reserveFactorMantissa
underlyingDecimals
supplierCount
borrowerCount
totalBorrowsMantissa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ query Markets {
reservesMantissa
supplyRateMantissa
symbol
underlyingAddress
underlyingName
underlyingSymbol
underlyingToken {
address
name
symbol
decimals
}
vTokenDecimals
borrowCapMantissa
accrualBlockNumber
Expand All @@ -26,7 +29,6 @@ query Markets {
reserveFactorMantissa
lastUnderlyingPriceCents
lastUnderlyingPriceBlockNumber
underlyingDecimals
supplyCapMantissa
accessControlManagerAddress
supplierCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ query RewardsDistributors {
rewardsDistributors {
id
address
rewardTokenAddress
rewardToken {
address
}
isTimeBased
marketRewards {
id
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/tests/Pool/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ describe('Pool Events', () => {
const id = getMarketActionId(vTokenAddress, action).toHexString();

assert.fieldEquals('MarketAction', id, 'id', id);
assert.fieldEquals('MarketAction', id, 'vToken', vTokenAddress.toHexString());
assert.fieldEquals('MarketAction', id, 'market', vTokenAddress.toHexString());
assert.fieldEquals('MarketAction', id, 'action', 'MINT');
assert.fieldEquals('MarketAction', id, 'pauseState', pauseState.toString());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../src/mappings/rewardsDistributor';
import { getMarketRewardId } from '../../src/utilities/ids';
import { createNewRewardsDistributor } from '../Pool/events';
import { createVBep20AndUnderlyingMock } from '../VToken/mocks';
import { createVBep20AndUnderlyingMock, createBep20Mock } from '../VToken/mocks';
import {
createRewardTokenBorrowSpeedUpdatedEvent,
createRewardTokenSupplySpeedUpdatedEvent,
Expand Down Expand Up @@ -48,6 +48,7 @@ beforeAll(() => {

beforeEach(() => {
createRewardsDistributorMock(rewardsDistributorAddress, tokenAddress);
createBep20Mock(tokenAddress, 'B0B Coin', 'B0B', BigInt.fromI32(18));
const newRewardsDistributorEvent = createNewRewardsDistributor(
comptrollerAddress,
rewardsDistributorAddress,
Expand Down
Loading

0 comments on commit 5175620

Please sign in to comment.