Skip to content

Instantly share code, notes, and snippets.

@ryanswrt

ryanswrt/lib.rs Secret

Created August 14, 2019 06:29
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 ryanswrt/5fd215fb7d4ef37ce566ca1778e5d8ff to your computer and use it in GitHub Desktop.
Save ryanswrt/5fd215fb7d4ef37ce566ca1778e5d8ff to your computer and use it in GitHub Desktop.
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