Skip to content

Commit

Permalink
add timeout to fetchers + update cNETA (#1)
Browse files Browse the repository at this point in the history
* add timeout to fetcher functions

Signed-off-by: Nguyen Le Vu Long <[email protected]>

* update cNETA

Signed-off-by: Nguyen Le Vu Long <[email protected]>

* fix CI

Signed-off-by: Nguyen Le Vu Long <[email protected]>
  • Loading branch information
longngn authored May 10, 2022
1 parent 3288853 commit dda9fda
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(30000);
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, SupplyFetcher> = {
"29d222ce763455e3d7a09a665ce554f00ac89d2e99a1a83d267170c64d494e": minFetcher,
Expand All @@ -27,4 +24,6 @@ export const supplyFetchers: Record<string, SupplyFetcher> = {
"8fef2d34078659493ce161a6c7fba4b56afefa8535296a5743f6958741414441":
aadaFetcher,
db30c7905f598ed0154de14f970de0f61f0cb3943ed82c891968480a434c4150: clapFetcher,
b34b3ea80060ace9427bda98690a73d33840e27aaa8d6edb7f0c757a634e455441:
cnetaFetcher,
};
13 changes: 9 additions & 4 deletions src/tokens/aada.ts
Original file line number Diff line number Diff line change
@@ -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",
]);
Expand Down
11 changes: 7 additions & 4 deletions src/tokens/agix.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
13 changes: 9 additions & 4 deletions src/tokens/clap.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
24 changes: 24 additions & 0 deletions src/tokens/cneta.ts
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 9 additions & 4 deletions src/tokens/lq.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/tokens/meld.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
13 changes: 9 additions & 4 deletions src/tokens/milk.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions src/tokens/min.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SupplyFetcher } from "..";
import { getAmountInAddresses } from "../utils";
import {
defaultFetcherOptions,
getAmountInAddresses,
getBlockFrostInstance,
SupplyFetcher,
} from "../utils";

const MIN = "29d222ce763455e3d7a09a665ce554f00ac89d2e99a1a83d267170c64d494e";
const TREASURY_ADDRESSES = [
Expand All @@ -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(),
Expand Down
11 changes: 7 additions & 4 deletions src/tokens/ntx.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
13 changes: 9 additions & 4 deletions src/tokens/sundae.ts
Original file line number Diff line number Diff line change
@@ -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",
]);
Expand Down
35 changes: 31 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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<bigint> {
Expand Down

0 comments on commit dda9fda

Please sign in to comment.