Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Created September 16, 2022 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdesl/fadac1ea7fe40164b541906e24a9cd2b to your computer and use it in GitHub Desktop.
Save mattdesl/fadac1ea7fe40164b541906e24a9cd2b to your computer and use it in GitHub Desktop.
roll a 6-sided die with the blockchain using Ethereum Proof of Stake RANDAO.
const Web3 = require("web3");
const BN = Web3.utils.BN;
(async () => {
const network = "mainnet";
const web3 = new Web3(
new Web3.providers.HttpProvider(
`https://${network}.infura.io/v3/${process.env.INFURA_API_KEY}`
)
);
// Get current block number or use a defined block:
const number = await web3.eth.getBlockNumber();
console.log(number);
// Get block data
const block = await web3.eth.getBlock(number);
// The PREVRANDAO, the random mix of the previously confirmed block
console.log(block.mixHash);
// Roll a 6-sided die
const index = BN(block.mixHash).mod(BN(6)).toNumber();
console.log('Dice Roll:', index + 1);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment