--- name: viem-sweep description: Comprehensive guide and reference for implementing advanced sweeping and transaction strategies (Legacy, Factory, Permit, Auth, EIP-7702) using viem v2. Use this skill when you need to implement token sweeping, wallet factories, gasless transfers, or account delegation. --- # Viem Sweep ## Overview This skill provides implementation patterns for advanced transaction strategies using `viem` v2. It covers methods for moving assets from multiple sources to a destination, ranging from simple private key transfers to advanced gas-optimized and signature-based patterns. ## Strategy Selection Guide Choose the appropriate strategy based on your requirements and infrastructure: ### 1. Legacy Strategy (Direct Transfer) **Best for:** General use case with private keys. - **Pros:** Works with any ERC20 token; simple logic. - **Cons:** High gas cost (requires ETH on every source account); management of dust ETH. - **Mechanism:** Admin funds source -> Source sends token. ### 2. Factory Strategy (CREATE2) **Best for:** High-volume deposit addresses (e.g., exchanges). - **Pros:** Lowest gas cost; clean address management; no private keys needed (just salt). - **Cons:** Requires initial setup (factory contract); addresses must be generated by factory. - **Mechanism:** Predict address -> Deploy (if needed) & Flush in one tx. ### 3. Permit Strategy (EIP-2612) **Best for:** Gasless user experiences with compatible tokens (e.g., UNI, DAI). - **Pros:** Gasless for user (relayer pays); single batch transaction. - **Cons:** Token MUST support EIP-2612; requires private key signature. - **Mechanism:** User signs Permit -> Admin submits batch. ### 4. Auth Strategy (EIP-3009) **Best for:** USDC and other tokens supporting TransferWithAuthorization. - **Pros:** Gasless for user; single batch transaction; handles non-sequential nonces (USDC). - **Cons:** Token MUST support EIP-3009. - **Mechanism:** User signs Authorization -> Admin submits batch. ### 5. EIP-7702 Strategy (Delegation) **Best for:** Future-proofing and "upgrading" EOAs to smart contracts temporarily. - **Pros:** Allows EOAs to act as contracts (batching, recovery, sponsored gas) without permanent migration. - **Cons:** Requires chain support (Prague hardfork+). - **Mechanism:** User signs delegation -> Admin executes tx with auth list. ## Implementation Details For detailed code examples, ABI snippets, and deep-dive explanations of each strategy, refer to the [Strategies Reference](references/strategies.md). The underlying Solidity contract logic can be found in the [Contracts Reference](references/contracts.md). ## Usage When implementing these strategies, ensure you have: 1. **Viem v2** installed. 2. Access to a **PublicClient** (for reads/simulation) and **WalletClient** (for signing/sending). 3. Relevant **ABI** definitions (standard ERC20, or your specific Factory/Sweeper contracts). To request specific implementation details, ask: - "How do I implement a factory sweep with viem?" - "Show me the EIP-7702 signing flow." - "What is the difference between Permit and Auth strategies?"