Context.sol 在 ERC-20 中的用法是什么?

What is the usage of Context.sol within ERC-20?

释放ERC-20代币时, OZ(OpenZeppelin) 的 util Context.sol 被继承.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/*
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

我读到,当使用诸如 EIP-2771 之类的元交易解决方案时,Context.sol 很有用。 https://forum.openzeppelin.com/t/help-understanding-contract-context/10579

    function _msgSender() internal view virtual override returns (address sender) {
        if (isTrustedForwarder(msg.sender)) {
            // The assembly code is more direct than the Solidity version using `abi.decode`.
            assembly { sender := shr(96, calldataload(sub(calldatasize(), 20))) }
        } else {
            return super._msgSender();
        }
    }

但是对于 ERC-20,我为什么要使用 Context.sol 的 _msgSender()?

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

这不是更安全吗?

Context.sol 在 ERC-20 中的用途是什么?

先谢谢了。

需要上下文来实现元交易,或代币转移的 gas free 交易。

有关 Gas Station Network documentation 中的上下文的更多信息。

如果您不打算支持此功能(提示:今天没有人这样做),则不需要 Context