Skip to content

meliodas113/privy-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Privy Wallet Management & USDC Sweeping Tool

A professional-grade tool for managing multiple Privy wallets and consolidating USDC tokens across the Base network. Built with both Python and JavaScript implementations, this tool provides secure, efficient wallet management with advanced features like smart account abstraction and gas optimization.

🎯 What This Tool Does

This project solves a common problem in DeFi: managing multiple wallets and consolidating funds efficiently. When you're running multiple wallets (for testing, user onboarding, or operational purposes), it becomes tedious to manually transfer funds between them or consolidate balances.

Key Use Cases:

  • Fund Consolidation: Sweep all USDC from multiple wallets into a single destination
  • Balance Monitoring: Check USDC balances across all your Privy wallets
  • Bulk Transfers: Transfer specific amounts between wallets programmatically
  • Wallet Management: List and manage all wallets in your Privy app

πŸ—οΈ Architecture Overview

The project consists of two implementations:

Python Version (sweep_to_wallet.py)

  • Simple & Reliable: Basic Web3.py integration with Privy API
  • Easy to Deploy: Minimal dependencies, works out of the box
  • Good for: Quick scripts, automation, or when you need something simple

JavaScript Version (Modular Architecture)

  • Advanced Features: Smart account abstraction, gas optimization
  • Production Ready: Uses modern Web3 libraries and best practices
  • Modular Design: Clean separation of concerns for easy maintenance
  • Good for: Production deployments, when you need enhanced security

JavaScript File Structure

β”œβ”€β”€ config.js           # Configuration constants and settings
β”œβ”€β”€ clients.js          # Web3 client initialization and setup
β”œβ”€β”€ walletOperations.js # Core wallet management functions
β”œβ”€β”€ cli.js             # Command-line interface (main entry point)
└── package.json       # Dependencies and scripts

πŸš€ Features

Core Functionality

  • βœ… Multi-Wallet Management: Handle unlimited number of Privy wallets
  • βœ… USDC Operations: Check balances, transfer specific amounts, sweep all funds
  • βœ… Base Network Support: Optimized for Base mainnet (Ethereum L2)
  • βœ… CLI Interface: Easy-to-use command line tools

Advanced Features (JavaScript Version)

  • πŸ”’ Smart Account Abstraction: Enhanced security through Kernel smart accounts
  • ⚑ Gas Optimization: Pimlico bundler and paymaster integration
  • πŸ›‘οΈ Rate Limiting: Built-in protection against API rate limits
  • πŸ“Š Transaction Monitoring: Real-time transaction status and hashes
  • πŸ—οΈ Modular Architecture: Clean separation of concerns for maintainability

πŸ“‹ Prerequisites

Before using this tool, you'll need:

  1. Privy App Setup

    • A Privy app with App ID and Secret
    • Server wallets configured in your Privy dashboard
  2. API Keys

    • Privy App ID and Secret
    • Pimlico API key (for JavaScript version)
  3. Network Access

    • Access to Base mainnet RPC endpoint
    • Internet connection for API calls

πŸ› οΈ Installation

Option 1: Python Version

# Clone the repository
git clone <your-repo-url>
cd privy-test

# Install Python dependencies
pip install web3 requests

# Set your Privy credentials in sweep_to_wallet.py
# Update privy_app_id and privy_app_secret variables

Option 2: JavaScript Version (Recommended)

# Install Node.js dependencies
npm install

# Set your credentials in config.js
# Update PRIVY_APP_ID, PRIVY_APP_SECRET, and PIMLICO_API_KEY

πŸ”§ Configuration

1. Update Privy Credentials

In config.js, replace the placeholder credentials:

const PRIVY_APP_ID = "your_actual_app_id";
const PRIVY_APP_SECRET = "your_actual_app_secret";

2. Update Pimlico API Key

const PIMLICO_API_KEY = "your_actual_pimlico_api_key";

πŸ“– Usage Examples

Using the New Modular JavaScript Version

List All Wallets

# Using npm scripts (recommended)
npm run wallets

# Direct node command
node cli.js wallets

Check USDC Balance

# Using npm scripts
npm run balance -- 0x1234...

# Direct node command
node cli.js balance_usdc 0x1234...

Transfer Specific Amount

# Using npm scripts
npm run transfer -- 100.5 abc123 0x5678...

# Direct node command
node cli.js transfer_usdc 100.5 abc123 0x5678...

Sweep All USDC (Consolidate Funds)

# Using npm scripts
npm run sweep -- 0x9999...

# Direct node command
node cli.js transfer_all_wallets_all_usdc 0x9999...

Using the Python Version

# List wallets
python3 sweep_to_wallet.py wallets

# Check balance
python3 sweep_to_wallet.py balance_usdc 0x1234...

# Transfer specific amount
python3 sweep_to_wallet.py transfer_usdc 100.5 abc123 0x5678...

# Sweep all USDC
python3 sweep_to_wallet.py transfer_all_wallets_all_usdc 0x9999...

πŸ” How It Works

1. Authentication Flow

  • Uses Privy App ID and Secret for API authentication
  • Creates secure headers for all API requests
  • Maintains session state for wallet operations

2. Wallet Discovery

  • Fetches all wallets from your Privy app
  • Displays wallet IDs, addresses, and chain types
  • Validates wallet accessibility and permissions

3. USDC Operations

  • Connects to Base mainnet USDC contract
  • Handles decimal precision (6 decimals for USDC)
  • Builds and sends transactions through Privy's infrastructure

4. Smart Account Integration (JavaScript)

  • Creates Viem accounts from Privy wallets
  • Converts to Kernel smart accounts for enhanced security
  • Uses Pimlico for gas optimization and bundling

5. Modular Architecture Benefits

  • config.js: Centralized configuration management
  • clients.js: Clean client initialization and setup
  • walletOperations.js: Focused business logic
  • cli.js: Simple command-line interface

🚨 Security Considerations

What's Secure

  • βœ… No Private Keys Stored: Uses Privy's secure wallet infrastructure
  • βœ… API-Based Operations: All operations go through Privy's secure APIs
  • βœ… Smart Account Abstraction: Enhanced security in JavaScript version
  • βœ… Rate Limiting: Built-in protection against abuse
  • βœ… Modular Design: Easier to audit and maintain security

What to Watch Out For

  • ⚠️ API Key Exposure: Keep your Privy and Pimlico keys secure
  • ⚠️ Network Security: Ensure you're on Base mainnet, not testnet
  • ⚠️ Transaction Verification: Always verify transaction hashes and addresses

πŸ§ͺ Testing

Test on Base Sepolia First

Before running on mainnet, test with small amounts on Base Sepolia:

  1. Update RPC URL to Sepolia endpoint in config.js
  2. Use test USDC contract address
  3. Test with minimal amounts
  4. Verify all operations work as expected

Dry Run Mode

The tool includes built-in validation:

  • Balance checks before transfers
  • Wallet existence verification
  • Transaction parameter validation

πŸš€ Production Deployment

Environment Variables

For production, use environment variables:

export PRIVY_APP_ID="your_app_id"
export PRIVY_APP_SECRET="your_app_secret"
export PIMLICO_API_KEY="your_pimlico_key"

Monitoring & Logging

  • All operations are logged to console
  • Transaction hashes are displayed for tracking
  • Error handling includes detailed error messages
  • Global error handlers for uncaught exceptions

πŸ”§ Troubleshooting

Common Issues

"Failed to fetch wallet"

  • Check your Privy App ID and Secret in config.js
  • Verify the wallet ID exists in your app
  • Ensure your app has the necessary permissions

"Insufficient balance"

  • Verify the source wallet has enough USDC
  • Check if the balance includes gas fees
  • Ensure you're on the correct network

"Transaction failed"

  • Check gas prices and network congestion
  • Verify the destination address is valid
  • Ensure the wallet has enough ETH for gas

Debug Mode

Add more verbose logging by modifying the console.log statements or adding debug flags.

🀝 Contributing

This is a professional tool, so contributions should follow enterprise standards:

  1. Code Quality: Follow existing patterns and add comprehensive comments
  2. Testing: Test all changes on testnet before mainnet
  3. Documentation: Update README and inline comments
  4. Security: Review all changes for security implications
  5. Architecture: Maintain the modular structure and separation of concerns

πŸ“„ License

This project is licensed under the ISC License - see the package.json file for details.

⚠️ Disclaimer

This tool is for professional use only. Always:

  • Test thoroughly on testnets before mainnet use
  • Verify all addresses and amounts before executing
  • Keep your API keys secure and never commit them to version control
  • Monitor transactions and verify their success

πŸ†˜ Support

For issues or questions:

  1. Check the troubleshooting section above
  2. Review Privy and Pimlico documentation
  3. Verify your configuration and credentials
  4. Test with minimal amounts first

Built with ❀️ for the DeFi community

This tool demonstrates professional-grade wallet management practices and should be used responsibly in production environments.

About

Privy Wallet Management & USDC Sweeping Tool is a professional-grade Python & JavaScript toolkit for managing multiple Privy wallets and consolidating USDC on the Base network. It supports bulk transfers, balance monitoring, and automated sweeping, with advanced features like smart account abstraction and gas optimization for production use.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors