msg.sender 和 solidity 智能合约中的地址有什么区别,发起交易的都是同一个人?

What is the difference between msg.sender and from address in solidity smart contracts, are both the same who are initiating the transaction?

我刚开始接触 solidity 和智能合约,但我很困惑 msg.sender 和 ERC20 智能合约创建指南中的地址有什么区别

msg.sender 实际上是调用函数时使用的地址。这是您未定义的全局变量。据我所知,在代币合约中,“发件人”是您要用来花费一定数量代币的帐户地址。

  • msg.sender指调用函数的账户,可以是EOA(外部拥有账户)或合约地址。
  • from 是函数的参数。

示例:

// Send an amount from any address to any address    
function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
        _transfer(from, to, amount);
        return true;
    }

// Send an amount from the function caller to any address    
function transferFromCaller(address to, uint256 amount) public virtual override returns (bool) {
        _transfer(msg.sender, to, amount);
        return true;
    }