From d955625e71d370226368930c770782d60f6e2cc2 Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Tue, 26 Dec 2023 17:55:39 +0100 Subject: [PATCH] Merge pull request #4 from gear-foundation/vs/price-api --- src/config/networks.ts | 2 +- src/consts.ts | 6 +++--- src/contexts/Plugins/index.tsx | 6 +++--- src/library/Hooks/usePrices/index.tsx | 2 +- src/library/Hooks/useUnitPrice/index.tsx | 12 ++++++------ src/library/NetworkBar/index.tsx | 2 +- src/locale/cn/modals.json | 2 +- src/modals/Settings/index.tsx | 6 +++--- src/pages/Overview/BalanceChart.tsx | 2 +- src/types/index.ts | 4 ++-- 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/config/networks.ts b/src/config/networks.ts index 489812cc58..e0ff395a79 100644 --- a/src/config/networks.ts +++ b/src/config/networks.ts @@ -62,7 +62,7 @@ export const NetworkList: Networks = { }, api: { unit: 'VARA', - priceTicker: 'VARAUSDT', + id: 'vara-network', }, params: { ...DefaultParams, diff --git a/src/consts.ts b/src/consts.ts index dc2994e0f2..ecbb9820d0 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -35,7 +35,7 @@ export const TipsThresholdMedium = 1200; */ export const PluginsList: Plugin[] = [ 'subscan', - 'binance_spot', + 'coingecko', 'tips', 'polkawatch', ]; @@ -64,9 +64,9 @@ export const MaxEraRewardPointsEras = 14; * Third party API keys and endpoints */ export const ApiSubscanKey = 'd37149339f64775155a82a53f4253b27'; -export const EndpointPrice = 'https://api.binance.com/api/v3'; +export const EndpointPrice = 'https://api.coingecko.com/api/v3'; export const ApiEndpoints = { - priceChange: `${EndpointPrice}/ticker/24hr?symbol=`, + priceChange: `${EndpointPrice}/simple/price`, subscanRewardSlash: '/api/v2/scan/account/reward_slash', subscanPoolRewards: '/api/scan/nomination_pool/rewards', subscanEraStat: '/api/scan/staking/era_stat', diff --git a/src/contexts/Plugins/index.tsx b/src/contexts/Plugins/index.tsx index 53158c2346..6396d7f24b 100644 --- a/src/contexts/Plugins/index.tsx +++ b/src/contexts/Plugins/index.tsx @@ -21,10 +21,10 @@ export const PluginsProvider = ({ true ) as Plugin[]; - // if fiat is disabled, remove binance_spot service + // if fiat is disabled, remove coingecko service const DISABLE_FIAT = Number(import.meta.env.VITE_DISABLE_FIAT ?? 0); - if (DISABLE_FIAT && localPlugins.includes('binance_spot')) { - const index = localPlugins.indexOf('binance_spot'); + if (DISABLE_FIAT && localPlugins.includes('coingecko')) { + const index = localPlugins.indexOf('coingecko'); if (index !== -1) localPlugins.splice(index, 1); } return localPlugins; diff --git a/src/library/Hooks/usePrices/index.tsx b/src/library/Hooks/usePrices/index.tsx index 6280659d1f..49f8df7221 100644 --- a/src/library/Hooks/usePrices/index.tsx +++ b/src/library/Hooks/usePrices/index.tsx @@ -70,7 +70,7 @@ export const usePrices = () => { // servie toggle useEffect(() => { - if (plugins.includes('binance_spot')) { + if (plugins.includes('coingecko')) { if (priceHandle === null) { initiatePriceInterval(); } diff --git a/src/library/Hooks/useUnitPrice/index.tsx b/src/library/Hooks/useUnitPrice/index.tsx index e7e4c1a98f..7b07f24242 100644 --- a/src/library/Hooks/useUnitPrice/index.tsx +++ b/src/library/Hooks/useUnitPrice/index.tsx @@ -10,26 +10,26 @@ export const useUnitPrice = () => { const fetchUnitPrice = async () => { const urls = [ - `${ApiEndpoints.priceChange}${NetworkList[network].api.priceTicker}`, + `${ApiEndpoints.priceChange}?ids=${NetworkList[network].api.id}&vs_currencies=usd&include_24hr_change=true`, ]; const responses = await Promise.all( urls.map((u) => fetch(u, { method: 'GET' })) ); const texts = await Promise.all(responses.map((res) => res.json())); - const newPrice = texts[0]; + const newPrice = texts[0][NetworkList[network].api.id]; if ( - newPrice.lastPrice !== undefined && - newPrice.priceChangePercent !== undefined + newPrice.usd !== undefined && + newPrice.usd_24h_change !== undefined ) { - const price: string = (Math.ceil(newPrice.lastPrice * 100) / 100).toFixed( + const price: string = (Math.ceil(newPrice.usd * 100) / 100).toFixed( 2 ); return { lastPrice: price, - change: (Math.round(newPrice.priceChangePercent * 100) / 100).toFixed( + change: (Math.round(newPrice.usd_24h_change * 100) / 100).toFixed( 2 ), }; diff --git a/src/library/NetworkBar/index.tsx b/src/library/NetworkBar/index.tsx index e59767ed14..c761119f75 100644 --- a/src/library/NetworkBar/index.tsx +++ b/src/library/NetworkBar/index.tsx @@ -73,7 +73,7 @@ export const NetworkBar = () => {
- {plugins.includes('binance_spot') && ( + {plugins.includes('coingecko') && ( <>
{ /> {!DISABLE_FIAT && ( togglePlugin('binance_spot')} + checked={plugins.includes('coingecko')} + label={t('coingeckoApi')} + onClick={() => togglePlugin('coingecko')} /> )} diff --git a/src/pages/Overview/BalanceChart.tsx b/src/pages/Overview/BalanceChart.tsx index 2e5a4d1a07..c428b3fd5a 100644 --- a/src/pages/Overview/BalanceChart.tsx +++ b/src/pages/Overview/BalanceChart.tsx @@ -161,7 +161,7 @@ export const BalanceChart = () => { zeroDecimals={2} /> - {plugins.includes('binance_spot') ? ( + {plugins.includes('coingecko') ? ( <> {usdFormatter.format(freeFiat.toNumber())} ) : null} diff --git a/src/types/index.ts b/src/types/index.ts index 3834bf40bd..0051ed23d6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -58,7 +58,7 @@ export interface Network { }; api: { unit: string; - priceTicker: string; + id: string; }; params: Record; defaultFeeReserve: number; @@ -100,7 +100,7 @@ export type MaybeAddress = string | null; export type MaybeString = string | null; // list of available plugins. -export type Plugin = 'subscan' | 'binance_spot' | 'tips' | 'polkawatch'; +export type Plugin = 'subscan' | 'coingecko' | 'tips' | 'polkawatch'; // track the status of a syncing / fetching process. export type Sync = 'unsynced' | 'syncing' | 'synced';