From e8d022d03f5f80850deeb6294b4d027016c61f44 Mon Sep 17 00:00:00 2001 From: alan4dev Date: Wed, 25 Dec 2024 10:48:21 +0700 Subject: [PATCH] add a new config: etherscanConfig.singleRequest #2 --- doc/example.md | 5 +++-- libs/AssistedJsonRpcProvider.js | 3 ++- libs/validator.js | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/doc/example.md b/doc/example.md index f2c47a1..b6b481c 100644 --- a/doc/example.md +++ b/doc/example.md @@ -13,7 +13,7 @@ const { AssistedJsonRpcProvider } = require('assisted-json-rpc-provider') // * config.rangeThreshold {number}. Threshold range to know whether to use Log API or ethers.provider's getLogs. Default: 5000. // * config.rateLimitCount and config.rateLimitDuration {number}. Default: Rate-limit's Log API is 1 calls/5second // * config.apiKeys {string[]}. Using api-key to improve rate-limit -// +// * config.singleRequest {boolean}. If true, only return the results of the first request // * return the customized ethers.Provider for getLogs with massive ranges const provider = new AssistedJsonRpcProvider( @@ -24,7 +24,8 @@ const provider = new AssistedJsonRpcProvider( rangeThreshold: 1000, rateLimitCount: 1, rateLimitDuration: 5000, - apiKeys:['apikey'] + apiKeys:['apikey'], + singleRequest: false } ) ``` \ No newline at end of file diff --git a/libs/AssistedJsonRpcProvider.js b/libs/AssistedJsonRpcProvider.js index e007c3b..fe6bf90 100644 --- a/libs/AssistedJsonRpcProvider.js +++ b/libs/AssistedJsonRpcProvider.js @@ -14,6 +14,7 @@ const { standardizeStartConfiguration } = require('./validator'); * url: 'https://api.etherscan.io/api', * maxResults: 1000, * apiKeys: ['YourApiKeyToken'], + * singleRequest: false * } */ class AssistedJsonRpcProvider extends Provider { @@ -223,7 +224,7 @@ class AssistedJsonRpcProvider extends Provider { } let logs = await this.search(url); - if (logs.length < this.etherscanConfig.maxResults) { + if (logs.length < this.etherscanConfig.maxResults || this.etherscanConfig.singleRequest) { return result.concat(logs); } let maxLog = _.maxBy(logs, 'blockNumber') diff --git a/libs/validator.js b/libs/validator.js index 2e6971b..d5a8307 100644 --- a/libs/validator.js +++ b/libs/validator.js @@ -10,13 +10,15 @@ function standardizeStartConfiguration(config) { 'rateLimitDuration', 'url', 'maxResults', - 'apiKeys' + 'apiKeys', + 'singleRequest' ] _validateRangeThreshold(config.rangeThreshold) _validateRateLimitCount(config.rateLimitCount) _validateRateLimitDuration(config.rateLimitDuration) _validateMaxResult(config.maxResults) + _validateSingleRequest(config.singleRequest) const unknownProp = Object.keys(config).find(prop => !knownProps.includes(prop)) if (unknownProp) { @@ -27,7 +29,8 @@ function standardizeStartConfiguration(config) { maxResults: 1000, rateLimitCount: 1, rateLimitDuration: 5 * 1000, - apiKeys: [] + apiKeys: [], + singleRequest: false } return Object.assign(defaultConfig, config) } @@ -72,6 +75,16 @@ function _validateMaxResult(value){ } } +function _validateSingleRequest(value){ + if (value == null) { + return + } + + if (typeof value !== "boolean") { + throw new Error('invalid configuration "singleRequest"') + } +} + module.exports = { standardizeStartConfiguration } \ No newline at end of file