Skip to content

Commit

Permalink
add test_cross_contract
Browse files Browse the repository at this point in the history
  • Loading branch information
bbyalcinkaya committed Nov 22, 2024
1 parent 7ac0b08 commit dd20168
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"] }
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"contracts": [
"../../../../../../../deps/soroban-examples/cross_contract/contract_a",
"../../../../../../../deps/soroban-examples/cross_contract/contract_b"
]
}
Original file line number Diff line number Diff line change
@@ -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)
}

}

0 comments on commit dd20168

Please sign in to comment.