use nexus_sdk::{
solidity::{*, SafeMath},
contract_api::{ez::ret, events},
require,
};
use nexus_sdk::solidity::uint256 as U256;
// State variables
static COUNTER: Mapping<&[u8], U256> = Mapping::new(b"counter");
static OWNER: Mapping<&[u8], Address> = Mapping::new(b"owner");
nexus_fn! {
fn init() {
let sender = Blockchain::msg.sender();
OWNER.set(&b"val".as_slice(), sender);
COUNTER.set(&b"val".as_slice(), U256::from(0));
emit("Initialized", &[]);
ret::u32(1)
}
}
nexus_fn! {
fn increment() {
let current = COUNTER.get(&b"val".as_slice());
let new_value = current.add(U256::from(1));
COUNTER.set(&b"val".as_slice(), new_value);
emit("Incremented", &[]);
let mut out = [0u8; 32];
new_value.to_little_endian(&mut out);
ret::u256(&out)
}
}
nexus_fn! {
fn get_count() {
let current = COUNTER.get(&b"val".as_slice());
let mut out = [0u8; 32];
current.to_little_endian(&mut out);
ret::u256(&out)
}
}
nexus_fn! {
fn reset() {
let caller = Blockchain::msg.sender();
let owner = OWNER.get(&b"val".as_slice());
require!(caller == owner, "Unauthorized");
COUNTER.set(&b"val".as_slice(), U256::from(0));
emit("Reset", &[]);
ret::u32(1)
}
}