Skip to content

Fund and Sweep Process

Overview

The fund and sweep process is a two-step operation designed to handle token transfers from secondary wallet addresses back to the main account (account 0). This process is particularly important in blockchain networks where transaction fees must be paid in the native currency of the network.

For example, in Solana, you need SOL to pay for transaction fees even when transferring other tokens. Similar requirements exist in other networks:

  • Ethereum requires ETH for gas fees
  • Polygon requires MATIC for gas fees
  • BSC requires BNB for gas fees

Process Flow

Step 1: Funding Process

The funding process ensures that a wallet address has enough native currency to pay for transaction fees when it needs to transfer tokens.

When is it needed?

  • When a secondary wallet address receives tokens
  • When the address needs to send these tokens back to the main account
  • When the address doesn't have enough native currency to pay for transaction fees

How it works

  1. Validation

    • Confirms the target address is not the main account
    • Verifies the main account exists
  2. Fee Estimation

    • Calculates the amount of native currency needed for the future token sweep operation
    • Uses network-specific fee estimation methods
    • Considers current network conditions and gas prices
  3. Balance Check

    • Verifies the main account has enough native currency to fund the operation
    • Prevents failed transactions due to insufficient funds
  4. Transaction Execution

    • Creates a transaction to send the estimated amount
    • Transfers native currency from the main account to the target address
    • Returns the transaction hash upon successful completion

Implementation Example (Solana)

typescript
// Example of funding a wallet address in Solana
const fundingService = new FundSolanaWalletAddress(
  walletMainAccountFinder,
  solanaProvider,
  solanaTransactionBuilder
)

const txHash = await fundingService.run(walletAddressNeedingFunds)

Step 2: Token Sweep Process

After the address has been funded with native currency, a separate service handles the token sweeping operation:

  • Uses the received native currency to pay for transaction fees
  • Transfers the tokens from the secondary address to the main account
  • Ensures all tokens are consolidated in the main account

Technical Considerations

Native Currency Requirements

  • The main account must maintain sufficient balance to fund secondary addresses
  • The funding amount is calculated based on estimated transaction fees
  • Extra safety margin might be included in the fee estimation
  • Different networks have different fee structures and requirements

Error Handling

  • Handles cases where the main account is not found
  • Manages situations with insufficient balance
  • Provides clear error messages for troubleshooting
  • Network-specific error handling (e.g., out of gas, insufficient funds)

Security

  • Only allows transfers from the main account for funding
  • Validates all addresses and balances before transactions
  • Ensures proper authorization through wallet signatures
  • Implements network-specific security best practices

Network-Specific Implementations

Solana

  • Uses SOL for transaction fees
  • Requires separate token accounts for each SPL token
  • Implements Solana-specific transaction building and signing

Ethereum (Example)

  • Uses ETH for gas fees
  • Considers EIP-1559 fee structure
  • Handles ERC-20 token approvals and transfers

Other Networks

  • Each network implementation follows the same general pattern
  • Adapts to network-specific requirements and features
  • Uses appropriate SDKs and libraries for each network
  • TransactionBuilder: Network-specific transaction creation and signing
  • WalletMainAccountFinder: Locates the main account for a given wallet
  • Provider: Interfaces with the specific blockchain network
  • Token sweep service: Handles the actual token transfer back to the main account