Skip to content

Commit

Permalink
Add support for shard parameter in Eip1193Provider request method
Browse files Browse the repository at this point in the history
  • Loading branch information
rileystephens28 committed Sep 11, 2024
1 parent 5496a30 commit dbff840
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
24 changes: 14 additions & 10 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 @@ -72,8 +74,8 @@ export class BrowserProvider extends JsonRpcApiProvider {

if (this.initResolvePromise) this.initResolvePromise();

this.#request = async (method: string, params: Array<any> | Record<string, any>) => {
const payload = { method, params };
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 @@ -117,11 +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> {
console.log('BrowserProvider.send', method, params);
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 @@ -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<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

0 comments on commit dbff840

Please sign in to comment.