Skip to content

Commit 69be175

Browse files
committed
wip: update JsonRpcRequest typings
1 parent 1fda0ff commit 69be175

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

app/core/RPCMethods/RPCMethodMiddleware.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import {
2-
JsonRpcEngine,
3-
JsonRpcMiddleware,
4-
} from '@metamask/json-rpc-engine';
5-
import type { Json, JsonRpcFailure, JsonRpcParams, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccess, } from '@metamask/utils';
6-
import type { Transaction } from '@metamask/transaction-controller';
1+
import { JsonRpcEngine, JsonRpcMiddleware } from '@metamask/json-rpc-engine';
2+
import type {
3+
Json,
4+
JsonRpcFailure,
5+
JsonRpcParams,
6+
JsonRpcRequest,
7+
JsonRpcResponse,
8+
JsonRpcSuccess,
9+
} from '@metamask/utils';
710
import type { ProviderConfig } from '@metamask/network-controller';
811
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
912
import Engine from '../Engine';
@@ -827,7 +830,7 @@ describe('getRpcMethodMiddleware', () => {
827830
it('returns a JSON-RPC error if an error is thrown when adding this transaction', async () => {
828831
// Omit `from` and `chainId` here to skip validation for simplicity
829832
// Downcast needed here because `from` is required by this type
830-
const mockTransactionParameters = {} as JsonRpcParams
833+
const mockTransactionParameters = {} as JsonRpcParams;
831834
// Transaction fails before returning a result
832835
mockAddTransaction.mockImplementation(async () => {
833836
throw new Error('Failed to add transaction');
@@ -840,7 +843,7 @@ describe('getRpcMethodMiddleware', () => {
840843
jsonrpc,
841844
id: 1,
842845
method: 'eth_sendTransaction',
843-
params: [mockTransactionParameters]
846+
params: [mockTransactionParameters],
844847
};
845848
const expectedError = rpcErrors.internal('Failed to add transaction');
846849

@@ -857,7 +860,7 @@ describe('getRpcMethodMiddleware', () => {
857860
it('returns a JSON-RPC error if an error is thrown after approval', async () => {
858861
// Omit `from` and `chainId` here to skip validation for simplicity
859862
// Downcast needed here because `from` is required by this type
860-
const mockTransactionParameters = {} as JsonRpcParams
863+
const mockTransactionParameters = {} as JsonRpcParams;
861864
setupGlobalState({
862865
addTransactionResult: Promise.reject(
863866
new Error('Failed to process transaction'),

app/core/RPCMethods/eth_sendTransaction.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// eslint-disable-next-line import/no-nodejs-modules
22
import { inspect } from 'util';
3-
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
3+
import type {
4+
Json,
5+
JsonRpcParams,
6+
JsonRpcRequest,
7+
PendingJsonRpcResponse,
8+
} from '@metamask/utils';
49
import type {
510
Transaction,
611
TransactionController,
@@ -40,8 +45,8 @@ jest.mock('../../util/transaction-controller', () => ({
4045
* @returns The JSON-RPC request.
4146
*/
4247
function constructSendTransactionRequest(
43-
params: unknown,
44-
): JsonRpcRequest<unknown> & { method: 'eth_sendTransaction' } {
48+
params: JsonRpcParams,
49+
): JsonRpcRequest<JsonRpcParams> & { method: 'eth_sendTransaction' } {
4550
return {
4651
jsonrpc: '2.0',
4752
id: 1,
@@ -55,7 +60,7 @@ function constructSendTransactionRequest(
5560
*
5661
* @returns A pending JSON-RPC response.
5762
*/
58-
function constructPendingJsonRpcResponse(): PendingJsonRpcResponse<unknown> {
63+
function constructPendingJsonRpcResponse(): PendingJsonRpcResponse<Json> {
5964
return {
6065
jsonrpc: '2.0',
6166
id: 1,
@@ -136,13 +141,17 @@ function getMockAddTransaction({
136141
describe('eth_sendTransaction', () => {
137142
it('sends the transaction and returns the resulting hash', async () => {
138143
const mockAddress = '0x0000000000000000000000000000000000000001';
139-
const mockTransactionParameters = { from: mockAddress };
144+
const mockTransactionParameters = {
145+
from: mockAddress,
146+
} as Transaction;
140147
const expectedResult = 'fake-hash';
141148
const pendingResult = constructPendingJsonRpcResponse();
142149

143150
await eth_sendTransaction({
144151
hostname: 'example.metamask.io',
145-
req: constructSendTransactionRequest([mockTransactionParameters]),
152+
req: constructSendTransactionRequest([
153+
mockTransactionParameters as unknown as JsonRpcParams,
154+
]),
146155
res: pendingResult,
147156
sendTransaction: getMockAddTransaction({
148157
expectedTransaction: mockTransactionParameters,

app/core/RPCMethods/eth_sendTransaction.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
1+
import type {
2+
Hex,
3+
Json,
4+
JsonRpcParams,
5+
JsonRpcRequest,
6+
PendingJsonRpcResponse,
7+
} from '@metamask/utils';
28
import {
39
TransactionController,
10+
Transaction,
411
WalletDevice,
512
} from '@metamask/transaction-controller';
613
import { rpcErrors } from '@metamask/rpc-errors';
@@ -46,10 +53,10 @@ const hasProperty = <
4653
): objectToCheck is ObjectToCheck & Record<Property, unknown> =>
4754
Object.hasOwnProperty.call(objectToCheck, name);
4855

49-
type SendArgs = {
56+
interface SendArgs {
5057
from: string;
51-
chainId?: number;
52-
};
58+
chainId?: Hex;
59+
}
5360

5461
/**
5562
* Handle a `eth_sendTransaction` request.
@@ -71,7 +78,9 @@ async function eth_sendTransaction({
7178
validateAccountAndChainId,
7279
}: {
7380
hostname: string;
74-
req: JsonRpcRequest<SendArgs[]> & { method: 'eth_sendTransaction' };
81+
req: JsonRpcRequest<[Transaction & JsonRpcParams]> & {
82+
method: 'eth_sendTransaction';
83+
};
7584
res: PendingJsonRpcResponse<Json>;
7685
sendTransaction: TransactionController['addTransaction'];
7786
validateAccountAndChainId: (args: SendArgs) => Promise<void>;

app/core/RPCMethods/wallet_watchAsset.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
} from '../../constants/error';
1212
import { selectChainId } from '../../selectors/networkController';
1313
import { isValidAddress } from 'ethereumjs-util';
14-
import { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/json-rpc-engine';
14+
import {
15+
JsonRpcRequest,
16+
PendingJsonRpcResponse,
17+
} from '@metamask/json-rpc-engine';
1518

1619
const wallet_watchAsset = async ({
1720
req,

app/core/SanitizationMiddleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export const permittedKeys = [
4040
* @param parameter - The parameter to sanitize.
4141
* @returns The given parameter containing just permitted keys.
4242
*/
43-
function sanitizeRpcParameter(parameter: Record<PropertyKey, unknown>): Record<string, Json> {
43+
function sanitizeRpcParameter(
44+
parameter: Record<PropertyKey, unknown>,
45+
): Record<string, Json> {
4446
return permittedKeys.reduce<Record<string, Json>>((copy, permitted) => {
4547
if (permitted in parameter) {
4648
const value = parameter[permitted];

0 commit comments

Comments
 (0)