Skip to content

Commit e9e9e27

Browse files
committed
update docs to v2 interface
1 parent 962918c commit e9e9e27

File tree

7 files changed

+84
-116
lines changed

7 files changed

+84
-116
lines changed

images/entropy-1.png

-210 KB
Binary file not shown.

images/entropy-2.png

-269 KB
Binary file not shown.

pages/entropy/create-your-first-entropy-app.mdx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,17 @@ Create a new file `CoinFlip.sol` in `contracts/src` directory and add the follow
4040
```solidity copy
4141
// SPDX-License-Identifier: UNLICENSED
4242
pragma solidity ^0.8.13;
43-
import "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
43+
import "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
4444
import "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
4545
4646
contract CoinFlip is IEntropyConsumer {
4747
event FlipRequested(uint64 sequenceNumber);
4848
event FlipResult(uint64 sequenceNumber, bool isHeads);
4949
5050
IEntropy entropy;
51-
address provider;
5251
53-
constructor(address _entropy, address _provider) {
52+
constructor(address _entropy) {
5453
entropy = IEntropy(_entropy);
55-
provider = _provider;
5654
}
5755
5856
// This method is required by the IEntropyConsumer interface
@@ -63,7 +61,7 @@ contract CoinFlip is IEntropyConsumer {
6361
6462
```
6563

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.
6765

6866
### Request a coin flip
6967

@@ -73,17 +71,14 @@ Copy the following code into `CoinFlip.sol`.
7371
contract CoinFlip {
7472
// ... prior code omitted
7573
76-
function request(bytes32 userRandomNumber) external payable {
74+
function request() external payable {
7775
// get the required fee
78-
uint128 requestFee = entropy.getFee(provider);
76+
uint128 requestFee = entropy.getFeeV2();
7977
// check if the user has sent enough fees
8078
if (msg.value < requestFee) revert("not enough fees");
8179
8280
// pay the fees and request a random number from entropy
83-
uint64 sequenceNumber = entropy.requestWithCallback{ value: requestFee }(
84-
provider,
85-
userRandomNumber
86-
);
81+
uint64 sequenceNumber = entropy.requestV2{ value: requestFee }();
8782
8883
// emit event
8984
emit FlipRequested(sequenceNumber);
@@ -92,7 +87,7 @@ contract CoinFlip {
9287
9388
```
9489

95-
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.
9691

9792
### Handle the callback
9893

@@ -119,7 +114,7 @@ contract CoinFlip {
119114
```
120115

121116
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`.
123118
Finally, the method emits a `FlipResult` event with the result of the flip.
124119

125120
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
158153

159154
```bash copy
160155
export ENTROPY_ADDRESS=0x4821932D0CDd71225A6d914706A621e0389D7061
161-
export PROVIDER_ADDRESS=0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344
162156
```
163157

164158
Finally, let's deploy the contracts. Run the following command:
@@ -167,7 +161,7 @@ Finally, let's deploy the contracts. Run the following command:
167161
forge create src/CoinFlip.sol:CoinFlip \
168162
--private-key $PRIVATE_KEY \
169163
--rpc-url $RPC_URL \
170-
--constructor-args $ENTROPY_ADDRESS $PROVIDER_ADDRESS
164+
--constructor-args $ENTROPY_ADDRESS
171165
```
172166

173167
You should see an output similar to:
@@ -206,7 +200,7 @@ Create a `script.js` file in `app` and add the following code to the script.
206200
```javascript copy
207201
const { Web3 } = require("web3");
208202
const CoinFlipAbi = require("../contracts/out/CoinFlip.sol/CoinFlip.json");
209-
const EntropyAbi = require("@pythnetwork/entropy-sdk-solidity/abis/IEntropy.json");
203+
const EntropyAbi = require("@pythnetwork/entropy-sdk-solidity/abis/IEntropyV2.json");
210204

211205
async function main() {
212206
const web3 = new Web3(process.env["RPC_URL"]);
@@ -240,14 +234,11 @@ async main() {
240234

241235
// Request a random number
242236

243-
// Generate user random number
244-
const userRandomNumber = web3.utils.randomHex(32);
245-
246-
const fee = await entropyContract.methods.getFee(process.env["PROVIDER_ADDRESS"]).call()
237+
const fee = await entropyContract.methods.getFeeV2().call()
247238
console.log(`fee : ${fee}`);
248239

249240
const requestReceipt = await coinFlipContract.methods
250-
.request(userRandomNumber)
241+
.request()
251242
.send({
252243
value: fee,
253244
from: address,

pages/entropy/current-fees.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Note that the fees shown below will vary over time with prevailing gas prices on
88
## Mainnet
99

1010
<Callout emoji="⚠️">
11-
The fees for mainnet are dynamically set. Always use the onchain method `entropy.getFee(entropyProvider){:solidity}` to get the current fee.
11+
The fees for mainnet are dynamically set. Always use the on-chain method `entropy.getFee(entropyProvider){:solidity}` to get the current fee.
1212
</Callout>
1313

1414
<EntropyFeeTable

pages/entropy/debug-callback-failures.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import { Callout } from "nextra/components";
44

5+
# TODO: replace this whole page with a guide about how to use the entropy explorer
6+
57
<Callout type="info" emoji="🔍">
68
**Quick Debug Tool**: Use the [Entropy
79
Debugger](https://entropy-debugger.pyth.network/) to quickly diagnose and

pages/entropy/generate-random-numbers/evm.mdx

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,114 +36,77 @@ Then add the following line to your `remappings.txt` :
3636
The Solidity SDK exports two interfaces:
3737

3838
- [`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.
4040
You will need the address of an Entropy contract on your blockchain.
4141
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:
4343

4444
```solidity copy
4545
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";
4747
4848
// @param entropyAddress The address of the entropy contract.
4949
contract YourContract is IEntropyConsumer {
50-
IEntropy public entropy;
50+
IEntropyV2 public entropy;
5151
5252
constructor(address entropyAddress) {
53-
entropy = IEntropy(entropyAddress);
53+
entropy = IEntropyV2(entropyAddress);
5454
}
5555
}
5656
5757
```
5858

59-
<Callout type="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
66-
[Entropy contract addresses](../contract-addresses).
67-
68-
</Callout>
69-
70-
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-
7659
## Usage
7760

7861
To generate a random number, follow these steps.
7962

80-
### 1. Generate a random number
81-
82-
Generate a 32-byte random number on the client side.
63+
### 1. Request a number from Entropy
8364

84-
<Tabs items={['web3.js', 'ethers.js']}>
85-
<Tabs.Tab>
86-
```javascript
87-
const userRandomNumber = web3.utils.randomHex(32);
88-
```
89-
</Tabs.Tab>
90-
91-
<Tabs.Tab>
92-
```javascript
93-
const userRandomNumber = ethers.utils.randomBytes(32);
94-
```
95-
</Tabs.Tab>
96-
</Tabs>
65+
# TODO: fix links and events
9766

98-
### 2. Request a number from Entropy
67+
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.
9969

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:
10573

10674
```solidity copy
107-
function requestRandomNumber(bytes32 userRandomNumber) external payable {
108-
uint256 fee = entropy.getFee(entropyProvider);
75+
function requestRandomNumber() external payable {
76+
uint256 fee = entropy.getFeeV2();
10977
110-
uint64 sequenceNumber = entropy.requestWithCallback{ value: fee }(
111-
entropyProvider,
112-
userRandomNumber
113-
);
78+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
11479
}
11580
11681
```
11782

11883
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.
11984

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
12189

12290
```solidity {28-42} copy
12391
pragma solidity ^0.8.0;
12492
12593
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";
12795
12896
contract YourContract is IEntropyConsumer {
129-
IEntropy entropy;
97+
IEntropyV2 entropy;
13098
13199
// @param entropyAddress The address of the entropy contract.
132100
constructor(address entropyAddress) {
133-
entropy = IEntropy(entropyAddress);
101+
entropy = IEntropyV2(entropyAddress);
134102
}
135103
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
139-
address entropyProvider = entropy.getDefaultProvider();
140-
uint256 fee = entropy.getFee(entropyProvider);
104+
function requestRandomNumber() external payable {
105+
// Get the fee for the request
106+
uint256 fee = entropy.getFeeV2();
141107
142108
// Request the random number with the callback
143-
uint64 sequenceNumber = entropy.requestWithCallback{ value: fee }(
144-
entropyProvider,
145-
userRandomNumber
146-
);
109+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
147110
// Store the sequence number to identify the callback request
148111
}
149112
@@ -154,6 +117,7 @@ contract YourContract is IEntropyConsumer {
154117
// This method **must** be implemented on the same contract that requested the random number.
155118
// This method should **never** return an error -- if it returns an error, then the keeper will not be able to invoke the callback.
156119
// If you are having problems receiving the callback, the most likely cause is that the callback is erroring.
120+
// TODO: point to debugger
157121
// See the callback debugging guide here to identify the error https://docs.pyth.network/entropy/debug-callback-failures
158122
function entropyCallback(
159123
uint64 sequenceNumber,
@@ -200,3 +164,20 @@ Check the [Current Fees](../current-fees) to find the current fee for each provi
200164
### Best Practices
201165

202166
Check out the [Best Practices](../best-practices) guide for tips to limit gas usage, or generate multiple random numbers in a single transaction.
167+
168+
<Callout type="info">
169+
Entropy also requires selecting a **randomness provider**. The randomness provider is a third-party
170+
who participates in the generation process. Each provider is identified by an address and hosts
171+
a keeper service for fullfilling requests.
172+
173+
The simplest way to choose a provider is to use the [default provider](../contract-addresses).
174+
The default provider for each contract and their corresponding URI is also listed in the
175+
[Entropy contract addresses](../contract-addresses).
176+
177+
</Callout>
178+
179+
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:
180+
181+
```solidity copy
182+
address provider = entropy.getDefaultProvider();
183+
```

0 commit comments

Comments
 (0)