Created
September 16, 2022 12:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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