Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Sep 10, 2024
1 parent 3814c02 commit 9cc87ff
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 96 deletions.
76 changes: 52 additions & 24 deletions dist/esm/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,51 +99,79 @@ 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)=>_optionalChain$2([entry, 'optionalAccess', _ => _.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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) {
if (_optionalChain$2([payload, 'optionalAccess', _2 => _2.error])) {
const error = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
inflightRequest.reject(error);
} else if(_optionalChain$2([payload, 'optionalAccess', _2 => _2.result])) {
} else if(_optionalChain$2([payload, 'optionalAccess', _3 => _3.result])) {
inflightRequest.resolve(payload.result);
} else {
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 (e) {

chunk.forEach((inflightRequest) => {
inflightRequest.reject();
});
}
}).catch((error) => this.handleError(error, attempt, chunk))
} catch (error){ this.handleError(error, attempt, chunk); }
}

send(method, params) {
Expand Down
76 changes: 52 additions & 24 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,51 +42,79 @@ class StaticJsonRpcBatchProvider extends ethers.providers.JsonRpcProvider {
this._pendingBatch = [];
}

handleError(error, attempt, chunk) {
if(attempt < MAX_RETRY$1 && 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)=>_optionalChain$5([entry, 'optionalAccess', _ => _.error]))) {
if(attempt < MAX_RETRY$1) {
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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$5([payload, 'optionalAccess', _ => _.error])) {
if (_optionalChain$5([payload, 'optionalAccess', _2 => _2.error])) {
const error = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
inflightRequest.reject(error);
} else if(_optionalChain$5([payload, 'optionalAccess', _2 => _2.result])) {
} else if(_optionalChain$5([payload, 'optionalAccess', _3 => _3.result])) {
inflightRequest.resolve(payload.result);
} else {
inflightRequest.reject();
}
});
}).catch((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, attempt+1);
} else {
chunk.forEach((inflightRequest) => {
inflightRequest.reject(error);
});
}
})

} catch (e) {

chunk.forEach((inflightRequest) => {
inflightRequest.reject();
});
}
}).catch((error) => this.handleError(error, attempt, chunk))
} catch (error){ this.handleError(error, attempt, chunk); }
}

send(method, params) {
Expand Down
76 changes: 52 additions & 24 deletions dist/umd/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,51 +106,79 @@
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__default["default"].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)=>_optionalChain$2([entry, 'optionalAccess', _ => _.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.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
chunk.forEach((inflightRequest, index) => {
const payload = result[index];
if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) {
if (_optionalChain$2([payload, 'optionalAccess', _2 => _2.error])) {
const error = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
inflightRequest.reject(error);
} else if(_optionalChain$2([payload, 'optionalAccess', _2 => _2.result])) {
} else if(_optionalChain$2([payload, 'optionalAccess', _3 => _3.result])) {
inflightRequest.resolve(payload.result);
} else {
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 (e) {

chunk.forEach((inflightRequest) => {
inflightRequest.reject();
});
}
}).catch((error) => this.handleError(error, attempt, chunk))
} catch (error){ this.handleError(error, attempt, chunk); }
}

send(method, params) {
Expand Down
Loading

0 comments on commit 9cc87ff

Please sign in to comment.