Skip to content

Commit

Permalink
Update to ink! 5 (#60)
Browse files Browse the repository at this point in the history
* Update to ink-5.0.0-rc.1
  • Loading branch information
smiasojed authored Feb 20, 2024
1 parent b6c64d6 commit f19fcdc
Show file tree
Hide file tree
Showing 73 changed files with 900 additions and 1,023 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ jobs:
RUST_BACKTRACE: full

strategy:
fail-fast: false
matrix:
${{ fromJson(needs.prepare_matrix.outputs.MATRIX) }}

Expand Down
17 changes: 6 additions & 11 deletions basic-contract-caller/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
[package]
name = "basic_contract_caller"
version = "4.3.0"
name = "basic-contract-caller"
version = "5.0.0-rc.1"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "4.3", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true }
ink = { version = "5.0.0-rc.1", default-features = false }

# Note: We **need** to specify the `ink-as-dependency` feature.
#
# If we don't we will end up with linking errors!
other_contract = { path = "other_contract", default-features = false, features = ["ink-as-dependency"] }
other-contract = { path = "other-contract", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
ink_e2e = { version = "4.3" }
ink_e2e = { version = "5.0.0-rc.1" }

[lib]
path = "lib.rs"
Expand All @@ -26,12 +23,10 @@ path = "lib.rs"
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",

# Note: The metadata generation step requires `std`. If we don't specify this the metadata
# generation for our contract will fail!
"other_contract/std",
"other-contract/std",
]
ink-as-dependency = []
e2e-tests = []
23 changes: 23 additions & 0 deletions basic-contract-caller/other-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "other-contract"
version = "5.0.0-rc.1"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "5.0.0-rc.1", default-features = false }

[dev-dependencies]
ink_e2e = { version = "5.0.0-rc.1" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
]
ink-as-dependency = []
e2e-tests = []
File renamed without changes.
28 changes: 0 additions & 28 deletions basic-contract-caller/other_contract/Cargo.toml

This file was deleted.

15 changes: 3 additions & 12 deletions call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
[package]
name = "call-runtime"
version = "4.0.0"
version = "5.0.0-rc.1"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "4.3", default-features = false, features = ["call-runtime"] }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true }
ink = { version = "5.0.0-rc.1", default-features = false }

# Substrate
#
Expand All @@ -21,7 +18,7 @@ sp-io = { version = "23.0.0", default-features = false, features = ["disable_pan
sp-runtime = { version = "24.0.0", default-features = false }

[dev-dependencies]
ink_e2e = { version = "4.3" }
ink_e2e = { version = "5.0.0-rc.1" }

[lib]
path = "lib.rs"
Expand All @@ -30,14 +27,8 @@ path = "lib.rs"
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
"sp-runtime/std",
"sp-io/std",
]
ink-as-dependency = []
e2e-tests = []

# Assumes that the node used in E2E testing allows using the `call-runtime` API, including triggering
# `Balances::transfer` extrinsic.
permissive-node = []
123 changes: 59 additions & 64 deletions call-runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sp_runtime::MultiAddress;
/// You can investigate the full `RuntimeCall` definition by either expanding
/// `construct_runtime!` macro application or by using secondary tools for reading chain
/// metadata, like `subxt`.
#[derive(scale::Encode)]
#[ink::scale_derive(Encode)]
enum RuntimeCall {
/// This index can be found by investigating runtime configuration. You can check the
/// pallet order inside `construct_runtime!` block and read the position of your
Expand All @@ -25,7 +25,7 @@ enum RuntimeCall {
Balances(BalancesCall),
}

#[derive(scale::Encode)]
#[ink::scale_derive(Encode)]
enum BalancesCall {
/// This index can be found by investigating the pallet dispatchable API. In your
/// pallet code, look for `#[pallet::call]` section and check
Expand Down Expand Up @@ -54,16 +54,19 @@ mod runtime_call {
#[derive(Default)]
pub struct RuntimeCaller;

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
#[derive(Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum RuntimeError {
CallRuntimeFailed,
}

impl From<EnvError> for RuntimeError {
fn from(e: EnvError) -> Self {
use ink::env::ReturnErrorCode;
match e {
EnvError::CallRuntimeFailed => RuntimeError::CallRuntimeFailed,
EnvError::ReturnError(ReturnErrorCode::CallRuntimeFailed) => {
RuntimeError::CallRuntimeFailed
}
_ => panic!("Unexpected error from `pallet-contracts`."),
}
}
Expand Down Expand Up @@ -114,6 +117,10 @@ mod runtime_call {
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::{
ChainBackend,
ContractsBackend,
};

use ink::{
env::{
Expand All @@ -122,7 +129,6 @@ mod runtime_call {
},
primitives::AccountId,
};
use ink_e2e::build_message;

type E2EResult<T> = Result<T, Box<dyn std::error::Error>>;

Expand All @@ -145,56 +151,52 @@ mod runtime_call {
const INSUFFICIENT_TRANSFER_VALUE: Balance = 1;

/// Positive case scenario:
/// - `call_runtime` is enabled
/// - the call is valid
/// - the call execution succeeds
#[ink_e2e::test]
async fn transfer_with_call_runtime_works(
mut client: Client<C, E>,
async fn transfer_with_call_runtime_works<Client: E2EBackend>(
mut client: Client,
) -> E2EResult<()> {
// given
let constructor = RuntimeCallerRef::new();
let contract_acc_id = client
.instantiate(
"call-runtime",
&ink_e2e::alice(),
constructor,
CONTRACT_BALANCE,
None,
)
let mut constructor = RuntimeCallerRef::new();
let contract = client
.instantiate("call-runtime", &ink_e2e::alice(), &mut constructor)
.value(CONTRACT_BALANCE)
.submit()
.await
.expect("instantiate failed")
.account_id;
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<RuntimeCaller>();

let receiver: AccountId = default_accounts::<DefaultEnvironment>().bob;

let contract_balance_before = client
.balance(contract_acc_id)
.free_balance(contract.account_id)
.await
.expect("Failed to get account balance");
let receiver_balance_before = client
.balance(receiver)
.free_balance(receiver)
.await
.expect("Failed to get account balance");

// when
let transfer_message = build_message::<RuntimeCallerRef>(contract_acc_id)
.call(|caller| caller.transfer_through_runtime(receiver, TRANSFER_VALUE));
let transfer_message =
call_builder.transfer_through_runtime(receiver, TRANSFER_VALUE);

let call_res = client
.call(&ink_e2e::alice(), transfer_message, 0, None)
.call(&ink_e2e::alice(), &transfer_message)
.submit()
.await
.expect("call failed");

assert!(call_res.return_value().is_ok());

// then
let contract_balance_after = client
.balance(contract_acc_id)
.free_balance(contract.account_id)
.await
.expect("Failed to get account balance");
let receiver_balance_after = client
.balance(receiver)
.free_balance(receiver)
.await
.expect("Failed to get account balance");

Expand All @@ -211,38 +213,34 @@ mod runtime_call {
}

/// Negative case scenario:
/// - `call_runtime` is enabled
/// - the call is valid
/// - the call execution fails
#[ink_e2e::test]
async fn transfer_with_call_runtime_fails_when_execution_fails(
mut client: Client<C, E>,
async fn transfer_with_call_runtime_fails_when_execution_fails<
Client: E2EBackend,
>(
mut client: Client,
) -> E2EResult<()> {
// given
let constructor = RuntimeCallerRef::new();
let contract_acc_id = client
.instantiate(
"call-runtime",
&ink_e2e::alice(),
constructor,
CONTRACT_BALANCE,
None,
)
let mut constructor = RuntimeCallerRef::new();
let contract = client
.instantiate("call-runtime", &ink_e2e::alice(), &mut constructor)
.value(CONTRACT_BALANCE)
.submit()
.await
.expect("instantiate failed")
.account_id;
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<RuntimeCaller>();

let receiver: AccountId = default_accounts::<DefaultEnvironment>().bob;

// when
let transfer_message = build_message::<RuntimeCallerRef>(contract_acc_id)
.call(|caller| {
caller.transfer_through_runtime(receiver, INSUFFICIENT_TRANSFER_VALUE)
});
let transfer_message = call_builder
.transfer_through_runtime(receiver, INSUFFICIENT_TRANSFER_VALUE);

let call_res = client
.call_dry_run(&ink_e2e::alice(), &transfer_message, 0, None)
.await
.call(&ink_e2e::alice(), &transfer_message)
.dry_run()
.await?
.return_value();

// then
Expand All @@ -252,32 +250,29 @@ mod runtime_call {
}

/// Negative case scenario:
/// - `call_runtime` is enabled
/// - the call is invalid
#[ink_e2e::test]
async fn transfer_with_call_runtime_fails_when_call_is_invalid(
mut client: Client<C, E>,
async fn transfer_with_call_runtime_fails_when_call_is_invalid<
Client: E2EBackend,
>(
mut client: Client,
) -> E2EResult<()> {
// given
let constructor = RuntimeCallerRef::new();
let contract_acc_id = client
.instantiate(
"call-runtime",
&ink_e2e::alice(),
constructor,
CONTRACT_BALANCE,
None,
)
let mut constructor = RuntimeCallerRef::new();
let contract = client
.instantiate("call-runtime", &ink_e2e::alice(), &mut constructor)
.value(CONTRACT_BALANCE)
.submit()
.await
.expect("instantiate failed")
.account_id;
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<RuntimeCaller>();

// when
let transfer_message = build_message::<RuntimeCallerRef>(contract_acc_id)
.call(|caller| caller.call_nonexistent_extrinsic());
let transfer_message = call_builder.call_nonexistent_extrinsic();

let call_res = client
.call_dry_run(&ink_e2e::alice(), &transfer_message, 0, None)
.call(&ink_e2e::alice(), &transfer_message)
.dry_run()
.await;

// then
Expand Down
Loading

0 comments on commit f19fcdc

Please sign in to comment.