1- import type { EthereumRpcError } from 'eth-rpc-errors' ;
2- import { ethErrors } from 'eth-rpc-errors' ;
3- import type { JsonRpcMiddleware , JsonRpcRequest } from 'json-rpc-engine' ;
4- import { createAsyncMiddleware } from 'json-rpc-engine' ;
1+ import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine' ;
2+ import { createAsyncMiddleware } from '@metamask/json-rpc-engine' ;
3+ import type { JsonRpcError , DataWithOptionalCause } from '@metamask/rpc-errors' ;
4+ import { rpcErrors } from '@metamask/rpc-errors' ;
5+ import type { Json , JsonRpcParams , JsonRpcRequest } from '@metamask/utils' ;
56
67import type { Block } from './types' ;
78import { timeout } from './utils/timeout' ;
@@ -17,7 +18,7 @@ const RETRIABLE_ERRORS: string[] = [
1718 'Failed to fetch' ,
1819] ;
1920
20- export interface PayloadWithOrigin extends JsonRpcRequest < unknown > {
21+ export interface PayloadWithOrigin extends JsonRpcRequest {
2122 origin ?: string ;
2223}
2324interface Request {
@@ -53,7 +54,7 @@ export function createFetchMiddleware({
5354 fetch : typeof global . fetch ;
5455 rpcUrl : string ;
5556 originHttpHeaderKey ?: string ;
56- } ) : JsonRpcMiddleware < unknown , unknown > {
57+ } ) : JsonRpcMiddleware < JsonRpcParams , Json > {
5758 return createAsyncMiddleware ( async ( req , res , _next ) => {
5859 const { fetchUrl, fetchParams } = createFetchConfigFromReq ( {
5960 btoa,
@@ -104,7 +105,7 @@ function checkForHttpErrors(fetchRes: Response): void {
104105 // check for errors
105106 switch ( fetchRes . status ) {
106107 case 405 :
107- throw ethErrors . rpc . methodNotFound ( ) ;
108+ throw rpcErrors . methodNotFound ( ) ;
108109
109110 case 418 :
110111 throw createRatelimitError ( ) ;
@@ -121,15 +122,15 @@ function checkForHttpErrors(fetchRes: Response): void {
121122function parseResponse ( fetchRes : Response , body : Record < string , Block > ) : Block {
122123 // check for error code
123124 if ( fetchRes . status !== 200 ) {
124- throw ethErrors . rpc . internal ( {
125+ throw rpcErrors . internal ( {
125126 message : `Non-200 status code: '${ fetchRes . status } '` ,
126127 data : body ,
127128 } ) ;
128129 }
129130
130131 // check for rpc error
131132 if ( body . error ) {
132- throw ethErrors . rpc . internal ( {
133+ throw rpcErrors . internal ( {
133134 data : body . error ,
134135 } ) ;
135136 }
@@ -165,7 +166,7 @@ export function createFetchConfigFromReq({
165166
166167 // prepare payload
167168 // copy only canonical json rpc properties
168- const payload : JsonRpcRequest < unknown > = {
169+ const payload : JsonRpcRequest = {
169170 id : req . id ,
170171 jsonrpc : req . jsonrpc ,
171172 method : req . method ,
@@ -215,12 +216,12 @@ function normalizeUrlFromParsed(parsedUrl: URL): string {
215216 return result ;
216217}
217218
218- function createRatelimitError ( ) : EthereumRpcError < unknown > {
219- return ethErrors . rpc . internal ( { message : `Request is being rate limited.` } ) ;
219+ function createRatelimitError ( ) : JsonRpcError < DataWithOptionalCause > {
220+ return rpcErrors . internal ( { message : `Request is being rate limited.` } ) ;
220221}
221222
222- function createTimeoutError ( ) : EthereumRpcError < unknown > {
223+ function createTimeoutError ( ) : JsonRpcError < DataWithOptionalCause > {
223224 let msg = `Gateway timeout. The request took too long to process. ` ;
224225 msg += `This can happen when querying logs over too wide a block range.` ;
225- return ethErrors . rpc . internal ( { message : msg } ) ;
226+ return rpcErrors . internal ( { message : msg } ) ;
226227}
0 commit comments