Skip to content

Commit

Permalink
Merge pull request #1988 from zeitgeistpm/tr-fix-build
Browse files Browse the repository at this point in the history
Switch coin gecko price fetching for subsquid
  • Loading branch information
yornaath authored Nov 16, 2023
2 parents 61436de + 3fd0b89 commit f52cb02
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
3 changes: 3 additions & 0 deletions lib/constants/foreign-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ForeignAssetMetadata = {
withdrawSupported: boolean;
withdrawDestinationFee?: string;
tokenSymbol: string;
subsquidId?: string;
};
};

Expand Down Expand Up @@ -37,6 +38,7 @@ const BATTERY_STATION_FOREIGN_ASSET_METADATA: ForeignAssetMetadata = {
withdrawSupported: false,
coinGeckoId: "polkadot",
tokenSymbol: "DOT",
subsquidId: "DOT",
},
1: {
originChain: "Rococo",
Expand All @@ -61,6 +63,7 @@ const PROD_FOREIGN_ASSET_METADATA: ForeignAssetMetadata = {
withdrawSupported: true,
coinGeckoId: "polkadot",
tokenSymbol: "DOT",
subsquidId: "DOT",
},
};

Expand Down
2 changes: 1 addition & 1 deletion lib/gql/trending-markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const getTrendingMarkets = async (
return pools;
});

const basePrices = await getBaseAssetPrices();
const basePrices = await getBaseAssetPrices(sdk);

const trendingPoolIds = calcTrendingPools(historicalPools, basePrices, pools);

Expand Down
65 changes: 39 additions & 26 deletions lib/hooks/queries/useAssetUsdPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -85,34 +88,44 @@ export const useAllForeignAssetUsdPrices = (): {
};
};

export const getBaseAssetPrices = async (): Promise<ForeignAssetPrices> => {
const zeitgeistCoingeckoId = "zeitgeist";
const coinGeckoIds = [
...Object.values(FOREIGN_ASSET_METADATA).map((asset) => asset.coinGeckoId),
zeitgeistCoingeckoId,
];
const res = await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=${coinGeckoIds.join(
"%2C",
)}&vs_currencies=usd`,
export const getBaseAssetPrices = async (
sdk: Sdk<FullContext>,
): Promise<ForeignAssetPrices> => {
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<ForeignAssetPrices>(
(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 json = await res.json();

const assetPrices = Object.keys(
FOREIGN_ASSET_METADATA,
).reduce<ForeignAssetPrices>((prices, assetNumber) => {
const assetMetadata = FOREIGN_ASSET_METADATA[Number(assetNumber)];
const coinGeckoId = assetMetadata.coinGeckoId;
const assetPrice = json[coinGeckoId].usd;
prices[assetNumber] = new Decimal(assetPrice);

return prices;
}, {});

assetPrices["ztg"] = json[zeitgeistCoingeckoId].usd;
prices["ztg"] = new Decimal(
assetPrice.find((price) => price.pair.includes("ZTG"))?.price ?? 0,
);

return assetPrices;
return prices;
};

export const getForeignAssetPriceServerSide = async (
Expand All @@ -126,7 +139,7 @@ export const getForeignAssetPriceServerSide = async (
);

const json = await res.json();

console.log(json);
return new Decimal(json[coinGeckoId]?.usd ?? 0);
};
export const getForeignAssetPrice = async (foreignAsset: ForeignAssetId) => {
Expand Down

0 comments on commit f52cb02

Please sign in to comment.