Skip to content

Commit

Permalink
v10.18.7: fix solana rpc list and error rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Jun 30, 2024
1 parent a7d7952 commit 0c14a4c
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 112 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.3.6"></script>
<script crossorigin src="https://unpkg.com/@depay/web3-blockchains@9.4.2"></script>
<script src="tmp/index.dev.js"></script>
</head>
<body>
Expand Down
65 changes: 45 additions & 20 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,30 +265,55 @@ class StaticJsonRpcSequentialProvider extends Connection {
this._rpcRequest = this._rpcRequestReplacement.bind(this);
}

handleError(error, attempt, chunk) {
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
}

batchRequest(requests, attempt) {
return new Promise((resolve, reject) => {
if (requests.length === 0) resolve([]); // Do nothing if requests is empty

const batch = requests.map(params => {
return this._rpcClient.request(params.methodName, params.args)
});

fetch(
this._endpoint,
{
method: 'POST',
body: JSON.stringify(batch),
headers: { 'Content-Type': 'application/json' },
}
).then((response)=>{
if(response.ok) {
response.json().then((parsedJson)=>{
resolve(parsedJson);
}).catch(reject);
} else {
reject(`${response.status} ${response.text}`);
}
}).catch(reject);
})
}

requestChunk(chunk, attempt) {

const batch = chunk.map((inflight) => inflight.request);

const handleError = (error)=>{
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
};

try {
return this._provider._rpcBatchRequest(batch)
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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) {
Expand All @@ -302,8 +327,8 @@ class StaticJsonRpcSequentialProvider extends Connection {
inflightRequest.reject();
}
});
}).catch(handleError)
} catch (error){ return handleError(error) }
}).catch((error)=>this.handleError(error, attempt, chunk))
} catch (error){ return this.handleError(error, attempt, chunk) }
}

_rpcRequestReplacement(methodName, args) {
Expand Down
65 changes: 45 additions & 20 deletions dist/esm/index.solana.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,55 @@ class StaticJsonRpcSequentialProvider extends Connection {
this._rpcRequest = this._rpcRequestReplacement.bind(this);
}

handleError(error, attempt, chunk) {
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
}

batchRequest(requests, attempt) {
return new Promise((resolve, reject) => {
if (requests.length === 0) resolve([]); // Do nothing if requests is empty

const batch = requests.map(params => {
return this._rpcClient.request(params.methodName, params.args)
});

fetch(
this._endpoint,
{
method: 'POST',
body: JSON.stringify(batch),
headers: { 'Content-Type': 'application/json' },
}
).then((response)=>{
if(response.ok) {
response.json().then((parsedJson)=>{
resolve(parsedJson);
}).catch(reject);
} else {
reject(`${response.status} ${response.text}`);
}
}).catch(reject);
})
}

requestChunk(chunk, attempt) {

const batch = chunk.map((inflight) => inflight.request);

const handleError = (error)=>{
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
};

try {
return this._provider._rpcBatchRequest(batch)
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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) {
Expand All @@ -81,8 +106,8 @@ class StaticJsonRpcSequentialProvider extends Connection {
inflightRequest.reject();
}
});
}).catch(handleError)
} catch (error){ return handleError(error) }
}).catch((error)=>this.handleError(error, attempt, chunk))
} catch (error){ return this.handleError(error, attempt, chunk) }
}

_rpcRequestReplacement(methodName, args) {
Expand Down
65 changes: 45 additions & 20 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,30 +271,55 @@
this._rpcRequest = this._rpcRequestReplacement.bind(this);
}

handleError(error, attempt, chunk) {
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new solanaWeb3_js.Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
}

batchRequest(requests, attempt) {
return new Promise((resolve, reject) => {
if (requests.length === 0) resolve([]); // Do nothing if requests is empty

const batch = requests.map(params => {
return this._rpcClient.request(params.methodName, params.args)
});

fetch(
this._endpoint,
{
method: 'POST',
body: JSON.stringify(batch),
headers: { 'Content-Type': 'application/json' },
}
).then((response)=>{
if(response.ok) {
response.json().then((parsedJson)=>{
resolve(parsedJson);
}).catch(reject);
} else {
reject(`${response.status} ${response.text}`);
}
}).catch(reject);
})
}

requestChunk(chunk, attempt) {

const batch = chunk.map((inflight) => inflight.request);

const handleError = (error)=>{
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new solanaWeb3_js.Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
};

try {
return this._provider._rpcBatchRequest(batch)
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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) {
Expand All @@ -308,8 +333,8 @@
inflightRequest.reject();
}
});
}).catch(handleError)
} catch (error){ return handleError(error) }
}).catch((error)=>this.handleError(error, attempt, chunk))
} catch (error){ return this.handleError(error, attempt, chunk) }
}

_rpcRequestReplacement(methodName, args) {
Expand Down
65 changes: 45 additions & 20 deletions dist/umd/index.solana.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,55 @@
this._rpcRequest = this._rpcRequestReplacement.bind(this);
}

handleError(error, attempt, chunk) {
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new solanaWeb3_js.Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
}

batchRequest(requests, attempt) {
return new Promise((resolve, reject) => {
if (requests.length === 0) resolve([]); // Do nothing if requests is empty

const batch = requests.map(params => {
return this._rpcClient.request(params.methodName, params.args)
});

fetch(
this._endpoint,
{
method: 'POST',
body: JSON.stringify(batch),
headers: { 'Content-Type': 'application/json' },
}
).then((response)=>{
if(response.ok) {
response.json().then((parsedJson)=>{
resolve(parsedJson);
}).catch(reject);
} else {
reject(`${response.status} ${response.text}`);
}
}).catch(reject);
})
}

requestChunk(chunk, attempt) {

const batch = chunk.map((inflight) => inflight.request);

const handleError = (error)=>{
if(attempt < MAX_RETRY && error && [
'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400'
].some((errorType)=>error.toString().match(errorType))) {
const index = this._endpoints.indexOf(this._endpoint)+1;
this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index];
this._provider = new solanaWeb3_js.Connection(this._endpoint);
this.requestChunk(chunk, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
};

try {
return this._provider._rpcBatchRequest(batch)
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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) {
Expand All @@ -87,8 +112,8 @@
inflightRequest.reject();
}
});
}).catch(handleError)
} catch (error){ return handleError(error) }
}).catch((error)=>this.handleError(error, attempt, chunk))
} catch (error){ return this.handleError(error, attempt, chunk) }
}

_rpcRequestReplacement(methodName, args) {
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.6",
"version": "10.18.7",
"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.3.6",
"@depay/web3-blockchains": "^9.4.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.6",
"version": "10.18.7",
"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.25.1",
"@depay/web3-blockchains": "^9.3.6",
"@depay/web3-blockchains": "^9.4.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.25.1",
"@depay/web3-blockchains": "^9.3.6",
"@depay/web3-blockchains": "^9.4.2",
"@depay/web3-mock": "^14.17.0",
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-json": "^4.1.0",
Expand Down
Loading

0 comments on commit 0c14a4c

Please sign in to comment.