diff --git a/src/apis/api.ts b/src/apis/api.ts index b53cbb9..2a3aa8f 100644 --- a/src/apis/api.ts +++ b/src/apis/api.ts @@ -1,6 +1,7 @@ import fetch from 'node-fetch'; import Cache from '../common/cache'; import { Coin } from '../plugins/common/plugin'; +import { sleep } from '../common/utils'; export type Price = { price: number; @@ -8,7 +9,7 @@ export type Price = { }; export default class Api { - public async getJson(url: string) { + public async getJson(url: string): Promise { const cachedText = Cache.get(url); if (cachedText) { return JSON.parse(cachedText); @@ -19,6 +20,14 @@ export default class Api { Cache.write(url, text); return JSON.parse(text); } + if (response.status === 429) { + console.log( + `Hit rate limit (${response.status}), trying again in 6 seconds...`, + url + ); + await sleep(6000); + return this.getJson(url); + } return null; } } diff --git a/src/common/utils.ts b/src/common/utils.ts index 85aa888..c0e46f1 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -35,3 +35,5 @@ export const mergeDeep = (target: any, ...sources: any[]): any => { return mergeDeep(target, ...sources); }; + +export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); diff --git a/src/plugins/etherscan.ts b/src/plugins/etherscan.ts index 5286346..40d0ea1 100644 --- a/src/plugins/etherscan.ts +++ b/src/plugins/etherscan.ts @@ -7,10 +7,11 @@ export default class Etherscan extends Plugin { // "Txhash","UnixTimestamp","DateTime","From","To","Value","ContractAddress","TokenName","TokenSymbol" // "Txhash","UnixTimestamp","DateTime","From","To","TokenValue","USDValueDayOfTx","ContractAddress","TokenName","TokenSymbol" + // "Txhash","Blockno","UnixTimestamp","DateTime","From","To","TokenValue","USDValueDayOfTx","ContractAddress","TokenName","TokenSymbol" async convertRow(line: string[]): Promise { - const csvToken = line[9] || line[8]; - const csvTimestamp = line[1]; - const csvAmount = line[5]; + const csvToken = line[10] || line[9] || line[8]; + const csvTimestamp = line[2]; + const csvAmount = line[6]; if (!csvToken || !csvTimestamp) { console.warn( `Token (${csvToken}) or timestamp (${csvTimestamp}) not found!)`