简体   繁体   English

如何在其智能合约中签署 ERC20 代币转账

[英]How is an ERC20 token transfer signed in its smart contract

I'm new to ethereum and I've been trying to understand ERC20 smart contracts.我是以太坊的新手,我一直在尝试理解 ERC20 智能合约。

This is the transfer function from the OpenZeppelin ERC20 smart contract.这是来自 OpenZeppelin ERC20 智能合约的转账 function。

function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
    _transfer(_msgSender(), recipient, amount);
    return true;
}


function _transfer(
    address sender,
    address recipient,
    uint256 amount
) internal virtual {
    require(sender != address(0), "ERC20: transfer from the zero address");
    require(recipient != address(0), "ERC20: transfer to the zero address");

    _beforeTokenTransfer(sender, recipient, amount);

    uint256 senderBalance = _balances[sender];
    require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
    unchecked {
        _balances[sender] = senderBalance - amount;
    }
    _balances[recipient] += amount;

    emit Transfer(sender, recipient, amount);

    _afterTokenTransfer(sender, recipient, amount);
}

By the looks of this, a caller can just pick any two addresses and, as long as the balances are enough, the tokens will transfer.看起来,调用者可以选择任意两个地址,只要余额足够,代币就会转移。 But I know the transaction participants (at least the sender) need to sign.但我知道交易参与者(至少是发件人)需要签名。 Can't figure out what I'm missing here for this to work.无法弄清楚我在这里缺少什么才能使其正常工作。

There's an approve function as well, but it's similar where it just changes the values in an internal Hashmap without doing any cryptographic signature as far as I can tell.也有一个批准的 function ,但据我所知,它只是更改内部 Hashmap 中的值而不做任何加密签名。

the transaction participants (at least the sender) need to sign交易参与者(至少是发送者)需要签名

Correct.正确的。 The sender needs to sign the transaction with their private key in order to execute the public transfer() function (which invokes the internal _transfer() performing the actual balances settlement).发送者需要用他们的私钥签署交易,以执行公共transfer() function(调用内部_transfer()执行实际余额结算)。

The signature is performed offchain, before the transaction is even broadcasted to the network and mined.签名是在链下执行的,甚至在交易被广播到网络并被挖掘之前。

The contract function is executed during the mining process, but only the winning miner broadcasts the resulting state changes - the Transfer() event log, change of storage fields representing _balances[sender] and _balances[recipient] - to the rest of the network. The contract function is executed during the mining process, but only the winning miner broadcasts the resulting state changes - the Transfer() event log, change of storage fields representing _balances[sender] and _balances[recipient] - to the rest of the network.

So you won't see the signature process in the Solidity code.所以你不会在 Solidity 代码中看到签名过程。

There's an approve function as well还有一个批准的 function

That's used for approving another address to spend your tokens.这用于批准另一个地址来使用你的代币。

From the context, I'm assuming you've copy-pasted part of the OpenZeppelin ERC20 implementation.从上下文来看,我假设您已经复制粘贴了 OpenZeppelin ERC20 实现的一部分。 So here's a link to the code that they use to validate whether the sender is approved to spend someone else's tokens as part of the transferFrom() function.因此,这是他们用来验证发件人是否被批准使用其他人的代币作为transferFrom() function 的一部分的代码的链接

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

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