GSNBouncerBase.sol 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. pragma solidity ^0.5.0;
  2. import "../IRelayRecipient.sol";
  3. /*
  4. * @dev Base contract used to implement GSNBouncers.
  5. *
  6. * > This contract does not perform all required tasks to implement a GSN
  7. * recipient contract: end users should use `GSNRecipient` instead.
  8. */
  9. contract GSNBouncerBase is IRelayRecipient {
  10. uint256 constant private RELAYED_CALL_ACCEPTED = 0;
  11. uint256 constant private RELAYED_CALL_REJECTED = 11;
  12. // How much gas is forwarded to postRelayedCall
  13. uint256 constant internal POST_RELAYED_CALL_MAX_GAS = 100000;
  14. // Base implementations for pre and post relayedCall: only RelayHub can invoke them, and data is forwarded to the
  15. // internal hook.
  16. /**
  17. * @dev See `IRelayRecipient.preRelayedCall`.
  18. *
  19. * This function should not be overriden directly, use `_preRelayedCall` instead.
  20. *
  21. * * Requirements:
  22. *
  23. * - the caller must be the `RelayHub` contract.
  24. */
  25. function preRelayedCall(bytes calldata context) external returns (bytes32) {
  26. require(msg.sender == getHubAddr(), "GSNBouncerBase: caller is not RelayHub");
  27. return _preRelayedCall(context);
  28. }
  29. /**
  30. * @dev See `IRelayRecipient.postRelayedCall`.
  31. *
  32. * This function should not be overriden directly, use `_postRelayedCall` instead.
  33. *
  34. * * Requirements:
  35. *
  36. * - the caller must be the `RelayHub` contract.
  37. */
  38. function postRelayedCall(bytes calldata context, bool success, uint256 actualCharge, bytes32 preRetVal) external {
  39. require(msg.sender == getHubAddr(), "GSNBouncerBase: caller is not RelayHub");
  40. _postRelayedCall(context, success, actualCharge, preRetVal);
  41. }
  42. /**
  43. * @dev Return this in acceptRelayedCall to proceed with the execution of a relayed call. Note that this contract
  44. * will be charged a fee by RelayHub
  45. */
  46. function _approveRelayedCall() internal pure returns (uint256, bytes memory) {
  47. return _approveRelayedCall("");
  48. }
  49. /**
  50. * @dev See `GSNBouncerBase._approveRelayedCall`.
  51. *
  52. * This overload forwards `context` to _preRelayedCall and _postRelayedCall.
  53. */
  54. function _approveRelayedCall(bytes memory context) internal pure returns (uint256, bytes memory) {
  55. return (RELAYED_CALL_ACCEPTED, context);
  56. }
  57. /**
  58. * @dev Return this in acceptRelayedCall to impede execution of a relayed call. No fees will be charged.
  59. */
  60. function _rejectRelayedCall(uint256 errorCode) internal pure returns (uint256, bytes memory) {
  61. return (RELAYED_CALL_REJECTED + errorCode, "");
  62. }
  63. // Empty hooks for pre and post relayed call: users only have to define these if they actually use them.
  64. function _preRelayedCall(bytes memory) internal returns (bytes32) {
  65. // solhint-disable-previous-line no-empty-blocks
  66. }
  67. function _postRelayedCall(bytes memory, bool, uint256, bytes32) internal {
  68. // solhint-disable-previous-line no-empty-blocks
  69. }
  70. /*
  71. * @dev Calculates how much RelaHub will charge a recipient for using `gas` at a `gasPrice`, given a relayer's
  72. * `serviceFee`.
  73. */
  74. function _computeCharge(uint256 gas, uint256 gasPrice, uint256 serviceFee) internal pure returns (uint256) {
  75. // The fee is expressed as a percentage. E.g. a value of 40 stands for a 40% fee, so the recipient will be
  76. // charged for 1.4 times the spent amount.
  77. return (gas * gasPrice * (100 + serviceFee)) / 100;
  78. }
  79. }