-
-
Save ryanswrt/5fd215fb7d4ef37ce566ca1778e5d8ff to your computer and use it in GitHub Desktop.
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
use smart_contract::log; | |
use smart_contract_macros::smart_contract; | |
use smart_contract::payload::Parameters; | |
// The contract will store an instance of the same Universe struct implemented in the tutorial | |
// we also have a "stepped" construct that checks whether someone has already stepped the contract in a given consensus round | |
struct Contract { | |
universe: Universe, | |
stepped: [u8; 32], | |
} | |
#[smart_contract] | |
impl Contract { | |
pub fn init(_params: &mut Parameters) -> Self { | |
Self { | |
universe: Universe::new(), | |
stepped: [0; 32], | |
} | |
} | |
pub fn step(&mut self, params: &mut Parameters) -> Result<(), String> { | |
// only step once per round | |
if self.stepped == params.round_id { | |
return Ok(()) | |
} | |
self.stepped = params.round_id; | |
self.universe.tick(); | |
Ok(()) | |
} | |
pub fn render(&mut self, _params: &mut Parameters) -> Result<(), String> { | |
log(&self.universe.render()); | |
Ok(()) | |
} | |
} | |
// ... the rest of the code in the Rust Wasm tutorial https://rustwasm.github.io/docs/book/game-of-life/implementing.html#rust-implementation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment