-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic
ink!
to solidity call integration test (#1087)
- Loading branch information
Showing
7 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Ignore build artifacts from the local tests sub-crate. | ||
/target/ | ||
|
||
# Ignore backup files creates by cargo fmt. | ||
**/*.rs.bk | ||
|
||
# Remove Cargo.lock when creating an executable, leave it for libraries | ||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "caller" | ||
version = "0.1.0" | ||
authors = ["Cyrill Leutwiler <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
ink = { version = "4.0.0-beta", default-features = false } | ||
|
||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true } | ||
|
||
[lib] | ||
name = "caller" | ||
path = "lib.rs" | ||
crate-type = [ | ||
# Used for normal contract Wasm blobs. | ||
"cdylib", | ||
] | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink/std", | ||
"scale/std", | ||
"scale-info/std", | ||
] | ||
ink-as-dependency = [] | ||
|
||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
#[ink::contract] | ||
mod caller { | ||
use ink::env::{ | ||
call::{build_call, Call, ExecutionInput, Selector}, | ||
DefaultEnvironment, | ||
}; | ||
|
||
#[ink(storage)] | ||
#[derive(Default)] | ||
pub struct Caller {} | ||
|
||
impl Caller { | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Do a proxy call to `callee` and return its result. | ||
/// The message under `selector` should have exactly one `u32` arguemnt and return a `u32`. | ||
#[ink(message)] | ||
pub fn u32_proxy( | ||
&self, | ||
callee: AccountId, | ||
selector: [u8; 4], | ||
arg: u32, | ||
max_gas: Option<u64>, | ||
transfer_value: Option<u128>, | ||
) -> u32 { | ||
let my_return_value = build_call::<DefaultEnvironment>() | ||
.call_type( | ||
Call::new() | ||
.callee(callee) | ||
.gas_limit(max_gas.unwrap_or_default()), | ||
) | ||
.transferred_value(transfer_value.unwrap_or_default()) | ||
.exec_input(ExecutionInput::new(Selector::new(selector)).push_arg(arg)) | ||
.returns::<u32>() // FIXME: This should be Result<u32, u8> to respect LanguageError | ||
.fire(); | ||
my_return_value.unwrap() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import expect from 'expect'; | ||
import { weight, createConnection, deploy, transaction, aliceKeypair, query, } from './index'; | ||
import { ContractPromise } from '@polkadot/api-contract'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
|
||
describe('Test cross contract calls between ink and solidity', () => { | ||
let conn: ApiPromise; | ||
let alice: KeyringPair; | ||
|
||
let ink_contract: ContractPromise; | ||
|
||
let sol_contract: ContractPromise; | ||
let inkee_echo = [1, 2, 3, 4]; | ||
|
||
before(async function () { | ||
conn = await createConnection(); | ||
alice = aliceKeypair(); | ||
|
||
let ink_deployment = await deploy(conn, alice, 'ink/caller/target/ink/caller.contract', 0n); | ||
ink_contract = new ContractPromise(conn, ink_deployment.abi, ink_deployment.address); | ||
|
||
let sol_deployment = await deploy(conn, alice, 'Inkee.contract', 0n); | ||
sol_contract = new ContractPromise(conn, sol_deployment.abi, sol_deployment.address); | ||
}); | ||
|
||
it('calls solidity from ink', async function () { | ||
this.timeout(50000); | ||
|
||
async function proxy(goes_in: number) { | ||
const comes_out = await query(conn, alice, ink_contract, "u32_proxy", [sol_contract.address, inkee_echo, goes_in, null, null]); | ||
expect(comes_out.output?.toJSON()).toEqual({ "ok": goes_in }); | ||
} | ||
|
||
await proxy(0); | ||
await proxy(1); | ||
await proxy(1337); | ||
await proxy(0xffffffff); | ||
}); | ||
|
||
after(async function () { | ||
await conn.disconnect(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
contract Inkee { | ||
function echo(uint32 v) selector=hex"01020304" public pure returns (uint32) { | ||
return v; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters