Skip to content

Commit

Permalink
feat: add constructor args example
Browse files Browse the repository at this point in the history
Copying in the example from https://github.com/stellar/stellar-cli/pull/1574/files#diff-0d743a0fa81fee10cf91dbf7fb7480daa248d14468d1cecfa24577ef00597f5b

Also:

- add a test for it
- upgrade to p22 for whole workspace
- gitignore test_snapshots

We can circle back and use this repo as a submodule in the CLI once this
is merged, as we do already in js-stellar-sdk.
  • Loading branch information
chadoh committed Nov 12, 2024
1 parent a9f9fb6 commit d3b5f33
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 143 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.2"
soroban-token-sdk = "22.0.0-rc.2"

[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
17 changes: 17 additions & 0 deletions constructor-args/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "constructor-args"
version = "21.5.0"
authors = ["Stellar Development Foundation <[email protected]>"]
license = "Apache-2.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]
doctest = false

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
20 changes: 20 additions & 0 deletions constructor-args/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_std]
use soroban_sdk::{contract, contractimpl, symbol_short, Env, Symbol};

#[contract]
pub struct ImmutableCounter;
const COUNTER: Symbol = symbol_short!("COUNTER");

#[contractimpl]
impl ImmutableCounter {
/// Example constructor
pub fn __constructor(env: Env, counter: u32) {
env.storage().persistent().set(&COUNTER, &counter);
}
/// Counter value
pub fn counter(env: Env) -> u32 {
env.storage().persistent().get(&COUNTER).unwrap()
}
}

mod test;
12 changes: 12 additions & 0 deletions constructor-args/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![cfg(test)]

use super::*;
use soroban_sdk::{vec, xdr::ScVal, Env};

#[test]
fn constructor_args() {
let env = Env::default();
let contract_id = env.register(ImmutableCounter, vec![&env, ScVal::U32(42)]);
let client = ImmutableCounterClient::new(&env, &contract_id);
assert_eq!(42u32, client.counter());
}
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);
}
2 changes: 1 addition & 1 deletion increment/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate std;
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, IncrementContract);
let contract_id = env.register(IncrementContract, ());
let client = IncrementContractClient::new(&env, &contract_id);

assert_eq!(client.increment(), 1);
Expand Down
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 d3b5f33

Please sign in to comment.