Skip to content

Commit 2708969

Browse files
add intergation tests
Signed-off-by: salaheldinsoliman <salaheldin_sameh@aucegypt.edu>
1 parent 32a45ea commit 2708969

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1036
-612
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ jobs:
319319
with:
320320
node-version: '16'
321321
- uses: dtolnay/rust-toolchain@1.81.0
322+
with:
323+
target: wasm32-unknown-unknown
322324
- uses: actions/download-artifact@v4.1.8
323325
with:
324326
name: solang-linux-x86-64
@@ -329,7 +331,7 @@ jobs:
329331
echo "$(pwd)/bin" >> $GITHUB_PATH
330332
331333
- name: Install Soroban
332-
run: cargo install --locked soroban-cli --version 22.0.0
334+
run: cargo install --locked stellar-cli --version 22.0.0
333335
- name: Add cargo install location to PATH
334336
run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH
335337
- run: npm install

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ tests/create_me/
88

99
.helix/
1010
.vscode/
11+
12+
/test_snapshots

integration/soroban/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ node_modules
77
package-lock.json
88
*.txt
99
*.toml
10+
*.wasm
11+
*.abi
12+
target/

integration/soroban/callee.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
contract callee {
2+
function add (uint64 a, uint64 b, uint64 c) public returns (uint64) {
3+
print("add called in Solidity");
4+
return a + b +c;
5+
}
6+
}

integration/soroban/caller.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
contract caller {
2+
function add (address addr, uint64 a, uint64 b, uint64 c) public returns (uint64) {
3+
bytes payload = abi.encode("add", a, b, c);
4+
(bool suc, bytes returndata) = addr.call(payload);
5+
uint64 result = abi.decode(returndata, (uint64));
6+
return result;
7+
}
8+
}

integration/soroban/counter.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe('Counter', () => {
3535
it('get correct initial counter', async () => {
3636
// get the count
3737
let count = await call_contract_function("count", server, keypair, contract);
38-
expect(count.toString()).eq("10");
38+
console.log(count.returnValue().value());
39+
expect(count.returnValue().value().toString()).eq("10");
3940
});
4041

4142
it('increment counter', async () => {
@@ -44,7 +45,7 @@ describe('Counter', () => {
4445

4546
// get the count
4647
let count = await call_contract_function("count", server, keypair, contract);
47-
expect(count.toString()).eq("11");
48+
expect(count.returnValue().value()).eq("11");
4849
});
4950
});
5051

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as StellarSdk from '@stellar/stellar-sdk';
2+
import { readFileSync } from 'fs';
3+
import { expect } from 'chai';
4+
import path from 'path';
5+
import { fileURLToPath } from 'url';
6+
import { call_contract_function, extractLogEvent } from './test_helpers.js';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const dirname = path.dirname(__filename);
10+
const server = new StellarSdk.SorobanRpc.Server("https://soroban-testnet.stellar.org:443");
11+
12+
function readContractAddress(filename) {
13+
return readFileSync(path.join(dirname, '.soroban', 'contract-ids', filename), 'utf8').trim();
14+
}
15+
16+
describe('Cross Contract Calls', () => {
17+
let keypair, caller, callee, calleeRust;
18+
19+
before(async () => {
20+
console.log('Setting up cross contract tests...');
21+
22+
keypair = StellarSdk.Keypair.fromSecret(readFileSync('alice.txt', 'utf8').trim());
23+
caller = new StellarSdk.Contract(readContractAddress('caller.txt'));
24+
callee = new StellarSdk.Contract(readContractAddress('callee.txt'));
25+
calleeRust = new StellarSdk.Contract(readContractAddress('hello_world.txt'));
26+
});
27+
28+
it('calls Rust contract', async () => {
29+
let addr = calleeRust.address().toScVal();
30+
let values = [
31+
new StellarSdk.xdr.Uint64(BigInt(1)),
32+
new StellarSdk.xdr.Uint64(BigInt(2)),
33+
new StellarSdk.xdr.Uint64(BigInt(0))
34+
].map(StellarSdk.xdr.ScVal.scvU64);
35+
36+
let res = await call_contract_function("add", server, keypair, caller, addr, ...values);
37+
let returnValue = res.returnValue().value().toString();
38+
39+
console.log(returnValue);
40+
expect(returnValue).to.equal("3");
41+
42+
let logMessages = extractLogEvent(res.diagnosticEvents()).logMessages;
43+
console.log(logMessages);
44+
expect(logMessages[0]).to.contain('Soroban SDK add function called!');
45+
});
46+
47+
it('calls Solidity contract', async () => {
48+
let addr = callee.address().toScVal();
49+
let values = [
50+
new StellarSdk.xdr.Uint64(BigInt(1)),
51+
new StellarSdk.xdr.Uint64(BigInt(2)),
52+
new StellarSdk.xdr.Uint64(BigInt(0))
53+
].map(StellarSdk.xdr.ScVal.scvU64);
54+
55+
let res = await call_contract_function("add", server, keypair, caller, addr, ...values);
56+
let returnValue = res.returnValue().value().toString();
57+
58+
console.log(returnValue);
59+
expect(returnValue).to.equal("3");
60+
61+
let logMessages = extractLogEvent(res.diagnosticEvents()).logMessages;
62+
console.log(logMessages);
63+
expect(logMessages[0]).to.contain('add called in Solidity');
64+
});
65+
});

integration/soroban/runtime_error.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Runtime Error', () => {
3535
await call_contract_function("decrement", server, keypair, contract);
3636
});
3737

38-
it('get correct initial counter', async () => {
38+
it('prints error', async () => {
3939

4040
// decrement the counter again, resulting in a runtime error
4141
let res = await call_contract_function("decrement", server, keypair, contract);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![no_std]
2+
use soroban_sdk::{contract, contractimpl, Env, log};
3+
4+
#[contract]
5+
pub struct Contract;
6+
7+
#[contractimpl]
8+
impl Contract {
9+
pub fn add(env: Env, a: u64, b: u64, c: u64) -> u64 {
10+
log!(&env,"Soroban SDK add function called!");
11+
a + b + c
12+
}
13+
}

integration/soroban/setup.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function exe(command) {
2121
}
2222

2323
function generate_alice() {
24-
exe(`${soroban} keys generate alice --network testnet`);
24+
exe(`${soroban} keys generate alice --network testnet --overwrite`);
2525

2626
// get the secret key of alice and put it in alice.txt
2727
exe(`${soroban} keys show alice > alice.txt`);
@@ -32,6 +32,10 @@ function filenameNoExtension(filename) {
3232
return path.basename(filename, path.extname(filename));
3333
}
3434

35+
function build_rust() {
36+
exe(`soroban contract build --profile release-with-logs`);
37+
}
38+
3539
function deploy(wasm) {
3640

3741
let contractId = path.join(dirname, '.soroban', 'contract-ids', filenameNoExtension(wasm) + '.txt');
@@ -43,7 +47,13 @@ function deploy_all() {
4347
const contractsDir = path.join(dirname, '.soroban', 'contract-ids');
4448
mkdirSync(contractsDir, { recursive: true });
4549

46-
const wasmFiles = readdirSync(`${dirname}`).filter(file => file.endsWith('.wasm'));
50+
let wasmFiles = readdirSync(`${dirname}`).filter(file => file.endsWith('.wasm'));
51+
console.log(dirname);
52+
53+
let rust_wasm = path.join('rust','target','wasm32-unknown-unknown', 'release-with-logs', 'hello_world.wasm');
54+
55+
// add rust wasm file to the list of wasm files
56+
wasmFiles.push(rust_wasm);
4757

4858
wasmFiles.forEach(wasmFile => {
4959
deploy(path.join(dirname, wasmFile));
@@ -58,6 +68,7 @@ function add_testnet() {
5868
--network-passphrase "Test SDF Network ; September 2015"`);
5969
}
6070

71+
build_rust();
6172
add_testnet();
6273
generate_alice();
6374
deploy_all();

0 commit comments

Comments
 (0)