From 5a25a0369ed6690d2271a1de792e696de217815e Mon Sep 17 00:00:00 2001 From: Jayant Krishnamurthy Date: Wed, 14 May 2025 13:58:13 -0700 Subject: [PATCH] update entropy examples to use new interface --- entropy/coin_flip/app/src/flip_coin.ts | 15 +++------ entropy/coin_flip/contract/scripts/deploy.sh | 2 -- entropy/coin_flip/contract/src/CoinFlip.sol | 31 ++++++++----------- .../growing/contract/contracts/NFTGrowth.sol | 15 +++------ 4 files changed, 22 insertions(+), 41 deletions(-) diff --git a/entropy/coin_flip/app/src/flip_coin.ts b/entropy/coin_flip/app/src/flip_coin.ts index 982e8f2..af69c1b 100644 --- a/entropy/coin_flip/app/src/flip_coin.ts +++ b/entropy/coin_flip/app/src/flip_coin.ts @@ -73,21 +73,14 @@ async function main() { client, }); - console.log("1. Generating user's random number..."); - - const randomNumber: `0x${string}` = `0x${crypto - .randomBytes(32) - .toString("hex")}`; - console.log(`User Generated Random number: ${randomNumber}`); - - console.log("\n2. Requesting coin flip..."); + console.log("\n1. Requesting coin flip..."); const flipFee = await coinFlipContract.read.getFlipFee(); console.log(`Flip Fee: ${flipFee} wei`); - console.log("\n3. Sending request to flip coin..."); + console.log("\n2. Sending request to flip coin..."); - const flipTxHash = await coinFlipContract.write.requestFlip([randomNumber], { + const flipTxHash = await coinFlipContract.write.requestFlip([], { value: flipFee, }); console.log(`Transaction Hash: ${flipTxHash}`); @@ -106,7 +99,7 @@ async function main() { console.log(`\nSequence Number: ${sequenceNumber}`); - console.log("\n4. Waiting for flip result..."); + console.log("\n3. Waiting for flip result..."); const result = await new Promise((resolve, reject) => { const unwatch = coinFlipContract.watchEvent.FlipResult({ fromBlock: receipt.blockNumber - 1n, diff --git a/entropy/coin_flip/contract/scripts/deploy.sh b/entropy/coin_flip/contract/scripts/deploy.sh index ea03f50..d5a9f7e 100755 --- a/entropy/coin_flip/contract/scripts/deploy.sh +++ b/entropy/coin_flip/contract/scripts/deploy.sh @@ -5,7 +5,6 @@ RPC_URL=https://sepolia-rollup.arbitrum.io/rpc # 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 ENTROPY_CONTRACT_ADDRESS="0x549Ebba8036Ab746611B4fFA1423eb0A4Df61440" -PROVIDER="0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344" # Deployments # optimism-sepolia 0x2eE67fF5d8548fF544f2c178a0FcAFe503A634Be @@ -18,4 +17,3 @@ forge create src/CoinFlip.sol:CoinFlip \ --rpc-url $RPC_URL \ --constructor-args \ $ENTROPY_CONTRACT_ADDRESS \ - $PROVIDER diff --git a/entropy/coin_flip/contract/src/CoinFlip.sol b/entropy/coin_flip/contract/src/CoinFlip.sol index 86a294f..faf5e36 100644 --- a/entropy/coin_flip/contract/src/CoinFlip.sol +++ b/entropy/coin_flip/contract/src/CoinFlip.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.0; // Import the entropy SDK in order to interact with the entropy contracts -import "entropy-sdk-solidity/IEntropy.sol"; +import "entropy-sdk-solidity/IEntropyV2.sol"; import "entropy-sdk-solidity/IEntropyConsumer.sol"; library CoinFlipErrors { @@ -26,25 +26,20 @@ contract CoinFlip is IEntropyConsumer { // Event emitted when the result of the coin flip is known. event FlipResult(uint64 sequenceNumber, bool isHeads); - // Contracts using Pyth Entropy should import the solidity SDK and then store both the Entropy contract - // and a specific entropy provider to use for requests. Each provider commits to a sequence of random numbers. - // Providers are then responsible for fulfilling a request on chain by revealing their random number. - // Users should choose a reliable provider who they trust to uphold these commitments. - // (For the moment, the only available provider is 0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344) - IEntropy private entropy; - address private entropyProvider; + // Contracts using Pyth Entropy should import the solidity SDK and then store the Entropy contract + // address in the constructor. + IEntropyV2 private entropy; - constructor(address _entropy, address _entropyProvider) { + constructor(address _entropy) { entropy = IEntropy(_entropy); - entropyProvider = _entropyProvider; } - // Request to flip a coin. The caller should generate and pass in a random number when calling this method. - function requestFlip(bytes32 userRandomNumber) external payable { + // Request to flip a coin. + function requestFlip() external payable { // The entropy protocol requires the caller to pay a fee (in native gas tokens) per requested random number. // This fee can either be paid by the contract itself or passed on to the end user. // This implementation of the requestFlip method passes on the fee to the end user. - uint256 fee = entropy.getFee(entropyProvider); + uint256 fee = entropy.getFeeV2(); if (msg.value < fee) { revert CoinFlipErrors.InsufficientFee(); } @@ -52,17 +47,17 @@ contract CoinFlip is IEntropyConsumer { // Request the random number from the Entropy protocol. The call returns a sequence number that uniquely // identifies the generated random number. Callers can use this sequence number to match which request // is being revealed in the next stage of the protocol. - uint64 sequenceNumber = entropy.requestWithCallback{value: fee}( - entropyProvider, - userRandomNumber - ); + // + // Note that callers can also request a specific gas limit for the callback by passing a gasLimit parameter + // to this function. See the IEntropyV2 interface for details. + uint64 sequenceNumber = entropy.requestV2{value: fee}(); emit FlipRequest(sequenceNumber); } // Get the fee to flip a coin. See the comment above about fees. function getFlipFee() public view returns (uint256 fee) { - fee = entropy.getFee(entropyProvider); + fee = entropy.getFeeV2(); } // This method is required by the IEntropyConsumer interface. diff --git a/entropy/growing/contract/contracts/NFTGrowth.sol b/entropy/growing/contract/contracts/NFTGrowth.sol index d782df1..062b849 100644 --- a/entropy/growing/contract/contracts/NFTGrowth.sol +++ b/entropy/growing/contract/contracts/NFTGrowth.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.24; import {IEntropyConsumer} from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol"; -import {IEntropy} from "@pythnetwork/entropy-sdk-solidity/IEntropy.sol"; +import {IEntropyV2} from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol"; import "./NFT.sol"; @@ -60,7 +60,6 @@ event NftGrowthRequested( contract NFTGrowth is NFT, IEntropyConsumer { IEntropy entropy; - address entropyProvider; uint256 maxLevel = 5; uint256 successChance = 4000; uint256 failChance = 4000; @@ -72,9 +71,8 @@ contract NFTGrowth is NFT, IEntropyConsumer { mapping(uint256 => NFTLock) public nftLock; - constructor(address _entropy, address _provider) { + constructor(address _entropy) { entropy = IEntropy(_entropy); - entropyProvider = _provider; } function requireLock(uint256 tokenId) private view { @@ -104,16 +102,13 @@ contract NFTGrowth is NFT, IEntropyConsumer { require(nftInfo[tokenId].status == NFTStatus.ALIVE, "NFT is dead"); require(nftInfo[tokenId].level < maxLevel, "Already max level"); - uint128 requestFee = entropy.getFee(entropyProvider); + uint128 requestFee = entropy.getFeeV2(); require(msg.value >= requestFee, "Not enough fees"); nftLock[tokenId].status = LockStatus.LOCKED; nftLock[tokenId].timestamp = block.timestamp; - uint64 sequenceNumber = entropy.requestWithCallback{value: requestFee}( - entropyProvider, - userRandomNumber - ); + uint64 sequenceNumber = entropy.requestV2(); pendingRandomRequests[sequenceNumber] = RandomRequest({ sender: msg.sender, @@ -167,7 +162,7 @@ contract NFTGrowth is NFT, IEntropyConsumer { } function getGrowFee() public view returns (uint256 fee) { - fee = entropy.getFee(entropyProvider); + fee = entropy.getFeeV2(); } function unlock(uint256 tokenId) public {