-
Notifications
You must be signed in to change notification settings - Fork 71
/
lib.rs
43 lines (35 loc) · 1.17 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#![no_std]
use soroban_sdk::{contract, contracterror, contractimpl, log, symbol_short, Env, Symbol};
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
LimitReached = 1,
}
const COUNTER: Symbol = symbol_short!("COUNTER");
const MAX: u32 = 5;
#[contract]
pub struct IncrementContract;
#[contractimpl]
impl IncrementContract {
/// Increment increments an internal counter, and returns the value. Errors
/// if the value is attempted to be incremented past 5.
pub fn increment(env: Env) -> Result<u32, Error> {
// Get the current count.
let mut count: u32 = env.storage().instance().get(&COUNTER).unwrap_or(0); // If no value set, assume 0.
log!(&env, "count: {}", count);
// Increment the count.
count += 1;
// Check if the count exceeds the max.
if count <= MAX {
// Save the count.
env.storage().instance().set(&COUNTER, &count);
// Return the count to the caller.
Ok(count)
} else {
// Return an error if the max is exceeded.
Err(Error::LimitReached)
}
}
}
mod test;