Skip to main content
Complete deposit flow example using wagmi (React hooks) and the Earn SDK.

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:
  1. State Management: Tracks approval status, loading states, errors, and transaction hashes
  2. Approval Checking: checkApproval() verifies if tokens are already approved
  3. Token Approval: approve() prepares and executes approval transaction
  4. Deposit Execution: deposit() prepares and executes deposit transaction
  5. Transaction Tracking: Uses useWaitForTransactionReceipt to track confirmations
  6. Reset Functionality: reset() clears state for new deposits

Component (DepositComponent)

The React component provides the UI and orchestrates the deposit flow:
  1. Wallet Connection: Uses wagmi’s useConnect and useAccount hooks
  2. Form State: Manages deposit configuration (yield type, amount, slippage)
  3. Approval Workflow: Check approval → Approve if needed → Deposit
  4. Loading States: Disables buttons and shows loading indicators during transactions
  5. Error Handling: Displays error messages from hook
  6. Transaction Results: Shows Etherscan links for approval and deposit transactions

Key Features

Custom Hook Pattern

The useVaultDeposit hook provides a reusable, testable pattern for vault deposits:
Benefits:
  • 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)
Buttons are disabled during these operations to prevent duplicate transactions.

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
Solutions:
  1. Install MetaMask, Rainbow, or another Web3 wallet
  2. Unlock your wallet extension
  3. 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
Solutions:
  1. Check ETH balance for gas fees
  2. Verify deposit token address is correct
  3. Ensure wallet is connected to Ethereum mainnet

”Deposit transaction failed”

Possible causes:
  • Approval not successful
  • Insufficient deposit tokens
  • Slippage exceeded
Solutions:
  1. Wait for approval confirmation before depositing
  2. Check token balance in wallet
  3. 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
Solutions:
  1. Ensure WagmiProvider and QueryClientProvider wrap your app
  2. Use wagmi >= 2.0.0
  3. Add chains to wagmi config that you’re using

Next Steps