ERC2771ContextUpgradeable.sol 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (metatx/ERC2771Context.sol)
  3. pragma solidity ^0.8.9;
  4. import "../utils/ContextUpgradeable.sol";
  5. import "../proxy/utils/Initializable.sol";
  6. /**
  7. * @dev Context variant with ERC2771 support.
  8. */
  9. abstract contract ERC2771ContextUpgradeable is Initializable, ContextUpgradeable {
  10. /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
  11. address private immutable _trustedForwarder;
  12. /// @custom:oz-upgrades-unsafe-allow constructor
  13. constructor(address trustedForwarder) {
  14. _trustedForwarder = trustedForwarder;
  15. }
  16. function isTrustedForwarder(address forwarder) public view virtual returns (bool) {
  17. return forwarder == _trustedForwarder;
  18. }
  19. function _msgSender() internal view virtual override returns (address sender) {
  20. if (isTrustedForwarder(msg.sender)) {
  21. // The assembly code is more direct than the Solidity version using `abi.decode`.
  22. assembly {
  23. sender := shr(96, calldataload(sub(calldatasize(), 20)))
  24. }
  25. } else {
  26. return super._msgSender();
  27. }
  28. }
  29. function _msgData() internal view virtual override returns (bytes calldata) {
  30. if (isTrustedForwarder(msg.sender)) {
  31. return msg.data[:msg.data.length - 20];
  32. } else {
  33. return super._msgData();
  34. }
  35. }
  36. /**
  37. * @dev This empty reserved space is put in place to allow future versions to add new
  38. * variables without shifting down storage in the inheritance chain.
  39. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  40. */
  41. uint256[50] private __gap;
  42. }