Skip to content

Commit

Permalink
deps: Update eth-json-rpc and metamask packages (#95)
Browse files Browse the repository at this point in the history
* deps: @metamask/eth-json-rpc-provider@^1.0.0->^2.1.0

* deps: @metamask/utils@^5.0.1->^8.1.0

* deps: rpc-errors@^4.0.3 -> @metamask/rpc-errors@^6.0.0

* deps: json-rpc-engine@^6.1.0 -> @metamask/json-rpc-engine@^7.1.0

* import and type migrations
  • Loading branch information
legobeat authored Sep 21, 2023
1 parent 19a979d commit c4f81ac
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 417 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/eth-json-rpc-provider": "^1.0.0",
"@metamask/utils": "^5.0.1",
"eth-rpc-errors": "^4.0.3",
"json-rpc-engine": "^6.1.0",
"@metamask/eth-json-rpc-provider": "^2.1.0",
"@metamask/json-rpc-engine": "^7.1.1",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/utils": "^8.1.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {
Expand Down
35 changes: 21 additions & 14 deletions src/create-infura-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { EthereumRpcError } from 'eth-rpc-errors';
import { ethErrors } from 'eth-rpc-errors';
import { createAsyncMiddleware } from 'json-rpc-engine';
import type { PendingJsonRpcResponse } from 'json-rpc-engine';
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
import type { JsonRpcError } from '@metamask/rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';
import type {
Json,
JsonRpcParams,
PendingJsonRpcResponse,
} from '@metamask/utils';
// eslint-disable-next-line @typescript-eslint/no-shadow
import fetch from 'node-fetch';

Expand Down Expand Up @@ -33,7 +37,7 @@ const RETRIABLE_ERRORS = [
];

/**
* Builds [`json-rpc-engine`](https://github.com/MetaMask/json-rpc-engine)-compatible middleware designed
* Builds [`@metamask/json-rpc-engine`](https://github.com/MetaMask/@metamask/json-rpc-engine)-compatible middleware designed
* for interfacing with Infura's JSON-RPC endpoints.
* @param opts - The options.
* @param opts.network - A network that Infura supports; plugs into
Expand All @@ -44,7 +48,7 @@ const RETRIABLE_ERRORS = [
* by Infura for analytics purposes.
* @param opts.projectId - The Infura project id.
* @param opts.headers - Extra headers that will be used to make the request.
* @returns The `json-rpc-engine`-compatible middleware.
* @returns The `@metamask/json-rpc-engine`-compatible middleware.
*/
export function createInfuraMiddleware({
network = 'mainnet',
Expand Down Expand Up @@ -81,6 +85,7 @@ export function createInfuraMiddleware({
headers,
req,
);

await performFetch(network, projectId, headers, req, res, source);
// request was successful
break;
Expand Down Expand Up @@ -146,8 +151,8 @@ async function performFetch(
network: InfuraJsonRpcSupportedNetwork,
projectId: string,
extraHeaders: RequestHeaders,
req: ExtendedJsonRpcRequest<unknown>,
res: PendingJsonRpcResponse<unknown>,
req: ExtendedJsonRpcRequest<JsonRpcParams>,
res: PendingJsonRpcResponse<Json>,
source: string | undefined,
): Promise<void> {
const { fetchUrl, fetchParams } = fetchConfigFromReq({
Expand All @@ -163,7 +168,7 @@ async function performFetch(
if (!response.ok) {
switch (response.status) {
case 405:
throw ethErrors.rpc.methodNotFound();
throw rpcErrors.methodNotFound();

case 429:
throw createRatelimitError();
Expand All @@ -179,7 +184,9 @@ async function performFetch(

// special case for now
if (req.method === 'eth_getBlockByNumber' && rawData === 'Not Found') {
res.result = null;
// TODO Would this be more correct?
// delete res.result;
res.result = null as any as JsonRpcParams;
return;
}

Expand All @@ -196,7 +203,7 @@ async function performFetch(
* error.
* @returns The error object.
*/
function createRatelimitError(): EthereumRpcError<undefined> {
function createRatelimitError(): JsonRpcError<undefined> {
const msg = `Request is being rate limited.`;
return createInternalError(msg);
}
Expand All @@ -205,7 +212,7 @@ function createRatelimitError(): EthereumRpcError<undefined> {
* Builds a JSON-RPC 2.0 internal error object describing a timeout error.
* @returns The error object.
*/
function createTimeoutError(): EthereumRpcError<undefined> {
function createTimeoutError(): JsonRpcError<undefined> {
let msg = `Gateway timeout. The request took too long to process. `;
msg += `This can happen when querying logs over too wide a block range.`;
return createInternalError(msg);
Expand All @@ -216,8 +223,8 @@ function createTimeoutError(): EthereumRpcError<undefined> {
* @param msg - The message.
* @returns The error object.
*/
function createInternalError(msg: string): EthereumRpcError<undefined> {
return ethErrors.rpc.internal(msg);
function createInternalError(msg: string): JsonRpcError<undefined> {
return rpcErrors.internal(msg);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/create-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import { JsonRpcEngine } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';

import type { CreateInfuraMiddlewareOptions } from './create-infura-middleware';
import { createInfuraMiddleware } from './create-infura-middleware';
Expand Down
8 changes: 4 additions & 4 deletions src/fetch-config-from-req.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JsonRpcRequest } from 'json-rpc-engine';
import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';

import type {
ExtendedJsonRpcRequest,
Expand Down Expand Up @@ -41,7 +41,7 @@ export function fetchConfigFromReq({
network: InfuraJsonRpcSupportedNetwork;
projectId: string;
extraHeaders?: RequestHeaders;
req: ExtendedJsonRpcRequest<unknown>;
req: ExtendedJsonRpcRequest<JsonRpcParams>;
source?: string;
}): FetchConfig {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Expand Down Expand Up @@ -72,8 +72,8 @@ export function fetchConfigFromReq({
* @returns An object that describes a JSON-RPC request.
*/
function normalizeReq(
req: ExtendedJsonRpcRequest<unknown>,
): JsonRpcRequest<unknown> {
req: ExtendedJsonRpcRequest<JsonRpcParams>,
): JsonRpcRequest {
return {
id: req.id,
jsonrpc: req.jsonrpc,
Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { JsonRpcRequest } from 'json-rpc-engine';
import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';

export type RequestHeaders = Record<string, string>;

export type ExtendedJsonRpcRequest<Params> = JsonRpcRequest<Params> & {
origin?: string;
};
export type ExtendedJsonRpcRequest<Params extends JsonRpcParams> =
JsonRpcRequest<Params> & { origin?: string };

/**
* These are networks:
Expand Down
Loading

0 comments on commit c4f81ac

Please sign in to comment.