Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/routes/hydradx-ui/v1/stats/lrna.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion app/routes/hydradx-ui/v1/stats/tvl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 56 additions & 0 deletions app/routes/hydradx-ui/v1/trade/priceChart.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 });

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));
},
});
};
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions queries/hydradx-ui/v1/trade/priceChart.sql
Original file line number Diff line number Diff line change
@@ -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 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
)
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;
4 changes: 4 additions & 0 deletions variables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down