【问题标题】: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 和 from address 有什么区别,都是同一个发起交易的人吗?
【发布时间】:2021-11-02 04:43:23
【问题描述】:

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

【问题讨论】:

    标签: solidity smartcontracts


    【解决方案1】:
    • 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;
        }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-26
        • 2022-08-18
        • 1970-01-01
        • 2019-02-27
        • 2021-12-28
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多