Skip to content

Commit

Permalink
Merge pull request #3 from galacticcouncil/add-stats-queries
Browse files Browse the repository at this point in the history
New endpoints for hydradx-ui/stats and defillama
  • Loading branch information
vgantchev authored Aug 13, 2023
2 parents 85e3dd7 + 61cfdd5 commit 9d9a2c0
Show file tree
Hide file tree
Showing 25 changed files with 624 additions and 230 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: run.googleapis.com/v1
kind: Job
metadata:
name: cache-rpc-block-height-job
name: cache-hydradx-ui-stats-tvl-job
labels:
cloud.googleapis.com/location: europe-west3

Expand All @@ -20,7 +20,7 @@ spec:
- image: europe-west3-docker.pkg.dev/rich-principle-383410/hydradx-api/hydradx-api-jobs:latest
env:
- name: JOB_NAME
value: cache-rpc-block-height-job
value: cache-hydradx-ui-stats-tvl-job
- name: GOOGLE_CLOUD_RUN_JOB
value: "true"
resources:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ jobs:
- name: Set up gcloud beta
run: gcloud components install beta

- name: deploy cache_rpc_block_height_job
run: gcloud beta run jobs replace .gcp/jobs/cache_rpc_block_height_job.yml

- name: deploy cache_coingecko_tickers_job
run: gcloud beta run jobs replace .gcp/jobs/cache_coingecko_tickers_job.yml

- name: deploy cache_hydradx-ui_stats_tvl_job
run: gcloud beta run jobs replace .gcp/jobs/cache_hydradx-ui_stats_tvl_job.yml

- name: deploy api-app
run: gcloud beta run services replace .gcp/hydradx_api_app.yml
25 changes: 24 additions & 1 deletion app.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import path from "path";
import { dirname } from "./variables.mjs";
import { dirname, redisUri } from "./variables.mjs";
import AutoLoad from "@fastify/autoload";
import cors from "@fastify/cors";
import PG from "@fastify/postgres";
import Redis from "@fastify/redis";
import Swagger from "@fastify/swagger";
import SwaggerUi from "@fastify/swagger-ui";

import {
sqlPort,
sqlHost,
sqlUser,
sqlPass,
sqlDatabase,
} from "./variables.mjs";

// Pass --options via CLI arguments in command to enable these options.
export var options = {};

Expand Down Expand Up @@ -37,6 +47,19 @@ export default async (fastify, opts) => {
options: Object.assign({}, opts),
});

fastify.register(PG, {
host: sqlHost(),
port: sqlPort(),
user: sqlUser(),
password: sqlPass(),
database: sqlDatabase(),
max: 500,
});

fastify.register(Redis, {
url: redisUri(),
});

fastify.register(cors, {
allowedHeaders: "*",
});
Expand Down
8 changes: 5 additions & 3 deletions app/routes/coingecko/v1/pairs.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import yesql from "yesql";
import path from "path";
import { dirname, CACHE_SETTINGS } from "../../../../variables.mjs";
import { readSqlCacheOrUpdate } from "../../../../helpers/cache_helpers.mjs";
import { cachedFetch } from "../../../../helpers/cache_helpers.mjs";

const sqlQueries = yesql(path.join(dirname(), "queries/coingecko/v1/"), {
type: "pg",
Expand Down Expand Up @@ -33,12 +33,14 @@ export default async (fastify, opts) => {
handler: async (request, reply) => {
let cacheSetting = CACHE_SETTINGS["coingeckoV1Pairs"];

const result = await readSqlCacheOrUpdate(
const result = await cachedFetch(
fastify.pg,
fastify.redis,
cacheSetting,
sqlQueries.getPairs()
);

reply.send(JSON.parse(result));
reply.send(result);
},
});
};
6 changes: 4 additions & 2 deletions app/routes/coingecko/v1/tickers.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import yesql from "yesql";
import path from "path";
import { dirname, CACHE_SETTINGS } from "../../../../variables.mjs";
import { readSqlCacheOrUpdate } from "../../../../helpers/cache_helpers.mjs";
import { cachedFetch } from "../../../../helpers/cache_helpers.mjs";

const sqlQueries = yesql(path.join(dirname(), "queries/coingecko/v1/"), {
type: "pg",
Expand Down Expand Up @@ -40,7 +40,9 @@ export default async (fastify, opts) => {
handler: async (request, reply) => {
let cacheSetting = CACHE_SETTINGS["coingeckoV1Tickers"];

const result = await readSqlCacheOrUpdate(
const result = await cachedFetch(
fastify.pg,
fastify.redis,
cacheSetting,
sqlQueries.getTickers()
);
Expand Down
58 changes: 58 additions & 0 deletions app/routes/defillama/v1/tvl.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import yesql from "yesql";
import path from "path";
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/defillama/v1/"), {
type: "pg",
});

export default async (fastify, opts) => {
fastify.route({
url: "/tvl/:asset?",
method: ["GET"],
schema: {
description: "Current Omnipool TVL for DefiLlama.",
tags: ["defillama/v1"],
params: {
type: "object",
properties: {
asset: {
type: "string",
description: "Asset (symbol). Leave empty for all assets.",
},
},
},
response: {
200: {
description: "Success Response",
type: "array",
items: {
type: "object",
properties: {
tvl_usd: { type: "number" },
},
},
},
},
},
handler: async (request, reply) => {
const asset = request.params.asset ? request.params.asset : null;

const sqlQuery = sqlQueries.defillamaTvl({ asset });

let cacheSetting = { ...CACHE_SETTINGS["defillamaV1Tvl"] };
cacheSetting.key = cacheSetting.key + "_" + asset;

const result = await cachedFetch(
fastify.pg,
fastify.redis,
cacheSetting,
sqlQuery
);

reply.send(JSON.parse(result));
},
});
};
58 changes: 58 additions & 0 deletions app/routes/defillama/v1/volume.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import yesql from "yesql";
import path from "path";
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/defillama/v1/"), {
type: "pg",
});

export default async (fastify, opts) => {
fastify.route({
url: "/volume/:asset?",
method: ["GET"],
schema: {
description: "Current 24h rolling trading volume for DefiLlama.",
tags: ["defillama/v1"],
params: {
type: "object",
properties: {
asset: {
type: "string",
description: "Asset (symbol). Leave empty for all assets.",
},
},
},
response: {
200: {
description: "Success Response",
type: "array",
items: {
type: "object",
properties: {
volume_usd: { type: "number" },
},
},
},
},
},
handler: async (request, reply) => {
const asset = request.params.asset ? request.params.asset : null;

const sqlQuery = sqlQueries.defillamaVolume({ asset });

let cacheSetting = { ...CACHE_SETTINGS["defillamaV1Volume"] };
cacheSetting.key = cacheSetting.key + "_" + asset;

const result = await cachedFetch(
fastify.pg,
fastify.redis,
cacheSetting,
sqlQuery
);

reply.send(JSON.parse(result));
},
});
};
77 changes: 0 additions & 77 deletions app/routes/hydradx-ui/v1/stats.mjs

This file was deleted.

60 changes: 60 additions & 0 deletions app/routes/hydradx-ui/v1/stats/tvl.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import yesql from "yesql";
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",
});

export default async (fastify, opts) => {
fastify.route({
url: "/tvl/:asset?",
method: ["GET"],
schema: {
description: "Omnipool TVL for the HydraDX stats page.",
tags: ["hydradx-ui/v1"],
params: {
type: "object",
properties: {
asset: {
type: "string",
description: "Asset (symbol). Leave empty for all assets.",
},
},
},
response: {
200: {
description: "Success Response",
type: "array",
items: {
type: "object",
properties: {
interval: { type: "string" },
tvl_usd: { type: "number" },
},
},
},
},
},
handler: async (request, reply) => {
const asset = request.params.asset ? request.params.asset : null;

const sqlQuery = sqlQueries.statsTvl({ asset });

let cacheSetting = { ...CACHE_SETTINGS["hydradxUiV1StatsTvl"] };
cacheSetting.key = cacheSetting.key + "_" + asset;

const result = await cachedFetch(
fastify.pg,
fastify.redis,
cacheSetting,
sqlQuery
);

reply.send(JSON.parse(result));
},
});
};
Loading

0 comments on commit 9d9a2c0

Please sign in to comment.