From de4942ecb85fb813c69934446a88cfcfdf3a559a Mon Sep 17 00:00:00 2001 From: h2physics Date: Thu, 22 Jun 2023 16:12:20 +0700 Subject: [PATCH 1/4] add main function to fetch all market cap data --- internal/README.md | 1 + internal/main.ts | 16 ++++++++++++++++ package.json | 3 ++- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 internal/README.md create mode 100644 internal/main.ts diff --git a/internal/README.md b/internal/README.md new file mode 100644 index 00000000..af6c0b8e --- /dev/null +++ b/internal/README.md @@ -0,0 +1 @@ +Internal files used by Minswap team \ No newline at end of file diff --git a/internal/main.ts b/internal/main.ts new file mode 100644 index 00000000..8e7984a5 --- /dev/null +++ b/internal/main.ts @@ -0,0 +1,16 @@ +import { SupplyFetcherResponse, supplyFetchers } from "../src"; + +async function main(): Promise { + const marketCapData: Record = {}; + for (const [key, supplyFetcher] of Object.entries(supplyFetchers)) { + try { + const data = await supplyFetcher(); + marketCapData[key] = data; + } catch (err) { + /* empty */ + } + } + console.info(JSON.stringify(marketCapData)); +} + +void main(); diff --git a/package.json b/package.json index d7986062..6b307d55 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "build": "rimraf build && rollup -c rollup.config.js", "test": "jest", "format": "prettier --write **/*.{ts,tsx} && eslint --fix --ext .ts,.tsx .", - "check-format": "prettier --check **/*.{ts,tsx} && eslint --ext .ts,.tsx ." + "check-format": "prettier --check **/*.{ts,tsx} && eslint --ext .ts,.tsx .", + "run-market-cap": "ts-node internal/main.ts" }, "keywords": [ "minswap", From f9fe94ae0987b0354843400098101dad1e05f39c Mon Sep 17 00:00:00 2001 From: h2physics Date: Thu, 22 Jun 2023 17:38:36 +0700 Subject: [PATCH 2/4] write output to temp file --- internal/main.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/main.ts b/internal/main.ts index 8e7984a5..089f292d 100644 --- a/internal/main.ts +++ b/internal/main.ts @@ -1,3 +1,5 @@ +import fs from "fs"; + import { SupplyFetcherResponse, supplyFetchers } from "../src"; async function main(): Promise { @@ -10,7 +12,7 @@ async function main(): Promise { /* empty */ } } - console.info(JSON.stringify(marketCapData)); + fs.writeFileSync("/tmp/market-cap.json", JSON.stringify(marketCapData)); } void main(); From 7457877d6d963a112fd3a6eefe0bd4c5cf846d8e Mon Sep 17 00:00:00 2001 From: Nguyen Le Vu Long Date: Fri, 23 Jun 2023 16:35:52 +0700 Subject: [PATCH 3/4] use backoff Signed-off-by: Nguyen Le Vu Long --- internal/main.ts | 22 +++++++++++++++++++--- package-lock.json | 15 ++++++++++++++- package.json | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/internal/main.ts b/internal/main.ts index 089f292d..19c54b34 100644 --- a/internal/main.ts +++ b/internal/main.ts @@ -1,3 +1,4 @@ +import { backOff } from "exponential-backoff"; import fs from "fs"; import { SupplyFetcherResponse, supplyFetchers } from "../src"; @@ -6,10 +7,25 @@ async function main(): Promise { const marketCapData: Record = {}; for (const [key, supplyFetcher] of Object.entries(supplyFetchers)) { try { - const data = await supplyFetcher(); - marketCapData[key] = data; + await backOff( + async () => { + const data = await supplyFetcher(); + marketCapData[key] = data; + }, + { + retry(err, attempt): boolean { + console.error( + `fail to run fetcher for ${key}, retry ${attempt}...`, + err + ); + return true; + }, + startingDelay: 500, + numOfAttempts: 3, + } + ); } catch (err) { - /* empty */ + console.error(`fail to run fetcher for ${key}`, err); } } fs.writeFileSync("/tmp/market-cap.json", JSON.stringify(marketCapData)); diff --git a/package-lock.json b/package-lock.json index 33c3cf54..a62ffabc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@minswap/market-cap", - "version": "0.1.3", + "version": "0.1.12", "license": "MIT", "dependencies": { "@blockfrost/blockfrost-js": "^5.2.0", @@ -24,6 +24,7 @@ "eslint": "^8.34.0", "eslint-plugin-simple-import-sort": "^10.0.0", "eslint-plugin-unused-imports": "^2.0.0", + "exponential-backoff": "^3.1.1", "jest": "^29.4.2", "prettier": "^2.8.4", "rollup": "^2.70.2", @@ -5057,6 +5058,12 @@ "node": ">=0.10.0" } }, + "node_modules/exponential-backoff": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/exponential-backoff/-/exponential-backoff-3.1.1.tgz", + "integrity": "sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw==", + "dev": true + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -12746,6 +12753,12 @@ "resolved": "https://registry.npmjs.org/expected-node-version/-/expected-node-version-1.0.2.tgz", "integrity": "sha512-OSaCdgF02srujDqJz1JWGpqk8Rq3uNYHLmtpBHJrZN3BvuMvzijJMqRVxZN1qLJtKVwjXhmOp+lfsRUqx8n54w==" }, + "exponential-backoff": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/exponential-backoff/-/exponential-backoff-3.1.1.tgz", + "integrity": "sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw==", + "dev": true + }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", diff --git a/package.json b/package.json index 6b307d55..94019d36 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "eslint": "^8.34.0", "eslint-plugin-simple-import-sort": "^10.0.0", "eslint-plugin-unused-imports": "^2.0.0", + "exponential-backoff": "^3.1.1", "jest": "^29.4.2", "prettier": "^2.8.4", "rollup": "^2.70.2", From d01f0581e0048b90f456a60ecfd08ed142c61571 Mon Sep 17 00:00:00 2001 From: Nguyen Le Vu Long Date: Fri, 23 Jun 2023 17:45:25 +0700 Subject: [PATCH 4/4] fix format Signed-off-by: Nguyen Le Vu Long --- internal/main.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/main.ts b/internal/main.ts index 19c54b34..fdef986e 100644 --- a/internal/main.ts +++ b/internal/main.ts @@ -14,17 +14,18 @@ async function main(): Promise { }, { retry(err, attempt): boolean { + // eslint-disable-next-line no-console console.error( `fail to run fetcher for ${key}, retry ${attempt}...`, err ); return true; }, - startingDelay: 500, numOfAttempts: 3, } ); } catch (err) { + // eslint-disable-next-line no-console console.error(`fail to run fetcher for ${key}`, err); } }