Overview
TheuseSendTransaction 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
- Basic Usage
- With UI Customization
To send a transaction, use the
sendTransaction method from the useSendTransaction hook:Parameters
Transaction Object
The first parameter tosendTransaction 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
ThesendTransaction method returns a Promise that resolves with:
string
The hash of the broadcasted transaction.Example:
number
The chain ID where the transaction was sent.
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: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
Always validate user inputs
Always validate user inputs
Validate addresses and amounts before sending transactions:
Check wallet balance before transactions
Check wallet balance before transactions
Verify sufficient balance before attempting to send:
Provide transaction feedback
Provide transaction feedback
Keep users informed throughout the transaction process:
Use proper gas estimation
Use proper gas estimation
Let MoonKey handle gas estimation, but allow overrides when needed:
Handle transaction confirmations
Handle transaction confirmations
Wait for transaction confirmations before considering it final:
Display transaction links
Display transaction links
Help users track their transactions on block explorers:
Troubleshooting
Transaction fails silently
Transaction fails silently
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
User rejected transaction
User rejected transaction
Handle user cancellations gracefully:
Insufficient gas
Insufficient gas
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
Transaction stuck or pending
Transaction stuck or pending
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)
Wrong network
Wrong network
Ensure transactions are sent on the correct network:
Related Documentation
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
- Learn about signing transactions without broadcasting
- Explore contract interactions with viem and ethers
- Understand chain management for multi-network apps
- Customize the transaction UI to match your brand