Overview
This example demonstrates how to:- Check deposit approval status before depositing
- Approve deposit tokens when needed
- Execute deposit transactions with slippage protection
- Handle errors gracefully
- Use automatic vault discovery based on yield type and deposit token
Prerequisites
Before running this example, ensure you have:- Node.js >= 20 installed
- viem >= 2.0.0 installed
- @paxoslabs/earn-sdk installed
- Ethereum RPC URL (Alchemy, Infura, or similar)
- Private key for testing (use testnet or small amounts on mainnet)
- ETH for gas fees
- Deposit tokens (e.g., USDC)
Complete Code Example
What This Code Does
1. Setup Phase
- Creates viem wallet client from private key
- Creates public client for reading blockchain data
- Configures connection to Ethereum mainnet via RPC
2. Approval Check Phase
- Calls
isDepositSpendApproved()to verify if tokens are already approved - Checks allowance for the Teller contract to spend your deposit tokens
3. Approval Phase (if needed)
- Prepares approval transaction using
prepareApproveDepositToken() - Executes approval transaction with
walletClient.writeContract() - Waits for approval confirmation before proceeding
4. Deposit Phase
- Prepares deposit transaction using
prepareDepositTxData() - Automatically calculates minimum shares based on slippage tolerance
- Executes deposit transaction
- Waits for confirmation and reports success
Key Features
Automatic Vault Resolution
The SDK automatically finds the correct vault based on:- Yield type:
PRIME,TBILL, orLENDING - Deposit token address: e.g., USDC (
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) - Chain ID: e.g., Ethereum mainnet (
1)
Slippage Protection
This example uses 1% slippage tolerance (100 basis points):Approval-First Workflow
Configuration Options
Change Deposit Amount
Change Yield Type
Change Slippage Tolerance
Use Different Token
Expected Output
Troubleshooting
”Approval transaction failed”
Possible causes:- Insufficient ETH for gas fees
- Incorrect deposit token address
- RPC URL not working or rate limited
- Check your ETH balance:
await publicClient.getBalance({ address: account.address }) - Verify deposit token address is correct
- Test RPC connection with a simple call
”Deposit transaction failed”
Possible causes:- Approval not successful or pending
- Insufficient deposit token balance
- Slippage tolerance too low (exchange rate changed)
- Verify approval was confirmed before depositing
- Check token balance: Use
publicClient.readContract()with ERC20balanceOf - Increase slippage tolerance if exchange rate moved
”Vault not found”
Possible causes:- Invalid yield type
- Deposit token not supported on this chain
- Incorrect chain ID
- Verify yield type is
PRIME,TBILL, orLENDING - Check that deposit token is supported using vault discovery API
- Ensure chain ID matches the network you’re connected to
”API request timed out”
Possible causes:- Network connectivity issues
- Earn API server not reachable
- API server overloaded
- Check network connection
- Verify API is running (default:
http://localhost:8500) - Try again after a few seconds with exponential backoff
Next Steps
- React Integration: See the wagmi deposit example for React integration patterns
- API Documentation: Read the deposits API documentation for detailed parameter descriptions
- Gas Optimization: Learn about permit-based deposits to save on gas fees