Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lumerin/wallet-core",
"version": "1.0.88",
"version": "1.0.89",
"author": {
"name": "Lumerin",
"email": "developer@lumerin.io",
Expand Down
28 changes: 19 additions & 9 deletions src/plugins/eth/web3Http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ const Web3 = require('web3')
const https = require('https')
const logger = require('../../logger')

const isRateLimitError = (response) => {
const isRateLimitError = (response, payload) => {
const { result, ...data } = response
const code = response.error?.code
if (code === 429 || code === -32029 || code === -32097) {
return true
}

if (payload?.method === 'eth_call' && (response.error?.message?.includes('execution reverted') || response.error?.code === -32000)) {
return true
}

const message = response.error?.message?.toLowerCase()
if (!message) {
return false
Comment on lines +5 to 18
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function isRateLimitError is misleadingly named as it checks for more than just rate limit errors. It also checks for execution reversion and generic errors, which are not related to rate limiting. This can lead to confusion and incorrect error handling.

Recommended solution: Rename the function to accurately reflect the types of errors it checks for, or refactor the function to only check for rate limit errors and handle other error types separately.

Expand All @@ -24,13 +28,19 @@ const isRateLimitError = (response) => {
);
}

const MAX_RETRIES = 10;
const timeouts = {
0: 500,
1: 750,
2: 1000,
3: 1500,
4: 2000,
0: 1000,
1: 1200,
2: 1400,
3: 1600,
4: 1800,
5: 2000,
6: 2200,
7: 2400,
8: 2600,
9: 2800,
10: 3000,
}

class Web3Http extends Web3 {
Expand Down Expand Up @@ -65,9 +75,9 @@ class Web3Http extends Web3 {
const originalSend = this.currentProvider.send.bind(this.currentProvider)
this.currentProvider.send = (payload, callback) => {
originalSend(payload, async (error, response) => {
if (error || isRateLimitError(response)) {
if (error || isRateLimitError(response, payload)) {
// Avoid infinite loop
if (this.retryCount >= this.providers.length * 2) {
if (this.retryCount >= MAX_RETRIES) {
callback(error, response)
this.retryCount = 0
Comment thread
alex-sandrk marked this conversation as resolved.
return
Expand All @@ -79,7 +89,7 @@ class Web3Http extends Web3 {
this.retryCount += 1
const timeout = timeouts[this.retryCount] || 1000;
logger.error(
`Switched to provider: ${this.providers[this.currentIndex].host}, timeout: ${timeout}`
`Switched to provider: ${this.providers[this.currentIndex].host}, timeout: ${timeout}, retry count: ${this.retryCount}, request payload: ${JSON.stringify(payload)}`,
)
await new Promise((resolve) => setTimeout(resolve, timeout))

Expand Down