2
2
pragma solidity ^ 0.8.0 ;
3
3
4
4
// 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 " ;
7
9
8
10
library CoinFlipErrors {
9
11
error IncorrectSender ();
@@ -30,39 +32,73 @@ contract CoinFlip is IEntropyConsumer {
30
32
// and a specific entropy provider to use for requests. Each provider commits to a sequence of random numbers.
31
33
// Providers are then responsible for fulfilling a request on chain by revealing their random number.
32
34
// 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;
35
36
address private entropyProvider;
36
37
37
38
constructor (address _entropy , address _entropyProvider ) {
38
- entropy = IEntropy (_entropy);
39
+ entropy = IEntropyV2 (_entropy);
39
40
entropyProvider = _entropyProvider;
40
41
}
41
42
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 {
44
45
// The entropy protocol requires the caller to pay a fee (in native gas tokens) per requested random number.
45
46
// This fee can either be paid by the contract itself or passed on to the end user.
46
47
// 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 ( );
48
49
if (msg .value < fee) {
49
50
revert CoinFlipErrors.InsufficientFee ();
50
51
}
51
52
52
53
// Request the random number from the Entropy protocol. The call returns a sequence number that uniquely
53
54
// identifies the generated random number. Callers can use this sequence number to match which request
54
55
// 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}();
59
58
60
59
emit FlipRequest (sequenceNumber);
61
60
}
62
61
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;
66
102
}
67
103
68
104
// This method is required by the IEntropyConsumer interface.
@@ -84,5 +120,9 @@ contract CoinFlip is IEntropyConsumer {
84
120
return address (entropy);
85
121
}
86
122
123
+ function getFlipFee () public view returns (uint256 ) {
124
+ return entropy.getFeeV2 ();
125
+ }
126
+
87
127
receive () external payable {}
88
128
}
0 commit comments