Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bonds chart #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/apps/src/app/bonds/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class BondsApp extends TradeApp {
return html`
<uigc-paper class=${classMap(classes)}>
${when(
active,
this.chart,
() => html`
<gc-bonds-chart
.assetIn=${this.lbp.accumulated}
Expand Down
42 changes: 24 additions & 18 deletions packages/apps/src/app/bonds/chart/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class BondsChartApi {
}

const pools = await queryPool(this._squidUrl, account32);
const [poolData] = pools.lbpPoolData;
const poolData = pools.lbpPool;
return poolData;
}

Expand Down Expand Up @@ -80,21 +80,27 @@ export class BondsChartApi {
pool.id,
maturity,
);
queryPoolPrice(this._squidUrl, indexes).then(
({ historicalPoolPriceData }) => {
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(
Expand Down Expand Up @@ -128,7 +134,7 @@ export class BondsChartApi {
account32,
blockHeight,
).then();
return res.historicalPoolPriceData[0];
return res.lbpPoolHistoricalData.nodes[0];
}

async getLastBlock(
Expand All @@ -141,6 +147,6 @@ export class BondsChartApi {
account32,
blockHeight,
);
return res.historicalPoolPriceData[0];
return res.lbpPoolHistoricalData.nodes[0];
}
}
106 changes: 68 additions & 38 deletions packages/apps/src/app/bonds/chart/query.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<HistoricalPrice>;
lbpPoolHistoricalData: { nodes: Array<HistoricalPrice> };
}

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
}
}
}
`;
Expand All @@ -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
}
}
}
`;
Expand All @@ -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<HistoricalPrices>(squidUrl, QUERY_POOL_DATA, {
recordIds: ids,
export async function queryPoolPrice(recordIds: string[]) {
return await request<{
lbpPools: {
nodes: Array<{
relayChainBlockHeight: number;
lbpPoolAssetsDataByPoolId: {
nodes: Array<HistoricalBalance>;
};
}>;
};
}>(allBlocksSquidUrl, QUERY_POOL_DATA, {
recordIds,
});
}

const QUERY_POOL = gql`
query ($id: String!) {
lbpPoolData(where: { id_eq: $id }) {
lbpPool(id: $id) {
id
startBlockNumber
endBlockNumber
Expand All @@ -108,7 +140,7 @@ export interface LbpPoolData {
}

export interface LbpPools {
lbpPoolData: Array<LbpPoolData>;
lbpPool: LbpPoolData;
}

export async function queryPool(squidUrl: string, poolId: string) {
Expand All @@ -125,12 +157,10 @@ const QUERY_POOLS = gql`
assetBId: { equalTo: $assetOut }
}
) {
edges {
node {
id
assetAId
assetBId
}
nodes {
id
assetAId
assetBId
}
}
}
Expand All @@ -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<LbpPool> } }>(
squidUrl,
QUERY_POOLS,
{
Expand All @@ -156,5 +186,5 @@ export async function queryPools(
},
);

return data.lbpPools.edges.map((lpbPool) => lpbPool.node);
return data.lbpPools.nodes;
}
6 changes: 5 additions & 1 deletion packages/apps/src/app/bonds/chart/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down