From 3814c02179412f977a90a47722279f80b279a5d0 Mon Sep 17 00:00:00 2001 From: Sebastian Pape <0xneo11@gmail.com> Date: Tue, 10 Sep 2024 13:25:34 +0200 Subject: [PATCH] v10.18.13: fix rotating endpoints in case of errors and update web3-blockchain rpc list --- dev.html | 2 +- package.evm.json | 4 +- package.json | 6 +-- package.solana.json | 4 +- src/clients/ethers/provider.js | 72 +++++++++++++++++++++++----------- yarn.lock | 8 ++-- 6 files changed, 62 insertions(+), 34 deletions(-) diff --git a/dev.html b/dev.html index 2c767a3..3910016 100644 --- a/dev.html +++ b/dev.html @@ -8,7 +8,7 @@ - + diff --git a/package.evm.json b/package.evm.json index 6922429..5a2b02a 100644 --- a/package.evm.json +++ b/package.evm.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client-evm", "moduleName": "Web3Client", - "version": "10.18.12", + "version": "10.18.13", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.evm.js", "module": "dist/esm/index.evm.js", @@ -23,7 +23,7 @@ "homepage": "https://depay.com", "private": false, "peerDependencies": { - "@depay/web3-blockchains": "^9.4.3", + "@depay/web3-blockchains": "^9.5.2", "ethers": "^5.7.1" }, "engines": { diff --git a/package.json b/package.json index 5258d92..7f57304 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client", "moduleName": "Web3Client", - "version": "10.18.12", + "version": "10.18.13", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", @@ -34,7 +34,7 @@ "private": false, "peerDependencies": { "@depay/solana-web3.js": "^1.26.0", - "@depay/web3-blockchains": "^9.4.3", + "@depay/web3-blockchains": "^9.5.2", "ethers": "^5.7.1" }, "engines": { @@ -44,7 +44,7 @@ "@babel/core": "^7.12.9", "@babel/preset-env": "^7.12.7", "@depay/solana-web3.js": "^1.26.0", - "@depay/web3-blockchains": "^9.4.3", + "@depay/web3-blockchains": "^9.5.2", "@depay/web3-mock": "^14.18.0", "@rollup/plugin-commonjs": "^22.0.1", "@rollup/plugin-json": "^4.1.0", diff --git a/package.solana.json b/package.solana.json index 5f5acfc..d3ea701 100644 --- a/package.solana.json +++ b/package.solana.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client-solana", "moduleName": "Web3Client", - "version": "10.18.12", + "version": "10.18.13", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.solana.js", "module": "dist/esm/index.solana.js", @@ -23,7 +23,7 @@ "homepage": "https://depay.com", "private": false, "peerDependencies": { - "@depay/web3-blockchains": "^9.4.3", + "@depay/web3-blockchains": "^9.5.2", "@depay/solana-web3.js": "^1.26.0", "ethers": "^5.7.1" }, diff --git a/src/clients/ethers/provider.js b/src/clients/ethers/provider.js index 9ec1028..2227e43 100644 --- a/src/clients/ethers/provider.js +++ b/src/clients/ethers/provider.js @@ -17,16 +17,61 @@ class StaticJsonRpcBatchProvider extends ethers.providers.JsonRpcProvider { this._pendingBatch = [] } + handleError(error, attempt, chunk) { + if(attempt < MAX_RETRY && error) { + const index = this._endpoints.indexOf(this._endpoint)+1 + this._failover() + this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index] + this.requestChunk(chunk, this._endpoint, attempt+1) + } else { + chunk.forEach((inflightRequest) => { + inflightRequest.reject(error) + }) + } + } + detectNetwork() { return Promise.resolve(Blockchains.findByName(this._network).id) } + batchRequest(batch, attempt) { + return new Promise((resolve, reject) => { + + if (batch.length === 0) resolve([]) // Do nothing if requests is empty + + fetch( + this._endpoint, + { + method: 'POST', + body: JSON.stringify(batch), + headers: { 'Content-Type': 'application/json' }, + } + ).then((response)=>{ + if(response.ok) { + response.json().then((parsedJson)=>{ + if(parsedJson.find((entry)=>entry?.error)) { + if(attempt < MAX_RETRY) { + reject('Error in batch found!') + } else { + resolve(parsedJson); + } + } else { + resolve(parsedJson); + } + }).catch(reject) + } else { + reject(`${response.status} ${response.text}`) + } + }).catch(reject) + }) + } + requestChunk(chunk, endpoint, attempt) { - try { + const batch = chunk.map((inflight) => inflight.request) - const request = chunk.map((inflight) => inflight.request) - return ethers.utils.fetchJson(endpoint, JSON.stringify(request)) + try { + return this.batchRequest(batch, attempt) .then((result) => { // For each result, feed it to the correct Promise, depending // on whether it was a success or error @@ -43,25 +88,8 @@ class StaticJsonRpcBatchProvider extends ethers.providers.JsonRpcProvider { inflightRequest.reject() } }) - }).catch((error) => { - if(attempt < MAX_RETRY && error && error.code == 'SERVER_ERROR') { - const index = this._endpoints.indexOf(this._endpoint)+1 - this._failover() - this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index] - this.requestChunk(chunk, this._endpoint, attempt+1) - } else { - chunk.forEach((inflightRequest) => { - inflightRequest.reject(error) - }) - } - }) - - } catch { - - chunk.forEach((inflightRequest) => { - inflightRequest.reject() - }) - } + }).catch((error) => this.handleError(error, attempt, chunk)) + } catch (error){ this.handleError(error, attempt, chunk) } } send(method, params) { diff --git a/yarn.lock b/yarn.lock index bd8ab61..88f1e2d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -908,10 +908,10 @@ resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-9.1.4.tgz#f006c29c887c433e1824e2bfabf8f39ad13da907" integrity sha512-CQnXCNAt3sA1MphZDMPbrhAPtemzeQ/NKeHcd2aBF61nTjJCRUmSh1Ox8Z6rlSjgDP66842iy6JAoRiFDtlmFw== -"@depay/web3-blockchains@^9.4.3": - version "9.4.3" - resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-9.4.3.tgz#df7d1ae5bb6cf7ac1b1e556ab09eb314d7c5eee4" - integrity sha512-Pl+7EdTZGm1jUZWAYtShqE/xOszblCoWYQQ5p00eHuUkobHs3vg6Ih7fSINL3TvTIxaVTpTjhyyAc2afkbhbmQ== +"@depay/web3-blockchains@^9.5.2": + version "9.5.2" + resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-9.5.2.tgz#f19d0ededa3b059f21af455e2d5b280f23f07575" + integrity sha512-GocEM5I9aBFGrQ0mRAFlZd65SNMKYLAGzYJAE+UnYxTt1gA3p7eYI90VMxAp1KF+T38VAGEZLElilIql9Gb0QQ== "@depay/web3-mock@^14.18.0": version "14.18.0"