Skip to content

Commit

Permalink
fix: update etherscan/polygonscan plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tehfonsi committed Apr 14, 2023
1 parent 1387d5e commit 6b9d54d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/apis/api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;
coin: Coin;
};

export default class Api {
public async getJson(url: string) {
public async getJson(url: string): Promise<any | null> {
const cachedText = Cache.get(url);
if (cachedText) {
return JSON.parse(cachedText);
Expand All @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
7 changes: 4 additions & 3 deletions src/plugins/etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[] | null> {
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!)`
Expand Down

0 comments on commit 6b9d54d

Please sign in to comment.