Ethereum One-to-Many Transfer Implementation Code [ETH/ERC20 Tokens]

·

This article explains how to send Ethereum (ETH) or ERC20 tokens to multiple addresses while paying only one transaction fee.

Introduction

Traditional Ethereum native interfaces support only one-to-one transfers. Sending funds to hundreds or thousands of addresses via individual transactions would incur excessive gas costs and might exceed the block gas limit. Here, we demonstrate a gas-efficient solution using a smart contract to batch transfers with a single transaction.


1. Core Implementation Concept

How It Works

Transaction Flow

DApp → Contract → (Address 1, Address 2, ..., Address N)

2. ETH One-to-Many Transfer Code

pragma solidity ^0.4.18;
contract BatchSendETH {
    event MultiTransfer(
        address indexed _from,
        uint indexed _value,
        address _to,
        uint _amount
    );

    function multiTransfer(
        address[] _recipients,
        uint[] _amounts
    ) payable public {
        uint remainingBalance = msg.value;
        
        for (uint i = 0; i < _recipients.length; i++) {
            _recipients[i].transfer(_amounts[i]);
            remainingBalance -= _amounts[i];
            emit MultiTransfer(msg.sender, msg.value, _recipients[i], _amounts[i]);
        }
        
        // Return leftover ETH to sender
        msg.sender.transfer(remainingBalance);
    }
}
Note: This is a proof-of-concept (PoC). Audit and optimize before production use.

3. ERC20 Token One-to-Many Transfer Code

pragma solidity ^0.4.18;
import "erc20.sol";

contract BatchSendERC20 {
    event MultiERC20Transfer(
        address indexed _from,
        uint indexed _value,
        address _to,
        uint _amount,
        ERC20 _token
    );

    function multiERC20Transfer(
        ERC20 _token,
        address[] _recipients,
        uint[] _amounts
    ) public {
        for (uint i = 0; i < _recipients.length; i++) {
            _token.transferFrom(msg.sender, _recipients[i], _amounts[i]);
            emit MultiERC20Transfer(
                msg.sender,
                msg.value,
                _recipients[i],
                _amounts[i],
                _token
            );
        }
    }
}
Note: Ensure token approvals are set (approve()) before calling this function.

Key Considerations


FAQ

Q1: Can I use this for any ERC20 token?

Yes, if the token implements the standard transferFrom method and the sender has sufficient allowance.

Q2: What’s the maximum number of recipients per transaction?

It depends on block gas limits. Test with small batches first.

Q3: How do I optimize gas costs?


👉 Explore advanced DeFi solutions for scalable blockchain applications.