[Ethereum] Implementing a deposit/withdraw system

go-ethereum

I'm having difficulty coming up with suitable logic to handle a hot-wallet style deposit/withdraw system where funds that are deposited have to be sent out to any number of different addresses. This seems to be common place for exchanges or any other centralised system.

I've implemented this already for Bitcoin and that's rather easy to generate new addresses, get users to send coins to a central wallet, then sending out to any other wallet. This is easy due to Bitcoin's multiple input/outputs. This is proving to be a bigger challenge than I thought with Ethereum's single input and single output.

From my understanding it's not possible to have multiple addresses for a single account, since the account is the address. So the only option I'm left with is making deposit addresses as new accounts for every user that generates an address.

This is the major issue I'm facing. The funds that got deposited are now split over an unknown number of addresses/accounts. I've noticed that exchanges like Poloniex have a single main ETH account (which can be seen on etherscan).

So my main question is: How can I create different addresses and have the funds sent to a single address for full control?

Best Answer

You basically have two options:

First option

  1. Generate unique address for every user, you may derive user address deterministically from master public key and user ID, this way you will not need to deal with private keys in your back-end.
  2. Check balances of these addresses periodically.
  3. In case balance of some address exceeds certain threshold, publish transaction to move all ether (minus transfer fee) from this deposit address to your cold wallet.
  4. Track status of the transaction published at step 3
  5. Once transaction is mined and got decent number of confirmation, increase user balance in database

Pros: adding new users for free, user may send ether to deposit address from smart contracts via transfer method

Cons: deposits are more expensive, because second transaction is needed to move ether to cold storage, deposits take longer time

Second option

  1. For each user deploy smart contract that forwards all incoming ether to your cold wallet and logs an event with user ID and ether amount received
  2. Give address of deployed contract to the user as deposit address
  3. Monitor event log
  4. Once deposit event occurs in event log with transaction with decent number of confirmations, increase user balance in database

Pros: deposits are cheap and instant

Cons: adding new user costs gas that has to be paid by platform, not by user

Related Topic