GSNRecipientERC20Fee.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.0;
  3. import "./GSNRecipient.sol";
  4. import "../math/SafeMath.sol";
  5. import "../access/Ownable.sol";
  6. import "../token/ERC20/SafeERC20.sol";
  7. import "../token/ERC20/ERC20.sol";
  8. /**
  9. * @dev A xref:ROOT:gsn-strategies.adoc#gsn-strategies[GSN strategy] that charges transaction fees in a special purpose ERC20
  10. * token, which we refer to as the gas payment token. The amount charged is exactly the amount of Ether charged to the
  11. * recipient. This means that the token is essentially pegged to the value of Ether.
  12. *
  13. * The distribution strategy of the gas payment token to users is not defined by this contract. It's a mintable token
  14. * whose only minter is the recipient, so the strategy must be implemented in a derived contract, making use of the
  15. * internal {_mint} function.
  16. */
  17. contract GSNRecipientERC20Fee is GSNRecipient {
  18. using SafeERC20 for __unstable__ERC20Owned;
  19. using SafeMath for uint256;
  20. enum GSNRecipientERC20FeeErrorCodes {
  21. INSUFFICIENT_BALANCE
  22. }
  23. __unstable__ERC20Owned private _token;
  24. /**
  25. * @dev The arguments to the constructor are the details that the gas payment token will have: `name` and `symbol`. `decimals` is hard-coded to 18.
  26. */
  27. constructor(string memory name, string memory symbol) public {
  28. _token = new __unstable__ERC20Owned(name, symbol);
  29. }
  30. /**
  31. * @dev Returns the gas payment token.
  32. */
  33. function token() public view returns (IERC20) {
  34. return IERC20(_token);
  35. }
  36. /**
  37. * @dev Internal function that mints the gas payment token. Derived contracts should expose this function in their public API, with proper access control mechanisms.
  38. */
  39. function _mint(address account, uint256 amount) internal virtual {
  40. _token.mint(account, amount);
  41. }
  42. /**
  43. * @dev Ensures that only users with enough gas payment token balance can have transactions relayed through the GSN.
  44. */
  45. function acceptRelayedCall(
  46. address,
  47. address from,
  48. bytes memory,
  49. uint256 transactionFee,
  50. uint256 gasPrice,
  51. uint256,
  52. uint256,
  53. bytes memory,
  54. uint256 maxPossibleCharge
  55. )
  56. public
  57. view
  58. virtual
  59. override
  60. returns (uint256, bytes memory)
  61. {
  62. if (_token.balanceOf(from) < maxPossibleCharge) {
  63. return _rejectRelayedCall(uint256(GSNRecipientERC20FeeErrorCodes.INSUFFICIENT_BALANCE));
  64. }
  65. return _approveRelayedCall(abi.encode(from, maxPossibleCharge, transactionFee, gasPrice));
  66. }
  67. /**
  68. * @dev Implements the precharge to the user. The maximum possible charge (depending on gas limit, gas price, and
  69. * fee) will be deducted from the user balance of gas payment token. Note that this is an overestimation of the
  70. * actual charge, necessary because we cannot predict how much gas the execution will actually need. The remainder
  71. * is returned to the user in {_postRelayedCall}.
  72. */
  73. function _preRelayedCall(bytes memory context) internal virtual override returns (bytes32) {
  74. (address from, uint256 maxPossibleCharge) = abi.decode(context, (address, uint256));
  75. // The maximum token charge is pre-charged from the user
  76. _token.safeTransferFrom(from, address(this), maxPossibleCharge);
  77. return 0;
  78. }
  79. /**
  80. * @dev Returns to the user the extra amount that was previously charged, once the actual execution cost is known.
  81. */
  82. function _postRelayedCall(bytes memory context, bool, uint256 actualCharge, bytes32) internal virtual override {
  83. (address from, uint256 maxPossibleCharge, uint256 transactionFee, uint256 gasPrice) =
  84. abi.decode(context, (address, uint256, uint256, uint256));
  85. // actualCharge is an _estimated_ charge, which assumes postRelayedCall will use all available gas.
  86. // This implementation's gas cost can be roughly estimated as 10k gas, for the two SSTORE operations in an
  87. // ERC20 transfer.
  88. uint256 overestimation = _computeCharge(_POST_RELAYED_CALL_MAX_GAS.sub(10000), gasPrice, transactionFee);
  89. actualCharge = actualCharge.sub(overestimation);
  90. // After the relayed call has been executed and the actual charge estimated, the excess pre-charge is returned
  91. _token.safeTransfer(from, maxPossibleCharge.sub(actualCharge));
  92. }
  93. }
  94. /**
  95. * @title __unstable__ERC20Owned
  96. * @dev An ERC20 token owned by another contract, which has minting permissions and can use transferFrom to receive
  97. * anyone's tokens. This contract is an internal helper for GSNRecipientERC20Fee, and should not be used
  98. * outside of this context.
  99. */
  100. // solhint-disable-next-line contract-name-camelcase
  101. contract __unstable__ERC20Owned is ERC20, Ownable {
  102. uint256 private constant _UINT256_MAX = 2**256 - 1;
  103. constructor(string memory name, string memory symbol) public ERC20(name, symbol) { }
  104. // The owner (GSNRecipientERC20Fee) can mint tokens
  105. function mint(address account, uint256 amount) public onlyOwner {
  106. _mint(account, amount);
  107. }
  108. // The owner has 'infinite' allowance for all token holders
  109. function allowance(address tokenOwner, address spender) public view override returns (uint256) {
  110. if (spender == owner()) {
  111. return _UINT256_MAX;
  112. } else {
  113. return super.allowance(tokenOwner, spender);
  114. }
  115. }
  116. // Allowance for the owner cannot be changed (it is always 'infinite')
  117. function _approve(address tokenOwner, address spender, uint256 value) internal override {
  118. if (spender == owner()) {
  119. return;
  120. } else {
  121. super._approve(tokenOwner, spender, value);
  122. }
  123. }
  124. function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
  125. if (recipient == owner()) {
  126. _transfer(sender, recipient, amount);
  127. return true;
  128. } else {
  129. return super.transferFrom(sender, recipient, amount);
  130. }
  131. }
  132. }