Skip to content

Commit

Permalink
feat: add constructor args example (#2)
Browse files Browse the repository at this point in the history
* feat: add constructor args example

Update `increment` to have a `__constructor` as well as a `get` to make it a more useful and realistic example, as well as giving us a way to test constructor args.

Also:

- add a test for it
- upgrade to p22 for whole workspace
- gitignore test_snapshots
  • Loading branch information
chadoh authored Nov 13, 2024
1 parent a9f9fb6 commit 50aca41
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 150 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ Cargo.lock
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

# soroban-sdk test snapshots; see https://developers.stellar.org/docs/build/guides/testing/detecting-changes-with-test-snapshots
test_snapshots
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exclude = [
]

[workspace.dependencies]
soroban-sdk = "21.6.0"
soroban-token-sdk = "21.6.0"
soroban-sdk = "22.0.0-rc.3.1"
soroban-token-sdk = "22.0.0-rc.3.1"

[profile.release]
opt-level = "z"
Expand Down
2 changes: 1 addition & 1 deletion atomic-swap/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn create_token_contract<'a>(e: &Env, admin: &Address) -> (TokenClient<'a>, Toke
}

fn create_atomic_swap_contract(e: &Env) -> AtomicSwapContractClient {
AtomicSwapContractClient::new(e, &e.register_contract(None, AtomicSwapContract {}))
AtomicSwapContractClient::new(e, &e.register(AtomicSwapContract {}, ()))
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion custom-types/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use soroban_sdk::Env;
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, CustomTypesContract);
let contract_id = env.register(CustomTypesContract, ());
let client = CustomTypesContractClient::new(&env, &contract_id);
assert_eq!(client.u32_fail_on_even(&1), 1);
}
18 changes: 16 additions & 2 deletions increment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ pub struct IncrementContract;

#[contractimpl]
impl IncrementContract {
/// Increment increments an internal counter, and returns the value.
/// Initialize the counter with a value
pub fn __constructor(env: Env, counter: u32) {
env.storage().instance().set(&COUNTER, &counter);
}

/// Increment internal counter, return the value
pub fn increment(env: Env) -> u32 {
// Get the current count.
let mut count: u32 = env.storage().instance().get(&COUNTER).unwrap_or(0); // If no value set, assume 0.
let mut count: u32 = env
.storage()
.instance()
.get(&COUNTER)
.expect("counter not set!");
log!(&env, "count: {}", count);

// Increment the count.
Expand All @@ -29,6 +38,11 @@ impl IncrementContract {
// Return the count to the caller.
count
}

/// Get current counter value
pub fn get(env: Env) -> u32 {
env.storage().instance().get(&COUNTER).unwrap()
}
}

mod test;
13 changes: 7 additions & 6 deletions increment/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#![cfg(test)]

use super::{IncrementContract, IncrementContractClient};
use soroban_sdk::{testutils::Logs, Env};
use soroban_sdk::{testutils::Logs, vec, xdr::ScVal, Env};

extern crate std;

#[test]
fn test() {
fn construct_get_increment() {
let env = Env::default();
let contract_id = env.register_contract(None, IncrementContract);
let contract_id = env.register(IncrementContract, vec![&env, ScVal::U32(42)]);
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.get(), 42);
assert_eq!(client.increment(), 43);
assert_eq!(client.increment(), 44);
assert_eq!(client.increment(), 45);

std::println!("{}", env.logs().all().join("\n"));
}
4 changes: 2 additions & 2 deletions needs-a-signature/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn test() {
env.mock_all_auths();

let user = Address::generate(&env);
let contract_id = env.register_contract(None, SignedContract);
let signing_contract_id = env.register_contract(None, SigningContract);
let contract_id = env.register(SignedContract, ());
let signing_contract_id = env.register(SigningContract, ());
let client = SignedContractClient::new(&env, &contract_id);

let words = client.hello(&String::from_str(&env, "Dev"), &user, &signing_contract_id);
Expand Down
2 changes: 1 addition & 1 deletion this-one-signs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use soroban_sdk::{vec, Env, String};
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, SigningContract);
let contract_id = env.register(SigningContract, ());
let client = SigningContractClient::new(&env, &contract_id);

let words = client.hello(&String::from_str(&env, "Dev"));
Expand Down
132 changes: 0 additions & 132 deletions this-one-signs/test_snapshots/test/test.1.json

This file was deleted.

4 changes: 2 additions & 2 deletions token/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use soroban_sdk::{
};

fn create_token<'a>(e: &Env, admin: &Address) -> TokenClient<'a> {
let token = TokenClient::new(e, &e.register_contract(None, Token {}));
let token = TokenClient::new(e, &e.register(Token {}, ()));
token.initialize(admin, &7, &"name".into_val(e), &"symbol".into_val(e));
token
}
Expand Down Expand Up @@ -246,7 +246,7 @@ fn initialize_already_initialized() {
fn decimal_is_over_eighteen() {
let e = Env::default();
let admin = Address::generate(&e);
let token = TokenClient::new(&e, &e.register_contract(None, Token {}));
let token = TokenClient::new(&e, &e.register(Token {}, ()));
token.initialize(&admin, &19, &"name".into_val(&e), &"symbol".into_val(&e));
}

Expand Down

0 comments on commit 50aca41

Please sign in to comment.