Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser provider hang fix #295

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/providers/provider-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JsonRpcApiProvider, JsonRpcSigner } from './provider-jsonrpc.js';

import type { JsonRpcError, JsonRpcPayload, JsonRpcResult } from './provider-jsonrpc.js';
import type { Networkish } from './network.js';
import { Shard } from '../constants/index.js';

/**
* The interface to an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider, which is a standard used by most
Expand All @@ -18,9 +19,10 @@ export interface Eip1193Provider {
* @param {Object} request - The request object.
* @param {string} request.method - The method name.
* @param {any[] | Record<string, any>} [request.params] - The parameters for the method.
* @param {Shard} [request.shard] - The shard to send the request to.
* @returns {Promise<any>} The result of the request.
*/
request(request: { method: string; params?: Array<any> | Record<string, any> }): Promise<any>;
request(request: { method: string; params?: Array<any> | Record<string, any>; shard?: Shard }): Promise<any>;
}

/**
Expand Down Expand Up @@ -58,7 +60,7 @@ export type DebugEventBrowserProvider =
* @extends JsonRpcApiProvider
*/
export class BrowserProvider extends JsonRpcApiProvider {
#request: (method: string, params: Array<any> | Record<string, any>) => Promise<any>;
#request: (method: string, params: Array<any> | Record<string, any>, shard?: Shard) => Promise<any>;

/**
* Connect to the `ethereum` provider, optionally forcing the `network`.
Expand All @@ -70,8 +72,10 @@ export class BrowserProvider extends JsonRpcApiProvider {
constructor(ethereum: Eip1193Provider, network?: Networkish) {
super(network, { batchMaxCount: 1 });

this.#request = async (method: string, params: Array<any> | Record<string, any>) => {
const payload = { method, params };
if (this.initResolvePromise) this.initResolvePromise();

this.#request = async (method: string, params: Array<any> | Record<string, any>, shard?: Shard) => {
const payload = { method, params, shard };
this.emit('debug', undefined, { action: 'sendEip1193Request', payload });
try {
const result = await ethereum.request(payload);
Expand Down Expand Up @@ -115,10 +119,10 @@ export class BrowserProvider extends JsonRpcApiProvider {
* @param {any[] | Record<string, any>} params - The parameters for the method.
* @returns {Promise<any>} The result of the request.
*/
async send(method: string, params: Array<any> | Record<string, any>): Promise<any> {
async send(method: string, params: Array<any> | Record<string, any>, shard?: Shard): Promise<any> {
await this._start();

return await super.send(method, params);
return await super.send(method, params, shard);
}

/**
Expand All @@ -129,17 +133,20 @@ export class BrowserProvider extends JsonRpcApiProvider {
* @param {JsonRpcPayload | JsonRpcPayload[]} payload - The JSON-RPC payload.
* @returns {Promise<(JsonRpcResult | JsonRpcError)[]>} The result of the request.
*/
async _send(payload: JsonRpcPayload | Array<JsonRpcPayload>): Promise<Array<JsonRpcResult | JsonRpcError>> {
async _send(
payload: JsonRpcPayload | Array<JsonRpcPayload>,
shard?: Shard,
): Promise<Array<JsonRpcResult | JsonRpcError>> {
assertArgument(!Array.isArray(payload), 'EIP-1193 does not support batch request', 'payload', payload);

try {
const result = await this.#request(payload.method, payload.params || []);
const result = await this.#request(payload.method, payload.params || [], shard);
return [{ id: payload.id, result }];
} catch (e: any) {
return [
{
id: payload.id,
error: { code: e.code, data: e.data, message: e.message },
error: { code: e.code, data: e.data, message: e.message, shard: shard || undefined },
},
];
}
Expand Down
1 change: 1 addition & 0 deletions src/providers/provider-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export type JsonRpcError = {
code: number;
message?: string;
data?: any;
shard?: Shard;
};
};

Expand Down
Loading