Counter
The following is a simple example of a contract which implements a counter. Both the initialize()
and increment()
functions return the currently set value.
forc init --template counter my_counter_project
The use of storage
here is new and is still being stabilized, please see the Subcurrency example for writing storage manually.
contract;
abi TestContract {
fn initialize_counter(value: u64) -> u64;
fn increment_counter(amount: u64) -> u64;
}
storage {
counter: u64,
}
impl TestContract for Contract {
fn initialize_counter(value: u64) -> u64 {
storage.counter = value;
value
}
fn increment_counter(amount: u64) -> u64 {
let incremented = storage.counter + amount;
storage.counter = incremented;
incremented
}
}