Requesting signatures and transactions
To request signatures or transactions from a connected wallet, you can either:
- use the wallet's EIP-1193 provider to send JSON-RPC requests to the wallet directly.
- pass the wallet to a library like
viem
,ethers
, orwagmi
. - for the embedded wallet specifically, create a
transactionIntent
from the server andsignMessage
it with the embedded signer (client).
At a high-level, the EIP-1193 provider implements a method called request
that accepts an object with the following fields:
method
: the name of a JSON-RPC request to send to the walletparams
: any parameters to include with the request
Signatures#
The request
method of the EIP-1193 provider can be used to request signatures. First, get the provider:
Then use the provider's request
method with eth_sign
or related methods:
_10const message = "Sign this message";_10const signature = await provider.request({_10 method: 'personal_sign',_10 params: [message, address]_10});
Popular web3 libraries provide convenient methods for requesting signatures:
Library | Method |
---|---|
Ethers | Use the signer's signMessage method |
Wagmi | Use the useSignMessage hook |
Viem | Use the signMessage action |
Transactions#
Get the provider and optionally configure gas policy to make a sponsored transaction:
Then, using the provider's request
method, send a eth_sendTransaction
JSON-RPC to the wallet. In the params
array, include as the first entry an object containing the transaction's parameters, such as to, value, data, gasLimit, maxPriorityFeePerGas, maxFeePerGas, and gasPrice.
_10const transactionRequest = {_10 to: '0xRecipientAddress',_10 value: 100000,_10};_10const txHash = await provider.request({_10 method: 'eth_sendTransaction',_10 params: [transactionRequest],_10});
See these docs for the parameters that can be passed in eth_sendTransaction
.
You do not need to specify from as we populate it from the user's connected wallet, and you can pass either a number, bigint, or a hexadecimal string into the value parameter.
Popular web3 libraries provide methods for sending transactions:
Library | Method |
---|---|
Ethers | Use the signer's sendTransaction method. |
Wagmi | Use the useSendTransaction hook. |
Viem | sendTransaction action |
Smart Contracts#
Calling a smart contract is a special case of sending a transaction. To call a smart contract, you should send a transaction with the following parameters:
to
: the address of the smart contract to calldata
: the encoded function call datavalue
: any value to send (for payable functions)
To prepare the calldata for a smart contract interaction, we recommend using viem's encodeFunctionData method, like so:
_10import { encodeFunctionData } from 'viem';_10_10_10const data = encodeFunctionData({_10 abi: contractAbi,_10 functionName: 'methodName',_10 args: [arg1, arg2]_10});
You can then send a transaction from the wallet as normal, and pass the calldata in the data
field of the transaction request:
_10const transactionRequest = {_10 to: '0xTheContractAddress',_10 data: data,_10 value: 100000, // Only necessary for payable methods_10};_10const txHash = await provider.request({_10 method: 'eth_sendTransaction',_10 params: [transactionRequest]_10});
Batched transactions#
Smart wallets support sending a batch of transactions in a single, atomic submission to the network.
To send a batched transactions with a smart wallet, call the wallet_sendCalls
method with a calls array the transactions to batch together.
As an example, you might batch together a transaction to approve a USDC spender and to transfer USDC like so:
_32const transactionRequest = {_32 to: '0xTheContractAddress',_32 data: data,_32 value: 100000, // Only necessary for payable methods_32};_32const txHash = await provider.request({_32 method: 'wallet_sendCalls',_32 params: [_32 {_32 calls: [_32 // Approve transaction_32 {_32 to: USDC_ADDRESS,_32 data: encodeFunctionData({_32 abi: USDC_ABI,_32 functionName: 'approve',_32 args: ['insert-spender-address', BigInt(1e6)],_32 }),_32 },_32 // Transfer transaction_32 {_32 to: USDC_ADDRESS,_32 data: encodeFunctionData({_32 abi: USDC_ABI,_32 functionName: 'transfer',_32 args: ['insert-recipient-address', BigInt(1e6)],_32 }),_32 },_32 ],_32 },_32 ],_32});
Popular web3 libraries provide methods for sending batched transactions:
Library | Method |
---|---|
Wagmi | Use the useWriteContracts hook. |
Viem | sendCalls action |