You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This method is required by the IEntropyConsumer interface
@@ -63,7 +61,7 @@ contract CoinFlip is IEntropyConsumer {
63
61
64
62
```
65
63
66
-
The code implements a`CoinFlip` contract which inherits the `IEntropyConsumer` interface. We have also defined some events, properties and a constructor to instantiate the contract. One of the properties is of type `IEntropy` which is an interface imported from the Entropy SDK. The constructor also takes a `provider` argument. We will see how to populate these later.
64
+
The code implements a`CoinFlip` contract which inherits the `IEntropyConsumer` interface. We have also defined some events, properties and a constructor to instantiate the contract. One of the properties is of type `IEntropy` which is an interface imported from the Entropy SDK.
67
65
68
66
### Request a coin flip
69
67
@@ -73,17 +71,14 @@ Copy the following code into `CoinFlip.sol`.
73
71
contract CoinFlip {
74
72
// ... prior code omitted
75
73
76
-
function request(bytes32 userRandomNumber) external payable {
74
+
function request() external payable {
77
75
// get the required fee
78
-
uint128 requestFee = entropy.getFee(provider);
76
+
uint128 requestFee = entropy.getFeeV2();
79
77
// check if the user has sent enough fees
80
78
if (msg.value < requestFee) revert("not enough fees");
81
79
82
80
// pay the fees and request a random number from entropy
Users will invoke the `request` method to initiate a coin flip with a request fee and passes in a `userRandomNumber` argument — we’ll see how to generate this later. The method first retrieves the fee required to request a random number from Entropy. It then include the fee in the `requestWithCallback` method call to entropy. Finally, the method emits a `FlipRequested` event with a `sequenceNumber`. This event is also defined in the code snippet above.
90
+
Users will invoke the `request` method to initiate a coin flip, paying a fee in the process. The method first retrieves the fee required to request a random number from Entropy. It then includes the fee in the `requestV2` method call to Entropy. Finally, the method emits a `FlipRequested` event with a `sequenceNumber`. This event is also defined in the code snippet above.
96
91
97
92
### Handle the callback
98
93
@@ -119,7 +114,7 @@ contract CoinFlip {
119
114
```
120
115
121
116
Implement `entropyCallback` method which is required by the `IEntropyConsumer` Interface. Entropy calls back this method to fulfill a request. Entropy will call back this
122
-
method with the `sequenceNumber` of the request, the `providerAddress` from which the random number was requested and the generated `randomNumber`.
117
+
method with the `sequenceNumber` of the request, the `_providerAddress` from which the random number was requested and the generated `randomNumber`.
123
118
Finally, the method emits a `FlipResult` event with the result of the flip.
124
119
125
120
Yay! you have successfully implemented a coin flip contract.
@@ -158,7 +153,6 @@ The final step before deploying is to get the arguments for the contract's const
Copy file name to clipboardExpand all lines: pages/entropy/generate-random-numbers/evm.mdx
+44-63Lines changed: 44 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -36,114 +36,77 @@ Then add the following line to your `remappings.txt` :
36
36
The Solidity SDK exports two interfaces:
37
37
38
38
-[`IEntropyConsumer`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropyConsumer.sol) - The interface that your contract should implement. It makes sure that your contract is compliant with the Entropy contract.
39
-
-[`IEntropy`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol) - The interface to interact with the Entropy contract.
39
+
-[`IEntropyV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropyV2.sol) - The interface to interact with the Entropy contract.
40
40
You will need the address of an Entropy contract on your blockchain.
41
41
Consult the current [Entropy contract addresses](../contract-addresses) to find the address on your chain.
42
-
Once you have a contract address, instantiate an `IEntropy` contract in your solidity contract:
42
+
Once you have a contract address, instantiate an `IEntropyV2` contract in your solidity contract:
43
43
44
44
```solidity copy
45
45
import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
46
-
import { IEntropy } from "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
46
+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
47
47
48
48
// @param entropyAddress The address of the entropy contract.
49
49
contract YourContract is IEntropyConsumer {
50
-
IEntropy public entropy;
50
+
IEntropyV2 public entropy;
51
51
52
52
constructor(address entropyAddress) {
53
-
entropy = IEntropy(entropyAddress);
53
+
entropy = IEntropyV2(entropyAddress);
54
54
}
55
55
}
56
56
57
57
```
58
58
59
-
<Callouttype="info">
60
-
Entropy also requires selecting a **randomness provider**. The randomness provider is a third-party
61
-
who participates in the generation process. Each provider is identified by an address and hosts
62
-
a keeper service for fullfilling requests.
63
-
64
-
The simplest way to choose a provider is to use the [default provider](../contract-addresses).
65
-
The default provider for each contract and their corresponding URI is also listed in the
You can also get the default provider's address by calling the [`getDefaultProvider`](https://github.com/pyth-network/pyth-crosschain/blob/f8ebeb6af31d98f94ce73edade6da2ebab7b2456/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L94) method:
71
-
72
-
```solidity copy
73
-
address provider = entropy.getDefaultProvider();
74
-
```
75
-
76
59
## Usage
77
60
78
61
To generate a random number, follow these steps.
79
62
80
-
### 1. Generate a random number
81
-
82
-
Generate a 32-byte random number on the client side.
Invoke the [`requestV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L83) method of the `IEntropyV2` contract.
68
+
The `requestV2` method requires paying a fee in native gas tokens which is configured per-provider.
99
69
100
-
Invoke the [`requestWithCallback`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L83) method of the `IEntropy` contract.
101
-
The `requestWithCallback` method requires paying a fee in native gas tokens which is configured per-provider.
102
-
103
-
The fees differs for every chain and can be found at the [Current Fees](../current-fees) page. \
104
-
You can use the onchain method [`getFee`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L101) to calculate the fee for the default provider and send it as the value of the `requestWithCallback` call:
70
+
The fees differs for every chain and also varies over time depending on the chain's current gas price.
71
+
The current value for each chain can be found on the [Current Fees](../current-fees) page.
72
+
However, you should use the on-chain method [`getFeeV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L101) to compute the required fee and send it as the value of the `requestV2` call:
105
73
106
74
```solidity copy
107
-
function requestRandomNumber(bytes32 userRandomNumber) external payable {
This method returns a sequence number and emits a [`RequestedWithCallback`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/EntropyEvents.sol#L10) event. You can store this sequence number to identify the request in next step.
119
84
120
-
### 3. Implement callback for Entropy
85
+
Note that there are several variants of `requestV2` that allow the caller to configure the provider fulfilling the request and the gas limit for the callback.
86
+
Please see the method documentation in the IEntropyV2 interface. TODO: link
87
+
88
+
### 2. Implement the Entropy callback
121
89
122
90
```solidity {28-42} copy
123
91
pragma solidity ^0.8.0;
124
92
125
93
import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
126
-
import { IEntropy } from "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
94
+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
127
95
128
96
contract YourContract is IEntropyConsumer {
129
-
IEntropy entropy;
97
+
IEntropyV2 entropy;
130
98
131
99
// @param entropyAddress The address of the entropy contract.
132
100
constructor(address entropyAddress) {
133
-
entropy = IEntropy(entropyAddress);
101
+
entropy = IEntropyV2(entropyAddress);
134
102
}
135
103
136
-
// @param userRandomNumber The random number generated by the user.
137
-
function requestRandomNumber(bytes32 userRandomNumber) external payable {
138
-
// Get the default provider and the fee for the request
You can also get the default provider's address by calling the [`getDefaultProvider`](https://github.com/pyth-network/pyth-crosschain/blob/f8ebeb6af31d98f94ce73edade6da2ebab7b2456/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L94) method:
0 commit comments