IEntropy.sol 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "./EntropyEvents.sol";
  4. import "./EntropyEventsV2.sol";
  5. import "./EntropyStructsV2.sol";
  6. interface IEntropy is EntropyEvents, EntropyEventsV2 {
  7. // Register msg.sender as a randomness provider. The arguments are the provider's configuration parameters
  8. // and initial commitment. Re-registering the same provider rotates the provider's commitment (and updates
  9. // the feeInWei).
  10. //
  11. // chainLength is the number of values in the hash chain *including* the commitment, that is, chainLength >= 1.
  12. function register(
  13. uint128 feeInWei,
  14. bytes32 commitment,
  15. bytes calldata commitmentMetadata,
  16. uint64 chainLength,
  17. bytes calldata uri
  18. ) external;
  19. // Withdraw a portion of the accumulated fees for the provider msg.sender.
  20. // Calling this function will transfer `amount` wei to the caller (provided that they have accrued a sufficient
  21. // balance of fees in the contract).
  22. function withdraw(uint128 amount) external;
  23. // Withdraw a portion of the accumulated fees for provider. The msg.sender must be the fee manager for this provider.
  24. // Calling this function will transfer `amount` wei to the caller (provided that they have accrued a sufficient
  25. // balance of fees in the contract).
  26. function withdrawAsFeeManager(address provider, uint128 amount) external;
  27. /// @notice Request a random number using the default provider with default gas limit
  28. /// @return assignedSequenceNumber A unique identifier for this request
  29. /// @dev The address calling this function should be a contract that inherits from the IEntropyConsumer interface.
  30. /// The `entropyCallback` method on that interface will receive a callback with the returned sequence number and
  31. /// the generated random number.
  32. ///
  33. /// `entropyCallback` will be run with the `gasLimit` provided to this function.
  34. /// The `gasLimit` will be rounded up to a multiple of 10k (e.g., 19000 -> 20000), and furthermore is lower bounded
  35. /// by the provider's configured default limit.
  36. ///
  37. /// This method will revert unless the caller provides a sufficient fee (at least `getFeeV2()`) as msg.value.
  38. /// Note that the fee can change over time. Callers of this method should explicitly compute `getFeeV2()`
  39. /// prior to each invocation (as opposed to hardcoding a value). Further note that excess value is *not* refunded to the caller.
  40. ///
  41. /// Note that this method uses an in-contract PRNG to generate the user's portion of the random number.
  42. /// Users must trust this PRNG in order to prove the result is random. If you wish to avoid this trust assumption,
  43. /// call a variant of `requestV2` that accepts a `userRandomNumber` parameter.
  44. function requestV2()
  45. external
  46. payable
  47. returns (uint64 assignedSequenceNumber);
  48. /// @notice Request a random number using the default provider with specified gas limit
  49. /// @param gasLimit The gas limit for the callback function.
  50. /// @return assignedSequenceNumber A unique identifier for this request
  51. /// @dev The address calling this function should be a contract that inherits from the IEntropyConsumer interface.
  52. /// The `entropyCallback` method on that interface will receive a callback with the returned sequence number and
  53. /// the generated random number.
  54. ///
  55. /// `entropyCallback` will be run with the `gasLimit` provided to this function.
  56. /// The `gasLimit` will be rounded up to a multiple of 10k (e.g., 19000 -> 20000), and furthermore is lower bounded
  57. /// by the provider's configured default limit.
  58. ///
  59. /// This method will revert unless the caller provides a sufficient fee (at least `getFeeV2(gasLimit)`) as msg.value.
  60. /// Note that the fee can change over time. Callers of this method should explicitly compute `getFeeV2(gasLimit)`
  61. /// prior to each invocation (as opposed to hardcoding a value). Further note that excess value is *not* refunded to the caller.
  62. ///
  63. /// Note that this method uses an in-contract PRNG to generate the user's portion of the random number.
  64. /// Users must trust this PRNG in order to prove the result is random. If you wish to avoid this trust assumption,
  65. /// call a variant of `requestV2` that accepts a `userRandomNumber` parameter.
  66. function requestV2(
  67. uint32 gasLimit
  68. ) external payable returns (uint64 assignedSequenceNumber);
  69. /// @notice Request a random number from a specific provider with specified gas limit
  70. /// @param provider The address of the provider to request from
  71. /// @param gasLimit The gas limit for the callback function
  72. /// @return assignedSequenceNumber A unique identifier for this request
  73. /// @dev The address calling this function should be a contract that inherits from the IEntropyConsumer interface.
  74. /// The `entropyCallback` method on that interface will receive a callback with the returned sequence number and
  75. /// the generated random number.
  76. ///
  77. /// `entropyCallback` will be run with the `gasLimit` provided to this function.
  78. /// The `gasLimit` will be rounded up to a multiple of 10k (e.g., 19000 -> 20000), and furthermore is lower bounded
  79. /// by the provider's configured default limit.
  80. ///
  81. /// This method will revert unless the caller provides a sufficient fee (at least `getFeeV2(provider, gasLimit)`) as msg.value.
  82. /// Note that provider fees can change over time. Callers of this method should explicitly compute `getFeeV2(provider, gasLimit)`
  83. /// prior to each invocation (as opposed to hardcoding a value). Further note that excess value is *not* refunded to the caller.
  84. ///
  85. /// Note that this method uses an in-contract PRNG to generate the user's portion of the random number.
  86. /// Users must trust this PRNG in order to prove the result is random. If you wish to avoid this trust assumption,
  87. /// call a variant of `requestV2` that accepts a `userRandomNumber` parameter.
  88. function requestV2(
  89. address provider,
  90. uint32 gasLimit
  91. ) external payable returns (uint64 assignedSequenceNumber);
  92. /// @notice Request a random number from a specific provider with a user-provided random number and gas limit
  93. /// @param provider The address of the provider to request from
  94. /// @param userRandomNumber A random number provided by the user for additional entropy
  95. /// @param gasLimit The gas limit for the callback function
  96. /// @return assignedSequenceNumber A unique identifier for this request
  97. /// @dev The address calling this function should be a contract that inherits from the IEntropyConsumer interface.
  98. /// The `entropyCallback` method on that interface will receive a callback with the returned sequence number and
  99. /// the generated random number.
  100. ///
  101. /// `entropyCallback` will be run with the `gasLimit` provided to this function.
  102. /// The `gasLimit` will be rounded up to a multiple of 10k (e.g., 19000 -> 20000), and furthermore is lower bounded
  103. /// by the provider's configured default limit.
  104. ///
  105. /// This method will revert unless the caller provides a sufficient fee (at least `getFeeV2(provider, gasLimit)`) as msg.value.
  106. /// Note that provider fees can change over time. Callers of this method should explicitly compute `getFeeV2(provider, gasLimit)`
  107. /// prior to each invocation (as opposed to hardcoding a value). Further note that excess value is *not* refunded to the caller.
  108. function requestV2(
  109. address provider,
  110. bytes32 userRandomNumber,
  111. uint32 gasLimit
  112. ) external payable returns (uint64 assignedSequenceNumber);
  113. // As a user, request a random number from `provider`. Prior to calling this method, the user should
  114. // generate a random number x and keep it secret. The user should then compute hash(x) and pass that
  115. // as the userCommitment argument. (You may call the constructUserCommitment method to compute the hash.)
  116. //
  117. // This method returns a sequence number. The user should pass this sequence number to
  118. // their chosen provider (the exact method for doing so will depend on the provider) to retrieve the provider's
  119. // number. The user should then call fulfillRequest to construct the final random number.
  120. //
  121. // This method will revert unless the caller provides a sufficient fee (at least getFee(provider)) as msg.value.
  122. // Note that excess value is *not* refunded to the caller.
  123. function request(
  124. address provider,
  125. bytes32 userCommitment,
  126. bool useBlockHash
  127. ) external payable returns (uint64 assignedSequenceNumber);
  128. // Request a random number. The method expects the provider address and a secret random number
  129. // in the arguments. It returns a sequence number.
  130. //
  131. // The address calling this function should be a contract that inherits from the IEntropyConsumer interface.
  132. // The `entropyCallback` method on that interface will receive a callback with the generated random number.
  133. // `entropyCallback` will be run with the provider's default gas limit (see `getProviderInfo(provider).defaultGasLimit`).
  134. // If your callback needs additional gas, please use `requestWithCallbackAndGasLimit`.
  135. //
  136. // This method will revert unless the caller provides a sufficient fee (at least `getFee(provider)`) as msg.value.
  137. // Note that excess value is *not* refunded to the caller.
  138. function requestWithCallback(
  139. address provider,
  140. bytes32 userRandomNumber
  141. ) external payable returns (uint64 assignedSequenceNumber);
  142. // Fulfill a request for a random number. This method validates the provided userRandomness and provider's proof
  143. // against the corresponding commitments in the in-flight request. If both values are validated, this function returns
  144. // the corresponding random number.
  145. //
  146. // Note that this function can only be called once per in-flight request. Calling this function deletes the stored
  147. // request information (so that the contract doesn't use a linear amount of storage in the number of requests).
  148. // If you need to use the returned random number more than once, you are responsible for storing it.
  149. function reveal(
  150. address provider,
  151. uint64 sequenceNumber,
  152. bytes32 userRevelation,
  153. bytes32 providerRevelation
  154. ) external returns (bytes32 randomNumber);
  155. // Fulfill a request for a random number. This method validates the provided userRandomness
  156. // and provider's revelation against the corresponding commitment in the in-flight request. If both values are validated
  157. // and the requestor address is a contract address, this function calls the requester's entropyCallback method with the
  158. // sequence number, provider address and the random number as arguments. Else if the requestor is an EOA, it won't call it.
  159. //
  160. // Note that this function can only be called once per in-flight request. Calling this function deletes the stored
  161. // request information (so that the contract doesn't use a linear amount of storage in the number of requests).
  162. // If you need to use the returned random number more than once, you are responsible for storing it.
  163. //
  164. // Anyone can call this method to fulfill a request, but the callback will only be made to the original requester.
  165. function revealWithCallback(
  166. address provider,
  167. uint64 sequenceNumber,
  168. bytes32 userRandomNumber,
  169. bytes32 providerRevelation
  170. ) external;
  171. function getProviderInfo(
  172. address provider
  173. ) external view returns (EntropyStructs.ProviderInfo memory info);
  174. function getProviderInfoV2(
  175. address provider
  176. ) external view returns (EntropyStructsV2.ProviderInfo memory info);
  177. function getDefaultProvider() external view returns (address provider);
  178. function getRequest(
  179. address provider,
  180. uint64 sequenceNumber
  181. ) external view returns (EntropyStructs.Request memory req);
  182. function getRequestV2(
  183. address provider,
  184. uint64 sequenceNumber
  185. ) external view returns (EntropyStructsV2.Request memory req);
  186. // Get the fee charged by provider for a request with the default gasLimit (`request` or `requestWithCallback`).
  187. // If you are calling any of the `requestV2` methods, please use `getFeeV2`.
  188. function getFee(address provider) external view returns (uint128 feeAmount);
  189. // Get the fee charged by the default provider for the default gas limit.
  190. // Use this function to determine the fee to pass to `requestV2`.
  191. function getFeeV2() external view returns (uint128 feeAmount);
  192. // Get the fee charged by the default provider for the specified gas limit.
  193. // Use this function to determine the fee to pass to `requestV2`.
  194. function getFeeV2(
  195. uint32 gasLimit
  196. ) external view returns (uint128 feeAmount);
  197. // Get the fee charged by `provider` for a request with a specific `gasLimit`.
  198. // Use this function to determine the fee to pass to `requestV2`.
  199. function getFeeV2(
  200. address provider,
  201. uint32 gasLimit
  202. ) external view returns (uint128 feeAmount);
  203. function getAccruedPythFees()
  204. external
  205. view
  206. returns (uint128 accruedPythFeesInWei);
  207. function setProviderFee(uint128 newFeeInWei) external;
  208. function setProviderFeeAsFeeManager(
  209. address provider,
  210. uint128 newFeeInWei
  211. ) external;
  212. function setProviderUri(bytes calldata newUri) external;
  213. // Set manager as the fee manager for the provider msg.sender.
  214. // After calling this function, manager will be able to set the provider's fees and withdraw them.
  215. // Only one address can be the fee manager for a provider at a time -- calling this function again with a new value
  216. // will override the previous value. Call this function with the all-zero address to disable the fee manager role.
  217. function setFeeManager(address manager) external;
  218. // Set the maximum number of hashes to record in a request. This should be set according to the maximum gas limit
  219. // the provider supports for callbacks.
  220. function setMaxNumHashes(uint32 maxNumHashes) external;
  221. // Set the default gas limit for a request. If 0, no
  222. function setDefaultGasLimit(uint32 gasLimit) external;
  223. // Advance the provider commitment and increase the sequence number.
  224. // This is used to reduce the `numHashes` required for future requests which leads to reduced gas usage.
  225. function advanceProviderCommitment(
  226. address provider,
  227. uint64 advancedSequenceNumber,
  228. bytes32 providerRevelation
  229. ) external;
  230. function constructUserCommitment(
  231. bytes32 userRandomness
  232. ) external pure returns (bytes32 userCommitment);
  233. function combineRandomValues(
  234. bytes32 userRandomness,
  235. bytes32 providerRandomness,
  236. bytes32 blockHash
  237. ) external pure returns (bytes32 combinedRandomness);
  238. }