3

'Greetings!

I am wondering if anyone is aware of the structure that is used by centralized exchanges like Binance, Coinbase, KuCoin, etc. to handle their ETH/ERC20 Tokens deposits.

  • I started to handle this by providing the users an EOA address and having an EOA master account and transfer all the deposits to it.

  • But when I encountered to the lack of ETH for ERC20 transfer(from user's account to the master account) fee problem, I decided to implement a modified version of this contract address approach by providing each user a contract address rather than an EOA. I handled the auto transfer of ETH with zero fee to the master and the support for all the ERC20s issues that this article did not proposed.

  • But the problem of high fee for deploying smart contracts (after a lot of modifications, I had to pay 100 bucks for each deployment, which means creating a contract address for each user cost me $100) and still having to pay fee for ERC20 token transfer to the master account, which is not affordable economically, made me to explore more.

  • I am currently working on @mikko-ohtamaa's suggestion to use some alternative EVM compatible chains to lower the costs.

  • Now, the question is, when I see big centralized exchanges' structures at first glance, I see that they provide their users EOAs and not contract addresses (so they don't have to pay for the address creation) and I see that each deposit to the user's address will end up with a transfer to their master account, but am unable to track down the rest of the flow. I appreciate it if anyone share their knowledge on how they handle the aforementioned deposits issue or any suggestions to handle this side of transaction flows with the minimum possible charges.

Thanks in Advance, R.

  • Geth version: 1.10.12-stable
  • Geth's web3 api version: 0.20.1
  • OS: Windows
  • web3.py version: 5.25.0
  • python version: 3.10.1
  • I started my geth using the following command: geth console --goerli --syncmode "light" --http --http.port 20000 --http.api personal,eth,net,web3 --allow-insecure-unlock
1

1 Answer 1

1
+50

Your observations are all correct, each exchange choses its preferred way of handling, but most I've seen use an EOA.


Externally Owned Account

To lower costs, the exchange can postpone the withdrawal of assets to a time in the day when gas price is low, they usually got enough assets in their main wallet to have this flexibility.

Sending ETH is the cheapest and uses only 21k gas, unlike ERC20 transfers, so when gas price is low (10-20 Gwei) -

  1. Exchange sends ETH to the address (shouldn't cost more than $1-$3);
  2. Extracts the tokens ($4-$8);
  3. Keep the remaining ETH for a day or two;
  4. If no other deposits are made, withdraw the remaining ETH, leaving the EOA empty.

That's how FTX handles its deposits / withdrawals.


Contract Approach

Benefit of using a contract, are that you can use tap to Deposit events, so your backend will know a new deposit has arrived, instead of constantly checking all wallet's balance.

Another benefit is that for ETH deposits, you can lay the transaction cost on the sender by using the receive() payable to immediately redirect funds to contract owner.

To lower costs, you -

  1. Create a bulk of contract wallets when gas price is low;
  2. When user requests a deposit address, allocate an unused contract;
  3. User sends ETH to the contract;
  4. Contract forwards funds to owner, emits Deposit event;
  5. User pays gas fee.

For ERC20 deposits, a contract can save you the ETH for gas requirement - i.e. you won't have to send ETH to the contract in order to extract the tokens, then sending the ETH back, saving you 2 extra transactions.

ERC20 deposits using contract -

  1. Create a bulk of contract wallets when gas price is low;
  2. When user requests a deposit address, allocate an unused contract;
  3. User sends a token to the contract;
  4. When gas price is low, exchange initiates a call to contract's withdraw function to clean all tokens (possibly ETH too).

So it seems that the contract approach is mostly cheaper for the exchange.

One caveat though, the depositor tx sometimes fails due to being out of gas, that's because estimateGas sometimes calculates 21,000 gas when it's actually requires more for the additional transfer.

2
  • instead of creating a bunch of deposit contracts and keeping them for spare, why the exchanges don't take advantage of create2 deterministic deployment after the user makes a deposit?
    – hack3r-0m
    May 20 at 14:07
  • Deployment is very expensive, you want the lowest gas possible - if you pre-create them, you can choose to do so when gas price is at lowest. Otherwise, you'd have to wait (after user sent his deposit) to when gas price is low again, which may take a day or more, or pay high fee if you need his deposit and can't wait until it goes down.
    – Kof
    May 20 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.