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

Track token pools #69

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 2 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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?
}


Expand Down
8 changes: 7 additions & 1 deletion src/mappings/erc-20-pool-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/mappings/erc-721-pool-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/utils/pool/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,12 @@ 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 {
const pools = token.pools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not reassigning this, could just eliminate this line and use token.pools on 486.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly

// get current index of pool in token's list of pools
const index = pools.indexOf(pool.id)
if (index == -1) {
token.pools = token.pools.concat([pool.id])
}
}
1 change: 1 addition & 0 deletions src/utils/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down