0

Because EVM cannot watch events, we cannot send a token to an exchange and immediately get back some other token (EVM just does not "know" when it was send a token). As far as I understand, this is the reason why all exchanges I know require first create a wallet on the exchange site and first send the token to this wallet.

Then:

  1. How do exchanges create a wallet? How does the exchange generate the wallet address? (We know that Solidity cannot generate random numbers. Is it done off-chain?)

  2. How then the exchange could control this wallet, i.e. give it the "command" to send the token for the actual exchange operation? (E.g. if it is ERC-20, then the exchange could not control the token contract and so could not cause it to send the token for the exchange operation. If it is say an ERC-721 or ERC-1155, then the exchange also can't control the contract and the same trouble arises.)

So, I don't understand how exchanges work internally.

1

I don't think there is much official information on this as these are business secrets. But the only reasonable way for exchanges to work is something similar to the following.

1) They do not use actual blockchain wallets like you think. When you send tokens to an exchange address it's either a common wallet for many transfers or a pass-through wallet which forwards the tokens to a common wallet.

2) All internal trades are only changes in their internal ledgers outside blockchain. So if you exchange token A for token B the exchange only updates their internal databases and no actual blockchain transactions take place. Only when you deposit or withdraw tokens do they perform an actual blockchain transaction.

3) Exchanges have multiple common wallets in use. Some are hot (used to receive client tokens) and some cold (used for long term storage and not too many transactions). Tokens are transferred between these (with real blockchain transactions) when needed (as rarely as possible for cold wallets).

The reasons why exchanges try to avoid real blockchain transactions are mostly speed and costs: blockchain transactions are slow and costly.

Example

So when you transfer tokens to an exchange address something like this happens:

1) They give you an address where you can deposit the tokens and you send tokens there

2) They forward the tokens from that address to an internal hot or cold wallet

3) They update your token balance in their database

4) You trade with the token and they update their internal balances

5) You withdraw that token (or some other token): they update their database and send out the real token from their hot wallet.

Note that the same is true for both tokens and coins.

| improve this answer | |
  • "1) They do not use actual blockchain wallets like you think. When you send tokens to an exchange address it's either a common wallet for many transfers or a pass-through wallet which forwards the tokens to a common wallet." How is it implemented? – porton Apr 15 at 12:01
  • For ERC20 tokens there's probably a user-specific pass-through wallet but for other tokens and coins I guess it depends on its properties. All the handling is probably implemented outside blockchain so backend systems issue transactions to move the funds around. – Lauri Peltonen Apr 15 at 12:04
  • But there are decentralized exchanges. How do they work? For example, how an exchange wallet address is created in them? – porton Apr 17 at 10:42
  • oh, I'm not very familiar with decentralized exchanges. They all have some mechanism for atomic swaps so that assets can be interchanged in trustless manner. – Lauri Peltonen Apr 17 at 10:54

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.