diff --git a/schema.graphql b/schema.graphql index 1613cf3..5cf6c8a 100644 --- a/schema.graphql +++ b/schema.graphql @@ -1,5 +1,3 @@ -# TODO: track approvals? -# TODO: track token liquidity across all Ajna pools # this supports both ERC20 and ERC721 tokens, as union types aren't currently supported type Token @entity { # token address @@ -14,11 +12,12 @@ type Token @entity { tokenType: String! # ERC20 or ERC721 # number of pools including this token poolCount: BigInt! + # list of pools including this token + pools: [Pool!]! # total supply of the token totalSupply: BigInt # transactions across all pools that include this token txCount: BigInt! - # TODO: track which tokenIds have been used in pools? } diff --git a/src/mappings/erc-20-pool-factory.ts b/src/mappings/erc-20-pool-factory.ts index 1b7e8f8..ca823be 100644 --- a/src/mappings/erc-20-pool-factory.ts +++ b/src/mappings/erc-20-pool-factory.ts @@ -6,7 +6,7 @@ import { ERC20Pool as ERC20PoolContract } from "../../generated/templates/ERC20P import { ONE_BI, ZERO_BI } from "../utils/constants" import { addressToBytes, wadToDecimal } from "../utils/convert" import { getTokenDecimals, getTokenName, getTokenSymbol, getTokenTotalSupply } from "../utils/token-erc20" -import { getRatesAndFees, loadOrCreatePool } from "../utils/pool/pool" +import { getRatesAndFees, loadOrCreatePool, updateTokenPools } from "../utils/pool/pool" import { loadOrCreateFactory } from "../utils/pool/pool-factory" import { Bytes } from "@graphprotocol/graph-ts" @@ -52,6 +52,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { collateralToken.txCount = ZERO_BI collateralToken.tokenType = "ERC20" collateralToken.poolCount = ONE_BI + collateralToken.pools = [] } else { collateralToken.poolCount = collateralToken.poolCount.plus(ONE_BI) } @@ -66,6 +67,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { quoteToken.txCount = ZERO_BI quoteToken.tokenType = "ERC20" quoteToken.poolCount = ONE_BI + quoteToken.pools = [] } else { quoteToken.poolCount = quoteToken.poolCount.plus(ONE_BI) } @@ -74,6 +76,10 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { const pool = loadOrCreatePool(event.params.pool_) ERC20Pool.create(event.params.pool_) // create data source template + // update list of pools including these tokens + updateTokenPools(collateralToken, pool) + updateTokenPools(quoteToken, pool) + // record pool metadata pool.createdAtTimestamp = event.block.timestamp pool.createdAtBlockNumber = event.block.number diff --git a/src/mappings/erc-721-pool-factory.ts b/src/mappings/erc-721-pool-factory.ts index 20e4dd8..05a7eb9 100644 --- a/src/mappings/erc-721-pool-factory.ts +++ b/src/mappings/erc-721-pool-factory.ts @@ -6,7 +6,7 @@ import { ERC721Pool as ERC721PoolContract } from "../../generated/templates/ERC7 import { ONE_BI, ZERO_BI } from "../utils/constants" import { addressToBytes, wadToDecimal } from "../utils/convert" import { loadOrCreateFactory } from "../utils/pool/pool-factory" -import { getPoolSubsetHash, getRatesAndFees, loadOrCreatePool } from "../utils/pool/pool" +import { getPoolSubsetHash, getRatesAndFees, loadOrCreatePool, updateTokenPools } from "../utils/pool/pool" import { getTokenName as getTokenNameERC721, getTokenSymbol as getTokenSymbolERC721} from "../utils/token-erc721" import { getTokenDecimals, getTokenName, getTokenSymbol, getTokenTotalSupply } from "../utils/token-erc20" import { BigInt, ByteArray, Bytes, ethereum } from "@graphprotocol/graph-ts" @@ -76,6 +76,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { collateralToken.txCount = ZERO_BI collateralToken.poolCount = ONE_BI collateralToken.tokenType = "ERC721" + collateralToken.pools = [] } else { collateralToken.poolCount = collateralToken.poolCount.plus(ONE_BI) } @@ -90,6 +91,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { quoteToken.txCount = ZERO_BI quoteToken.tokenType = "ERC20" quoteToken.poolCount = ONE_BI + quoteToken.pools = [] } else { quoteToken.poolCount = quoteToken.poolCount.plus(ONE_BI) } @@ -98,6 +100,10 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { const pool = loadOrCreatePool(event.params.pool_) ERC721Pool.create(event.params.pool_) // create pool template + // update list of pools including these tokens + updateTokenPools(collateralToken, pool) + updateTokenPools(quoteToken, pool) + // record pool metadata pool.createdAtTimestamp = event.block.timestamp pool.createdAtBlockNumber = event.block.number diff --git a/src/utils/pool/pool.ts b/src/utils/pool/pool.ts index bab069e..92c75ab 100644 --- a/src/utils/pool/pool.ts +++ b/src/utils/pool/pool.ts @@ -479,3 +479,11 @@ export function depositUpToIndex(poolAddress: Address, index: u32): BigInt { const poolContract = ERC20Pool.bind(poolAddress) return poolContract.depositUpToIndex(BigInt.fromU32(index)); } + +export function updateTokenPools(token: Token, pool: Pool): void { + // get current index of pool in token's list of pools + const index = token.pools.indexOf(pool.id) + if (index == -1) { + token.pools = token.pools.concat([pool.id]) + } +} diff --git a/src/utils/position.ts b/src/utils/position.ts index c7eb567..a18facc 100644 --- a/src/utils/position.ts +++ b/src/utils/position.ts @@ -24,6 +24,7 @@ export function loadOrCreateLPToken(tokenAddress: Address): Token { token.tokenType = "ERC721" token.poolCount = ONE_BI token.totalSupply = ONE_BI + token.pools = [] } return token