Skip to content

Commit

Permalink
track token pools
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Sep 15, 2023
1 parent a6d2b8e commit 0513c4c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
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
// 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

0 comments on commit 0513c4c

Please sign in to comment.