Skip to content

Update entropy examples to use new interface #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions entropy/coin_flip/app/src/flip_coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions entropy/coin_flip/contract/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,4 +17,3 @@ forge create src/CoinFlip.sol:CoinFlip \
--rpc-url $RPC_URL \
--constructor-args \
$ENTROPY_CONTRACT_ADDRESS \
$PROVIDER
31 changes: 13 additions & 18 deletions entropy/coin_flip/contract/src/CoinFlip.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,43 +26,38 @@ 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();
}

// 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.
Expand Down
15 changes: 5 additions & 10 deletions entropy/growing/contract/contracts/NFTGrowth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -60,7 +60,6 @@ event NftGrowthRequested(

contract NFTGrowth is NFT, IEntropyConsumer {
IEntropy entropy;
address entropyProvider;
uint256 maxLevel = 5;
uint256 successChance = 4000;
uint256 failChance = 4000;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Loading