From c1db66cda74d6d4394be59515c09e87a38dbf89a Mon Sep 17 00:00:00 2001 From: Tom Robiquet Date: Thu, 16 Nov 2023 10:51:03 +0100 Subject: [PATCH] fetch prices from indexer --- lib/constants/foreign-asset.ts | 3 ++ lib/gql/trending-markets.ts | 2 +- lib/hooks/queries/useAssetUsdPrice.ts | 77 +++++++++++++-------------- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/lib/constants/foreign-asset.ts b/lib/constants/foreign-asset.ts index 6cfffacb8..b02c4837d 100644 --- a/lib/constants/foreign-asset.ts +++ b/lib/constants/foreign-asset.ts @@ -9,6 +9,7 @@ type ForeignAssetMetadata = { withdrawSupported: boolean; withdrawDestinationFee?: string; tokenSymbol: string; + subsquidId?: string; }; }; @@ -37,6 +38,7 @@ const BATTERY_STATION_FOREIGN_ASSET_METADATA: ForeignAssetMetadata = { withdrawSupported: false, coinGeckoId: "polkadot", tokenSymbol: "DOT", + subsquidId: "DOT", }, 1: { originChain: "Rococo", @@ -61,6 +63,7 @@ const PROD_FOREIGN_ASSET_METADATA: ForeignAssetMetadata = { withdrawSupported: true, coinGeckoId: "polkadot", tokenSymbol: "DOT", + subsquidId: "DOT", }, }; diff --git a/lib/gql/trending-markets.ts b/lib/gql/trending-markets.ts index 554fc28fa..77feda655 100644 --- a/lib/gql/trending-markets.ts +++ b/lib/gql/trending-markets.ts @@ -118,7 +118,7 @@ const getTrendingMarkets = async ( return pools; }); - const basePrices = await getBaseAssetPrices(); + const basePrices = await getBaseAssetPrices(sdk); const trendingPoolIds = calcTrendingPools(historicalPools, basePrices, pools); diff --git a/lib/hooks/queries/useAssetUsdPrice.ts b/lib/hooks/queries/useAssetUsdPrice.ts index da40dce37..960662c9f 100644 --- a/lib/hooks/queries/useAssetUsdPrice.ts +++ b/lib/hooks/queries/useAssetUsdPrice.ts @@ -2,13 +2,16 @@ import { useQueries, useQuery, UseQueryResult } from "@tanstack/react-query"; import { AssetId, ForeignAssetId, + FullContext, IOForeignAssetId, IOZtgAssetId, parseAssetId, + Sdk, } from "@zeitgeistpm/sdk"; import Decimal from "decimal.js"; import { environment } from "lib/constants"; import { FOREIGN_ASSET_METADATA } from "lib/constants/foreign-asset"; +import { isPresent } from "lib/types"; export const assetUsdPriceRootKey = "asset-usd-price"; @@ -85,48 +88,44 @@ export const useAllForeignAssetUsdPrices = (): { }; }; -export const getBaseAssetPrices = async (): Promise => { - const zeitgeistCoingeckoId = "zeitgeist"; - const coinGeckoIds = [ - ...Object.values(FOREIGN_ASSET_METADATA).map((asset) => asset.coinGeckoId), - zeitgeistCoingeckoId, - ]; - - console.log( - `https://api.coingecko.com/api/v3/simple/price?ids=${coinGeckoIds.join( - "%2C", - )}&vs_currencies=usd`, +export const getBaseAssetPrices = async ( + sdk: Sdk, +): Promise => { + const assets = [ + ...Object.values(FOREIGN_ASSET_METADATA).map((asset) => asset.subsquidId), + "ZTG", + ].filter(isPresent); + + const { assetPrice } = await sdk.indexer.client.request<{ + assetPrice: { price: number; pair: string }[]; + }>(`query AllAssetPrices { + assetPrice(base: [${assets.join(",")}], target: USD) { + price + pair + timestamp + } + }`); + + const prices = Object.keys(FOREIGN_ASSET_METADATA).reduce( + (prices, assetNumber) => { + const assetMetadata = FOREIGN_ASSET_METADATA[Number(assetNumber)]; + const subsquidId = assetMetadata.subsquidId; + const price = + (subsquidId && + assetPrice.find((price) => price.pair.includes(subsquidId))?.price) ?? + 0; + prices[assetNumber] = new Decimal(price); + + return prices; + }, + {}, ); - // const res = await fetch( - // `https://api.coingecko.com/api/v3/simple/price?ids=${coinGeckoIds.join( - // "%2C", - // )}&vs_currencies=usd`, - // ); - - // const json = await res.json(); - - // const assetPrices = Object.keys( - // FOREIGN_ASSET_METADATA, - // ).reduce((prices, assetNumber) => { - // const assetMetadata = FOREIGN_ASSET_METADATA[Number(assetNumber)]; - // const coinGeckoId = assetMetadata.coinGeckoId; - // const assetPrice = json[coinGeckoId].usd; - // prices[assetNumber] = new Decimal(assetPrice); - - // console.log(prices); - // return prices; - // }, {}); - - // assetPrices["ztg"] = json[zeitgeistCoingeckoId].usd; - // console.log(assetPrices); + prices["ztg"] = new Decimal( + assetPrice.find((price) => price.pair.includes("ZTG"))?.price ?? 0, + ); - return { - "0": new Decimal(5.39), - "1": new Decimal(5.39), - "3": new Decimal(5.39), - ztg: new Decimal(0.03269525), - }; + return prices; }; export const getForeignAssetPriceServerSide = async (