Skip to content

Commit

Permalink
add linehub trade tvl
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcrypt0 committed Jul 19, 2024
1 parent f33b001 commit bcee8bf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
15 changes: 12 additions & 3 deletions adapters/linehub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockData[]> => {
Expand Down Expand Up @@ -58,9 +58,10 @@ readBlocksFromCSV('hourly_blocks.csv').then(async (blocks: BlockData[]) => {
export const getUserTVLByBlock = async ({ blockNumber, blockTimestamp }: BlockData): Promise<OutputSchemaRow[]> => {
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
Expand All @@ -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({
Expand Down
1 change: 1 addition & 0 deletions adapters/linehub/src/sdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 45 additions & 2 deletions adapters/linehub/src/sdk/lib.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -101,6 +101,49 @@ export const getV2UserPositionsAtBlock = async (blockNumber: number): Promise<Us
return result
}

export const getTradeLiquidityForAddressByPoolAtBlock = async (blockNumber: number): Promise<UserLiquidity[]> => {
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,
Expand Down
6 changes: 6 additions & 0 deletions adapters/linehub/src/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ export type UserPosition = {
address: string,
balance: bigint,
}
}

export type UserLiquidity = {
user: string;
asset: string;
amount: bigint;
}

0 comments on commit bcee8bf

Please sign in to comment.