From 591dedb8b9a25ca1b892c5ced5a81efd8f17a867 Mon Sep 17 00:00:00 2001 From: readme-bot Date: Thu, 9 Nov 2023 05:03:19 +0800 Subject: [PATCH] add pre and post load hook --- src/middlelayers/data.ts | 2 ++ src/middlelayers/datafetch/coins/btc.ts | 5 +++++ src/middlelayers/datafetch/coins/cex/cex.ts | 5 +++++ src/middlelayers/datafetch/coins/doge.ts | 5 ++++- src/middlelayers/datafetch/coins/erc20.ts | 14 ++++++++++---- src/middlelayers/datafetch/coins/others.ts | 5 ++++- src/middlelayers/datafetch/coins/sol.ts | 5 ++++- src/middlelayers/datafetch/types.d.ts | 2 ++ 8 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/middlelayers/data.ts b/src/middlelayers/data.ts index 4386519..7627a8e 100644 --- a/src/middlelayers/data.ts +++ b/src/middlelayers/data.ts @@ -46,8 +46,10 @@ async function loadPortfoliosByConfig(config: CexConfig & TokenConfig): Promise< const anaName = a.getAnalyzeName() console.log("loading portfolio from ", anaName) try { + await a.preLoad() const portfolio = await a.loadPortfolio() console.log("loaded portfolio from ", anaName) + await a.postLoad() return portfolio } catch (e) { console.error("failed to load portfolio from ", anaName, e) diff --git a/src/middlelayers/datafetch/coins/btc.ts b/src/middlelayers/datafetch/coins/btc.ts index 6458e04..2bd4963 100644 --- a/src/middlelayers/datafetch/coins/btc.ts +++ b/src/middlelayers/datafetch/coins/btc.ts @@ -18,6 +18,11 @@ export class BTCAnalyzer implements Analyzer { this.btcQueriers = [new BlockCypher(), new Blockchain()] } + async preLoad(): Promise { + } + async postLoad(): Promise { + } + getAnalyzeName(): string { return "BTC Analyzer" } diff --git a/src/middlelayers/datafetch/coins/cex/cex.ts b/src/middlelayers/datafetch/coins/cex/cex.ts index b83850e..bfe5a4c 100644 --- a/src/middlelayers/datafetch/coins/cex/cex.ts +++ b/src/middlelayers/datafetch/coins/cex/cex.ts @@ -62,6 +62,11 @@ export class CexAnalyzer implements Analyzer { return portfolio } + async preLoad(): Promise { + } + async postLoad(): Promise { + } + async loadPortfolio(): Promise { const coinLists = await bluebird.map(this.exchanges, async ex => { const portfolio = await this.fetchTotalBalance(ex) diff --git a/src/middlelayers/datafetch/coins/doge.ts b/src/middlelayers/datafetch/coins/doge.ts index 272b2f8..499da38 100644 --- a/src/middlelayers/datafetch/coins/doge.ts +++ b/src/middlelayers/datafetch/coins/doge.ts @@ -20,7 +20,10 @@ export class DOGEAnalyzer implements Analyzer { getAnalyzeName(): string { return "DOGE Analyzer" } - + async preLoad(): Promise { + } + async postLoad(): Promise { + } private async query(address: string): Promise { for (const q of this.dogeQueriers) { try { diff --git a/src/middlelayers/datafetch/coins/erc20.ts b/src/middlelayers/datafetch/coins/erc20.ts index 58fe0b4..c56c550 100644 --- a/src/middlelayers/datafetch/coins/erc20.ts +++ b/src/middlelayers/datafetch/coins/erc20.ts @@ -133,9 +133,15 @@ export class ERC20Analyzer implements Analyzer { wallet: address })).value() } - + async preLoad(): Promise { + } + async postLoad(): Promise { + for (const q of this.queries) { + q.clean() + } + } async loadPortfolio(): Promise { - return this.loadPortfolioWith429Retry(5) + return this.loadPortfolioWith429Retry(10) .finally(async () => { if (this.errorResolver.isTried()) { await this.errorResolver.resolved() @@ -157,8 +163,8 @@ export class ERC20Analyzer implements Analyzer { if (!this.errorResolver.isTried()) { await this.errorResolver.tryResolve(getAddressList(this.config.erc20)[0]) } - // sleep 5s - await new Promise(resolve => setTimeout(resolve, 2000)) + // sleep 500ms + await new Promise(resolve => setTimeout(resolve, 500)) // try again return this.loadPortfolioWith429Retry(max - 1) diff --git a/src/middlelayers/datafetch/coins/others.ts b/src/middlelayers/datafetch/coins/others.ts index a03b79d..5e0d0cf 100644 --- a/src/middlelayers/datafetch/coins/others.ts +++ b/src/middlelayers/datafetch/coins/others.ts @@ -12,7 +12,10 @@ export class OthersAnalyzer implements Analyzer { getAnalyzeName(): string { return "Others Analyzer" } - + async preLoad(): Promise { + } + async postLoad(): Promise { + } async loadPortfolio(): Promise { return _(this.config.others).map(c => ({ symbol: c.symbol, diff --git a/src/middlelayers/datafetch/coins/sol.ts b/src/middlelayers/datafetch/coins/sol.ts index 367e59b..d1e444c 100644 --- a/src/middlelayers/datafetch/coins/sol.ts +++ b/src/middlelayers/datafetch/coins/sol.ts @@ -17,7 +17,10 @@ export class SOLAnalyzer implements Analyzer { getAnalyzeName(): string { return "SOL Analyzer" } - + async preLoad(): Promise { + } + async postLoad(): Promise { + } private async query(address: string): Promise { const resp = await sendHttpRequest<{ result: { value: string } }>("POST", this.queryUrl, 5000, {}, { diff --git a/src/middlelayers/datafetch/types.d.ts b/src/middlelayers/datafetch/types.d.ts index ea61bd6..0584ee1 100644 --- a/src/middlelayers/datafetch/types.d.ts +++ b/src/middlelayers/datafetch/types.d.ts @@ -7,7 +7,9 @@ export type WalletCoin = Coin & { wallet: string } export interface Analyzer { getAnalyzeName(): string + preLoad(): Promise loadPortfolio(): Promise + postLoad(): Promise } export type GlobalConfig = CexConfig & TokenConfig & {