Overview
This example demonstrates how to:- Connect wallet with wagmi in React
- Check deposit approval status
- Approve deposit tokens when needed
- Execute deposit transactions with slippage protection
- Create custom React hooks for reusable deposit logic
- Handle loading states and transaction confirmation tracking
- Display error messages and transaction results
Prerequisites
Before running this example, ensure you have:- Node.js >= 20 installed
- wagmi >= 2.0.0 and viem >= 2.0.0 installed
- @paxoslabs/earn-sdk installed
- @tanstack/react-query installed
- Modern web browser with Web3 wallet extension (MetaMask, Rainbow, etc.)
- ETH for gas fees
- Deposit tokens (e.g., USDC)
Complete Code Example
Custom Hook: useVaultDeposit.ts
First, create a custom hook that encapsulates all deposit logic:
Component: DepositComponent.tsx
Now create the React component that uses the custom hook:
What This Code Does
Custom Hook (useVaultDeposit)
The custom hook encapsulates all deposit logic and provides a clean API:
- State Management: Tracks approval status, loading states, errors, and transaction hashes
- Approval Checking:
checkApproval()verifies if tokens are already approved - Token Approval:
approve()prepares and executes approval transaction - Deposit Execution:
deposit()prepares and executes deposit transaction - Transaction Tracking: Uses
useWaitForTransactionReceiptto track confirmations - Reset Functionality:
reset()clears state for new deposits
Component (DepositComponent)
The React component provides the UI and orchestrates the deposit flow:
- Wallet Connection: Uses wagmi’s
useConnectanduseAccounthooks - Form State: Manages deposit configuration (yield type, amount, slippage)
- Approval Workflow: Check approval → Approve if needed → Deposit
- Loading States: Disables buttons and shows loading indicators during transactions
- Error Handling: Displays error messages from hook
- Transaction Results: Shows Etherscan links for approval and deposit transactions
Key Features
Custom Hook Pattern
TheuseVaultDeposit hook provides a reusable, testable pattern for vault deposits:
- Separation of Concerns: Business logic separated from UI
- Reusability: Use the same hook in multiple components
- Testability: Test logic independently from UI
- Type Safety: Full TypeScript support with interfaces
Approval-First Workflow
Loading States
The component shows appropriate loading states during:- Approval checking:
isCheckingApproval - Token approval:
isApprovingToken(includes waiting for confirmation) - Deposit execution:
isDepositing(includes waiting for confirmation)
Error Handling
Errors are caught at the hook level and exposed to components:Slippage Protection
Configuration Options
Change Deposit Amount
Change Yield Type
Change Slippage Tolerance
Use Different Token
Add More Chains
Update your wagmi config to support additional chains:Troubleshooting
”Connect Wallet” button not working
Possible causes:- No Web3 wallet extension installed
- Wallet extension disabled or locked
- Install MetaMask, Rainbow, or another Web3 wallet
- Unlock your wallet extension
- Refresh the page and try again
”Approval transaction failed”
Possible causes:- Insufficient ETH for gas fees
- Incorrect deposit token address
- Wrong network selected in wallet
- Check ETH balance for gas fees
- Verify deposit token address is correct
- Ensure wallet is connected to Ethereum mainnet
”Deposit transaction failed”
Possible causes:- Approval not successful
- Insufficient deposit tokens
- Slippage exceeded
- Wait for approval confirmation before depositing
- Check token balance in wallet
- Increase slippage tolerance if needed
Wagmi hook errors
Possible causes:- Wagmi or React Query providers not configured
- Incompatible wagmi version
- Missing chains in wagmi config
- Ensure
WagmiProviderandQueryClientProviderwrap your app - Use wagmi >= 2.0.0
- Add chains to wagmi config that you’re using
Next Steps
- Viem Example: See the viem deposit example for vanilla TypeScript integration
- Withdrawal Flow: See the wagmi withdrawal example for withdrawal patterns
- API Documentation: Read the deposits API documentation for detailed parameter descriptions