From bcee8bfae0cb337a53ed92249b7ab7e0947918a8 Mon Sep 17 00:00:00 2001 From: SdCrypt0 Date: Fri, 19 Jul 2024 14:28:07 +0300 Subject: [PATCH] add linehub trade tvl --- adapters/linehub/src/index.ts | 15 ++++++++-- adapters/linehub/src/sdk/config.ts | 1 + adapters/linehub/src/sdk/lib.ts | 47 ++++++++++++++++++++++++++++-- adapters/linehub/src/sdk/types.ts | 6 ++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/adapters/linehub/src/index.ts b/adapters/linehub/src/index.ts index 3800116c..33a7f5a0 100644 --- a/adapters/linehub/src/index.ts +++ b/adapters/linehub/src/index.ts @@ -3,7 +3,7 @@ import fs from 'fs'; import { write } from 'fast-csv'; import { BlockData, OutputSchemaRow } from './sdk/types'; -import { getV2UserPositionsAtBlock, getV3UserPositionsAtBlock } from './sdk/lib'; +import { getTradeLiquidityForAddressByPoolAtBlock, getV2UserPositionsAtBlock, getV3UserPositionsAtBlock } from './sdk/lib'; const readBlocksFromCSV = async (filePath: string): Promise => { @@ -58,9 +58,10 @@ readBlocksFromCSV('hourly_blocks.csv').then(async (blocks: BlockData[]) => { export const getUserTVLByBlock = async ({ blockNumber, blockTimestamp }: BlockData): Promise => { const result: OutputSchemaRow[] = [] - const [v2Positions, v3Positions] = await Promise.all([ + const [v2Positions, v3Positions, tradeLiquidities] = await Promise.all([ getV2UserPositionsAtBlock(blockNumber), - getV3UserPositionsAtBlock(blockNumber) + getV3UserPositionsAtBlock(blockNumber), + getTradeLiquidityForAddressByPoolAtBlock(blockNumber) ]) // combine v2 & v3 @@ -79,6 +80,14 @@ export const getUserTVLByBlock = async ({ blockNumber, blockTimestamp }: BlockDa (balances?.[position.user]?.[position.token1.address] ?? 0n) + position.token1.balance } + for (const position of tradeLiquidities) { + balances[position.user] = balances[position.user] || {} + + if (position.amount > 0n) + balances[position.user][position.asset] = + (balances?.[position.user]?.[position.asset] ?? 0n) + + position.amount + } for (const [user, tokenBalances] of Object.entries(balances)) { for (const [token, balance] of Object.entries(tokenBalances)) { result.push({ diff --git a/adapters/linehub/src/sdk/config.ts b/adapters/linehub/src/sdk/config.ts index abe2cc27..9b291d1a 100644 --- a/adapters/linehub/src/sdk/config.ts +++ b/adapters/linehub/src/sdk/config.ts @@ -3,6 +3,7 @@ import { linea } from "viem/chains" export const V2_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/55804/linehub-v2/version/latest" export const V3_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/55804/linehub-v3/version/latest" +export const TRADE_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/55804/linehub-trade/version/latest" export const client = createPublicClient({ chain: linea, diff --git a/adapters/linehub/src/sdk/lib.ts b/adapters/linehub/src/sdk/lib.ts index e45782e9..0ed2a4c4 100644 --- a/adapters/linehub/src/sdk/lib.ts +++ b/adapters/linehub/src/sdk/lib.ts @@ -1,5 +1,5 @@ -import { V2_SUBGRAPH_URL, V3_SUBGRAPH_URL, client } from "./config" -import { UserPosition } from "./types" +import { TRADE_SUBGRAPH_URL, V2_SUBGRAPH_URL, V3_SUBGRAPH_URL, client } from "./config" +import { UserLiquidity, UserPosition } from "./types" type V2Position = { balance: string, @@ -101,6 +101,49 @@ export const getV2UserPositionsAtBlock = async (blockNumber: number): Promise => { + let blockQuery = blockNumber !== 0 ? ` block: {number: ${blockNumber}}` : ``; + + let skip = 0; + let fetchNext = true; + let result: any[] = []; + let _stores: any[] = []; + while (fetchNext) { + let query = `{ + liquidities(where: {period: "user" amountDelta_gt: 1 } ${blockQuery} orderBy: createTimestamp, first:1000,skip:${skip}) { + user + asset + amountDelta + } + }`; + + // console.log(query) + + let response = await fetch(TRADE_SUBGRAPH_URL, { + method: "POST", + body: JSON.stringify({ query }), + headers: { "Content-Type": "application/json" }, + }); + const { data: { liquidities, stores } } = await response.json(); + _stores = stores + result = result.concat(liquidities) + + if (liquidities.length < 1000) { + fetchNext = false; + } else { + skip += 1000; + } + } + return result.map(r => { + return { + user: r.user, + asset: r.asset, + // user pool share is equal to user pool balance divided by the total balance. + amount: BigInt(r.amountDelta), + } + }); +} + type V3Position = { liquidity: string, owner: string, diff --git a/adapters/linehub/src/sdk/types.ts b/adapters/linehub/src/sdk/types.ts index 8f9ba2d8..122cd67c 100644 --- a/adapters/linehub/src/sdk/types.ts +++ b/adapters/linehub/src/sdk/types.ts @@ -23,4 +23,10 @@ export type UserPosition = { address: string, balance: bigint, } +} + +export type UserLiquidity = { + user: string; + asset: string; + amount: bigint; } \ No newline at end of file