From 909ab8b69533b142b5ff053fdbcbaea0081998fa Mon Sep 17 00:00:00 2001 From: vkulinich Date: Tue, 19 Nov 2024 12:46:45 +0100 Subject: [PATCH] FIx bonds chart --- packages/apps/src/app/bonds/App.ts | 2 +- packages/apps/src/app/bonds/chart/api.ts | 42 ++++---- packages/apps/src/app/bonds/chart/query.ts | 106 +++++++++++++-------- packages/apps/src/app/bonds/chart/utils.ts | 6 +- 4 files changed, 98 insertions(+), 58 deletions(-) diff --git a/packages/apps/src/app/bonds/App.ts b/packages/apps/src/app/bonds/App.ts index 65bfd4533e..3b43e7991e 100644 --- a/packages/apps/src/app/bonds/App.ts +++ b/packages/apps/src/app/bonds/App.ts @@ -205,7 +205,7 @@ export class BondsApp extends TradeApp { return html` ${when( - active, + this.chart, () => html` { - const lastBlock = historicalPoolPriceData[0]; - const datapoints: [number, number][] = historicalPoolPriceData.map( - (price) => { - return getBlockPrice(assetIn, assetOut, price, pool); - }, - ); - const historicalBalance = { - dataset: datapoints.reverse(), - lastBlock: lastBlock, - } as HistoricalBalance; - onSuccess(assetIn, assetOut, historicalBalance); - }, - ); + queryPoolPrice(indexes).then(({ lbpPools }) => { + const lastBlock = lbpPools[0]; + + const datapoints: [number, number][] = lbpPools.nodes.map( + ({ relayChainBlockHeight, lbpPoolAssetsDataByPoolId }) => { + const [balanceA, balanceB] = lbpPoolAssetsDataByPoolId.nodes; + const price = { + relayChainBlockHeight, + assetABalance: balanceB.balances.free, + assetBBalance: balanceA.balances.free, + }; + return getBlockPrice(assetIn, assetOut, price, pool); + }, + ); + + const historicalBalance = { + dataset: datapoints.reverse(), + lastBlock: lastBlock, + } as HistoricalBalance; + onSuccess(assetIn, assetOut, historicalBalance); + }); } getPoolPredictionPrices( @@ -128,7 +134,7 @@ export class BondsChartApi { account32, blockHeight, ).then(); - return res.historicalPoolPriceData[0]; + return res.lbpPoolHistoricalData.nodes[0]; } async getLastBlock( @@ -141,6 +147,6 @@ export class BondsChartApi { account32, blockHeight, ); - return res.historicalPoolPriceData[0]; + return res.lbpPoolHistoricalData.nodes[0]; } } diff --git a/packages/apps/src/app/bonds/chart/query.ts b/packages/apps/src/app/bonds/chart/query.ts index d7a3485fe9..28d634595a 100644 --- a/packages/apps/src/app/bonds/chart/query.ts +++ b/packages/apps/src/app/bonds/chart/query.ts @@ -1,5 +1,8 @@ import { gql, request } from 'graphql-request'; +const allBlocksSquidUrl = + 'https://galacticcouncil.squids.live/hydration-storage-dictionary:lbppool/api/graphql'; + export interface HistoricalPrice { pool: { assetAId: number; @@ -11,19 +14,33 @@ export interface HistoricalPrice { relayChainBlockHeight: number; } +export interface HistoricalBalance { + balances: { + free: string; + frozen: string; + rezerved: string; + }; + assetId: string; +} + export interface HistoricalPrices { - historicalPoolPriceData: Array; + lbpPoolHistoricalData: { nodes: Array }; } const QUERY_POOL_FIRST_BLOCK = gql` - query ($id: String!, $blockHeight: Int!) { - historicalPoolPriceData( - where: { relayChainBlockHeight_gte: $blockHeight, pool: { id_eq: $id } } - orderBy: relayChainBlockHeight_ASC - limit: 1 + query FirstBlock($id: String!, $blockHeight: Int!) { + lbpPoolHistoricalData( + filter: { + poolId: { equalTo: $id } + relayChainBlockHeight: { greaterThanOrEqualTo: $blockHeight } + } + first: 1 + orderBy: RELAY_CHAIN_BLOCK_HEIGHT_ASC ) { - relayChainBlockHeight - paraChainBlockHeight + nodes { + paraChainBlockHeight + relayChainBlockHeight + } } } `; @@ -40,14 +57,19 @@ export async function queryPoolFirstBlock( } const QUERY_POOL_LAST_BLOCK = gql` - query ($id: String!, $blockHeight: Int!) { - historicalPoolPriceData( - where: { relayChainBlockHeight_lte: $blockHeight, pool: { id_eq: $id } } - orderBy: relayChainBlockHeight_DESC - limit: 1 + query LastBlock($id: String!, $blockHeight: Int!) { + lbpPoolHistoricalData( + filter: { + poolId: { equalTo: $id } + relayChainBlockHeight: { lessThanOrEqualTo: $blockHeight } + } + first: 1 + orderBy: RELAY_CHAIN_BLOCK_HEIGHT_DESC ) { - relayChainBlockHeight - paraChainBlockHeight + nodes { + paraChainBlockHeight + relayChainBlockHeight + } } } `; @@ -65,31 +87,41 @@ export async function queryPoolLastBlock( const QUERY_POOL_DATA = gql` query ($recordIds: [String!]!) { - historicalPoolPriceData( - where: { id_in: $recordIds } - orderBy: relayChainBlockHeight_DESC + lbpPools( + filter: { id: { in: $recordIds } } + orderBy: RELAY_CHAIN_BLOCK_HEIGHT_DESC ) { - pool { - assetAId - assetBId + nodes { + relayChainBlockHeight + lbpPoolAssetsDataByPoolId { + nodes { + balances + assetId + } + } } - assetABalance - assetBBalance - relayChainBlockHeight - paraChainBlockHeight } } `; -export async function queryPoolPrice(squidUrl: string, ids: string[]) { - return await request(squidUrl, QUERY_POOL_DATA, { - recordIds: ids, +export async function queryPoolPrice(recordIds: string[]) { + return await request<{ + lbpPools: { + nodes: Array<{ + relayChainBlockHeight: number; + lbpPoolAssetsDataByPoolId: { + nodes: Array; + }; + }>; + }; + }>(allBlocksSquidUrl, QUERY_POOL_DATA, { + recordIds, }); } const QUERY_POOL = gql` query ($id: String!) { - lbpPoolData(where: { id_eq: $id }) { + lbpPool(id: $id) { id startBlockNumber endBlockNumber @@ -108,7 +140,7 @@ export interface LbpPoolData { } export interface LbpPools { - lbpPoolData: Array; + lbpPool: LbpPoolData; } export async function queryPool(squidUrl: string, poolId: string) { @@ -125,12 +157,10 @@ const QUERY_POOLS = gql` assetBId: { equalTo: $assetOut } } ) { - edges { - node { - id - assetAId - assetBId - } + nodes { + id + assetAId + assetBId } } } @@ -147,7 +177,7 @@ export async function queryPools( assetIn: string, assetOut: string, ) { - const data = await request<{ lbpPools: { edges: Array<{ node: LbpPool }> } }>( + const data = await request<{ lbpPools: { nodes: Array } }>( squidUrl, QUERY_POOLS, { @@ -156,5 +186,5 @@ export async function queryPools( }, ); - return data.lbpPools.edges.map((lpbPool) => lpbPool.node); + return data.lbpPools.nodes; } diff --git a/packages/apps/src/app/bonds/chart/utils.ts b/packages/apps/src/app/bonds/chart/utils.ts index f4c7953f80..8454fd8815 100644 --- a/packages/apps/src/app/bonds/chart/utils.ts +++ b/packages/apps/src/app/bonds/chart/utils.ts @@ -17,7 +17,11 @@ export const getPoolMaturity = (pool: LbpPoolData, price: HistoricalPrice) => { export const getBlockPrice = ( assetIn: Asset, assetOut: Asset, - historicalPrice: HistoricalPrice, + historicalPrice: { + relayChainBlockHeight: number; + assetABalance: string; + assetBBalance: string; + }, pool: LbpPoolData, ): [number, number] => { const { relayChainBlockHeight } = historicalPrice;