IEntropy.sol 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "./EntropyEvents.sol";
  4. interface IEntropy is EntropyEvents {
  5. // Register msg.sender as a randomness provider. The arguments are the provider's configuration parameters
  6. // and initial commitment. Re-registering the same provider rotates the provider's commitment (and updates
  7. // the feeInWei).
  8. //
  9. // chainLength is the number of values in the hash chain *including* the commitment, that is, chainLength >= 1.
  10. function register(
  11. uint128 feeInWei,
  12. bytes32 commitment,
  13. bytes calldata commitmentMetadata,
  14. uint64 chainLength,
  15. bytes calldata uri
  16. ) external;
  17. // Withdraw a portion of the accumulated fees for the provider msg.sender.
  18. // Calling this function will transfer `amount` wei to the caller (provided that they have accrued a sufficient
  19. // balance of fees in the contract).
  20. function withdraw(uint128 amount) external;
  21. // Withdraw a portion of the accumulated fees for provider. The msg.sender must be the fee manager for this provider.
  22. // Calling this function will transfer `amount` wei to the caller (provided that they have accrued a sufficient
  23. // balance of fees in the contract).
  24. function withdrawAsFeeManager(address provider, uint128 amount) external;
  25. // As a user, request a random number from `provider`. Prior to calling this method, the user should
  26. // generate a random number x and keep it secret. The user should then compute hash(x) and pass that
  27. // as the userCommitment argument. (You may call the constructUserCommitment method to compute the hash.)
  28. //
  29. // This method returns a sequence number. The user should pass this sequence number to
  30. // their chosen provider (the exact method for doing so will depend on the provider) to retrieve the provider's
  31. // number. The user should then call fulfillRequest to construct the final random number.
  32. //
  33. // This method will revert unless the caller provides a sufficient fee (at least getFee(provider)) as msg.value.
  34. // Note that excess value is *not* refunded to the caller.
  35. function request(
  36. address provider,
  37. bytes32 userCommitment,
  38. bool useBlockHash
  39. ) external payable returns (uint64 assignedSequenceNumber);
  40. // Request a random number. The method expects the provider address and a secret random number
  41. // in the arguments. It returns a sequence number.
  42. //
  43. // The address calling this function should be a contract that inherits from the IEntropyConsumer interface.
  44. // The `entropyCallback` method on that interface will receive a callback with the generated random number.
  45. //
  46. // This method will revert unless the caller provides a sufficient fee (at least getFee(provider)) as msg.value.
  47. // Note that excess value is *not* refunded to the caller.
  48. function requestWithCallback(
  49. address provider,
  50. bytes32 userRandomNumber
  51. ) external payable returns (uint64 assignedSequenceNumber);
  52. // Fulfill a request for a random number. This method validates the provided userRandomness and provider's proof
  53. // against the corresponding commitments in the in-flight request. If both values are validated, this function returns
  54. // the corresponding random number.
  55. //
  56. // Note that this function can only be called once per in-flight request. Calling this function deletes the stored
  57. // request information (so that the contract doesn't use a linear amount of storage in the number of requests).
  58. // If you need to use the returned random number more than once, you are responsible for storing it.
  59. function reveal(
  60. address provider,
  61. uint64 sequenceNumber,
  62. bytes32 userRevelation,
  63. bytes32 providerRevelation
  64. ) external returns (bytes32 randomNumber);
  65. // Fulfill a request for a random number. This method validates the provided userRandomness
  66. // and provider's revelation against the corresponding commitment in the in-flight request. If both values are validated
  67. // and the requestor address is a contract address, this function calls the requester's entropyCallback method with the
  68. // sequence number, provider address and the random number as arguments. Else if the requestor is an EOA, it won't call it.
  69. //
  70. // Note that this function can only be called once per in-flight request. Calling this function deletes the stored
  71. // request information (so that the contract doesn't use a linear amount of storage in the number of requests).
  72. // If you need to use the returned random number more than once, you are responsible for storing it.
  73. //
  74. // Anyone can call this method to fulfill a request, but the callback will only be made to the original requester.
  75. function revealWithCallback(
  76. address provider,
  77. uint64 sequenceNumber,
  78. bytes32 userRandomNumber,
  79. bytes32 providerRevelation
  80. ) external;
  81. function getProviderInfo(
  82. address provider
  83. ) external view returns (EntropyStructs.ProviderInfo memory info);
  84. function getDefaultProvider() external view returns (address provider);
  85. function getRequest(
  86. address provider,
  87. uint64 sequenceNumber
  88. ) external view returns (EntropyStructs.Request memory req);
  89. function getFee(address provider) external view returns (uint128 feeAmount);
  90. function getAccruedPythFees()
  91. external
  92. view
  93. returns (uint128 accruedPythFeesInWei);
  94. function setProviderFee(uint128 newFeeInWei) external;
  95. function setProviderFeeAsFeeManager(
  96. address provider,
  97. uint128 newFeeInWei
  98. ) external;
  99. function setProviderUri(bytes calldata newUri) external;
  100. // Set manager as the fee manager for the provider msg.sender.
  101. // After calling this function, manager will be able to set the provider's fees and withdraw them.
  102. // Only one address can be the fee manager for a provider at a time -- calling this function again with a new value
  103. // will override the previous value. Call this function with the all-zero address to disable the fee manager role.
  104. function setFeeManager(address manager) external;
  105. // Set the maximum number of hashes to record in a request. This should be set according to the maximum gas limit
  106. // the provider supports for callbacks.
  107. function setMaxNumHashes(uint32 maxNumHashes) external;
  108. // Advance the provider commitment and increase the sequence number.
  109. // This is used to reduce the `numHashes` required for future requests which leads to reduced gas usage.
  110. function advanceProviderCommitment(
  111. address provider,
  112. uint64 advancedSequenceNumber,
  113. bytes32 providerRevelation
  114. ) external;
  115. function constructUserCommitment(
  116. bytes32 userRandomness
  117. ) external pure returns (bytes32 userCommitment);
  118. function combineRandomValues(
  119. bytes32 userRandomness,
  120. bytes32 providerRandomness,
  121. bytes32 blockHash
  122. ) external pure returns (bytes32 combinedRandomness);
  123. }