Skip to content

Commit 5a25a03

Browse files
committed
update entropy examples to use new interface
1 parent 2cd32ee commit 5a25a03

File tree

4 files changed

+22
-41
lines changed

4 files changed

+22
-41
lines changed

entropy/coin_flip/app/src/flip_coin.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,14 @@ async function main() {
7373
client,
7474
});
7575

76-
console.log("1. Generating user's random number...");
77-
78-
const randomNumber: `0x${string}` = `0x${crypto
79-
.randomBytes(32)
80-
.toString("hex")}`;
81-
console.log(`User Generated Random number: ${randomNumber}`);
82-
83-
console.log("\n2. Requesting coin flip...");
76+
console.log("\n1. Requesting coin flip...");
8477

8578
const flipFee = await coinFlipContract.read.getFlipFee();
8679
console.log(`Flip Fee: ${flipFee} wei`);
8780

88-
console.log("\n3. Sending request to flip coin...");
81+
console.log("\n2. Sending request to flip coin...");
8982

90-
const flipTxHash = await coinFlipContract.write.requestFlip([randomNumber], {
83+
const flipTxHash = await coinFlipContract.write.requestFlip([], {
9184
value: flipFee,
9285
});
9386
console.log(`Transaction Hash: ${flipTxHash}`);
@@ -106,7 +99,7 @@ async function main() {
10699

107100
console.log(`\nSequence Number: ${sequenceNumber}`);
108101

109-
console.log("\n4. Waiting for flip result...");
102+
console.log("\n3. Waiting for flip result...");
110103
const result = await new Promise((resolve, reject) => {
111104
const unwatch = coinFlipContract.watchEvent.FlipResult({
112105
fromBlock: receipt.blockNumber - 1n,

entropy/coin_flip/contract/scripts/deploy.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ RPC_URL=https://sepolia-rollup.arbitrum.io/rpc
55

66
# The address of the Pyth contract on your network. See the list of contract addresses here https://docs.pyth.network/documentation/pythnet-price-feeds/evm
77
ENTROPY_CONTRACT_ADDRESS="0x549Ebba8036Ab746611B4fFA1423eb0A4Df61440"
8-
PROVIDER="0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344"
98

109
# Deployments
1110
# optimism-sepolia 0x2eE67fF5d8548fF544f2c178a0FcAFe503A634Be
@@ -18,4 +17,3 @@ forge create src/CoinFlip.sol:CoinFlip \
1817
--rpc-url $RPC_URL \
1918
--constructor-args \
2019
$ENTROPY_CONTRACT_ADDRESS \
21-
$PROVIDER

entropy/coin_flip/contract/src/CoinFlip.sol

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pragma 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";
5+
import "entropy-sdk-solidity/IEntropyV2.sol";
66
import "entropy-sdk-solidity/IEntropyConsumer.sol";
77

88
library CoinFlipErrors {
@@ -26,43 +26,38 @@ contract CoinFlip is IEntropyConsumer {
2626
// Event emitted when the result of the coin flip is known.
2727
event FlipResult(uint64 sequenceNumber, bool isHeads);
2828

29-
// Contracts using Pyth Entropy should import the solidity SDK and then store both the Entropy contract
30-
// and a specific entropy provider to use for requests. Each provider commits to a sequence of random numbers.
31-
// Providers are then responsible for fulfilling a request on chain by revealing their random number.
32-
// 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-
address private entropyProvider;
29+
// Contracts using Pyth Entropy should import the solidity SDK and then store the Entropy contract
30+
// address in the constructor.
31+
IEntropyV2 private entropy;
3632

37-
constructor(address _entropy, address _entropyProvider) {
33+
constructor(address _entropy) {
3834
entropy = IEntropy(_entropy);
39-
entropyProvider = _entropyProvider;
4035
}
4136

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 {
37+
// Request to flip a coin.
38+
function requestFlip() external payable {
4439
// The entropy protocol requires the caller to pay a fee (in native gas tokens) per requested random number.
4540
// This fee can either be paid by the contract itself or passed on to the end user.
4641
// This implementation of the requestFlip method passes on the fee to the end user.
47-
uint256 fee = entropy.getFee(entropyProvider);
42+
uint256 fee = entropy.getFeeV2();
4843
if (msg.value < fee) {
4944
revert CoinFlipErrors.InsufficientFee();
5045
}
5146

5247
// Request the random number from the Entropy protocol. The call returns a sequence number that uniquely
5348
// identifies the generated random number. Callers can use this sequence number to match which request
5449
// is being revealed in the next stage of the protocol.
55-
uint64 sequenceNumber = entropy.requestWithCallback{value: fee}(
56-
entropyProvider,
57-
userRandomNumber
58-
);
50+
//
51+
// Note that callers can also request a specific gas limit for the callback by passing a gasLimit parameter
52+
// to this function. See the IEntropyV2 interface for details.
53+
uint64 sequenceNumber = entropy.requestV2{value: fee}();
5954

6055
emit FlipRequest(sequenceNumber);
6156
}
6257

6358
// Get the fee to flip a coin. See the comment above about fees.
6459
function getFlipFee() public view returns (uint256 fee) {
65-
fee = entropy.getFee(entropyProvider);
60+
fee = entropy.getFeeV2();
6661
}
6762

6863
// This method is required by the IEntropyConsumer interface.

entropy/growing/contract/contracts/NFTGrowth.sol

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pragma solidity ^0.8.24;
33

44
import {IEntropyConsumer} from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
5-
import {IEntropy} from "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
5+
import {IEntropyV2} from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
66

77
import "./NFT.sol";
88

@@ -60,7 +60,6 @@ event NftGrowthRequested(
6060

6161
contract NFTGrowth is NFT, IEntropyConsumer {
6262
IEntropy entropy;
63-
address entropyProvider;
6463
uint256 maxLevel = 5;
6564
uint256 successChance = 4000;
6665
uint256 failChance = 4000;
@@ -72,9 +71,8 @@ contract NFTGrowth is NFT, IEntropyConsumer {
7271

7372
mapping(uint256 => NFTLock) public nftLock;
7473

75-
constructor(address _entropy, address _provider) {
74+
constructor(address _entropy) {
7675
entropy = IEntropy(_entropy);
77-
entropyProvider = _provider;
7876
}
7977

8078
function requireLock(uint256 tokenId) private view {
@@ -104,16 +102,13 @@ contract NFTGrowth is NFT, IEntropyConsumer {
104102
require(nftInfo[tokenId].status == NFTStatus.ALIVE, "NFT is dead");
105103
require(nftInfo[tokenId].level < maxLevel, "Already max level");
106104

107-
uint128 requestFee = entropy.getFee(entropyProvider);
105+
uint128 requestFee = entropy.getFeeV2();
108106
require(msg.value >= requestFee, "Not enough fees");
109107

110108
nftLock[tokenId].status = LockStatus.LOCKED;
111109
nftLock[tokenId].timestamp = block.timestamp;
112110

113-
uint64 sequenceNumber = entropy.requestWithCallback{value: requestFee}(
114-
entropyProvider,
115-
userRandomNumber
116-
);
111+
uint64 sequenceNumber = entropy.requestV2();
117112

118113
pendingRandomRequests[sequenceNumber] = RandomRequest({
119114
sender: msg.sender,
@@ -167,7 +162,7 @@ contract NFTGrowth is NFT, IEntropyConsumer {
167162
}
168163

169164
function getGrowFee() public view returns (uint256 fee) {
170-
fee = entropy.getFee(entropyProvider);
165+
fee = entropy.getFeeV2();
171166
}
172167

173168
function unlock(uint256 tokenId) public {

0 commit comments

Comments
 (0)