Skip to content

Commit

Permalink
fetch prices from indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Robiquet committed Nov 16, 2023
1 parent 294db7e commit c1db66c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 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
77 changes: 38 additions & 39 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,48 +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,
];

console.log(
`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 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<ForeignAssetPrices>((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 (
Expand Down

0 comments on commit c1db66c

Please sign in to comment.