Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pro check before load erc20 assets #185

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/middlelayers/datafetch/coins/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bluebird from 'bluebird'
import { invoke } from '@tauri-apps/api'
import { getLicenseIfIsPro } from '@/middlelayers/configuration'
import { getClientID } from '@/utils/app'
import { LicenseCenter } from '@/middlelayers/license'

type QueryAssetResp = {
data: {
Expand Down Expand Up @@ -153,15 +154,15 @@ export class ERC20NormalAnalyzer implements Analyzer {
}

async loadPortfolio(): Promise<WalletCoin[]> {
return this.loadPortfolioWith429Retry(10)
return this.loadPortfolioWithRetry(10)
.finally(async () => {
if (this.errorResolver.isTried()) {
await this.errorResolver.resolved()
}
})
}

async loadPortfolioWith429Retry(max: number): Promise<WalletCoin[]> {
async loadPortfolioWithRetry(max: number): Promise<WalletCoin[]> {
try {
if (max <= 0) {
throw new Error("failed to query erc20 assets")
Expand All @@ -179,7 +180,7 @@ export class ERC20NormalAnalyzer implements Analyzer {
await new Promise(resolve => setTimeout(resolve, 500))

// try again
return this.loadPortfolioWith429Retry(max - 1)
return this.loadPortfolioWithRetry(max - 1)
} else {
throw e
}
Expand All @@ -198,21 +199,20 @@ export class ERC20ProAnalyzer extends ERC20NormalAnalyzer {
const license = await getLicenseIfIsPro()

// if not pro license, use normal analyzer
if (!license) {
if (!license || await this.validateLicense(license) === false) {
// return super.loadPortfolio()
console.debug("not pro license, fallback to normal erc20 analyzer")
return super.loadPortfolio()
}

try {
const res = await this.loadPortfolioPro(license)
return res
} catch (e) {
// fallback to normal analyzer
console.error("failed to query pro erc20 assets, fallback to normal erc20 analyzer", e)
return super.loadPortfolio()
}
console.debug("pro license, use pro erc20 analyzer")

return this.loadProPortfolioWithRetry(license, 5)
}

async validateLicense(license: string): Promise<boolean> {
const { isPro } = await LicenseCenter.getInstance().validateLicense(license)
return isPro
}

async loadPortfolioPro(license: string): Promise<WalletCoin[]> {
Expand All @@ -237,4 +237,24 @@ export class ERC20ProAnalyzer extends ERC20NormalAnalyzer {
wallet: d.wallet
})).value()).flatten().value()
}

async loadProPortfolioWithRetry(license: string, max: number): Promise<WalletCoin[]> {
try {
if (max <= 0) {
throw new Error("failed to query erc20 assets")
}
const coins = await this.loadPortfolioPro(license)
return coins
} catch (e) {
if (e instanceof Error && (e.message.includes("504") || e.message.includes("503"))) {
console.error("failed to query pro erc20 assets, retrying...")
// sleep 2s
await new Promise(resolve => setTimeout(resolve, 2000))

return this.loadProPortfolioWithRetry(license, max - 1)
} else {
throw e
}
}
}
}
Loading