Skip to main content
Complete deposit flow example using viem (vanilla TypeScript) and the Earn SDK.

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)
Security: Never commit private keys to version control. Use environment variables or secure key management solutions in production.

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, or LENDING
  • Deposit token address: e.g., USDC (0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48)
  • Chain ID: e.g., Ethereum mainnet (1)
No need to manually specify vault addresses or search for vault configurations!

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
Solutions:
  1. Check your ETH balance: await publicClient.getBalance({ address: account.address })
  2. Verify deposit token address is correct
  3. 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)
Solutions:
  1. Verify approval was confirmed before depositing
  2. Check token balance: Use publicClient.readContract() with ERC20 balanceOf
  3. 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
Solutions:
  1. Verify yield type is PRIME, TBILL, or LENDING
  2. Check that deposit token is supported using vault discovery API
  3. 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
Solutions:
  1. Check network connection
  2. Verify API is running (default: http://localhost:8500)
  3. Try again after a few seconds with exponential backoff

Next Steps