Skip to content

Instantly share code, notes, and snippets.

@aesedepece
Last active March 14, 2022 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aesedepece/5daa8730faff65ef55b91f1ecfca616b to your computer and use it in GitHub Desktop.
Save aesedepece/5daa8730faff65ef55b91f1ecfca616b to your computer and use it in GitHub Desktop.
Witnet RNG roll-a-die simple code snippet
// 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;
}
}
@datgous
Copy link

datgous commented Mar 14, 2022

To get the code to compile in Remix:

s/IWitnetRNG/IWitnetRandomness/
s/blockNumber/blockHeight/

address("<address of the WitnetRNG contract>") -> delete the quotes and paste the address of your selected test/production network.
eg. for Rinkeby, use address(0xd72b1a10f8261A174b8861a13159A468A166B708)

@aesedepece
Copy link
Author

@datgous thanks for your comment! @guidiaz do you think I should incorporate those changes to this gist?

@guidiaz
Copy link

guidiaz commented Mar 14, 2022

Yep, as for using IWitnetRandomness instead of IWitnetRNG. 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.

@datgous
Copy link

datgous commented Mar 14, 2022

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