Migrated and updated from SE-1 and with new withdrawalsRoot header added after the Ethereum Shanghai hard fork upgrade https://github.com/scaffold-eth/scaffold-eth/blob/dice-game-using-future-difficulty-using-block-header
Ethereum PoS introduces randomness using block.difficulty. Look at https://eips.ethereum.org/EIPS/eip-4399 for more information.
In this dice game the user can bet on a number between 0 and 15 and then some blocks after the bet, he can roll the dice to check if he won.
When the user bets, the current block number is saved on the contract along with the bet number. Then a future block number is used to determine what number is gotten from the dice. To do that, the rollTheDice
method receives the RLP encoded block header (this is the block header for the future block where the dice number gets). For example, if the bet is on block number 100 and the futureBlocks
on the contract is set to 10, then the rollTheDice
method needs to receive the header for block 110.
The contract checks that it's the right block number and the block hash is ok (comparing blockhash(number) and the hash calculated from the block header sent to the method). If it's ok, the contract uses the field mixHash
from the header as a randomness source as described at https://eips.ethereum.org/EIPS/eip-4399
The frontend has to get the block data, set it in the right order and RLP encode it before sending the data to the rollTheDice
method:
const blockData = await publicClient.getBlock({ blockNumber: targetBlockNumber });
console.log("blockData: ", blockData);
const values: `0x${string}`[] = [];
values.push(blockData.parentHash);
values.push(blockData.sha3Uncles);
values.push(blockData.miner as `0x${string}`);
values.push(blockData.stateRoot);
values.push(blockData.transactionsRoot);
values.push(blockData.receiptsRoot);
values.push(blockData.logsBloom);
values.push(`0x${blockData.difficulty.toString(16)}`);
values.push(`0x${blockData.number.toString(16)}`);
values.push(`0x${blockData.gasLimit.toString(16)}`);
values.push(`0x${blockData.gasUsed.toString(16)}`);
values.push(`0x${blockData.timestamp.toString(16)}`);
values.push(blockData.extraData);
values.push(blockData.mixHash);
values.push(blockData.nonce);
if ("baseFeePerGas" in blockData && blockData.baseFeePerGas !== null) {
values.push(`0x${blockData.baseFeePerGas.toString(16)}`);
}
if ("withdrawalsRoot" in blockData && blockData.withdrawalsRoot !== undefined) {
values.push(blockData.withdrawalsRoot);
}
console.log("blockData values: ", values);
for (let i = 0; i < values.length; i++) {
if (values[i] === "0x0") {
values[i] = "0x";
}
if (values[i].length % 2 === 1) {
values[i] = ("0x0" + values[i].substring(2)) as `0x${string}`;
}
}
console.log("blockData values after: ", values);
const rlpEncodedValues = toRlp(values);
console.log("blockData RLP: ", rlpEncodedValues);
const blockHash = keccak256(rlpEncodedValues);
console.log("blockData hash: ", blockHash);
if (blockHash !== blockData.hash) {
notification.error("Block hash mismatch");
return;
}
You can find this code on this file https://github.com/damianmarti/dice-game-randao/blob/main/packages/nextjs/pages/index.tsx#L128
The block hash is calculated, and you can check if it's right by checking the actual block hash.
The contract is deployed to the Sepolia testnet and the frontend is deployed at https://dice-game-randao.vercel.app
🧪 An open-source, up-to-date toolkit for building decentralized applications (dapps) on the Ethereum blockchain. It's designed to make it easier for developers to create and deploy smart contracts and build user interfaces that interact with those contracts.
⚙️ Built using NextJS, RainbowKit, Hardhat, Wagmi, and Typescript.
- ✅ Contract Hot Reload: Your frontend auto-adapts to your smart contract as you edit it.
- 🔥 Burner Wallet & Local Faucet: Quickly test your application with a burner wallet and local faucet.
- 🔐 Integration with Wallet Providers: Connect to different wallet providers and interact with the Ethereum network.
Before you begin, you need to install the following tools:
- Node (v18 LTS)
- Yarn (v1 or v2+)
- Git
To get started with Scaffold-ETH 2, follow the steps below:
- Clone this repo & install dependencies
git clone https://github.com/scaffold-eth/scaffold-eth-2.git
cd scaffold-eth-2
yarn install
- Run a local network in the first terminal:
yarn chain
This command starts a local Ethereum network using Hardhat. The network runs on your local machine and can be used for testing and development. You can customize the network configuration in hardhat.config.ts
.
- On a second terminal, deploy the test contract:
yarn deploy
This command deploys a test smart contract to the local network. The contract is located in packages/hardhat/contracts
and can be modified to suit your needs. The yarn deploy
command uses the deploy script located in packages/hardhat/deploy
to deploy the contract to the network. You can also customize the deploy script.
- On a third terminal, start your NextJS app:
yarn start
Visit your app on: http://localhost:3000
. You can interact with your smart contract using the Debug Contracts
page. You can tweak the app config in packages/nextjs/scaffold.config.ts
.
Run smart contract test with yarn hardhat:test
- Edit your smart contract
YourContract.sol
inpackages/hardhat/contracts
- Edit your frontend in
packages/nextjs/pages
- Edit your deployment scripts in
packages/hardhat/deploy
Visit our docs to learn how to start building with Scaffold-ETH 2.
To know more about its features, check out our website.
We welcome contributions to Scaffold-ETH 2!
Please see CONTRIBUTING.MD for more information and guidelines for contributing to Scaffold-ETH 2.