Skip to content

Commit

Permalink
Merge pull request #3 from derion-io/feat/add-single-request-mode
Browse files Browse the repository at this point in the history
add a new config: etherscanConfig.singleRequest #2
  • Loading branch information
alan4dev authored Dec 25, 2024
2 parents 3d6036e + e8d022d commit 8d03132
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions doc/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -24,7 +24,8 @@ const provider = new AssistedJsonRpcProvider(
rangeThreshold: 1000,
rateLimitCount: 1,
rateLimitDuration: 5000,
apiKeys:['apikey']
apiKeys:['apikey'],
singleRequest: false
}
)
```
3 changes: 2 additions & 1 deletion libs/AssistedJsonRpcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { standardizeStartConfiguration } = require('./validator');
* url: 'https://api.etherscan.io/api',
* maxResults: 1000,
* apiKeys: ['YourApiKeyToken'],
* singleRequest: false
* }
*/
class AssistedJsonRpcProvider extends Provider {
Expand Down Expand Up @@ -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')
Expand Down
17 changes: 15 additions & 2 deletions libs/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -27,7 +29,8 @@ function standardizeStartConfiguration(config) {
maxResults: 1000,
rateLimitCount: 1,
rateLimitDuration: 5 * 1000,
apiKeys: []
apiKeys: [],
singleRequest: false
}
return Object.assign(defaultConfig, config)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 8d03132

Please sign in to comment.