Skip to content

Commit

Permalink
v10.18.13: fix rotating endpoints in case of errors and update web3-b…
Browse files Browse the repository at this point in the history
…lockchain rpc list
  • Loading branch information
0xNe0x1 committed Sep 10, 2024
1 parent 2d71d2a commit 3814c02
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdn.ethers.io/lib/ethers-5.7.umd.min.js" type="application/javascript"></script>
<script crossorigin src="https://unpkg.com/@depay/solana-web3.js@1"></script>
<script crossorigin src="https://unpkg.com/@depay/web3-blockchains@9.4.3"></script>
<script crossorigin src="https://unpkg.com/@depay/web3-blockchains@9.5.2"></script>
<script src="tmp/index.dev.js"></script>
</head>
<body>
Expand Down
4 changes: 2 additions & 2 deletions package.evm.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package.solana.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
72 changes: 50 additions & 22 deletions src/clients/ethers/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3814c02

Please sign in to comment.