Skip to content

Commit

Permalink
fix: query properly resolved on multiple hourly entries
Browse files Browse the repository at this point in the history
  • Loading branch information
LayneHaber committed May 22, 2024
1 parent 487cc17 commit 76ccd1b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions adapters/connext/src/utils/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LpAccountBalanceHourly, SubgraphResult } from "./types";
import { linea } from "viem/chains";
import { createPublicClient, http, parseUnits } from "viem";

export const CONNEXT_SUBGRAPH_QUERY_URL = "https://api.goldsky.com/api/public/project_clssc64y57n5r010yeoly05up/subgraphs/amarok-stableswap-analytics/1.0/gn";
export const CONNEXT_SUBGRAPH_QUERY_URL = "https://api.goldsky.com/api/public/project_clssc64y57n5r010yeoly05up/subgraphs/connext/stableswap-analytics-linea-0.0.1/gn";
export const LINEA_CHAIN_ID = 59144;
export const CONNEXT_LINEA_ADDRESS = "0xa05eF29e9aC8C75c530c2795Fa6A800e188dE0a9";

Expand Down Expand Up @@ -37,10 +37,17 @@ const CONNEXT_ABI = [
const LP_HOURLY_QUERY_BY_BLOCK = (
first: number,
offset: number,
blockNumber: number
blockNumber: number,
account?: string
): string => `
query LpAccountBalanceHourly {
lpAccountBalanceHourlies(first: ${first}, offset: ${offset}, where: { block_lte: ${blockNumber} }) {
lpAccountBalanceHourlies(
first: ${first},
skip: ${offset},
orderBy: modified,
orderDirection: asc,
where: { block_lte: ${blockNumber}${account ? `, account: "${account.toLowerCase()}"` : ''} }
) {
amount
account {
id
Expand All @@ -61,7 +68,13 @@ const LP_HOURLY_QUERY_BY_TIMESTAMP = (
timestamp: number
): string => `
query LpAccountBalanceHourly {
lpAccountBalanceHourlies(first: ${first}, offset: ${offset}, where: { modified_lte: ${timestamp} }) {
lpAccountBalanceHourlies(
first: ${first},
offset: ${offset},
orderBy: modified,
orderDirection: asc,
where: { modified_lte: ${timestamp} })
{
amount
account {
id
Expand Down Expand Up @@ -91,15 +104,15 @@ const executeSubgraphQuery = async (query: string): Promise<SubgraphResult> => {
return data;
}

export const getLpAccountBalanceAtBlock = async (block: number, interval = 1000): Promise<LpAccountBalanceHourly[]> => {
export const getLpAccountBalanceAtBlock = async (block: number, interval = 1000, account?: string): Promise<LpAccountBalanceHourly[]> => {
let hasMore = true;
let offset = 0;
const balances = new Map<string, LpAccountBalanceHourly[]>()

while (hasMore) {
const {
lpAccountBalanceHourlies
} = await executeSubgraphQuery(LP_HOURLY_QUERY_BY_BLOCK(interval, offset, block))
} = await executeSubgraphQuery(LP_HOURLY_QUERY_BY_BLOCK(interval, offset, block, account))
appendSubgraphData(lpAccountBalanceHourlies, balances);
hasMore = lpAccountBalanceHourlies.length === interval;
offset += interval;
Expand Down Expand Up @@ -148,7 +161,7 @@ export const getCompositeBalances = async (data: LpAccountBalanceHourly[]): Prom
const client = createPublicClient({ chain: linea, transport: http() });

// get composite balances
const balances = await Promise.all(data.map(async ({ token, amount }) => {
const balances = await Promise.all(data.map(async ({ token, amount, block }) => {
const poolId = token.id.toLowerCase();
const pool = poolInfo.get(poolId);
if (!pool) {
Expand All @@ -159,7 +172,8 @@ export const getCompositeBalances = async (data: LpAccountBalanceHourly[]): Prom
address: CONNEXT_LINEA_ADDRESS,
functionName: "calculateRemoveSwapLiquidity",
args: [pool.key, parseUnits(amount, 18)],
abi: CONNEXT_ABI
abi: CONNEXT_ABI,
blockNumber: BigInt(block)
}) as [bigint, bigint];
return withdrawn.map(w => w.toString());
}));
Expand Down

0 comments on commit 76ccd1b

Please sign in to comment.