Skip to content

Commit

Permalink
Merge pull request #125 from clober-dex/feat/update-pool-perf
Browse files Browse the repository at this point in the history
feat: update api spec
  • Loading branch information
Dorvin authored Oct 23, 2024
2 parents f9e7ec7 + 96fe8af commit 7e3c957
Show file tree
Hide file tree
Showing 12 changed files with 604 additions and 308 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clober/v2-sdk",
"version": "0.0.76",
"version": "0.0.77",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand Down
2 changes: 1 addition & 1 deletion src/apis/chart-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CHART_LOG_INTERVALS, ChartLog } from '../type'
import { ChartLogDto } from '../model/chart-log'
import { Subgraph } from '../constants/subgraph'

const CHART_LOG_INTERVAL_TIMESTAMP: {
export const CHART_LOG_INTERVAL_TIMESTAMP: {
[key in CHART_LOG_INTERVALS]: number
} = {
[CHART_LOG_INTERVALS.oneMinute]: 60,
Expand Down
16 changes: 13 additions & 3 deletions src/apis/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
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 { CHART_LOG_INTERVALS, Market } from '../type'
import { Subgraph } from '../constants/subgraph'

import { fetchMarket } from './market'
Expand All @@ -20,25 +20,35 @@ export const fetchPoolPerformance = async (
poolKey: `0x${string}`,
volumeFromTimestamp: number,
snapshotFromTimestamp: number,
snapshotIntervalType: CHART_LOG_INTERVALS,
spreadProfitFromTimestamp: number,
) => {
return Subgraph.get<{
const result = await Subgraph.get<{
data: {
poolVolumes: PoolVolumeDto[]
poolSnapshots: PoolSnapshotDto[]
poolSnapshotBefore: PoolSnapshotDto[]
poolSpreadProfits: PoolSpreadProfitDto[]
}
}>(
chainId,
'getPoolPerformanceData',
'query getPoolPerformanceData($poolKey: String!, $volumeFrom: BigInt!, $snapshotFrom: BigInt!, $spreadProfitFrom: 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 } poolSpreadProfits( where: { intervalType: "1h", timestamp_gte: $spreadProfitFrom, } ) { id intervalType timestamp accumulatedProfitInUsd } }',
'query getPoolPerformanceData($poolKey: String!, $volumeFrom: BigInt!, $snapshotFrom: BigInt!, $snapshotIntervalType: String!, $spreadProfitFrom: BigInt!) { poolVolumes(where: { poolKey: $poolKey, intervalType: "1d", timestamp_gte: $volumeFrom, }) { id poolKey intervalType timestamp currencyAVolume currencyBVolume bookACurrencyAVolume bookACurrencyBVolume bookBCurrencyAVolume bookBCurrencyBVolume } poolSnapshots( first: 1000, skip: 0, orderBy: timestamp, orderDirection: asc where: { poolKey: $poolKey, intervalType: $snapshotIntervalType, timestamp_gte: $snapshotFrom, } ) { id poolKey intervalType timestamp price liquidityA liquidityB totalSupply } poolSnapshotBefore: poolSnapshots( first: 1, skip: 0, orderBy: timestamp, orderDirection: desc, where: { poolKey: $poolKey, intervalType: $snapshotIntervalType, timestamp_lt: $snapshotFrom, } ) { id poolKey intervalType timestamp price liquidityA liquidityB totalSupply } poolSpreadProfits( where: { intervalType: "1h", timestamp_gte: $spreadProfitFrom, } ) { id intervalType timestamp accumulatedProfitInUsd } }',
{
poolKey,
volumeFrom: volumeFromTimestamp,
snapshotFrom: snapshotFromTimestamp,
snapshotIntervalType: snapshotIntervalType.valueOf(),
spreadProfitFrom: spreadProfitFromTimestamp,
},
)
return {
poolVolumes: result.data.poolVolumes,
poolSnapshots: result.data.poolSnapshotBefore.concat(
result.data.poolSnapshots,
),
poolSpreadProfits: result.data.poolSpreadProfits,
}
}

export async function fetchPool(
Expand Down
10 changes: 8 additions & 2 deletions src/model/pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { formatUnits } from 'viem'

import { CHAIN_IDS, Currency, Market, Pool as PoolType } from '../type'
import {
CHAIN_IDS,
CHART_LOG_INTERVALS,
Currency,
Market,
Pool as PoolType,
} from '../type'
import { CONTRACT_ADDRESSES } from '../constants/addresses'
import { toPoolKey } from '../utils/pool-key'

Expand Down Expand Up @@ -192,7 +198,7 @@ export type PoolVolumeDto = {
export type PoolSnapshotDto = {
id: string
poolKey: `0x${string}`
intervalType: '1h'
intervalType: CHART_LOG_INTERVALS
timestamp: bigint
price: bigint
liquidityA: bigint
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export type PoolVolumeDto = {

export type PoolSnapshotDto = {
poolKey: `0x${string}`
intervalType: '1h'
intervalType: CHART_LOG_INTERVALS
timestamp: number
price: string
liquidityA: CurrencyAmount
Expand Down
26 changes: 24 additions & 2 deletions src/utils/time-series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@ export function fillAndSortByTimestamp<
>(
data: T[],
granularity: number,
emptyObjectGenerator: (timestamp: number, prev: T) => T,
fromTimestamp: number,
toTimestamp: number,
emptyObjectGenerator: (timestamp: number, prev: T | null) => T,
): T[] {
const sortedData = data.sort(
(a, b) => Number(a.timestamp) - Number(b.timestamp),
)

if (
sortedData.findIndex((d) => Number(d.timestamp) === fromTimestamp) === -1
) {
const before =
sortedData.filter((d) => Number(d.timestamp) < fromTimestamp).pop() ||
null
sortedData.unshift(emptyObjectGenerator(fromTimestamp, before))
}

if (sortedData.findIndex((d) => Number(d.timestamp) === toTimestamp) === -1) {
const before =
sortedData.filter((d) => Number(d.timestamp) < toTimestamp).pop() || null
null
sortedData.push(emptyObjectGenerator(toTimestamp, before))
}

const result: T[] = []

for (let i = 0; i < sortedData.length; i++) {
Expand All @@ -27,5 +45,9 @@ export function fillAndSortByTimestamp<
}
}

return result
return result.filter(
(d) =>
Number(d.timestamp) >= fromTimestamp &&
Number(d.timestamp) <= toTimestamp,
)
}
41 changes: 30 additions & 11 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { CHART_LOG_INTERVALS } from './type'
import { formatPrice, parsePrice } from './utils/prices'
import { fetchOpenOrder, fetchOpenOrdersByUserAddress } from './apis/open-order'
import { OpenOrder } from './model/open-order'
import { fetchChartLogs, fetchLatestChartLog } from './apis/chart-logs'
import {
CHART_LOG_INTERVAL_TIMESTAMP,
fetchChartLogs,
fetchLatestChartLog,
} from './apis/chart-logs'
import { getMarketId } from './utils/market'
import { CONTRACT_ADDRESSES } from './constants/addresses'
import { invertTick, toPrice } from './utils/tick'
Expand Down Expand Up @@ -200,17 +204,25 @@ export const getPoolPerformance = async ({
token1,
salt,
volumeFromTimestamp,
volumeToTimestamp,
snapshotFromTimestamp,
snapshotToTimestamp,
snapshotIntervalType,
spreadProfitFromTimestamp,
spreadProfitToTimestamp,
options,
}: {
chainId: CHAIN_IDS
token0: `0x${string}`
token1: `0x${string}`
salt: `0x${string}`
volumeFromTimestamp: number
volumeToTimestamp: number
snapshotFromTimestamp: number
snapshotToTimestamp: number
snapshotIntervalType: CHART_LOG_INTERVALS
spreadProfitFromTimestamp: number
spreadProfitToTimestamp: number
options?: {
pool?: Pool
useSubgraph?: boolean
Expand Down Expand Up @@ -246,11 +258,14 @@ export const getPoolPerformance = async ({
pool.key,
volumeFromTimestamp,
snapshotFromTimestamp,
snapshotIntervalType,
spreadProfitFromTimestamp,
)
const poolVolumes = fillAndSortByTimestamp(
poolPerformance.data.poolVolumes,
poolPerformance.poolVolumes,
24 * 60 * 60,
volumeFromTimestamp,
volumeToTimestamp,
(timestamp: number) => {
const emptyPoolVolume: ModelPoolVolume = {
id: '',
Expand All @@ -268,25 +283,29 @@ export const getPoolPerformance = async ({
},
)
const poolSnapshots = fillAndSortByTimestamp(
poolPerformance.data.poolSnapshots,
60 * 60,
(timestamp: number, prev: ModelPoolSnapshot) => {
poolPerformance.poolSnapshots,
CHART_LOG_INTERVAL_TIMESTAMP[snapshotIntervalType],
snapshotFromTimestamp,
snapshotToTimestamp,
(timestamp: number, prev: ModelPoolSnapshot | null) => {
const emptyPoolSnapshot: ModelPoolSnapshot = {
id: '',
poolKey: pool.key,
intervalType: '1h',
intervalType: snapshotIntervalType,
timestamp: BigInt(timestamp),
price: prev.price,
liquidityA: prev.liquidityA,
liquidityB: prev.liquidityB,
totalSupply: prev.totalSupply,
price: prev ? prev.price : 0n,
liquidityA: prev ? prev.liquidityA : 0n,
liquidityB: prev ? prev.liquidityB : 0n,
totalSupply: prev ? prev.totalSupply : 0n,
}
return emptyPoolSnapshot
},
)
const poolSpreadProfits = fillAndSortByTimestamp(
poolPerformance.data.poolSpreadProfits,
poolPerformance.poolSpreadProfits,
60 * 60,
spreadProfitFromTimestamp,
spreadProfitToTimestamp,
(timestamp: number) => {
const emptyPoolSpreadProfit: ModelPoolSpreadProfit = {
id: '',
Expand Down
Loading

0 comments on commit 7e3c957

Please sign in to comment.