Last active
March 14, 2022 19:03
-
-
Save aesedepece/5daa8730faff65ef55b91f1ecfca616b to your computer and use it in GitHub Desktop.
Witnet RNG roll-a-die simple code snippet
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
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.7.0 <0.9.0; | |
import "witnet-solidity-bridge/contracts/interfaces/IWitnetRNG.sol"; | |
contract DieContract { | |
uint32 sides; | |
struct Guess { | |
uint32 guessedNumber; | |
uint256 blockHeight; | |
} | |
mapping (address => Guess) public guesses; | |
IWitnetRNG witnet; | |
event Right(string message); | |
event Wrong(string message); | |
constructor (uint32 _sides) { | |
sides = _sides; | |
witnet = IWitnetRNG(address("<address of the WitnetRNG contract>")); | |
} | |
function guessNumber(uint32 _number) external payable { | |
assert(_number > 0); | |
assert(_number <= sides); | |
assert(guesses[msg.sender].guessedNumber == 0); | |
guesses[msg.sender].guessedNumber = _number; | |
guesses[msg.sender].blockNumber = block.number; | |
witnet.randomize(); | |
} | |
function roll() external { | |
assert(guesses[msg.sender].guessedNumber != 0); | |
uint32 luckyNumber = 1 + witnet.random(sides, 0, guesses[msg.sender].blockNumber); | |
if (luckyNumber == guesses[msg.sender].guessedNumber) { | |
emit Right("Congratulations! You guessed the right number!"); | |
} else { | |
emit Wront("Sorry! You didn't guess the right number!"); | |
} | |
guesses[msg.sender].guessedNumber = 0; | |
} | |
} |
Sure, thanks @guidiaz , @aesedepece. But rather than just addressing the typos, this was more of a proposal to isolate examples in gists, as the code can then be directly consumed from Remix (and other systems, presumably). The real suggestion here is "can we make this trivial for users to try"
. I don't think the same can be done directly from code blocks, but I could be wrong.
If the github-embed plugin you are using can be leveraged to embed gists in the documentation, then you would only need to maintain the code in a single location, which is probably good. Just my 2cents.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, as for using
IWitnetRandomness
instead ofIWitnetRNG
. There are more concerns to take into account to make this example work just as proposed, though. Please, see example as currently published in docs site:Concerning whether to say "block number" or "block height", i think it's more a matter of personal preference. Solidity docs refers to "block number", for instance.