Skip to main content
Send transactions from a user’s embedded Ethereum wallet to interact with smart contracts, transfer ETH, or execute any onchain action.

Overview

The useSendTransaction hook from MoonKey’s React SDK provides a simple interface for sending transactions from embedded wallets. It handles transaction preparation, signing, and broadcasting to the network.
MoonKey automatically handles transaction population (gas estimation, nonce management, etc.) and prompts users for confirmation before broadcasting.

React SDK

To send a transaction, use the sendTransaction method from the useSendTransaction hook:

Parameters

Transaction Object

The first parameter to sendTransaction is the transaction object:
string
required
The recipient address for the transaction.Example:
string
The amount of ETH to send in wei, as a hexadecimal string. Omit for contract interactions with no ETH transfer.Example:
string
The encoded function call data for contract interactions, as a hexadecimal string.Example:
number
The chain ID for the transaction. If not provided, uses the wallet’s current chain.Example:
string
Optional gas limit as a hexadecimal string. MoonKey estimates gas automatically if not provided.Example:
string
Optional maximum fee per gas for EIP-1559 transactions, as a hexadecimal string.Example:
string
Optional maximum priority fee per gas for EIP-1559 transactions, as a hexadecimal string.Example:
number
Optional nonce for the transaction. MoonKey determines this automatically if not provided.Example:

Options Object

The second parameter is an optional configuration object:
object
Configuration for the transaction confirmation modal UI.
Wallet
Specific wallet to use for the transaction. If not provided, uses the user’s default wallet.Example:

Returns

The sendTransaction method returns a Promise that resolves with:
string
The hash of the broadcasted transaction.Example:
number
The chain ID where the transaction was sent.
A successful return indicates the transaction was broadcasted to the network. It may still fail during confirmation. Always verify transaction status onchain or listen for confirmation events.

Complete Examples

Basic ETH Transfer

ERC-20 Token Transfer

UI Customization

Customize the transaction confirmation modal to match your application’s branding:

Hide Modal

For certain flows, you may want to send transactions without showing the UI modal:
Hiding the confirmation modal removes the user’s ability to review transaction details before signing. Only use this for trusted operations or after obtaining explicit user consent.

Working with Viem

MoonKey’s embedded wallets are compatible with viem, allowing you to leverage its powerful utilities:

Error Handling

Handle common transaction errors gracefully:

Best Practices

Validate addresses and amounts before sending transactions:
Verify sufficient balance before attempting to send:
Keep users informed throughout the transaction process:
Let MoonKey handle gas estimation, but allow overrides when needed:
Wait for transaction confirmations before considering it final:

Troubleshooting

If transactions fail without clear errors:
  • Check that the user’s wallet has sufficient balance
  • Verify the recipient address is valid
  • Ensure the contract address is correct for contract interactions
  • Check that the encoded data is properly formatted
  • Try on a testnet first to debug
Handle user cancellations gracefully:
If transactions fail due to gas issues:
  • MoonKey usually handles gas estimation automatically
  • For complex transactions, consider providing a custom gas limit
  • Ensure the wallet has enough ETH for both value + gas
If a transaction appears stuck:
  • Check the network status (congestion, downtime)
  • Verify the transaction on a block explorer
  • Consider the gas price used - low gas prices can cause delays
  • Wait for network confirmation (can take minutes during congestion)
Ensure transactions are sent on the correct network:

Sign Transaction

Sign transactions without broadcasting

Sign Message

Sign arbitrary messages

Send Transaction UI

Customize the transaction confirmation UI

Switch Chains

Switch between Ethereum networks

Next Steps