Skip to content

custom

The custom Transport connects to a JSON-RPC API via custom. Wraps Viem's custom Transport.

Import

ts
import { custom } from '@wagmi/core'

Usage

ts
import { 
  createConfig, 
  custom
} from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { customRpc } from './rpc'

export const config = createConfig({
  chains: [mainnet],
  connectors: [injected()],
  transports: {
    [mainnet.id]: custom({ 
      async request({ method, params }) { 
        const response = await customRpc.request(method, params)
        return response
      } 
    }) 
  },
})

Example: Conditional RPCs

ts
import { http, custom } from "viem";
const rpc1 = http(`https://rpc1.url`)({});
const rpc2 = http(`https://rpc2.url`)({});

export const combinedTransport = custom({
	request: (request) => {
		return request.method === "eth_sendRawTransaction"
			? rpc1(request)
			: rpc2(request);
	},
});
ts
import { createConfig } from "@wagmi/core";
import { mainnet } from "@wagmi/core/chains";
import { combinedTransport } from "./customRpc.ts";
import { injected } from "@wagmi/connectors";

export const config = createConfig({
	chains: [mainnet],
	connectors: [injected()],
	transports: {
		[mainnet.id]: combinedTransport,
	},
});

Parameters

provider

{ request({ method: string, params: unknown[] }): Promise<unknown> }

An EIP-1193 request function function.

ts
import { customRpc } from './rpc'

const transport = custom({
  async request({ method, params }) { 
    const response = await customRpc.request(method, params)
    return response
  }
})

key (optional)

string

A key for the Transport. Defaults to "custom".

ts
const transport = custom(
  provider,
  { 
    key: 'windowProvider', 
  }
)

name (optional)

string

A name for the Transport. Defaults to "Ethereum Provider".

ts
const transport = custom(
  provider,
  { 
    name: 'Window Ethereum Provider', 
  }
)

retryCount (optional)

number

The max number of times to retry when a request fails. Defaults to 3.

ts
const transport = custom(provider, {
  retryCount: 5, 
})

retryDelay (optional)

number

The base delay (in ms) between retries. By default, the Transport will use exponential backoff (~~(1 << count) * retryDelay), which means the time between retries is not constant.

ts
const transport = custom(provider, {
  retryDelay: 100, 
})

Released under the MIT License.