简体   繁体   English

用户帐户如何拥有 ERC20 令牌

[英]How does a User account own an ERC20 Token

This question is a little conceptual, so hopefully this picture will help clear up my misunderstanding.这个问题有点概念性,所以希望这张图片能帮助消除我的误解。 在此处输入图像描述

Image there is a crowdsale smart contract deployed on address 0x2.图片在地址 0x2 上部署了一个众筹智能合约。 A User at address 0x01 buys a token.地址为 0x01 的用户购买了代币。 Here is my understanding of what happens:这是我对发生的事情的理解:

  1. The crowdsale contract (@ address: 0x2) accepts ether from the user account (@ address: 0x1)众筹合同(@地址:0x2)接受来自用户账户(@地址:0x1)的以太币
  2. The crowdsale contract stores 0x1 as having purchased a token (important: this information is stored in the smart contract @address 0x2)众筹合约将 0x1 存储为已购买代币(重要:此信息存储在智能合约@address 0x2 中)

Now my Question: If 0x1 is a user account (and not a smart contract) there is no code at address 0x1.现在我的问题是:如果 0x1 是用户帐户(而不是智能合约),则地址 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?我以为一个用户帐户只包含一个地址 + 与该地址相关联的以太币,它怎么还可以存储 0x1 拥有 ERC20 令牌的事实呢? 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?例如,我可以登录到 MetaMask 并且(在单击“添加令牌”选项之前)MetaMask 可以看到我有一个令牌……这怎么可能?

Every ERC20 contract has the following function:每个 ERC20 合约都具有以下功能:
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.因为它是一个视图函数,所以它不消耗任何 gas。

I recon most ERC20 token get added rather quickly to a wallet like Metamask or MEW.我发现大多数 ERC20 代币会很快添加到 Metamask 或 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.但是如果你的余额没有自动显示,你可以手动添加合约地址(至少在 MEW 中,不确定 Metamask),它会在之后显示。

In solidity there are two ways to get the address of the person who sent the transaction在 Solidity 中,有两种方法可以获取发送交易的人的地址

  • 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在您的示例中,在ERC20 Token.sol内的方法中,值tx.origin将为0x1 ,而msg.sender将为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 .因此,要回答您的问题,ERC20 代币如何知道0x2是:这取决于代币合约的编写方式以及它使用的是tx.origin还是msg.sender I would imagine it uses msg.sender , because that is the more prevalent one.我想它会使用msg.sender ,因为这是更普遍的一种。

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.如果它确实使用msg.sender ,您仍然可以通过首先购买代币然后立即将代币从众筹合同转移给调用者来使众筹合同生效。

For more information, refer to What's the difference between 'msg.sender' and 'tx.origin'?有关详细信息,请参阅“msg.sender”和“tx.origin”之间有什么区别?

how can it also store the fact that 0x1 owns an ERC20 token?它如何存储 0x1 拥有 ERC20 令牌这一事实?

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.在这种情况下,分类账是 ERC-20 智能合约,它在内部保持余额谁拥有以及balances映射中的内容。 Or, the smart contract manage the storage (EVM SSTORE instructions) where the records of ownership are kept.或者,智能合约管理保存所有权记录的存储(EVM SSTORE指令)。

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).请注意,其他一些区块链,如 Telos 和 EOS(以及 mayne Solana)可能是相反的,并且存储在用户帐户上维护(用户帐户具有关联的 RAM 和用户拥有的任何令牌的表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM