I'm new in Ethereum based crypto concepts. I would like to know how can I differ the incomming crypto amounts based on their orginal token type?
Imagine I have an address on Ethereum blockchain. Person 1 has sent 0.1 Tether to my address, and the second person has sent 0.2 Ether to the same address. How the explorer detect the origin types?
Is there any flag, or someting else that defines the type of token/coin while sending?
2 Answers
Your understanding is intuitive but the actual situation calls for inverting the conceptualization of tokens.
First, ETH is a special case since it's the native currency OF the network. Tokens are contracts that run ON the network. They have commonality because they, generally, adhere to the ERC20 standard.
Your contract will know when it has received ETH because:
- Functions have to be explicitly marked with the
payable
modifier. - The function will use
msg.value
to inspect the money received. It can do things likeif(msg.value == expectedAmount) { ...
.
Tokens are different. They are contracts that implement a ledger of user addresses and balances and the methods of moving funds between accounts. It's a subtle difference, but Alice doesn't exactly have a token ... a token contract says there is a token in Alice's account.
I would like to know how can I differ the incomming crypto amounts based on their orginal token type?
This is tricky because things can be going on in other contracts and there is no easy way to monitor them all. As an aside, there is ERC223 that attempts to address this but it is less common than ERC20. A situation can arise in which Alice has received a token (T) in contract (T) but Alice isn't aware of it.
It's not a big problem in practical terms. Consider a real-world comparison. One doesn't go around putting random currencies in bank accounts for other people without telling those people, if ever. Indeed, in the rare case that one did, one would inform the recipient (Hey, I opened a numbered account for you, somewhere, and I put some money in) - otherwise, how would the recipient ever know about this good deed?
Similarly, one doesn't go into a donut shop and leave random currency in random places and expect that a) the currency is accepted and b) the cashier is aware. If one did, then that cash would most likely be lost. One wouldn't expect a donut unless both of those conditions are met. That is, an acceptable currency is given to the shop in a specific way - usually to the person operating the cash register and they will be informed about what one wants to buy.
Most contracts operate on a similar basis, with one or more acceptable tokens defined and a specific way to place your order and pay. In ERC20 contracts there is a pattern called approve and transferFrom. Since the tokens are in another contract and the shop will be informed of transfers, the flow is reversed:
- Customer approves the shop to withdraw a certain amount of tokens.
- Customer places an order.
- The shop contract attempts to transferFrom to see if funds are approved, as expected. If that works, then the tokens are moved to the shop's address in the ledger.
- If the transferFrom succeeds, then the shop delivers the product.
One might think, "Hey! A dishonest shop might just pocket the tokens" and that would be partially correct. However, the shop's contract can and should wrap that process in an atomic transaction so either the tokens are moved and product delivered, or neither of those things happened. The shop contract should be open to inspection and users should not send tokens to contracts they don't trust. The protocol is unopinionated about what is an honest process. After all, it could be a charitable donation.
In that flow, the shop is informed in step 2 and has visibility into it in step 3. The user (or auditors) should be able to see that steps 3 and 4 are inseparable.
Hope it helps.
-
1Thank you Rob. Your complete and well explained asnwer made a bright view on what is happenin on Ethereum based transactions and contracts in my mind. Jan 2, 2020 at 5:35
In general, every coin has its own contract which does the book-keeping (which address owns which amount). If the address of the contract for a coin is known and the coin contracts implements one of the well-defined interfaces like ERC20, then the explorer has enough information to sort out which coin was sent to which address.
-
1May you give me more information in this regard? for example, I want to send USDT to somebody else which has an Ethereum based address. What kinds of information I have to put in the sent data? Is there any sample you may know? Jan 1, 2020 at 11:34