GSNRecipientERC20Fee.sol 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. pragma solidity ^0.5.0;
  2. import "./GSNRecipient.sol";
  3. import "../math/SafeMath.sol";
  4. import "../ownership/Secondary.sol";
  5. import "../token/ERC20/SafeERC20.sol";
  6. import "../token/ERC20/ERC20.sol";
  7. import "../token/ERC20/ERC20Detailed.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__ERC20PrimaryAdmin;
  19. using SafeMath for uint256;
  20. enum GSNRecipientERC20FeeErrorCodes {
  21. INSUFFICIENT_BALANCE
  22. }
  23. __unstable__ERC20PrimaryAdmin 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__ERC20PrimaryAdmin(name, symbol, 18);
  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 {
  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 calldata,
  49. uint256 transactionFee,
  50. uint256 gasPrice,
  51. uint256,
  52. uint256,
  53. bytes calldata,
  54. uint256 maxPossibleCharge
  55. )
  56. external
  57. view
  58. returns (uint256, bytes memory)
  59. {
  60. if (_token.balanceOf(from) < maxPossibleCharge) {
  61. return _rejectRelayedCall(uint256(GSNRecipientERC20FeeErrorCodes.INSUFFICIENT_BALANCE));
  62. }
  63. return _approveRelayedCall(abi.encode(from, maxPossibleCharge, transactionFee, gasPrice));
  64. }
  65. /**
  66. * @dev Implements the precharge to the user. The maximum possible charge (depending on gas limit, gas price, and
  67. * fee) will be deducted from the user balance of gas payment token. Note that this is an overestimation of the
  68. * actual charge, necessary because we cannot predict how much gas the execution will actually need. The remainder
  69. * is returned to the user in {_postRelayedCall}.
  70. */
  71. function _preRelayedCall(bytes memory context) internal returns (bytes32) {
  72. (address from, uint256 maxPossibleCharge) = abi.decode(context, (address, uint256));
  73. // The maximum token charge is pre-charged from the user
  74. _token.safeTransferFrom(from, address(this), maxPossibleCharge);
  75. }
  76. /**
  77. * @dev Returns to the user the extra amount that was previously charged, once the actual execution cost is known.
  78. */
  79. function _postRelayedCall(bytes memory context, bool, uint256 actualCharge, bytes32) internal {
  80. (address from, uint256 maxPossibleCharge, uint256 transactionFee, uint256 gasPrice) =
  81. abi.decode(context, (address, uint256, uint256, uint256));
  82. // actualCharge is an _estimated_ charge, which assumes postRelayedCall will use all available gas.
  83. // This implementation's gas cost can be roughly estimated as 10k gas, for the two SSTORE operations in an
  84. // ERC20 transfer.
  85. uint256 overestimation = _computeCharge(POST_RELAYED_CALL_MAX_GAS.sub(10000), gasPrice, transactionFee);
  86. actualCharge = actualCharge.sub(overestimation);
  87. // After the relayed call has been executed and the actual charge estimated, the excess pre-charge is returned
  88. _token.safeTransfer(from, maxPossibleCharge.sub(actualCharge));
  89. }
  90. }
  91. /**
  92. * @title __unstable__ERC20PrimaryAdmin
  93. * @dev An ERC20 token owned by another contract, which has minting permissions and can use transferFrom to receive
  94. * anyone's tokens. This contract is an internal helper for GSNRecipientERC20Fee, and should not be used
  95. * outside of this context.
  96. */
  97. // solhint-disable-next-line contract-name-camelcase
  98. contract __unstable__ERC20PrimaryAdmin is ERC20, ERC20Detailed, Secondary {
  99. uint256 private constant UINT256_MAX = 2**256 - 1;
  100. constructor(string memory name, string memory symbol, uint8 decimals) public ERC20Detailed(name, symbol, decimals) {
  101. // solhint-disable-previous-line no-empty-blocks
  102. }
  103. // The primary account (GSNRecipientERC20Fee) can mint tokens
  104. function mint(address account, uint256 amount) public onlyPrimary {
  105. _mint(account, amount);
  106. }
  107. // The primary account has 'infinite' allowance for all token holders
  108. function allowance(address owner, address spender) public view returns (uint256) {
  109. if (spender == primary()) {
  110. return UINT256_MAX;
  111. } else {
  112. return super.allowance(owner, spender);
  113. }
  114. }
  115. // Allowance for the primary account cannot be changed (it is always 'infinite')
  116. function _approve(address owner, address spender, uint256 value) internal {
  117. if (spender == primary()) {
  118. return;
  119. } else {
  120. super._approve(owner, spender, value);
  121. }
  122. }
  123. function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
  124. if (recipient == primary()) {
  125. _transfer(sender, recipient, amount);
  126. return true;
  127. } else {
  128. return super.transferFrom(sender, recipient, amount);
  129. }
  130. }
  131. }