For any platform handling Ethereum (ETH) deposits, efficiently managing incoming assets is crucial. A key process in this management is "change organization" or "fund sweeping"—the systematic transfer of user-deposited ETH to a more secure cold wallet. This practice enhances security and simplifies asset management.
Why Organize Change?
When users deposit ETH, these funds are typically held in hot wallets for processing. However, leaving substantial assets in hot wallets increases security risks. Regularly moving these funds to cold storage minimizes exposure to potential threats.
Additionally, consolidating funds from multiple deposits into a single transaction reduces network fees (gas costs) and streamlines accounting processes. This is especially relevant for platforms handling numerous small deposits.
Core Process Overview
The change organization process can be broken down into three main stages:
- Generating Transaction Data: Identifying unprocessed deposits, grouping them by address, and creating the necessary transaction data.
- Sending Transactions: Broadcasting the signed transactions to the Ethereum network.
- Confirming Transactions: Monitoring the blockchain to confirm successful transaction inclusion.
Generating the Transaction Data
The entire operation begins with identifying the funds that need to be moved.
Identifying Unprocessed Deposits
Incoming deposits are tracked in a database table, often named something like t_tx. This table logs essential details such as the transaction ID, the deposit address, the amount received, and a status flag indicating if it has been processed for sweeping.
Example t_tx Table:
| id | tx_id | from_address | value | sweep_status |
|---|---|---|---|---|
| 1 | 0x1... | 0xa... | 0.11 ETH | 0 |
| 2 | 0x2... | 0xb... | 0.12 ETH | 0 |
| 3 | 0x3... | 0xa... | 0.08 ETH | 0 |
| 4 | 0x4... | 0xc... | 0.17 ETH | 0 |
In this example, status 0 indicates a deposit that has not yet been swept. Notice that addresses can have multiple deposits (e.g., 0xa... has two transactions). To optimize gas fees, transactions from the same address are batched together.
Grouping and Processing Transactions
The logic for generating sweep transactions follows these steps:
- Query Database: Retrieve all deposit records where the
sweep_statusis0(unprocessed). - Group by Address: Sum the amounts for each unique deposit address, creating a map like
map[address]total_sum. Process Each Address: For each address in the map:
- Retrieve Private Key: Access the private key for the deposit address (from secure storage).
- Check Balance: Verify that the total balance for the address is greater than the estimated gas fee required for the transfer. If not, it may be skipped until more deposits arrive.
Get Nonce: The nonce is a critical value that prevents transaction replay. The system must determine the correct nonce to use.
- Check the local database for the highest nonce used by this address and increment it by one.
- Query the Ethereum node via RPC to get the current transaction count for the address.
- Use the maximum value from these two sources to ensure no nonce is reused.
- Create and Sign Transaction: Using the nonce, gas parameters, cold wallet destination address, and total value, construct the transaction. Sign it with the private key to authorize it.
Example code snippet for transaction creation and signing:
// Code example for transaction creation and signing
tx := types.NewTransaction(
uint64(nonce),
coldAddress,
big.NewInt(sendBalance),
uint64(gasLimit),
big.NewInt(gasPrice),
data, // usually empty for simple ETH transfers
)
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
rawTxBytes := signedTx.MarshalRLP()
rawTxHex := hex.EncodeToString(rawTxBytes)
txHash := signedTx.Hash().Hex()Storing Transaction Data
Once a transaction is generated, its data is stored in a separate t_send table. This table acts as a sending queue and audit log.
The t_send table structure typically includes:
- A unique ID and relation fields to link back to the original deposits.
- The transaction hash (
tx_id), sender (from_address), and receiver (to_address). - The value transferred in both Wei and Ether.
- Gas parameters (
gas,gas_price,nonce). - The raw signed transaction hex (
hex), which is needed to broadcast the tx. - Status fields (
handle_status,handle_msg,handle_time) to track progress.
For a batch of deposits from one address, only one primary record in t_send needs the full transaction data. Other related deposit records are linked to this transaction but don't duplicate the hex data. Finally, the status of the original deposit records (t_tx.sweep_status) is updated to prevent them from being processed again.
Sending the Transactions
The next stage involves broadcasting the signed transactions stored in the t_send table to the Ethereum network.
Processing the Send Queue
A dedicated service processes records in t_send with a "pending send" status.
- Decode Transaction: The raw transaction hex (
hexfield) is decoded from the database back into a transaction object that the Ethereum node can understand. - Send via RPC: The decoded transaction is sent to an Ethereum node using the
eth_sendRawTransactionJSON-RPC method. This method broadcasts the transaction to the network to be picked up by miners.
// Example code for decoding and sending
rawTxBytes, err := hex.DecodeString(sendRow.Hex)
err = rlp.DecodeBytes(rawTxBytes, &tx)
err = ethClient.SendRawTransaction(context.Background(), tx)After a successful broadcast, the transaction hash returned by the network is logged, and the record's status in t_send is updated to "sent."
👉 Explore advanced transaction management strategies
Confirming Transaction Receipt
Simply sending a transaction isn't enough; the system must confirm it has been successfully included in a block.
Monitoring the Blockchain
A confirmation service monitors transactions that are in the "sent" state.
- Query Transaction Status: For each sent transaction, the service uses its hash to query an Ethereum node via the
eth_getTransactionByHashRPC call. - Check for Confirmation: The response indicates if the transaction is still pending in the mempool or if it has been mined into a block. A mined transaction will have a block number assigned to it.
- Update Status: Once a transaction is confirmed in a block, its status in the
t_sendtable is updated to "confirmed" or "success." This may also trigger updates to the status of the originalt_txrecords to mark them as fully processed.
// Example code for checking transaction status
tx, isPending, err := client.TransactionByHash(ctx, txHash)
if err != nil {
// Handle error
}
if !isPending {
// Transaction is confirmed!
updateStatusToConfirmed(txHash)
}This three-stage process—generate, send, confirm—completes the automated sweeping of ETH deposits into secure cold storage, optimizing fees and maximizing security.
Frequently Asked Questions
What is change organization in cryptocurrency?
Change organization, or fund sweeping, refers to the process of automatically consolidating smaller amounts of cryptocurrency from multiple incoming transactions into a single, larger transaction sent to a secure storage address. This is done to improve security and operational efficiency.
Why is it important to use the correct nonce?
The nonce is a sequential number tied to an Ethereum address that ensures each transaction is unique. Using a nonce that is too low will result in the transaction being rejected. Using a nonce that has already been used (a "replay") can cause financial losses. Always using the maximum known nonce is a critical safety practice.
How does batching transactions save on gas fees?
Instead of sending a separate transaction for each small deposit (each incurring a base fee), you batch the total value from one address into a single transaction. You pay the base fee once for the batch instead of multiple times, resulting in significant overall gas savings.
What is the difference between a hot wallet and a cold wallet?
A hot wallet is connected to the internet and is used for frequent transactions like processing deposits and withdrawals. A cold wallet is stored offline (e.g., on a hardware device or in a paper wallet) and is used for long-term, secure storage of assets. Sweeping funds from hot to cold wallets reduces vulnerability to online attacks.
What happens if a sweep transaction fails?
The system should monitor for transaction failures (e.g., due to insufficient gas or low balance for fees). If a transaction fails, it should be flagged for review. The system may need to recalculate gas, adjust the nonce, or wait for more deposits to cover fees before retrying.
Can this process be applied to other blockchains?
The general concept of consolidating funds for security and efficiency is universal. However, the specific implementation details—such as transaction structure, signature algorithms, and RPC API calls—will differ significantly between blockchains like Bitcoin, BSC, or Solana.