2

This question is a little conceptual, so hopefully this picture will help clear up my misunderstanding.enter image description here

Image there is a crowdsale smart contract deployed on address 0x2. A User at address 0x01 buys a token. Here is my understanding of what happens:

  1. The crowdsale contract (@ address: 0x2) accepts ether from the user account (@ address: 0x1)
  2. The crowdsale contract stores 0x1 as having purchased a token (important: this information is stored in the smart contract @address 0x2)

Now my Question: If 0x1 is a user account (and not a smart contract) there is no code at address 0x1. I thought a user account just consisted of an address + ether associated with the address, how can it also store the fact that 0x1 owns an ERC20 token? For example, I can login to MetaMask and (before clicking the "add token" option) MetaMask can see that I have a token... how is this possible?

Test1 Test2
  • 327
  • 2
  • 9
  • If your wallet supports ERC20 tokens it can look in the data and see what your balance is. – tadman Mar 26 '18 at 19:35
  • Yes I have done that. I can see "step 2" takes place. What I don't see happening is anything that would allow metaMask to see that user 0x01 owns a token (without looking into the data stored at 0x02). – Test1 Test2 Mar 26 '18 at 19:38
  • The data's stored within the Ethereum blockchain, so any wallet client can just look at it and know. That's how tools like [Ethplorer](https://ethplorer.io) do it. – tadman Mar 26 '18 at 19:39
  • Yes I understand your first point. What I don't understand is: How metaMask knows about the wallet at 0x02 when the only piece of information is has is 0x01. I don't think metaMask would scan every wallet on the blockchain to see if a user owns a token. – Test1 Test2 Mar 26 '18 at 19:44
  • It doesn't have to scan every wallet, it just scans the contract's data store that spells out who owns what. – tadman Mar 26 '18 at 19:45
  • but my question is "How does metaMask knows about the wallet at 0x02". If I login to metaMask with just address 0x01 how does metaMask know which contract's data store to scan. As I said, MetaMask knew I had a token before clicking the "add token" option. – Test1 Test2 Mar 26 '18 at 19:52
  • What token did you buy? My assumption is that MetaMask automatically queries popular tokens. It's certainly _possible_ that it tracks every ERC 20 `Transfer` event or calls out to a service that does. (This is what Ethplorer and Etherscan do.) – user94559 Mar 26 '18 at 19:54
  • Once they've discovered the contract it's pretty trivial to relate all the holders back to their wallet addresses in their database. Crawling the whole blockchain is time-consuming, but not impossible and it's what sites like that tend to do in real-time. – tadman Mar 26 '18 at 19:55
  • It's definitely not a popular token, its an ERC20 based token which i just created :) Ok thanks for the info – Test1 Test2 Mar 26 '18 at 20:01

3 Answers3

1

Every ERC20 contract has the following function:
function balanceOf(address _owner) public view returns (uint256 balance) { return balances[_owner]; }

Your wallet just calls this function from the known token contracts with your address. Since it's a view function it doesn't cost any gas.

I recon most ERC20 token get added rather quickly to a wallet like Metamask or MEW. But if your balance doesn't automatically show, you can add the contract address manually (in MEW at least, not sure about Metamask) and it will show up afterwards.

Fischa7
  • 181
  • 3
0

In solidity there are two ways to get the address of the person who sent the transaction

  • tx.origin
  • msg.sender

In your example, in the method inside ERC20 Token.sol, the value tx.origin will be 0x1 and msg.sender will be 0x2

So to answer your question, how does the ERC20 token know about 0x2 is: it depends on how the token contract is written and whether it uses tx.origin or msg.sender. I would imagine it uses msg.sender, because that is the more prevalent one.

If it does use msg.sender you can still make the crowdsale contract work by first buying the tokens and then immediatelly transfering the tokens from the crowdsale contract to the caller.

For more information, refer to What's the difference between 'msg.sender' and 'tx.origin'?

gaiazov
  • 1,908
  • 14
  • 26
0

how can it also store the fact that 0x1 owns an ERC20 token?

Token transfers, or transfers in accounting in general, are kept in a ledger. In this case, the ledger is ERC-20 smart contract that internally keeps balances who owns and what in its balances mapping. Or, the smart contract manage the storage (EVM SSTORE instructions) where the records of ownership are kept.

Note that some other blockchains, like Telos and EOS (and mayne Solana) might be opposite and there the storage is maintained on the user account (user account has associated RAM and tables for any token user owns).

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435