Skip to content

Commit

Permalink
feat: add get pool performance api
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorvin committed Oct 8, 2024
1 parent 95b7356 commit 92b2c04
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/apis/pool.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import { PublicClient } from 'viem'

import { CHAIN_IDS } from '../constants/chain'
import { Pool } from '../model/pool'
import { Pool, PoolSnapshotDto, PoolVolumeDto } from '../model/pool'
import { CONTRACT_ADDRESSES } from '../constants/addresses'
import { toPoolKey } from '../utils/pool-key'
import { REBALANCER_ABI } from '../abis/rebalancer/rebalancer-abi'
import { Market } from '../type'
import { Subgraph } from '../constants/subgraph'

import { fetchMarket } from './market'

export const fetchPoolPerformance = async (
chainId: CHAIN_IDS,
poolKey: `0x${string}`,
volumeFromTimestamp: number,
snapshotFromTimestamp: number,
) => {
return Subgraph.get<{
data: {
poolVolumes: PoolVolumeDto[]
poolSnapshots: PoolSnapshotDto[]
}
}>(
chainId,
'getPoolPerformanceData',
'query getPoolPerformanceData($poolKey: String!, $volumeFrom: BigInt!, $snapshotFrom: BigInt!) { poolVolumes(where: { poolKey: $poolKey, intervalType: "1d", timestamp_gte: $volumeFrom, }) { id poolKey intervalType timestamp currencyAVolume currencyBVolume bookACurrencyAVolume bookACurrencyBVolume bookBCurrencyAVolume bookBCurrencyBVolume } poolSnapshots( where: { poolKey: $poolKey, intervalType: "1h", timestamp_gte: $snapshotFrom, } ) { id poolKey intervalType timestamp price liquidityA liquidityB totalSupply } }',
{
poolKey,
volumeFrom: BigInt(volumeFromTimestamp),
snapshotFrom: BigInt(snapshotFromTimestamp),
},
)
}

export async function fetchPool(
publicClient: PublicClient,
chainId: CHAIN_IDS,
Expand Down
25 changes: 25 additions & 0 deletions src/model/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class Pool {
strategy: this.strategy,
currencyA: this.currencyA,
currencyB: this.currencyB,
currencyLp: this.currencyLp,
liquidityA: {
total: {
currency: this.currencyA,
Expand Down Expand Up @@ -169,3 +170,27 @@ export class Pool {
}
}
}

export type PoolVolumeDto = {
id: string
poolKey: `0x${string}`
intervalType: '1d'
timestamp: bigint
currencyAVolume: bigint
currencyBVolume: bigint
bookACurrencyAVolume: bigint
bookACurrencyBVolume: bigint
bookBCurrencyAVolume: bigint
bookBCurrencyBVolume: bigint
}

export type PoolSnapshotDto = {
id: string
poolKey: `0x${string}`
intervalType: '1h'
timestamp: bigint
price: bigint
liquidityA: bigint
liquidityB: bigint
totalSupply: bigint
}
24 changes: 24 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type Pool = {
strategy: `0x${string}`
currencyA: Currency
currencyB: Currency
currencyLp: Currency6909
liquidityA: {
total: CurrencyAmount
reserve: CurrencyAmount
Expand Down Expand Up @@ -135,3 +136,26 @@ export enum CHART_LOG_INTERVALS {

export type CurrencyAmount = { currency: Currency; value: string }
export type Currency6909Amount = { currency: Currency6909; value: string }

export type PoolVolumeDto = {
poolKey: `0x${string}`
intervalType: '1d'
timestamp: number
currencyAVolume: CurrencyAmount
currencyBVolume: CurrencyAmount
}

export type PoolSnapshotDto = {
poolKey: `0x${string}`
intervalType: '1h'
timestamp: number
price: string
liquidityA: CurrencyAmount
liquidityB: CurrencyAmount
totalSupply: Currency6909Amount
}

export type PoolPerformanceData = {
poolVolumes: PoolVolumeDto[]
poolSnapshots: PoolSnapshotDto[]
}
89 changes: 88 additions & 1 deletion src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
DefaultReadContractOptions,
Market,
Pool,
PoolPerformanceData,
StrategyPrice,
} from './type'
import { CHART_LOG_INTERVALS } from './type'
Expand All @@ -26,7 +27,7 @@ import { getMarketId } from './utils/market'
import { CONTRACT_ADDRESSES } from './constants/addresses'
import { invertTick, toPrice } from './utils/tick'
import { MAX_TICK, MIN_TICK } from './constants/tick'
import { fetchPool } from './apis/pool'
import { fetchPool, fetchPoolPerformance } from './apis/pool'
import { fetchStrategyPrice } from './apis/strategy'
import { Subgraph } from './constants/subgraph'

Expand Down Expand Up @@ -186,6 +187,92 @@ export const getPool = async ({
return pool.toJson()
}

export const getPoolPerformance = async ({
chainId,
token0,
token1,
salt,
volumeFromTimestamp,
snapshotFromTimestamp,
options,
}: {
chainId: CHAIN_IDS
token0: `0x${string}`
token1: `0x${string}`
salt: `0x${string}`
volumeFromTimestamp: number
snapshotFromTimestamp: number
options?: {
pool?: Pool
useSubgraph?: boolean
} & DefaultReadContractOptions
}): Promise<PoolPerformanceData> => {
if (isAddressEqual(token0, token1)) {
throw new Error('Token0 and token1 must be different')
}
if (!options?.useSubgraph) {
throw new Error('useSubgraph must be true')
}
let pool: Pool
if (options?.pool) {
pool = options.pool
} else {
const publicClient = createPublicClient({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
})
pool = (
await fetchPool(
publicClient,
chainId,
[token0, token1],
salt,
!!(options && options.useSubgraph),
undefined,
)
).toJson()
}
const poolPerformance = await fetchPoolPerformance(
chainId,
pool.key,
volumeFromTimestamp,
snapshotFromTimestamp,
)
return {
poolVolumes: poolPerformance.data.poolVolumes.map((poolVolume) => ({
poolKey: poolVolume.poolKey,
intervalType: poolVolume.intervalType,
timestamp: Number(poolVolume.timestamp),
currencyAVolume: {
currency: pool.currencyA,
value: formatUnits(poolVolume.currencyAVolume, pool.currencyA.decimals),
},
currencyBVolume: {
currency: pool.currencyB,
value: formatUnits(poolVolume.currencyBVolume, pool.currencyB.decimals),
},
})),
poolSnapshots: poolPerformance.data.poolSnapshots.map((poolSnapshot) => ({
poolKey: poolSnapshot.poolKey,
intervalType: poolSnapshot.intervalType,
timestamp: Number(poolSnapshot.timestamp),
price: formatUnits(poolSnapshot.price, 8),
liquidityA: {
currency: pool.currencyA,
value: formatUnits(poolSnapshot.liquidityA, pool.currencyA.decimals),
},
liquidityB: {
currency: pool.currencyB,
value: formatUnits(poolSnapshot.liquidityB, pool.currencyB.decimals),
},
totalSupply: {
currency: pool.currencyLp,
value: formatUnits(poolSnapshot.totalSupply, pool.currencyLp.decimals),
},
})),
}
}

export const getStrategyPrice = async ({
chainId,
token0,
Expand Down

0 comments on commit 92b2c04

Please sign in to comment.