EVM Provider
After setting up your wallet, you can access the EVM provider:
import { useTomo, getWalletState } from '@tomo-inc/tomo-web-sdk';
// react
const { providers } = useTomo()
const { ethereumProvider } = providers;
// pure js
const ethereumProvider = window.tomo_ethereum
Switch Chain ID
You can switch chains between different EVM networks (see Supported Chains for the enabled network list). By default, you use the Ethereum mainnet.
//interface
switchChain(chainId: string): Promise<void>
getChainId(): number | string
const chainId = '1'
// react
const current = await ethereumProvider.getChainId();
const res = await ethereumProvider.switchChain(chainId);
// pure js
const current = await window.tomo_ethereum.getChainId();
const res = await window.tomo_ethereum.switchChain(chainId);
Get the EVM address
The EVM address from the wallet state or provider.
import { useTomo, getWalletState } from '@tomo-inc/tomo-web-sdk';
const { walletState, providers } = useTomo()
// get from wallet state
const address = walletState.address
// or get from provider
const address = await providers.ethereumProvider.request({ method: "eth_accounts" })?.[0]
/** pure js */
// get from wallet state
const address = getWalletState().address
// or get from provider
const address = await window.tomo_ethereum.request({ method: "eth_accounts" })?.[0]
Provider Functions
providers.ethereumProvider
exposes three functions for sending requests to the user's wallet:
Signing a Message
It uses the Ethereum provider to sign a plaintext offchain message, which returns the message's signature with the message header. We also provide a shortcut for sign type data where the msg
is a json string of the data.
// interface
signMessage(msg: string, address:string): Promise<string>
signTypedData(address: string, msg: string): Promise<string>
// react
const personalSignResult = await ethereumProvider.request(msg, address);
// pure js
const personalSignResult = await window.tomo_ethereum.request(msg, address);
Send Transaction
You can also send the transaction directly through this sendTransaction
method. The return value is the transaction signature if signed and sent correctly.
// interface
sendTransaction(params: EvmTxParams): Promise<any>
interface EvmTxParams {
from: string;
to: string;
value: string;
gasPrice?: string;
gasLimit?: string;
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
data?: string;
}
const Tx= {
from: address,
to: address,
value: "0x5af3107a4000",
nonce: "0x0",
},
// react
const res = await ethereumProvider.sendTransaction(Tx);
// pure js
const res = await window.tomo_ethereum.sendTransaction(Tx);
Requests
We also support the standard wallet JSON RPC API for wallet actions
//interface
request<Result = any, Params = any>(request: RequestArguments<Params>, context?: any): Promise<Result>
interface RequestArguments<T = any> {
method: string;
params?: T;
id?: number;
}
const walletActions = {
eth_requestAccounts: "eth_requestAccounts",
eth_accounts: "eth_accounts",
wallet_watchAsset: "wallet_watchAsset",
eth_addEthereumChain: "eth_addEthereumChain",
eth_switchEthereumChain: "eth_switchEthereumChain",
wallet_switchEthereumChain: "wallet_switchEthereumChain",
personal_sign: "personal_sign",
eth_signTypedData_v4: "eth_signTypedData_v4",
wallet_getPermissions: "wallet_getPermissions",
wallet_requestPermissions: "wallet_requestPermissions",
eth_sendTransaction: "eth_sendTransaction",
eth_sendRawTransaction: "eth_sendRawTransaction",
personal_ecRecover: "personal_ecRecover",
eth_swapTransaction: "eth_swapTransaction",
eth_approveTransaction: "eth_approveTransaction",
eth_chainId: "eth_chainId",
};
const data = {
method: "eth_sendTransaction",
params: [
{
from: address,
to: address,
value: "0x5af3107a4000",
nonce: "0x0",
},
],
}
// react
const res = await ethereumProvider.request(data);
// pure js
const res = await window.tomo_ethereum.request(data);
Last updated