Skip to content

Commit

Permalink
add pre and post load hook
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Nov 8, 2023
1 parent 2d2b24d commit 591dedb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/middlelayers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/middlelayers/datafetch/coins/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class BTCAnalyzer implements Analyzer {
this.btcQueriers = [new BlockCypher(), new Blockchain()]
}

async preLoad(): Promise<void> {
}
async postLoad(): Promise<void> {
}

getAnalyzeName(): string {
return "BTC Analyzer"
}
Expand Down
5 changes: 5 additions & 0 deletions src/middlelayers/datafetch/coins/cex/cex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export class CexAnalyzer implements Analyzer {
return portfolio
}

async preLoad(): Promise<void> {
}
async postLoad(): Promise<void> {
}

async loadPortfolio(): Promise<WalletCoin[]> {
const coinLists = await bluebird.map(this.exchanges, async ex => {
const portfolio = await this.fetchTotalBalance(ex)
Expand Down
5 changes: 4 additions & 1 deletion src/middlelayers/datafetch/coins/doge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export class DOGEAnalyzer implements Analyzer {
getAnalyzeName(): string {
return "DOGE Analyzer"
}

async preLoad(): Promise<void> {
}
async postLoad(): Promise<void> {
}
private async query(address: string): Promise<number> {
for (const q of this.dogeQueriers) {
try {
Expand Down
14 changes: 10 additions & 4 deletions src/middlelayers/datafetch/coins/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,15 @@ export class ERC20Analyzer implements Analyzer {
wallet: address
})).value()
}

async preLoad(): Promise<void> {
}
async postLoad(): Promise<void> {
for (const q of this.queries) {
q.clean()
}
}
async loadPortfolio(): Promise<WalletCoin[]> {
return this.loadPortfolioWith429Retry(5)
return this.loadPortfolioWith429Retry(10)
.finally(async () => {
if (this.errorResolver.isTried()) {
await this.errorResolver.resolved()
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/middlelayers/datafetch/coins/others.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export class OthersAnalyzer implements Analyzer {
getAnalyzeName(): string {
return "Others Analyzer"
}

async preLoad(): Promise<void> {
}
async postLoad(): Promise<void> {
}
async loadPortfolio(): Promise<WalletCoin[]> {
return _(this.config.others).map(c => ({
symbol: c.symbol,
Expand Down
5 changes: 4 additions & 1 deletion src/middlelayers/datafetch/coins/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export class SOLAnalyzer implements Analyzer {
getAnalyzeName(): string {
return "SOL Analyzer"
}

async preLoad(): Promise<void> {
}
async postLoad(): Promise<void> {
}
private async query(address: string): Promise<number> {
const resp = await sendHttpRequest<{ result: { value: string } }>("POST", this.queryUrl, 5000, {},
{
Expand Down
2 changes: 2 additions & 0 deletions src/middlelayers/datafetch/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export type WalletCoin = Coin & { wallet: string }

export interface Analyzer {
getAnalyzeName(): string
preLoad(): Promise<void>
loadPortfolio(): Promise<WalletCoin[]>
postLoad(): Promise<void>
}

export type GlobalConfig = CexConfig & TokenConfig & {
Expand Down

0 comments on commit 591dedb

Please sign in to comment.