Skip to content

Commit

Permalink
v12.10.10: improve solana rpc failovers
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Jun 30, 2024
1 parent 9714223 commit 88cef99
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 78 deletions.
4 changes: 2 additions & 2 deletions dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/react-dialog-stack@8"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/react-shadow-dom@5"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/js-verify-signature-web@3"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/[email protected].0"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/web3-client@10"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/[email protected].2"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/web3-client@10.18.7"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/web3-tokens@10"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/react-token-image@5"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@depay/coinbase-wallet-sdk@3"></script>
Expand Down
4 changes: 2 additions & 2 deletions dist/esm/index.bundle.js

Large diffs are not rendered by default.

79 changes: 53 additions & 26 deletions dist/esm/index.solana.js
Original file line number Diff line number Diff line change
Expand Up @@ -29383,6 +29383,7 @@ const getConfiguration$1 = () =>{
function _optionalChain$3$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
const BATCH_INTERVAL$1$1 = 10;
const CHUNK_SIZE$1$1 = 99;
const MAX_RETRY$1 = 3;

class StaticJsonRpcBatchProvider$1 extends ethers.providers.JsonRpcProvider {

Expand All @@ -29399,7 +29400,7 @@ class StaticJsonRpcBatchProvider$1 extends ethers.providers.JsonRpcProvider {
return Promise.resolve(Blockchains.findByName(this._network).id)
}

requestChunk(chunk, endpoint) {
requestChunk(chunk, endpoint, attempt) {

try {

Expand All @@ -29422,11 +29423,11 @@ class StaticJsonRpcBatchProvider$1 extends ethers.providers.JsonRpcProvider {
}
});
}).catch((error) => {
if(error && error.code == 'SERVER_ERROR') {
if(attempt < MAX_RETRY$1 && 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);
this.requestChunk(chunk, this._endpoint, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
Expand Down Expand Up @@ -29480,7 +29481,7 @@ class StaticJsonRpcBatchProvider$1 extends ethers.providers.JsonRpcProvider {
chunks.forEach((chunk)=>{
// Get the request as an array of requests
chunk.map((inflight) => inflight.request);
return this.requestChunk(chunk, this._endpoint)
return this.requestChunk(chunk, this._endpoint, 1)
});
}, getConfiguration$1().batchInterval || BATCH_INTERVAL$1$1);
}
Expand Down Expand Up @@ -29603,6 +29604,7 @@ var EVM = {
function _optionalChain$2$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
const BATCH_INTERVAL = 10;
const CHUNK_SIZE = 99;
const MAX_RETRY = 3;

class StaticJsonRpcSequentialProvider extends Connection {

Expand All @@ -29617,30 +29619,55 @@ class StaticJsonRpcSequentialProvider extends Connection {
this._rpcRequest = this._rpcRequestReplacement.bind(this);
}

requestChunk(chunk) {
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);
});
}
}

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

const handleError = (error)=>{
if(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);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
};
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);

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$2([payload, 'optionalAccess', _ => _.error])) {
Expand All @@ -29654,8 +29681,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 Expand Up @@ -29691,7 +29718,7 @@ class StaticJsonRpcSequentialProvider extends Connection {
chunks.forEach((chunk)=>{
// Get the request as an array of requests
chunk.map((inflight) => inflight.request);
return this.requestChunk(chunk)
return this.requestChunk(chunk, 1)
});
}, getConfiguration$1().batchInterval || BATCH_INTERVAL);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/index.solana.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/umd/index.bundle.js

Large diffs are not rendered by default.

79 changes: 53 additions & 26 deletions dist/umd/index.solana.js
Original file line number Diff line number Diff line change
Expand Up @@ -29381,6 +29381,7 @@
function _optionalChain$3$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
const BATCH_INTERVAL$1$1 = 10;
const CHUNK_SIZE$1$1 = 99;
const MAX_RETRY$1 = 3;

class StaticJsonRpcBatchProvider$1 extends ethers.ethers.providers.JsonRpcProvider {

Expand All @@ -29397,7 +29398,7 @@
return Promise.resolve(Blockchains__default['default'].findByName(this._network).id)
}

requestChunk(chunk, endpoint) {
requestChunk(chunk, endpoint, attempt) {

try {

Expand All @@ -29420,11 +29421,11 @@
}
});
}).catch((error) => {
if(error && error.code == 'SERVER_ERROR') {
if(attempt < MAX_RETRY$1 && 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);
this.requestChunk(chunk, this._endpoint, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
Expand Down Expand Up @@ -29478,7 +29479,7 @@
chunks.forEach((chunk)=>{
// Get the request as an array of requests
chunk.map((inflight) => inflight.request);
return this.requestChunk(chunk, this._endpoint)
return this.requestChunk(chunk, this._endpoint, 1)
});
}, getConfiguration$1().batchInterval || BATCH_INTERVAL$1$1);
}
Expand Down Expand Up @@ -29601,6 +29602,7 @@
function _optionalChain$2$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
const BATCH_INTERVAL = 10;
const CHUNK_SIZE = 99;
const MAX_RETRY = 3;

class StaticJsonRpcSequentialProvider extends solanaWeb3_js.Connection {

Expand All @@ -29615,30 +29617,55 @@
this._rpcRequest = this._rpcRequestReplacement.bind(this);
}

requestChunk(chunk) {
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);
});
}
}

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

const handleError = (error)=>{
if(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);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
};
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);

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$2([payload, 'optionalAccess', _ => _.error])) {
Expand All @@ -29652,8 +29679,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 Expand Up @@ -29689,7 +29716,7 @@
chunks.forEach((chunk)=>{
// Get the request as an array of requests
chunk.map((inflight) => inflight.request);
return this.requestChunk(chunk)
return this.requestChunk(chunk, 1)
});
}, getConfiguration$1().batchInterval || BATCH_INTERVAL);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/umd/index.solana.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.evm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/widgets-evm",
"moduleName": "DePayWidgets",
"version": "12.10.9",
"version": "12.10.10",
"description": "Web3 Payments with any token. DePay simplifies and improves Web3 Payments with the power of DeFi. Accept any token with on-the-fly conversion.",
"main": "./dist/umd/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -29,8 +29,8 @@
"@depay/react-token-image-evm": "^5.0.0",
"@depay/walletconnect-v2": "^2.12.2",
"@depay/web3-assets-evm": "^7.3.1",
"@depay/web3-blockchains": "^9.4.0",
"@depay/web3-client-evm": "^10.18.5",
"@depay/web3-blockchains": "^9.4.2",
"@depay/web3-client-evm": "^10.18.7",
"@depay/web3-exchanges-evm": "^13.8.1",
"@depay/web3-payments-evm": "^13.8.0",
"@depay/web3-tokens-evm": "^10.3.0",
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/widgets",
"moduleName": "DePayWidgets",
"version": "12.10.9",
"version": "12.10.10",
"description": "Web3 Payments with any token. DePay simplifies and improves Web3 Payments with the power of DeFi. Accept any token with on-the-fly conversion.",
"main": "./dist/umd/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -43,8 +43,8 @@
"@depay/solana-web3.js": "^1.26.0",
"@depay/walletconnect-v2": "^2.12.2",
"@depay/web3-assets": "^7.3.1",
"@depay/web3-blockchains": "^9.4.0",
"@depay/web3-client": "^10.18.5",
"@depay/web3-blockchains": "^9.4.2",
"@depay/web3-client": "^10.18.7",
"@depay/web3-exchanges": "^13.8.1",
"@depay/web3-payments": "^13.8.0",
"@depay/web3-tokens": "^10.3.0",
Expand Down
6 changes: 3 additions & 3 deletions package.solana.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/widgets-solana",
"moduleName": "DePayWidgets",
"version": "12.10.9",
"version": "12.10.10",
"description": "Web3 Payments with any token. DePay simplifies and improves Web3 Payments with the power of DeFi. Accept any token with on-the-fly conversion.",
"main": "./dist/umd/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -30,8 +30,8 @@
"@depay/solana-web3.js": "^1.26.0",
"@depay/walletconnect-v2": "^2.12.2",
"@depay/web3-assets-solana": "^7.3.1",
"@depay/web3-blockchains": "^9.4.0",
"@depay/web3-client-solana": "^10.18.5",
"@depay/web3-blockchains": "^9.4.2",
"@depay/web3-client-solana": "^10.18.7",
"@depay/web3-exchanges-solana": "^13.8.1",
"@depay/web3-payments-solana": "^13.8.0",
"@depay/web3-tokens-solana": "^10.3.0",
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1347,15 +1347,15 @@
resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-9.2.0.tgz#22ed38c94fc61293f117419ae118320d96588e37"
integrity sha512-w7UOZqsCjNg+wwfhQ5vF1vV0qTcMJpIJj8dMmGo31PWu7eiL301zYfVEWyxPUDd4xIJY3hcUeB78xapfhrxKRQ==

"@depay/web3-blockchains@^9.4.0":
version "9.4.0"
resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-9.4.0.tgz#db3c7d970aebee9eb34965d46fd559c550c9b48c"
integrity sha512-j4bB848HoLI6BOMejPfyv62sqTOhRhT+8iWhwLma97hP/J3S0F4N8gW5w7R8Vbfg9D05cej1FNkxQkGtKF/dZg==

"@depay/web3-client@^10.18.5":
version "10.18.5"
resolved "https://registry.yarnpkg.com/@depay/web3-client/-/web3-client-10.18.5.tgz#edc56c3c44f48a0b617f470bd531a5f1c4a540dd"
integrity sha512-LSUa3c55/K8h768HTJQu7g4KabRDex23uGrHk4bJv8BNkBLmwTM9BZJ2EjxjFfrc99SvhB2gDWNv1X8KVHB2kw==
"@depay/web3-blockchains@^9.4.2":
version "9.4.2"
resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-9.4.2.tgz#61cbe215482b6c2e70255291d10c7c9b4d2bd90c"
integrity sha512-1aT9ZhMEXl87BOZsdFgVEl9aY1MgO0uB7/Qf4Wb4+L0omiydygIhITPh4JlCNz2yVdFtcU35N4/xS+4jli+nCg==

"@depay/web3-client@^10.18.7":
version "10.18.7"
resolved "https://registry.yarnpkg.com/@depay/web3-client/-/web3-client-10.18.7.tgz#b9131383d680c616e8485fad54c79ed8e33dbe03"
integrity sha512-5XsYPAbGZ91P7RLrRuZvSY0d2JT2NsUFIJHXf0YfzJQacy1gIuqQwUU7nMq+niL42AfvCqQ7wOAnP9tQcByXzw==

"@depay/web3-exchanges@^13.8.1":
version "13.8.1"
Expand Down

0 comments on commit 88cef99

Please sign in to comment.