Skip to content

Commit b52cb40

Browse files
authored
Add basic ink! to solidity call integration test (#1087)
1 parent 54d6715 commit b52cb40

File tree

7 files changed

+139
-1
lines changed

7 files changed

+139
-1
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ jobs:
320320
echo "$(pwd)/bin" >> $GITHUB_PATH
321321
- run: npm install
322322
working-directory: ./integration/substrate
323+
- name: Build ink! contracts
324+
run: npm run build-ink
325+
working-directory: ./integration/substrate
323326
- name: Build Solang contracts
324327
run: npm run build
325328
working-directory: ./integration/substrate
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ignore build artifacts from the local tests sub-crate.
2+
/target/
3+
4+
# Ignore backup files creates by cargo fmt.
5+
**/*.rs.bk
6+
7+
# Remove Cargo.lock when creating an executable, leave it for libraries
8+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
9+
Cargo.lock
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "caller"
3+
version = "0.1.0"
4+
authors = ["Cyrill Leutwiler <cyrill@parity.io>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
ink = { version = "4.0.0-beta", default-features = false }
9+
10+
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
11+
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }
12+
13+
[lib]
14+
name = "caller"
15+
path = "lib.rs"
16+
crate-type = [
17+
# Used for normal contract Wasm blobs.
18+
"cdylib",
19+
]
20+
21+
[features]
22+
default = ["std"]
23+
std = [
24+
"ink/std",
25+
"scale/std",
26+
"scale-info/std",
27+
]
28+
ink-as-dependency = []
29+
30+
[workspace]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#![cfg_attr(not(feature = "std"), no_std)]
4+
5+
#[ink::contract]
6+
mod caller {
7+
use ink::env::{
8+
call::{build_call, Call, ExecutionInput, Selector},
9+
DefaultEnvironment,
10+
};
11+
12+
#[ink(storage)]
13+
#[derive(Default)]
14+
pub struct Caller {}
15+
16+
impl Caller {
17+
#[ink(constructor)]
18+
pub fn new() -> Self {
19+
Default::default()
20+
}
21+
22+
/// Do a proxy call to `callee` and return its result.
23+
/// The message under `selector` should have exactly one `u32` arguemnt and return a `u32`.
24+
#[ink(message)]
25+
pub fn u32_proxy(
26+
&self,
27+
callee: AccountId,
28+
selector: [u8; 4],
29+
arg: u32,
30+
max_gas: Option<u64>,
31+
transfer_value: Option<u128>,
32+
) -> u32 {
33+
let my_return_value = build_call::<DefaultEnvironment>()
34+
.call_type(
35+
Call::new()
36+
.callee(callee)
37+
.gas_limit(max_gas.unwrap_or_default()),
38+
)
39+
.transferred_value(transfer_value.unwrap_or_default())
40+
.exec_input(ExecutionInput::new(Selector::new(selector)).push_arg(arg))
41+
.returns::<u32>() // FIXME: This should be Result<u32, u8> to respect LanguageError
42+
.fire();
43+
my_return_value.unwrap()
44+
}
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import expect from 'expect';
2+
import { weight, createConnection, deploy, transaction, aliceKeypair, query, } from './index';
3+
import { ContractPromise } from '@polkadot/api-contract';
4+
import { ApiPromise } from '@polkadot/api';
5+
import { KeyringPair } from '@polkadot/keyring/types';
6+
7+
describe('Test cross contract calls between ink and solidity', () => {
8+
let conn: ApiPromise;
9+
let alice: KeyringPair;
10+
11+
let ink_contract: ContractPromise;
12+
13+
let sol_contract: ContractPromise;
14+
let inkee_echo = [1, 2, 3, 4];
15+
16+
before(async function () {
17+
conn = await createConnection();
18+
alice = aliceKeypair();
19+
20+
let ink_deployment = await deploy(conn, alice, 'ink/caller/target/ink/caller.contract', 0n);
21+
ink_contract = new ContractPromise(conn, ink_deployment.abi, ink_deployment.address);
22+
23+
let sol_deployment = await deploy(conn, alice, 'Inkee.contract', 0n);
24+
sol_contract = new ContractPromise(conn, sol_deployment.abi, sol_deployment.address);
25+
});
26+
27+
it('calls solidity from ink', async function () {
28+
this.timeout(50000);
29+
30+
async function proxy(goes_in: number) {
31+
const comes_out = await query(conn, alice, ink_contract, "u32_proxy", [sol_contract.address, inkee_echo, goes_in, null, null]);
32+
expect(comes_out.output?.toJSON()).toEqual({ "ok": goes_in });
33+
}
34+
35+
await proxy(0);
36+
await proxy(1);
37+
await proxy(1337);
38+
await proxy(0xffffffff);
39+
});
40+
41+
after(async function () {
42+
await conn.disconnect();
43+
});
44+
});

integration/substrate/inkee.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
contract Inkee {
2+
function echo(uint32 v) selector=hex"01020304" public pure returns (uint32) {
3+
return v;
4+
}
5+
}

integration/substrate/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "tsc; ts-mocha -t 20000 *.spec.ts",
8-
"build": "parallel solang compile -v --target substrate ::: *.sol test/*.sol"
8+
"build": "parallel solang compile -v --target substrate ::: *.sol test/*.sol",
9+
"build-ink": "docker run --rm -v $(pwd)/ink/caller:/opt/contract paritytech/contracts-ci-linux:latest cargo contract build --manifest-path /opt/contract/Cargo.toml"
910
},
1011
"contributors": [
1112
{

0 commit comments

Comments
 (0)