1

When releasing the ERC-20 token, OZ(OpenZeppelin)'s util Context.sol is inherited.

// 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;
    }
}

I read that when using a meta-transaction solution such as EIP-2771, Context.sol is useful. 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();
        }
    }

But in the case of ERC-20, why shoud I use Context.sol's _msgSender()?

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

Doesn't make it more secure?

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

Thank you in advance.

DongMyoung
  • 13
  • 3

1 Answers1

0

Context is needed to implement metatransactions, or gas free transactions for token transfers.

More about Context in Gas Station Network documentation.

If you do not plan to support this feature (hint: no one does today) you do not need to Context.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435