From d727eff6b69216d2adf0119bc371dc5c1c78a10c Mon Sep 17 00:00:00 2001 From: Valery Gantchev Date: Tue, 26 Sep 2023 15:43:15 +0200 Subject: [PATCH 1/4] Add hydradx-ui/v1/trade/price_chart --- app/routes/hydradx-ui/v1/trade/priceChart.mjs | 61 +++++++++++++++++++ package-lock.json | 6 ++ package.json | 1 + queries/hydradx-ui/v1/trade/priceChart.sql | 35 +++++++++++ variables.mjs | 4 ++ 5 files changed, 107 insertions(+) create mode 100644 app/routes/hydradx-ui/v1/trade/priceChart.mjs create mode 100644 queries/hydradx-ui/v1/trade/priceChart.sql diff --git a/app/routes/hydradx-ui/v1/trade/priceChart.mjs b/app/routes/hydradx-ui/v1/trade/priceChart.mjs new file mode 100644 index 0000000..2b6bcc9 --- /dev/null +++ b/app/routes/hydradx-ui/v1/trade/priceChart.mjs @@ -0,0 +1,61 @@ +import yesql from "yesql"; +import path from "path"; +import dayjs from "dayjs"; +import { dirname } from "../../../../../variables.mjs"; +import { CACHE_SETTINGS } from "../../../../../variables.mjs"; +import { cachedFetch } from "../../../../../helpers/cache_helpers.mjs"; + +const sqlQueries = yesql(path.join(dirname(), "queries/hydradx-ui/v1/trade"), { + type: "pg", +}); + +export const VALID_TIMEFRAMES = ["hourly", "daily"]; + +export default async (fastify, opts) => { + fastify.route({ + url: "/price_chart/:assetIn-:assetOut", + method: ["GET"], + schema: { + description: "Price chart (Omnipool) for the trade page.", + tags: ["hydradx-ui/v1"], + params: { + type: "object", + properties: { + assetIn: { + type: "string", + description: "Symbol for assetIn", + }, + assetOut: { + type: "string", + description: "Symbol for assetOut", + }, + }, + required: ["assetIn", "assetOut"], + }, + }, + handler: async (request, reply) => { + const assetIn = request.params.assetIn; + const assetOut = request.params.assetOut; + + const endOfDay = dayjs().endOf("day").format("YYYY-MM-DDTHH:mm:ss"); + + const sqlQuery = sqlQueries.priceChart({ assetIn, assetOut, endOfDay }); + + console.log("DGDGDGD"); + console.log(endOfDay); + console.log(sqlQuery); + + let cacheSetting = { ...CACHE_SETTINGS["hydradxUiV1TradePriceChart"] }; + cacheSetting.key = cacheSetting.key + "_" + assetIn + "_" + assetOut; + + const result = await cachedFetch( + fastify.pg, + fastify.redis, + cacheSetting, + sqlQuery + ); + + reply.send(JSON.parse(result)); + }, + }); +}; diff --git a/package-lock.json b/package-lock.json index 56a42c4..5659583 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@fastify/swagger-ui": "^1.8.1", "@polkadot/api": "^10.4.1", "close-with-grace": "^1.2.0", + "dayjs": "^1.11.7", "dotenv": "^16.0.3", "fastify": "^4.0.0", "fastify-cli": "^5.7.1", @@ -1735,6 +1736,11 @@ "node": "*" } }, + "node_modules/dayjs": { + "version": "1.11.10", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz", + "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==" + }, "node_modules/debug": { "version": "4.3.4", "license": "MIT", diff --git a/package.json b/package.json index 1889b1d..95f6548 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@fastify/swagger-ui": "^1.8.1", "@polkadot/api": "^10.4.1", "close-with-grace": "^1.2.0", + "dayjs": "^1.11.7", "dotenv": "^16.0.3", "fastify": "^4.0.0", "fastify-cli": "^5.7.1", diff --git a/queries/hydradx-ui/v1/trade/priceChart.sql b/queries/hydradx-ui/v1/trade/priceChart.sql new file mode 100644 index 0000000..6c04214 --- /dev/null +++ b/queries/hydradx-ui/v1/trade/priceChart.sql @@ -0,0 +1,35 @@ +-- priceChart + +WITH nor_trades AS ( + SELECT + timestamp, + block.height AS block, + args->>'who' AS who, + name AS operation, + token_metadata_in.symbol AS asset_in, + token_metadata_out.symbol AS asset_out, + (args->>'amountIn')::numeric / (10 ^ token_metadata_in.decimals) AS amount_in, + (args->>'amountOut')::numeric / (10 ^ token_metadata_out.decimals) AS amount_out + FROM event + INNER JOIN block ON block_id = block.id + INNER JOIN token_metadata AS token_metadata_in ON (args->>'assetIn')::integer = token_metadata_in.id + INNER JOIN token_metadata AS token_metadata_out ON (args->>'assetOut')::integer = token_metadata_out.id + WHERE name IN ('Omnipool.BuyExecuted', 'Omnipool.SellExecuted') + AND timestamp BETWEEN '2023-01-06T13:00:00.000Z' AND :endOfDay +), +pair_price AS ( + SELECT + timestamp, + CASE + WHEN asset_in = :assetIn AND asset_out = :assetOut THEN amount_in / amount_out + WHEN asset_in = :assetOut AND asset_out = :assetIn THEN amount_out / amount_in + END AS price + FROM nor_trades +) +SELECT + floor(extract(epoch from "timestamp")/3600)*3600 AS "time", + max(price) AS "price" +FROM pair_price +WHERE price IS NOT NULL +GROUP BY 1 +ORDER BY 1; diff --git a/variables.mjs b/variables.mjs index 2c05159..188ace4 100644 --- a/variables.mjs +++ b/variables.mjs @@ -66,6 +66,10 @@ export const CACHE_SETTINGS = { key: "hydradx-ui_v1_stats_current_volume", expire_after: 10 * 60, }, + hydradxUiV1TradePriceChart: { + key: "hydradx-ui_v1_trade_price_chart", + expire_after: 5, + }, defillamaV1Tvl: { key: "defillama_v1_tvl", expire_after: 10 * 60, From 3250ceef458f3102a8e2b226242a1d9339db4951 Mon Sep 17 00:00:00 2001 From: Valery Gantchev Date: Tue, 26 Sep 2023 17:38:22 +0200 Subject: [PATCH 2/4] cleanup --- app/routes/hydradx-ui/v1/stats/lrna.mjs | 1 - app/routes/hydradx-ui/v1/stats/tvl.mjs | 1 - 2 files changed, 2 deletions(-) diff --git a/app/routes/hydradx-ui/v1/stats/lrna.mjs b/app/routes/hydradx-ui/v1/stats/lrna.mjs index 652a54c..0dcfab1 100644 --- a/app/routes/hydradx-ui/v1/stats/lrna.mjs +++ b/app/routes/hydradx-ui/v1/stats/lrna.mjs @@ -3,7 +3,6 @@ import path from "path"; import { dirname } from "../../../../../variables.mjs"; import { CACHE_SETTINGS } from "../../../../../variables.mjs"; import { cachedFetch } from "../../../../../helpers/cache_helpers.mjs"; -import { getAssets } from "../../../../../helpers/asset_helpers.mjs"; const sqlQueries = yesql(path.join(dirname(), "queries/hydradx-ui/v1/stats"), { type: "pg", diff --git a/app/routes/hydradx-ui/v1/stats/tvl.mjs b/app/routes/hydradx-ui/v1/stats/tvl.mjs index 01de825..cf37e89 100644 --- a/app/routes/hydradx-ui/v1/stats/tvl.mjs +++ b/app/routes/hydradx-ui/v1/stats/tvl.mjs @@ -3,7 +3,6 @@ import path from "path"; import { dirname } from "../../../../../variables.mjs"; import { CACHE_SETTINGS } from "../../../../../variables.mjs"; import { cachedFetch } from "../../../../../helpers/cache_helpers.mjs"; -import { getAssets } from "../../../../../helpers/asset_helpers.mjs"; const sqlQueries = yesql(path.join(dirname(), "queries/hydradx-ui/v1/stats"), { type: "pg", From f126e9dced2117ee44864278b2f05bdb18da5cbf Mon Sep 17 00:00:00 2001 From: Valery Gantchev Date: Tue, 26 Sep 2023 17:38:32 +0200 Subject: [PATCH 3/4] update chart --- queries/hydradx-ui/v1/trade/priceChart.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/queries/hydradx-ui/v1/trade/priceChart.sql b/queries/hydradx-ui/v1/trade/priceChart.sql index 6c04214..a8af423 100644 --- a/queries/hydradx-ui/v1/trade/priceChart.sql +++ b/queries/hydradx-ui/v1/trade/priceChart.sql @@ -21,8 +21,8 @@ pair_price AS ( SELECT timestamp, CASE - WHEN asset_in = :assetIn AND asset_out = :assetOut THEN amount_in / amount_out - WHEN asset_in = :assetOut AND asset_out = :assetIn THEN amount_out / amount_in + WHEN asset_in = :assetIn AND asset_out = :assetOut AND amount_in != 0 AND amount_out != 0 THEN amount_in / amount_out + WHEN asset_in = :assetOut AND asset_out = :assetIn AND amount_in != 0 AND amount_out != 0 THEN amount_out / amount_in END AS price FROM nor_trades ) From 0b7f5c9e495e0b6e3c73049b8effca2e0d76aae1 Mon Sep 17 00:00:00 2001 From: Valery Gantchev Date: Tue, 26 Sep 2023 17:40:16 +0200 Subject: [PATCH 4/4] cleanup --- app/routes/hydradx-ui/v1/trade/priceChart.mjs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/routes/hydradx-ui/v1/trade/priceChart.mjs b/app/routes/hydradx-ui/v1/trade/priceChart.mjs index 2b6bcc9..6ba1bae 100644 --- a/app/routes/hydradx-ui/v1/trade/priceChart.mjs +++ b/app/routes/hydradx-ui/v1/trade/priceChart.mjs @@ -36,15 +36,10 @@ export default async (fastify, opts) => { handler: async (request, reply) => { const assetIn = request.params.assetIn; const assetOut = request.params.assetOut; - const endOfDay = dayjs().endOf("day").format("YYYY-MM-DDTHH:mm:ss"); const sqlQuery = sqlQueries.priceChart({ assetIn, assetOut, endOfDay }); - console.log("DGDGDGD"); - console.log(endOfDay); - console.log(sqlQuery); - let cacheSetting = { ...CACHE_SETTINGS["hydradxUiV1TradePriceChart"] }; cacheSetting.key = cacheSetting.key + "_" + assetIn + "_" + assetOut;