Appearance
Custom Methods
Common for all custom methods is the need for a custom Viem Client that can call JSON RPC methods. You can either extend a custom Client you may already be using, or use the provided code snippet.
Code Examples
Below is an example schema that includes both custom methods and legacy methods along with the required types for calling the exchange rate API:
typescript
import { Address, Hex, WalletRpcSchema } from "viem";
type CustomMethodRpcSchema = [
{
Method: "minipay_getExchangeRate";
Parameters: [from: string, to: string];
ReturnType: number;
},
];
type LegacyRpcSchema = [
{
Method: "eth_signTypedData_v3";
Parameters: [address: Address, message: string];
ReturnType: Hex;
},
];
export type MiniPayRpcSchema = [...WalletRpcSchema, ...LegacyRpcSchema, ...CustomMethodRpcSchema];
Once you have defined your custom RPC schema, you can create a hook that will provide the custom client for calling the methods defined in your schema:
typescript
const useMiniPayClient = (config: Config): WalletClient<Transport, Chain, undefined, MiniPayRpcSchema> | null => {
const chainId = useChainId({ config });
const chain = config.chains[chainId];
if (typeof window === "undefined" || typeof window.ethereum === "undefined") {
return { client: null };
}
const client = createWalletClient({
chain: chain,
transport: custom(window.ethereum),
rpcSchema: rpcSchema<MiniPayRpcSchema>(),
});
return { client };
};
Once the hook is set up, you can simply call it in your components to get access to the client that can invoke the custom methods:
typescript
const { client } = useMiniPayClient(config);
Example Methods
Currently there are only one custom method: