首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Context.sol在ERC-20中的用途是什么?

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

Stack Overflow用户
提问于 2022-05-27 05:20:09
回答 1查看 185关注 0票数 1

在释放ERC-20令牌时,继承OZ(OpenZeppelin)的util Context.sol

代码语言:javascript
复制
// 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

代码语言:javascript
复制
    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()呢?

代码语言:javascript
复制
 function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

不会让它更安全吗?

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

--谢谢--

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-27 06:13:37

需要上下文来实现元转换,或用于令牌传输的无气体事务。

更多关于加油站网络文件上下文的信息。

如果您不打算支持此特性(提示:今天没有人支持),则不需要Context

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72400789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档