From dda9fdafe94e6ae5969625a07b5d61399621435e Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Tue, 10 May 2022 16:56:38 +0700 Subject: [PATCH] add timeout to fetchers + update cNETA (#1) * add timeout to fetcher functions Signed-off-by: Nguyen Le Vu Long * update cNETA Signed-off-by: Nguyen Le Vu Long * fix CI Signed-off-by: Nguyen Le Vu Long --- .github/workflows/ci.yml | 2 +- jest.config.ts | 2 +- jest.setup.js | 1 + src/index.ts | 9 ++++----- src/tokens/aada.ts | 13 +++++++++---- src/tokens/agix.ts | 11 +++++++---- src/tokens/clap.ts | 13 +++++++++---- src/tokens/cneta.ts | 24 ++++++++++++++++++++++++ src/tokens/lq.ts | 13 +++++++++---- src/tokens/meld.ts | 11 +++++++---- src/tokens/milk.ts | 13 +++++++++---- src/tokens/min.ts | 14 ++++++++++---- src/tokens/ntx.ts | 11 +++++++---- src/tokens/sundae.ts | 13 +++++++++---- src/utils.ts | 35 +++++++++++++++++++++++++++++++---- 15 files changed, 138 insertions(+), 47 deletions(-) create mode 100644 jest.setup.js create mode 100644 src/tokens/cneta.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c0bcc8f5..6a6cddfb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,6 @@ jobs: - name: Run test run: npm test env: - BLOCKFROST_PROJECT_ID_MAINNET: ${{ secrets.BLOCKFROST_PROJECT_ID_MAINNET }} + BLOCKFROST_PROJECT_ID: ${{ secrets.BLOCKFROST_PROJECT_ID }} - name: Run lint run: npm run lint diff --git a/jest.config.ts b/jest.config.ts index d36a763a..046229a7 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -135,7 +135,7 @@ export default { // setupFiles: [], // A list of paths to modules that run some code to configure or set up the testing framework before each test - // setupFilesAfterEnv: [], + setupFilesAfterEnv: ["./jest.setup.js"], // The number of seconds after which a test is considered as slow and reported as such in the results. // slowTestThreshold: 5, diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 00000000..1399c911 --- /dev/null +++ b/jest.setup.js @@ -0,0 +1 @@ +jest.setTimeout(30000); diff --git a/src/index.ts b/src/index.ts index 3c580ac8..5f119683 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,14 @@ import aadaFetcher from "./tokens/aada"; import agixFetcher from "./tokens/agix"; import clapFetcher from "./tokens/clap"; +import cnetaFetcher from "./tokens/cneta"; import liqwidFetcher from "./tokens/lq"; import meldFetcher from "./tokens/meld"; import milkFetcher from "./tokens/milk"; import minFetcher from "./tokens/min"; import ntxFetcher from "./tokens/ntx"; import sundaeFetcher from "./tokens/sundae"; - -export type SupplyFetcher = () => Promise<{ - circulating: string; - total: string; -}>; +import { SupplyFetcher } from "./utils"; export const supplyFetchers: Record = { "29d222ce763455e3d7a09a665ce554f00ac89d2e99a1a83d267170c64d494e": minFetcher, @@ -27,4 +24,6 @@ export const supplyFetchers: Record = { "8fef2d34078659493ce161a6c7fba4b56afefa8535296a5743f6958741414441": aadaFetcher, db30c7905f598ed0154de14f970de0f61f0cb3943ed82c891968480a434c4150: clapFetcher, + b34b3ea80060ace9427bda98690a73d33840e27aaa8d6edb7f0c757a634e455441: + cnetaFetcher, }; diff --git a/src/tokens/aada.ts b/src/tokens/aada.ts index f3e2627a..a77cad90 100644 --- a/src/tokens/aada.ts +++ b/src/tokens/aada.ts @@ -1,11 +1,16 @@ -import { SupplyFetcher } from ".."; -import { getAmountInAddresses } from "../utils"; +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; const AADA = "8fef2d34078659493ce161a6c7fba4b56afefa8535296a5743f6958741414441"; -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); const total = 29_500_000; - const treasuryRaw = await getAmountInAddresses(AADA, [ + const treasuryRaw = await getAmountInAddresses(blockFrost, AADA, [ "addr1qysm5h2w24tlffmpscazkymz746tp72hyq7tgdx6j3ca4p60mv7emuks64z8l55krwm68n59574j7cdfk7ja2s684fvqm72l6y", "addr1qxka8z8c4qglsjuzpl5llfkrgwklhh7mg245jfpgauwqgvqkwvwuue8g9j06a4jpprgu59xf02d8x2dperyd9dglrxdsl04fwp", ]); diff --git a/src/tokens/agix.ts b/src/tokens/agix.ts index 9b68a408..1e36e217 100644 --- a/src/tokens/agix.ts +++ b/src/tokens/agix.ts @@ -1,8 +1,11 @@ -import axios from "axios"; +import { + defaultFetcherOptions, + getAxiosInstance, + SupplyFetcher, +} from "../utils"; -import { SupplyFetcher } from ".."; - -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const axios = getAxiosInstance(options); const circulating = await axios( "https://tokensupply.singularitynet.io/tokensupply?tokensymbol=agix&q=circulatingsupply" ).then((res) => res.data); diff --git a/src/tokens/clap.ts b/src/tokens/clap.ts index b146cc0e..707573a9 100644 --- a/src/tokens/clap.ts +++ b/src/tokens/clap.ts @@ -1,11 +1,16 @@ -import { SupplyFetcher } from ".."; -import { getAmountInAddresses } from "../utils"; +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; const CLAP = "db30c7905f598ed0154de14f970de0f61f0cb3943ed82c891968480a434c4150"; -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); const total = 1e9; // 1 billion - const treasuryRaw = await getAmountInAddresses(CLAP, [ + const treasuryRaw = await getAmountInAddresses(blockFrost, CLAP, [ "addr1qxkcc74fpfvdenelc7m4zxtezrgvnhmd7z78jlkgr4gy9x9cjxt7rq9rfwqqh3wv24adfv2hex0ttwu3zh7e276xmcesgw34jc", ]); const treasury = Number(treasuryRaw) / 1e3; diff --git a/src/tokens/cneta.ts b/src/tokens/cneta.ts new file mode 100644 index 00000000..bdb79340 --- /dev/null +++ b/src/tokens/cneta.ts @@ -0,0 +1,24 @@ +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; + +const CNETA = + "b34b3ea80060ace9427bda98690a73d33840e27aaa8d6edb7f0c757a634e455441"; + +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); + const total = 1_000_000_000n; + const treasury = await getAmountInAddresses(blockFrost, CNETA, [ + "addr1qx08dp9krl0tvv88e5mhqphevtcuujefmx8k0p3nu08yjakgzmuckmpqk4ge4g66qs6shuu2r7uu7ygq3vkjxxl6rpsqt0dwyt", + "addr1q9wjw9vl3g45f63kvsr487m45xq25f88p2jjg37gelcllw5r65qxyr79pkpgm225d3z3n53fwnqcfhdmv9xcemgns98q4x08su", + ]); + return { + circulating: (total - treasury).toString(), + total: total.toString(), + }; +}; + +export default fetcher; diff --git a/src/tokens/lq.ts b/src/tokens/lq.ts index 65b706f1..842e5865 100644 --- a/src/tokens/lq.ts +++ b/src/tokens/lq.ts @@ -1,11 +1,16 @@ -import { SupplyFetcher } from ".."; -import { getAmountInAddresses } from "../utils"; +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; const LQ = "da8c30857834c6ae7203935b89278c532b3995245295456f993e1d244c51"; -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); const total = 21_000_000; - const treasuryRaw = await getAmountInAddresses(LQ, [ + const treasuryRaw = await getAmountInAddresses(blockFrost, LQ, [ "addr1q9qpk9suska6ugxcr76ek2l7u7d4twmugsppl3e7gkzsgu5pvftpwnj9wytn4h9e3kgdw60pwtffa6jqsll0jr5gjxnqrnmd24", ]); const treasury = Number(treasuryRaw) / 1e6; diff --git a/src/tokens/meld.ts b/src/tokens/meld.ts index 5e2c111e..461d88ac 100644 --- a/src/tokens/meld.ts +++ b/src/tokens/meld.ts @@ -1,8 +1,11 @@ -import axios from "axios"; +import { + defaultFetcherOptions, + getAxiosInstance, + SupplyFetcher, +} from "../utils"; -import { SupplyFetcher } from ".."; - -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const axios = getAxiosInstance(options); const circulating = await axios( "https://app-backend.meld.com/api/market/meld/supply" ).then((res) => res.data); diff --git a/src/tokens/milk.ts b/src/tokens/milk.ts index ee3f3df9..f44197cf 100644 --- a/src/tokens/milk.ts +++ b/src/tokens/milk.ts @@ -1,11 +1,16 @@ -import { SupplyFetcher } from ".."; -import { getAmountInAddresses } from "../utils"; +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; const MILK = "8a1cfae21368b8bebbbed9800fec304e95cce39a2a57dc35e2e3ebaa4d494c4b"; -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); const total = 10_000_000n; - const treasury = await getAmountInAddresses(MILK, [ + const treasury = await getAmountInAddresses(blockFrost, MILK, [ "addr1v8c3mztrzpjqxzrcl8rvxln8xyvanz6pufuaju7rwkglnychv3cg3", ]); return { diff --git a/src/tokens/min.ts b/src/tokens/min.ts index 82699b4c..c35f1f5c 100644 --- a/src/tokens/min.ts +++ b/src/tokens/min.ts @@ -1,5 +1,9 @@ -import { SupplyFetcher } from ".."; -import { getAmountInAddresses } from "../utils"; +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; const MIN = "29d222ce763455e3d7a09a665ce554f00ac89d2e99a1a83d267170c64d494e"; const TREASURY_ADDRESSES = [ @@ -8,10 +12,12 @@ const TREASURY_ADDRESSES = [ "addr1qxkmr0m22xeqludcg5rjdmecjxasu9fat0680qehtcsnftaadgykewa9ufvegeuca9yyq03d9v7ea2y2zthgu7hfgjtsddp6gr", // yield farming ]; -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); const total = 5e9; // 5 billion const treasury = - Number(await getAmountInAddresses(MIN, TREASURY_ADDRESSES)) / 1e6; + Number(await getAmountInAddresses(blockFrost, MIN, TREASURY_ADDRESSES)) / + 1e6; return { circulating: (total - treasury).toString(), total: total.toString(), diff --git a/src/tokens/ntx.ts b/src/tokens/ntx.ts index 25db15fb..d9fe637a 100644 --- a/src/tokens/ntx.ts +++ b/src/tokens/ntx.ts @@ -1,8 +1,11 @@ -import axios from "axios"; +import { + defaultFetcherOptions, + getAxiosInstance, + SupplyFetcher, +} from "../utils"; -import { SupplyFetcher } from ".."; - -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const axios = getAxiosInstance(options); const circulating = await axios( "https://tokensupply.singularitynet.io/tokensupply?tokensymbol=ntx&q=circulatingsupply" ).then((res) => res.data); diff --git a/src/tokens/sundae.ts b/src/tokens/sundae.ts index 0fc7b56e..0fe0b392 100644 --- a/src/tokens/sundae.ts +++ b/src/tokens/sundae.ts @@ -1,12 +1,17 @@ -import { SupplyFetcher } from ".."; -import { getAmountInAddresses } from "../utils"; +import { + defaultFetcherOptions, + getAmountInAddresses, + getBlockFrostInstance, + SupplyFetcher, +} from "../utils"; const SUNDAE = "9a9693a9a37912a5097918f97918d15240c92ab729a0b7c4aa144d7753554e444145"; -const fetcher: SupplyFetcher = async () => { +const fetcher: SupplyFetcher = async (options = defaultFetcherOptions) => { + const blockFrost = getBlockFrostInstance(options); const total = 2e9; // 2 billion - const treasuryRaw = await getAmountInAddresses(SUNDAE, [ + const treasuryRaw = await getAmountInAddresses(blockFrost, SUNDAE, [ "addr1w9pgj46r4wh80ujlrdf5h6sxuuvfac2wurfnsn4xgqw584sfa966l", "addr1w9742z4fewans7ry6cjp95pc4ecv7y54cx298lp5qfw7s9gv8ukrj", ]); diff --git a/src/utils.ts b/src/utils.ts index 7579e5ee..c3f9c581 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,38 @@ import { BlockFrostAPI } from "@blockfrost/blockfrost-js"; +import axios, { AxiosInstance } from "axios"; -export const blockFrost = new BlockFrostAPI({ - projectId: process.env["BLOCKFROST_PROJECT_ID"] ?? "", - isTestnet: false, -}); +export type FetcherOptions = { + /** + * Fetch timeout in milliseconds. Default to 20s + */ + timeout?: number; +}; + +export const defaultFetcherOptions: FetcherOptions = { + timeout: 20_000, +}; + +export type SupplyFetcher = (options?: FetcherOptions) => Promise<{ + circulating: string; + total: string; +}>; + +export function getBlockFrostInstance(options: FetcherOptions): BlockFrostAPI { + return new BlockFrostAPI({ + projectId: process.env["BLOCKFROST_PROJECT_ID"] ?? "", + isTestnet: false, + requestTimeout: options.timeout, + }); +} + +export function getAxiosInstance(options: FetcherOptions): AxiosInstance { + return axios.create({ + timeout: options.timeout, + }); +} export async function getAmountInAddresses( + blockFrost: BlockFrostAPI, token: string, addresses: string[] ): Promise {