Search code examples
solidityether

What is Context.sol uses for in Openzepplin


I'm new to solidity and trying to deploy a ERC20 token using openzepplin.There is one thing that doesn't make sense to me is the context.sol file. From the comment section it's seem like the main function of the context.sol is to implement a GSN compatible contract so instead of using msg.sender you use _msgSender()

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

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

From my limited experience with solidity it seem doing exactly the same thing with msg.sender.


Solution

  • In this case, it's the same thing.

    I believe that the OpenZeppelin devs implemented this function to make it more easily interchangeable if that's going to be needed. So if for some reason msg.sender becomes obsolete, it's just a matter of changing one line instead of multiple lines throughout multiple contracts (and possibly forgetting about some).

    A similar situation has happened before, when tx.origin was prefered by some blockchain developers, but then it was deprecated (by the Solidity lang core team) in favor of msg.sender.