-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a464dcd
commit cb268ee
Showing
3 changed files
with
38 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,53 @@ | ||
import { RetryableError, wrapRetryable } from "./retryable.mjs"; | ||
|
||
/** | ||
* Cache startBlocks | ||
* @type {Map<string, BigInt>} | ||
*/ | ||
let startBlocks = new Map(); | ||
|
||
export const getTokenTx = wrapRetryable(async function getTokenTx(etherscan, params) { | ||
// Construct URL without startBlock | ||
const url = new URL(`https://api.${etherscan}/api`); | ||
url.searchParams.append("module", "account"); | ||
url.searchParams.append("action", "tokentx"); | ||
url.searchParams.append("page", "1"); | ||
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key.toLowerCase(), value)); | ||
url.searchParams.append("sort", "desc"); | ||
|
||
// Calculate Cache key before appending startBlock | ||
const key = url.toString(); | ||
|
||
// Append startBlock from cache if present | ||
const startBlock = startBlocks.get(key); | ||
if (startBlock) url.searchParams.append("startblock", startBlock.toString()); | ||
|
||
let response; | ||
try { | ||
response = await fetch(url); | ||
} catch (error) { | ||
throw new RetryableError(`${etherscan} connection error`, true, error); | ||
} | ||
if (!response.ok) | ||
throw new RetryableError(`${etherscan} got ${response.status}`, response.status >= 500); | ||
throw new RetryableError( | ||
`${etherscan} got ${response.status}`, | ||
response.status >= 500, | ||
url | ||
); | ||
const text = await response.text(); | ||
const json = JSON.parse(text); | ||
if (json.status !== "1") | ||
throw new RetryableError(`${etherscan} error "${text}}"`, json.result === "Invalid API Key" || json.message === "No transactions found"); | ||
return json.result; | ||
throw new RetryableError( | ||
`${etherscan} error "${text}}"`, | ||
json.result === "Invalid API Key" || json.message === "No transactions found", | ||
url | ||
); | ||
const { result } = json; | ||
|
||
// If transactions present cache last transaction start block | ||
if (result.length !== 0) { | ||
result.sort((a, b) => Number(BigInt(a.blockNumber) - BigInt(b.blockNumber))); | ||
startBlocks.set(key, BigInt(result.at(-1).blockNumber) - 1n); | ||
} | ||
return result; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters