diff --git a/src/json-rpc-provider-types.test-d.ts b/src/json-rpc-provider-types.test-d.ts index 3891c8142..f616d72bb 100644 --- a/src/json-rpc-provider-types.test-d.ts +++ b/src/json-rpc-provider-types.test-d.ts @@ -1,12 +1,34 @@ -import { expectAssignable, expectNotAssignable } from 'tsd'; -import EthQuery from '@metamask/eth-query'; import { Web3Provider } from '@ethersproject/providers'; +import EthQuery from '@metamask/eth-query'; +import { expectAssignable, expectNotAssignable } from 'tsd'; -import type { - LegacyEthereumProvider -} from '.'; +import type { JsonRpcRequest, LegacyEthereumProvider } from '.'; // Known legacy providers - expectAssignable(new EthQuery({} as any)); expectAssignable(new Web3Provider({} as any)); +expectAssignable({ + send: async (method: string, params: string[]) => + Promise.resolve([method, params]), +}); +expectAssignable({ + // eslint-disable-next-line @typescript-eslint/no-empty-function + send: (_req: JsonRpcRequest, _cb: () => void) => {}, +}); +expectAssignable({ + send: async (req: JsonRpcRequest, _cb: (_x: null, _result: null) => void) => + Promise.resolve(req), +}); + +expectNotAssignable({ foo: '123' }); +expectNotAssignable({ send: '123' }); + +expectNotAssignable({ + send: (method: string, params: string[]) => [method, params], +}); +expectNotAssignable({ + send: async ( + req: JsonRpcRequest, + _cb: (_x: null, _result: undefined) => void, + ) => Promise.resolve(req), +}); diff --git a/src/json-rpc-provider-types.ts b/src/json-rpc-provider-types.ts index a6bd68e70..1f9d97b28 100644 --- a/src/json-rpc-provider-types.ts +++ b/src/json-rpc-provider-types.ts @@ -33,20 +33,27 @@ export type EIP1993Provider = SafeEventEmitter & { * [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193). It is not compliant with * any Ethereum provider standard. */ -export type LegacyEthereumProvider = LegacyEthersProvider | LegacyEthJsQueryProvider | LegacyWeb3Provider; +export type LegacyEthereumProvider = + | LegacyEthersProvider + | LegacyEthJsQueryProvider + | LegacyWeb3Provider; -type LegacyWeb3Provider = { +type LegacyEthersProvider = { /** - * Send a provider request asynchronously. + * Send a provider request asynchronously. (ethers v5 Web3Provider) * - * @param req - The request to send. - * @param callback - A function that is called upon the success or failure of the request. + * @param method - The RPC method to call. + * @param params - Array with method parameters. + * @returns A promise resolving with the result of the RPC call, or rejecting on failure. */ - send?( - req: Partial, - callback: SendAsyncCallback, - ): void; -} + send(method: string, params: any[]): Promise; + /* + send( + method: string, + params: any[], + ): Promise; + */ +}; type LegacyEthJsQueryProvider = { /** @@ -55,24 +62,20 @@ type LegacyEthJsQueryProvider = { * @param req - The request to send. * @param callback - A function that is called upon the success or failure of the request. */ - sendAsync?( + sendAsync( req: Partial, callback: SendAsyncCallback, ): void; }; -type LegacyEthersProvider = { +type LegacyWeb3Provider = { /** - * Send a provider request asynchronously. (ethers v5 Web3Provider) + * Send a provider request asynchronously. * - * @param method - The RPC method to call. - * @param params - Array with method parameters. - * @returns - A promise resolving with the result of the RPC call, or rejecting on failure. + * @param req - The request to send. + * @param callback - A function that is called upon the success or failure of the request. */ - send?( - method: string, - params: any[], - ): Promise; + send(req: Partial, callback: SendAsyncCallback): void; }; type SendAsyncCallback = (