forked from stellar/soroban-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b084f9c
commit 6cc2f11
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ members = [ | |
"timelock", | ||
"token", | ||
"logging", | ||
"errors", | ||
] | ||
|
||
[profile.release-with-logs] | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "soroban-errors-contract" | ||
version = "0.0.0" | ||
authors = ["Stellar Development Foundation <[email protected]>"] | ||
license = "Apache-2.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
doctest = false | ||
|
||
[features] | ||
testutils = ["soroban-sdk/testutils"] | ||
|
||
[dependencies] | ||
soroban-sdk = "0.1.0" | ||
|
||
[dev_dependencies] | ||
soroban-sdk = { version = "0.1.0", features = ["testutils"] } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![no_std] | ||
use soroban_sdk::{contracterror, contractimpl, log, symbol, Env, Symbol}; | ||
|
||
#[contracterror] | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
#[repr(u32)] | ||
pub enum Error { | ||
LimitReached = 1, | ||
} | ||
|
||
const COUNTER: Symbol = symbol!("COUNTER"); | ||
const MAX: u32 = 10; | ||
|
||
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 10. | ||
pub fn increment(env: Env) -> Result<u32, Error> { | ||
// Get the current count. | ||
let mut count: u32 = env | ||
.data() | ||
.get(COUNTER) | ||
.unwrap_or(Ok(0)) // If no value set, assume 0. | ||
.unwrap(); // Panic if the value of COUNTER is not u32. | ||
log!(&env, "count: {}", count); | ||
|
||
// Increment the count. | ||
count += 1; | ||
|
||
// Check if the count exceeds the max. | ||
if count <= MAX { | ||
// Save the count. | ||
env.data().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; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#![cfg(test)] | ||
|
||
use super::*; | ||
use soroban_sdk::{testutils::Logger, Env}; | ||
|
||
extern crate std; | ||
|
||
#[test] | ||
fn test() { | ||
let env = Env::default(); | ||
let contract_id = env.register_contract(None, IncrementContract); | ||
let client = IncrementContractClient::new(&env, &contract_id); | ||
|
||
assert_eq!(client.try_increment(), Ok(Ok(1))); | ||
assert_eq!(client.try_increment(), Ok(Ok(2))); | ||
assert_eq!(client.try_increment(), Ok(Ok(3))); | ||
assert_eq!(client.try_increment(), Ok(Ok(4))); | ||
assert_eq!(client.try_increment(), Ok(Ok(5))); | ||
assert_eq!(client.try_increment(), Ok(Ok(6))); | ||
assert_eq!(client.try_increment(), Ok(Ok(7))); | ||
assert_eq!(client.try_increment(), Ok(Ok(8))); | ||
assert_eq!(client.try_increment(), Ok(Ok(9))); | ||
assert_eq!(client.try_increment(), Ok(Ok(10))); | ||
assert_eq!(client.try_increment(), Err(Ok(Error::LimitReached))); | ||
|
||
std::println!("{}", env.logger().all().join("\n")); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Status(ContractError(1))")] | ||
fn test_panic() { | ||
let env = Env::default(); | ||
let contract_id = env.register_contract(None, IncrementContract); | ||
let client = IncrementContractClient::new(&env, &contract_id); | ||
|
||
assert_eq!(client.increment(), 1); | ||
assert_eq!(client.increment(), 2); | ||
assert_eq!(client.increment(), 3); | ||
assert_eq!(client.increment(), 4); | ||
assert_eq!(client.increment(), 5); | ||
assert_eq!(client.increment(), 6); | ||
assert_eq!(client.increment(), 7); | ||
assert_eq!(client.increment(), 8); | ||
assert_eq!(client.increment(), 9); | ||
assert_eq!(client.increment(), 10); | ||
client.increment(); | ||
} |