Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a new config: etherscanConfig.singleRequest #2 #3

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}