From dbff840cfec067e7c49eb3fa685555819837a775 Mon Sep 17 00:00:00 2001 From: rileystephens28 Date: Wed, 11 Sep 2024 15:44:15 -0500 Subject: [PATCH] Add support for shard parameter in Eip1193Provider request method --- src/providers/provider-browser.ts | 24 ++++++++++++++---------- src/providers/provider-jsonrpc.ts | 1 + 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/providers/provider-browser.ts b/src/providers/provider-browser.ts index 87a96b0e..08292a6d 100644 --- a/src/providers/provider-browser.ts +++ b/src/providers/provider-browser.ts @@ -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 @@ -18,9 +19,10 @@ export interface Eip1193Provider { * @param {Object} request - The request object. * @param {string} request.method - The method name. * @param {any[] | Record} [request.params] - The parameters for the method. + * @param {Shard} [request.shard] - The shard to send the request to. * @returns {Promise} The result of the request. */ - request(request: { method: string; params?: Array | Record }): Promise; + request(request: { method: string; params?: Array | Record; shard?: Shard }): Promise; } /** @@ -58,7 +60,7 @@ export type DebugEventBrowserProvider = * @extends JsonRpcApiProvider */ export class BrowserProvider extends JsonRpcApiProvider { - #request: (method: string, params: Array | Record) => Promise; + #request: (method: string, params: Array | Record, shard?: Shard) => Promise; /** * Connect to the `ethereum` provider, optionally forcing the `network`. @@ -72,8 +74,8 @@ export class BrowserProvider extends JsonRpcApiProvider { if (this.initResolvePromise) this.initResolvePromise(); - this.#request = async (method: string, params: Array | Record) => { - const payload = { method, params }; + this.#request = async (method: string, params: Array | Record, shard?: Shard) => { + const payload = { method, params, shard }; this.emit('debug', undefined, { action: 'sendEip1193Request', payload }); try { const result = await ethereum.request(payload); @@ -117,11 +119,10 @@ export class BrowserProvider extends JsonRpcApiProvider { * @param {any[] | Record} params - The parameters for the method. * @returns {Promise} The result of the request. */ - async send(method: string, params: Array | Record): Promise { - console.log('BrowserProvider.send', method, params); + async send(method: string, params: Array | Record, shard?: Shard): Promise { await this._start(); - return await super.send(method, params); + return await super.send(method, params, shard); } /** @@ -132,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): Promise> { + async _send( + payload: JsonRpcPayload | Array, + shard?: Shard, + ): Promise> { 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 }, }, ]; } diff --git a/src/providers/provider-jsonrpc.ts b/src/providers/provider-jsonrpc.ts index f61ae8cc..0dadae95 100644 --- a/src/providers/provider-jsonrpc.ts +++ b/src/providers/provider-jsonrpc.ts @@ -157,6 +157,7 @@ export type JsonRpcError = { code: number; message?: string; data?: any; + shard?: Shard; }; };