Skip to content

Chore(entropy) update guide v2 #769

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

Merged
merged 2 commits into from
Jul 25, 2025
Merged
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
48 changes: 30 additions & 18 deletions pages/entropy/create-your-first-entropy-app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,58 @@ In this tutorial we will implement and deploy a coin flip contract which will us

Before we start, please make sure you have the following tools installed:

Foundry - [https://book.getfoundry.sh/getting-started/installation](https://book.getfoundry.sh/getting-started/installation).
- [Foundry](https://book.getfoundry.sh/getting-started/installation).

- Run `forge --version` to confirm it is installed. You should get an output similar to `forge 0.2.0 (23aa303 2023-11-28T00:35:15.730515000Z)`

Node (version > 18) - [https://nodejs.org/en/download](https://nodejs.org/en/download)

- Run `node -v` to confirm. You should get an output with version >= `v18.0.0`.
- [Node](https://nodejs.org/en/download). Run `node -v{:jsx}` to confirm. You should get an output with version >= `v18.0.0`.

## Getting Started

Create a directory named `coin-flip` in your filesystem. Open a terminal with `coin-flip` as the working directory and run `forge init contracts` to create a new Solidity project. You will see a new directory in `coin-flip` named `contracts`. `contracts/src` is where all your contract code will be.
Create a directory named `coin-flip{:bash}` in your filesystem.
We will use this directory as the working directory for the rest of the tutorial.
Let's initialize a new project in `coin-flip{:bash}` by running `forge init contracts{:bash}`.

This will create a new directory in `coin-flip{:bash}` named `contracts/src`, which will contain the smart contract code.
Comment on lines +15 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my knowledge, why are these annotated with {:bash}? is it for syntax highlighting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Run `cd contracts` to make it your terminal’s working directory — the following commands will need to be run from here.
```bash copy
mkdir coin-flip
cd coin-flip
forge init contracts
```

Next, install the Pyth Entropy SDK by running the following commands.
Now we will install the Pyth Entropy SDK in the `contracts` directory.

```bash copy
cd contracts
npm init -y
npm install @pythnetwork/entropy-sdk-solidity
```

Add a `remappings.txt` file to `contracts` directory with the following content.
Add a `remappings.txt` file to `contracts` directory with the following content to tell Foundry where to find the Pyth Entropy SDK.

```text copy
@pythnetwork/entropy-sdk-solidity/=node_modules/@pythnetwork/entropy-sdk-solidity
```

## Implementation

Create a new file `CoinFlip.sol` in `contracts/src` directory and add the following code into it to start
Create a new file `CoinFlip.sol{:solidity}` in `contracts/src` directory and add the following code into it to start.

```solidity copy
// contracts/src/CoinFlip.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
import "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";

contract CoinFlip is IEntropyConsumer {
event FlipRequested(uint64 sequenceNumber);
event FlipResult(uint64 sequenceNumber, bool isHeads);

IEntropy entropy;
IEntropyV2 entropy;

constructor(address _entropy) {
entropy = IEntropy(_entropy);
entropy = IEntropyV2(_entropy);
}

// This method is required by the IEntropyConsumer interface
Expand All @@ -61,11 +68,13 @@ contract CoinFlip is IEntropyConsumer {

```

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 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 `IEntropyV2` which is an interface imported from the Entropy SDK.

### Request a coin flip

Copy the following code into `CoinFlip.sol`.
Copy the following code into `CoinFlip.sol{:solidity}`.

```solidity copy
contract CoinFlip {
Expand All @@ -87,11 +96,14 @@ contract CoinFlip {

```

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.
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{:bash}` method call to Entropy.
Finally, the method emits a `FlipRequested{:bash}` event with a `sequenceNumber`. This event is also defined in the code snippet above.

### Handle the callback

Copy the following code into `CoinFlip.sol`.
Copy the following code into `CoinFlip.sol{:solidity}`.

```solidity copy
contract CoinFlip {
Expand Down Expand Up @@ -176,7 +188,7 @@ Deployed to: 0x8676ba0Dd492AB9813BC21D5Dce318427d1d73ae
Transaction hash: 0x2178aa6d402c94166a93e81822248d00dd003827675ebd49b3c542970f5a0189
```

Lets export the coin flip contract address as environment variable for later use:
Let's export the coin flip contract address as environment variable for later use:

```bash copy
export COINFLIP_ADDRESS=<Deployed to address from above>
Expand Down
Loading