diff --git a/src/tests/integration/data/soroban/contracts/test_cross_contract/Cargo.toml b/src/tests/integration/data/soroban/contracts/test_cross_contract/Cargo.toml new file mode 100644 index 0000000..8e24916 --- /dev/null +++ b/src/tests/integration/data/soroban/contracts/test_cross_contract/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "test_cross_contract" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] +doctest = false + +[dependencies] +soroban-sdk = { workspace = true } + +[dev-dependencies] +soroban-sdk = { workspace = true, features = ["testutils"] } diff --git a/src/tests/integration/data/soroban/contracts/test_cross_contract/README.md b/src/tests/integration/data/soroban/contracts/test_cross_contract/README.md new file mode 100644 index 0000000..77fd68c --- /dev/null +++ b/src/tests/integration/data/soroban/contracts/test_cross_contract/README.md @@ -0,0 +1,4 @@ +This test demonstrates deploying two contracts (`contract_a and contract_b`) and testing their interaction. +`contract_b` calls a function in `contract_a` during execution. The test setup is defined in `kasmer.json`, +listing the directories of the two contracts. +Komet automatically compiles the contracts and runs the test. \ No newline at end of file diff --git a/src/tests/integration/data/soroban/contracts/test_cross_contract/kasmer.json b/src/tests/integration/data/soroban/contracts/test_cross_contract/kasmer.json new file mode 100644 index 0000000..ae05388 --- /dev/null +++ b/src/tests/integration/data/soroban/contracts/test_cross_contract/kasmer.json @@ -0,0 +1,6 @@ +{ + "contracts": [ + "../../../../../../../deps/soroban-examples/cross_contract/contract_a", + "../../../../../../../deps/soroban-examples/cross_contract/contract_b" + ] +} \ No newline at end of file diff --git a/src/tests/integration/data/soroban/contracts/test_cross_contract/src/lib.rs b/src/tests/integration/data/soroban/contracts/test_cross_contract/src/lib.rs new file mode 100644 index 0000000..ea396b5 --- /dev/null +++ b/src/tests/integration/data/soroban/contracts/test_cross_contract/src/lib.rs @@ -0,0 +1,50 @@ +#![no_std] +use soroban_sdk::{contract, contractclient, contractimpl, symbol_short, Address, Bytes, Env, FromVal, Symbol, Val}; + +extern "C" { + fn kasmer_create_contract(addr_val: u64, hash_val: u64) -> u64; +} + +fn create_contract(env: &Env, addr: &Bytes, hash: &Bytes) -> Address { + unsafe { + let res = kasmer_create_contract(addr.as_val().get_payload(), hash.as_val().get_payload()); + Address::from_val(env, &Val::from_payload(res)) + } +} + +#[contract] +pub struct TestCrossContract; + +#[contractclient(name = "ContractBClient")] +trait ContractB { + fn add_with(e: Env, address: Address, x: u32, y: u32) -> u32; +} + +const ADDR_A: &[u8; 32] = b"contract_a______________________"; +const ADDR_A_KEY: Symbol = symbol_short!("ctr_a"); +const ADDR_B: &[u8; 32] = b"contract_b______________________"; +const ADDR_B_KEY: Symbol = symbol_short!("ctr_b"); + +#[contractimpl] +impl TestCrossContract { + pub fn init(env: Env, hash_a: Bytes, hash_b: Bytes) { + let address_a = create_contract(&env, &Bytes::from_array(&env, ADDR_A), &hash_a); + let address_b = create_contract(&env, &Bytes::from_array(&env, ADDR_B), &hash_b); + + env.storage().instance().set(&ADDR_A_KEY, &address_a); + env.storage().instance().set(&ADDR_B_KEY, &address_b); + } + + pub fn test_add_with(env: Env, x: u32, y: u32) -> bool { + if x > 100 || y > 100 { + return true; + } + + let address_a : Address = env.storage().instance().get(&ADDR_A_KEY).unwrap(); + let address_b : Address = env.storage().instance().get(&ADDR_B_KEY).unwrap(); + + let client = ContractBClient::new(&env, &address_b); + x + y == client.add_with(&address_a, &x, &y) + } + +}