Skip to content

Commit

Permalink
fix: fix all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Feb 19, 2025
1 parent d187317 commit c767198
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ jobs:
- run: rustup update
- run: rustup target add wasm32-unknown-unknown
- uses: taiki-e/install-action@just
- uses: taiki-e/install-action@nextest
- uses: cargo-bins/cargo-binstall@main
- run: just setup
- run: just create
- run: just test
- run: just test-ci
- run: just redeploy

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
.soroban/
.env
.env
test_snapshots/
52 changes: 32 additions & 20 deletions crates/loam-soroban-sdk/src/loam_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,41 +103,46 @@ where
V: IntoVal<Env, Val> + TryFromVal<Env, Val>,
K: LoamKey + Default,
{

pub fn key(&self) -> Val {
K::default().to_key()
}

pub fn get(&self) -> Option<V> {
let key = K::default().to_key();
let key = self.key();
env().storage().persistent().get(&key)
}

pub fn set(&mut self, value: &V) {
let key = K::default().to_key();
let key = self.key();
env().storage().persistent().set(&key, value);
}

pub fn has(&self) -> bool {
let key = K::default().to_key();
let key = self.key();
env().storage().persistent().has(&key)
}

pub fn update(&self, f: impl FnOnce(Option<V>) -> V) -> V {
let key = K::default().to_key();
let key = self.key();
env().storage().persistent().update(&key, f)
}

pub fn try_update<E>(&self, f: impl FnOnce(Option<V>) -> Result<V, E>) -> Result<V, E> {
let key = K::default().to_key();
let key = self.key();
env().storage().persistent().try_update(&key, f)
}

pub fn extend_ttl(&self, threshold: u32, extend_to: u32) {
let key = K::default().to_key();
let key = self.key();
env()
.storage()
.persistent()
.extend_ttl(&key, threshold, extend_to);
}

pub fn remove(&self) {
let key = K::default().to_key();
let key = self.key();
env().storage().persistent().remove(&key);
}
}
Expand Down Expand Up @@ -323,28 +328,31 @@ where
V: IntoVal<Env, Val> + TryFromVal<Env, Val>,
K: LoamKey + Default,
{
pub fn key(&self) -> Val {
K::default().to_key()
}
pub fn get(&self) -> Option<V> {
let key = K::default().to_key();
let key = self.key();
env().storage().instance().get(&key)
}

pub fn set(&mut self, value: &V) {
let key = K::default().to_key();
let key = self.key();
env().storage().instance().set(&key, value);
}

pub fn has(&self) -> bool {
let key = K::default().to_key();
let key = self.key();
env().storage().instance().has(&key)
}

pub fn update(&self, f: impl FnOnce(Option<V>) -> V) -> V {
let key = K::default().to_key();
let key = self.key();
env().storage().instance().update(&key, f)
}

pub fn try_update<E>(&self, f: impl FnOnce(Option<V>) -> Result<V, E>) -> Result<V, E> {
let key = K::default().to_key();
let key = self.key();
env().storage().instance().try_update(&key, f)
}

Expand All @@ -353,7 +361,7 @@ where
}

pub fn remove(&self) {
let key = K::default().to_key();
let key = self.key();
env().storage().instance().remove(&key);
}
}
Expand All @@ -373,41 +381,45 @@ where
V: IntoVal<Env, Val> + TryFromVal<Env, Val>,
K: LoamKey + Default,
{
pub fn key(&self) -> Val {
K::default().to_key()
}

pub fn get(&self) -> Option<V> {
let key = K::default().to_key();
let key = self.key();
env().storage().temporary().get(&key)
}

pub fn set(&mut self, value: &V) {
let key = K::default().to_key();
let key = self.key();
env().storage().temporary().set(&key, value);
}

pub fn has(&self) -> bool {
let key = K::default().to_key();
let key = self.key();
env().storage().temporary().has(&key)
}

pub fn update(&self, f: impl FnOnce(Option<V>) -> V) -> V {
let key = K::default().to_key();
let key = self.key();
env().storage().temporary().update(&key, f)
}

pub fn try_update<E>(&self, f: impl FnOnce(Option<V>) -> Result<V, E>) -> Result<V, E> {
let key = K::default().to_key();
let key = self.key();
env().storage().temporary().try_update(&key, f)
}

pub fn extend_ttl(&self, threshold: u32, extend_to: u32) {
let key = K::default().to_key();
let key = self.key();
env()
.storage()
.temporary()
.extend_ttl(&key, threshold, extend_to);
}

pub fn remove(&self) {
let key = K::default().to_key();
let key = self.key();
env().storage().temporary().remove(&key);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/soroban/auth/src/subcontract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use loam_sdk::{
loamstorage,
soroban_sdk::{self, env, Address, Lazy, PersistentMap},
soroban_sdk::{self, Address, Lazy, PersistentMap},
subcontract,
};

Expand Down
2 changes: 1 addition & 1 deletion examples/soroban/custom_types/src/subcontract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use loam_sdk::{
loamstorage,
soroban_sdk::{self, contracttype, env, Lazy, PersistentItem},
soroban_sdk::{self, contracttype, Lazy, PersistentItem},
subcontract,
};

Expand Down
12 changes: 0 additions & 12 deletions examples/soroban/deep_contract_auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,4 @@ loam-subcontract-core = { workspace = true }
[dev-dependencies]
loam-sdk = { workspace = true, features = ["soroban-sdk-testutils"] }

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
12 changes: 0 additions & 12 deletions examples/soroban/deployer/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,4 @@ loam-subcontract-core = { workspace = true }
[dev-dependencies]
loam-sdk = { workspace = true, features = ["soroban-sdk-testutils"] }

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
12 changes: 0 additions & 12 deletions examples/soroban/deployer/deployer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,4 @@ loam-subcontract-core = { workspace = true }
[dev-dependencies]
loam-sdk = { workspace = true, features = ["soroban-sdk-testutils"] }

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
4 changes: 2 additions & 2 deletions examples/soroban/increment-init/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait IsCountable {
/// Increment increments an internal counter, and returns the value.
fn increment(&mut self) -> u32;

fn init(&mut self, num: u32);
fn __constructor(&mut self, num: u32);
}

impl IsCountable for Counter {
Expand All @@ -26,7 +26,7 @@ impl IsCountable for Counter {
count
}

fn init(&mut self, num: u32) {
fn __constructor(&mut self, num: u32) {
self.count.set(&num);
}
}
2 changes: 1 addition & 1 deletion examples/soroban/increment-init/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate std;
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register(SorobanContract__, ());
let contract_id = env.register(SorobanContract__, (0u32,));
let client = SorobanContract__Client::new(&env, &contract_id);

assert_eq!(client.increment(), 1);
Expand Down
4 changes: 2 additions & 2 deletions examples/soroban/logging/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn test() {

let id_bytes = BytesN::from_array(&env, &[8; 32]);

let _addr: Address =
let addr: Address =
Address::try_from_val(&env, &ScAddress::Contract(Hash(id_bytes.to_array()))).unwrap();
let contract_id = env.register( SorobanContract__, ());
let contract_id = env.register_at(&addr, SorobanContract__, ());
let client = SorobanContract__Client::new(&env, &contract_id);

client.hello(&symbol_short!("Dev"));
Expand Down
24 changes: 10 additions & 14 deletions examples/soroban/simple_account/src/subcontract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use loam_sdk::{
loamstorage, soroban_sdk::{self, auth::Context, BytesN, InstanceItem, Lazy, Vec}, subcontract,
loamstorage, soroban_sdk::{self, auth::Context, env, BytesN, InstanceItem, Lazy, Vec}, subcontract,
};

use crate::error::Error;
Expand All @@ -15,7 +15,7 @@ pub trait IsSimpleAccount {
fn __check_auth(
&self,
signature_payload: BytesN<32>,
signatures: Vec<BytesN<64>>,
signature: BytesN<64>,
auth_context: Vec<Context>,
) -> Result<(), Error>;
}
Expand All @@ -32,19 +32,15 @@ impl IsSimpleAccount for SimpleAccountManager {
#[allow(non_snake_case)]
fn __check_auth(
&self,
_signature_payload: BytesN<32>,
_signatures: Vec<BytesN<64>>,
signature_payload: BytesN<32>,
signature: BytesN<64>,
_auth_context: Vec<Context>,
) -> Result<(), Error> {
// if signatures.len() != 1 {
// return Err(Error::IncorrectSignatureCount);
// }

// env().crypto().ed25519_verify(
// &self.owner.get().unwrap(),
// &signature_payload.into(),
// &signatures.get(0).unwrap(),
// );
)-> Result<(), Error> {
env().crypto().ed25519_verify(
&self.owner.get().unwrap(),
&signature_payload.into(),
&signature,
);

Ok(())
}
Expand Down
16 changes: 9 additions & 7 deletions examples/soroban/simple_account/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ extern crate std;

use ed25519_dalek::Keypair;
use ed25519_dalek::Signer;
use loam_sdk::soroban_sdk::Error;
use loam_sdk::soroban_sdk::Val;
use loam_sdk::soroban_sdk::{testutils::BytesN as _, vec, BytesN, Env, IntoVal};
use loam_sdk::soroban_sdk;
use rand::thread_rng;
use soroban_sdk::Error;
use soroban_sdk::Val;
use soroban_sdk::{testutils::BytesN as _, vec, BytesN, Env, IntoVal};

use crate::SorobanContract__ as SimpleAccount;
use crate::SorobanContract__Client as SimpleAccountClient;

use crate::SorobanContract__;
use crate::SorobanContract__Client;
fn generate_keypair() -> Keypair {
Keypair::generate(&mut thread_rng())
}

fn create_account_contract(e: &Env) -> SorobanContract__Client {
SorobanContract__Client::new(e, &e.register(SorobanContract__, ()))
fn create_account_contract(e: &Env) -> SimpleAccountClient {
SimpleAccountClient::new(e, &e.register(SimpleAccount, ()))
}

fn sign(e: &Env, signer: &Keypair, payload: &BytesN<32>) -> Val {
Expand Down
4 changes: 2 additions & 2 deletions examples/soroban/status_message/src/status_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Currently need to import `self` because `contracttype` expects it in the namespace
use loam_sdk::{
loamstorage,
soroban_sdk::{self, env, Address, Lazy, PersistentMap, String},
soroban_sdk::{self, Address, Lazy, PersistentMap, String},
subcontract,
};

Expand All @@ -15,7 +15,7 @@ pub struct StatusMessage {
pub trait IsPostable {
/// Documentation ends up in the contract's metadata and thus the CLI, etc
fn messages_get(&self, author: loam_sdk::soroban_sdk::Address)
-> loam_sdk::soroban_sdk::String;
-> Option<loam_sdk::soroban_sdk::String>;

/// Only the author can set the message
fn messages_set(
Expand Down
14 changes: 2 additions & 12 deletions examples/soroban/ttl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ doctest = false
loam-sdk = { workspace = true, features = ["loam-soroban-sdk"] }
loam-subcontract-core = { workspace = true }

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
[dev-dependencies]
loam-sdk = { workspace = true, features = ["soroban-sdk-testutils"] }
Loading

0 comments on commit c767198

Please sign in to comment.