From 04a44448107bbb877a33f6e8f0b0fecf327065c7 Mon Sep 17 00:00:00 2001 From: Alexandr Date: Fri, 29 Nov 2024 20:57:14 +0300 Subject: [PATCH 1/2] add view for asset price --- server/server.ts | 22 +++++++++++++++++++++- server/utils/utils.ts | 3 ++- src/pages/assets/index.tsx | 12 ++++++++++-- src/utils/methods.ts | 4 ++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/server/server.ts b/server/server.ts index 6871de1..33515ad 100644 --- a/server/server.ts +++ b/server/server.ts @@ -1145,7 +1145,27 @@ const requestsLimiter = rateLimit({ } else { return res.json({ success: true, asset: dbAsset }); } - })); + }) +); + + app.get('/api/get_asset_price_rate', exceptionHandler(async (req, res) => { + const { assetId } = req.query; + if (!assetId) { + return res.json({ success: false, data: "Asset id not provided" }); + } + const assetPriceResponse = await axios({ + method: 'get', + url: config.trade_api_url + '/dex/get-asset-price-rate', + params: { assetId }, + }); + + if (assetPriceResponse.data.success) { + return res.json({ success: true, priceRate: assetPriceResponse.data.priceRate }); + } else { + return res.json({ success: false, data: "Asset not found" }) + } + + })) io.on('connection', async (socket) => { diff --git a/server/utils/utils.ts b/server/utils/utils.ts index d30dd2c..ed67e09 100644 --- a/server/utils/utils.ts +++ b/server/utils/utils.ts @@ -14,7 +14,8 @@ export const config = { "enabled_during_sync": process.env.WEBSOCKET_ENABLED_DURING_SYNC === "true" }, "enableVisibilityInfo": process.env.ENABLE_VISIBILITY_INFO === "true", - "maxDaemonRequestCount": parseInt(process.env.MAX_DAEMON_REQUEST_COUNT || "", 10) || 1000 + "maxDaemonRequestCount": parseInt(process.env.MAX_DAEMON_REQUEST_COUNT || "", 10) || 1000, + "trade_api_url": process.env.TRADE_API_URL } export function log(msg: string) { diff --git a/src/pages/assets/index.tsx b/src/pages/assets/index.tsx index 5b06040..1d584bb 100644 --- a/src/pages/assets/index.tsx +++ b/src/pages/assets/index.tsx @@ -180,7 +180,15 @@ function Assets(props: AssetsPageProps) { if (newFetchId !== fetchIdRef.current) return; - const resultAssets = result; + const resultAssets = await Promise.all(result.map( async (asset:any) => { + const assetPriceRateResponse = await Fetch.getAssetPriceRate(asset.asset_id); + const zanoPrice = await Utils.getZanoPrice(); + if (assetPriceRateResponse.success && assetPriceRateResponse.priceRate && zanoPrice) { + asset.price = (assetPriceRateResponse.priceRate * zanoPrice).toFixed(2); + } + return asset + })) + if (!resultAssets || !(resultAssets instanceof Array)) return; fetchZanoPrice(resultAssets); @@ -196,7 +204,7 @@ function Assets(props: AssetsPageProps) { } const tableHeaders = [ "NAME", "TICKER", "ASSET ID", "PRICE (POWERED BY COINGECKO)" ]; - + const tableElements = assets.map(e => [ e?.full_name || "", e?.ticker || "", diff --git a/src/utils/methods.ts b/src/utils/methods.ts index 6a79d1e..c9e1225 100644 --- a/src/utils/methods.ts +++ b/src/utils/methods.ts @@ -102,6 +102,10 @@ class Fetch { static async getTxPoolInfo(count: number) { return await fetch(this.proxyPath + `/get_tx_pool_details/${encodeURIComponent(count)}`).then(res => res.json()); } + + static async getAssetPriceRate(assetId: string){ + return await fetch(this.proxyPath + `/get_asset_price_rate/?assetId=${assetId}`).then(res => res.json()) + } } export default Fetch; \ No newline at end of file From 502d5dc7895139a36d28e1cd440163461787b8fe Mon Sep 17 00:00:00 2001 From: Alexandr Date: Mon, 2 Dec 2024 19:02:16 +0300 Subject: [PATCH 2/2] fix reduce requests quantity --- server/server.ts | 29 ++++++++++++++++++----------- src/pages/assets/index.tsx | 22 +++++++++++++++------- src/utils/methods.ts | 11 +++++++---- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/server/server.ts b/server/server.ts index 33515ad..412b905 100644 --- a/server/server.ts +++ b/server/server.ts @@ -24,6 +24,8 @@ import { ITransaction } from "./schemes/Transaction"; import BigNumber from "bignumber.js"; import next from "next"; import { rateLimit } from 'express-rate-limit'; +import bodyParser from 'body-parser'; + import fs from "fs"; // @ts-ignore const __dirname = import.meta.dirname; @@ -73,6 +75,8 @@ const requestsLimiter = rateLimit({ }) app.use(express.static(path.resolve(__dirname, "../build/"))); + app.use(bodyParser.json()); + app.use([ "/api/find_outs_in_recent_blocks" ], requestsLimiter); @@ -1101,7 +1105,6 @@ const requestsLimiter = rateLimit({ return res.json(responseData); })); - app.get('/api/get_asset_details/:asset_id', exceptionHandler(async (req, res) => { const { asset_id } = req.params; @@ -1148,26 +1151,30 @@ const requestsLimiter = rateLimit({ }) ); - app.get('/api/get_asset_price_rate', exceptionHandler(async (req, res) => { - const { assetId } = req.query; - if (!assetId) { + + app.post('/api/get_assets_price_rates', exceptionHandler(async (req, res) => { + const { assetsIds } = req.body; + if (!assetsIds) { return res.json({ success: false, data: "Asset id not provided" }); } - const assetPriceResponse = await axios({ - method: 'get', - url: config.trade_api_url + '/dex/get-asset-price-rate', - params: { assetId }, + const assetsPricesResponse = await axios({ + method: 'post', + url: config.trade_api_url + '/dex/get-assets-price-rates', + data: { assetsIds }, }); - if (assetPriceResponse.data.success) { - return res.json({ success: true, priceRate: assetPriceResponse.data.priceRate }); + const assetsPrices = assetsPricesResponse.data; + + if (assetsPricesResponse?.data?.success) { + return res.json({ success: true, priceRates: assetsPrices.priceRates }); } else { - return res.json({ success: false, data: "Asset not found" }) + return res.json({ success: false, data: "Assets not found" }) } })) + io.on('connection', async (socket) => { socket.on('get-socket-info', () => { emitSocketInfo(socket); diff --git a/src/pages/assets/index.tsx b/src/pages/assets/index.tsx index 1d584bb..d3800db 100644 --- a/src/pages/assets/index.tsx +++ b/src/pages/assets/index.tsx @@ -180,14 +180,22 @@ function Assets(props: AssetsPageProps) { if (newFetchId !== fetchIdRef.current) return; - const resultAssets = await Promise.all(result.map( async (asset:any) => { - const assetPriceRateResponse = await Fetch.getAssetPriceRate(asset.asset_id); - const zanoPrice = await Utils.getZanoPrice(); - if (assetPriceRateResponse.success && assetPriceRateResponse.priceRate && zanoPrice) { - asset.price = (assetPriceRateResponse.priceRate * zanoPrice).toFixed(2); + const assetsIds = result.map((asset: any) => asset.asset_id); + + const assetsPriceRatesResponse = await Fetch.getAssetsPriceRates(assetsIds); + + const assetsPriceRates = assetsPriceRatesResponse?.priceRates; + + const zanoPrice = await Utils.getZanoPrice(); + + const resultAssets = result.map((resultAsset:any) => { + if (assetsPriceRatesResponse?.success && zanoPrice) { + const targetAsset = assetsPriceRates.find((asset: any)=> asset.asset_id === resultAsset.asset_id); + if (!targetAsset) return resultAsset; + resultAsset.price = (targetAsset.rate * zanoPrice).toFixed(2); } - return asset - })) + return resultAsset + }) if (!resultAssets || !(resultAssets instanceof Array)) return; diff --git a/src/utils/methods.ts b/src/utils/methods.ts index c9e1225..2af5146 100644 --- a/src/utils/methods.ts +++ b/src/utils/methods.ts @@ -3,13 +3,13 @@ import { chartRequestNames } from "./constants"; const PORT = process.env.SERVER_PORT; async function postFetch(path: string, body: Object) { - return await fetch("/api/user/set-theme", { + return await fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) - }).then(res => res.json()); + }); } class Fetch { @@ -103,8 +103,11 @@ class Fetch { return await fetch(this.proxyPath + `/get_tx_pool_details/${encodeURIComponent(count)}`).then(res => res.json()); } - static async getAssetPriceRate(assetId: string){ - return await fetch(this.proxyPath + `/get_asset_price_rate/?assetId=${assetId}`).then(res => res.json()) + static async getAssetsPriceRates(assetsIds: string[]){ + return await postFetch( + this.proxyPath + `/get_assets_price_rates`, + { assetsIds }, + ).then(res => res.json()); } }