22pragma solidity ^ 0.8.0 ;
33
44// Import the entropy SDK in order to interact with the entropy contracts
5- import "entropy-sdk-solidity/IEntropy.sol " ;
6- import "entropy-sdk-solidity/IEntropyConsumer.sol " ;
5+ import "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol " ;
6+ import "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol " ;
7+ // Import the EntropyStructsV2 contract to get the ProviderInfo struct
8+ import "@pythnetwork/entropy-sdk-solidity/EntropyStructsV2.sol " ;
79
810library CoinFlipErrors {
911 error IncorrectSender ();
@@ -30,39 +32,73 @@ contract CoinFlip is IEntropyConsumer {
3032 // and a specific entropy provider to use for requests. Each provider commits to a sequence of random numbers.
3133 // Providers are then responsible for fulfilling a request on chain by revealing their random number.
3234 // Users should choose a reliable provider who they trust to uphold these commitments.
33- // (For the moment, the only available provider is 0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344)
34- IEntropy private entropy;
35+ IEntropyV2 private entropy;
3536 address private entropyProvider;
3637
3738 constructor (address _entropy , address _entropyProvider ) {
38- entropy = IEntropy (_entropy);
39+ entropy = IEntropyV2 (_entropy);
3940 entropyProvider = _entropyProvider;
4041 }
4142
42- // Request to flip a coin. The caller should generate and pass in a random number when calling this method.
43- function requestFlip (bytes32 userRandomNumber ) external payable {
43+ // Request to flip a coin.
44+ function requestFlip () external payable {
4445 // The entropy protocol requires the caller to pay a fee (in native gas tokens) per requested random number.
4546 // This fee can either be paid by the contract itself or passed on to the end user.
4647 // This implementation of the requestFlip method passes on the fee to the end user.
47- uint256 fee = entropy.getFee (entropyProvider );
48+ uint256 fee = entropy.getFeeV2 ( );
4849 if (msg .value < fee) {
4950 revert CoinFlipErrors.InsufficientFee ();
5051 }
5152
5253 // Request the random number from the Entropy protocol. The call returns a sequence number that uniquely
5354 // identifies the generated random number. Callers can use this sequence number to match which request
5455 // is being revealed in the next stage of the protocol.
55- uint64 sequenceNumber = entropy.requestWithCallback {value: fee}(
56- entropyProvider,
57- userRandomNumber
58- );
56+ // This requestV2 function will trust the provider to draw a random number.
57+ uint64 sequenceNumber = entropy.requestV2 {value: fee}();
5958
6059 emit FlipRequest (sequenceNumber);
6160 }
6261
63- // Get the fee to flip a coin. See the comment above about fees.
64- function getFlipFee () public view returns (uint256 fee ) {
65- fee = entropy.getFee (entropyProvider);
62+ // Request to flip a coin with a custom gas limit.
63+ function requestFlipWithCustomGasLimit (uint32 gasLimit ) external payable {
64+ uint256 fee = entropy.getFeeV2 (gasLimit);
65+ if (msg .value < fee) {
66+ revert CoinFlipErrors.InsufficientFee ();
67+ }
68+
69+ uint64 sequenceNumber = entropy.requestV2 {value: fee}(gasLimit);
70+
71+ emit FlipRequest (sequenceNumber);
72+ }
73+
74+ // Request to flip a coin with a custom provider and custom gas limit.
75+ function requestFlipWithCustomProviderAndGasLimit (address provider , uint32 gasLimit ) external payable {
76+ uint256 fee = entropy.getFeeV2 (provider, gasLimit);
77+ if (msg .value < fee) {
78+ revert CoinFlipErrors.InsufficientFee ();
79+ }
80+
81+ uint64 sequenceNumber = entropy.requestV2 {value: fee}(provider, gasLimit);
82+
83+ emit FlipRequest (sequenceNumber);
84+ }
85+
86+ // Request to flip a coin with a custom provider and custom gas limit and userContribution / Random Number.
87+ function requestFlipWithCustomProviderAndGasLimitAndUserContribution (address provider , uint32 gasLimit , bytes32 userContribution ) external payable {
88+ uint256 fee = entropy.getFeeV2 (provider, gasLimit);
89+ if (msg .value < fee) {
90+ revert CoinFlipErrors.InsufficientFee ();
91+ }
92+
93+ uint64 sequenceNumber = entropy.requestV2 {value: fee}(provider, userContribution, gasLimit);
94+
95+ emit FlipRequest (sequenceNumber);
96+ }
97+
98+ // Get the default gas limit for the default provider.
99+ function getDefaultProviderGasLimit () public view returns (uint32 ) {
100+ EntropyStructsV2.ProviderInfo memory providerInfo = entropy.getProviderInfoV2 (entropy.getDefaultProvider ());
101+ return providerInfo.defaultGasLimit;
66102 }
67103
68104 // This method is required by the IEntropyConsumer interface.
@@ -84,5 +120,9 @@ contract CoinFlip is IEntropyConsumer {
84120 return address (entropy);
85121 }
86122
123+ function getFlipFee () public view returns (uint256 ) {
124+ return entropy.getFeeV2 ();
125+ }
126+
87127 receive () external payable {}
88128}
0 commit comments