为什么openzeppelin的_msgSender()返回的address是0

IERC20(token0).transfer(address(this),token0Amount);

我调用上面的方法后,openzeppelin的_msgSender()返回的是0而不是用户地址或合约地址,这是为什么?

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

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        console.log("msg.sender: ",msg.sender);
        console.log("from: ",fromBalance);
        console.log("to: ",to);
        console.log("fromBalance: ",fromBalance);
        console.log("amount: ",amount);

        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
        }
        _balances[to] += amount;

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

我直接获取msg.sender,是能获取到合约地址的,为什么调用_msgSender()地址却是0

function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }
console.log:
    0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
    10000000000000000000000
    transfer token0:  1
    true
    bushu========
    msg.sender:  0x69c955af73a583c10a5b0f3218d3406f93c35338
    from:  0
    to:  0x69c955af73a583c10a5b0f3218d3406f93c35338
    fromBalance:  0
    amount:  1

openzeppelin也没做特殊处理,无法理解

请先 登录 后评论

2 个回答

nono
请先 登录 后评论
Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 2 关注
  • 0 收藏,2450 浏览
  • 点点点的点 提出于 2022-03-23 20:03