Skip to content

Commit

Permalink
Put contract back at workspace root again
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones committed Apr 8, 2024
1 parent fa066e7 commit 68163d5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 114 deletions.
13 changes: 8 additions & 5 deletions integration-tests/public/runtime-call-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = [ "flipper", "sandbox-runtime", "traits"]
members = ["sandbox-runtime", "traits"]

[workspace.package]
authors = ["Parity Technologies <[email protected]>"]
Expand All @@ -25,12 +25,15 @@ publish = false

[dependencies]
ink = { path = "../../../crates/ink", default-features = false }
ink_e2e = { path = "../../../crates/e2e", features = ["sandbox"] }
flipper-traits = { path = "traits", default-features = false }
flipper = { path = "flipper", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
ink_e2e = { path = "../../../crates/e2e", features = ["sandbox"] }
sandbox-runtime = { path = "sandbox-runtime", default-features = false }
scale-value = "0.14.1"
frame-support = { workspace = true }
# can't use workspace dependency because of `cargo-contract` build not
# working with workspace dependencies
frame-support = { version = "31.0.0", default-features = false }

[lib]
path = "lib.rs"
Expand All @@ -40,6 +43,6 @@ default = ["std"]
std = [
"ink/std",
"sandbox-runtime/std",
"flipper/std",
"flipper-traits/std",
]
ink-as-dependency = []
60 changes: 60 additions & 0 deletions integration-tests/public/runtime-call-contract/e2e_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::{
Flipper,
FlipperRef,
};
use ink_e2e::{
ChainBackend,
ContractsBackend,
};

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

/// Just instantiate a contract using non-default runtime.
#[ink_e2e::test(backend(runtime_only(sandbox = sandbox_runtime::ContractCallerSandbox)))]
async fn instantiate_and_get<Client: E2EBackend>(mut client: Client) -> E2EResult<()> {
use flipper_traits::Flip;

let initial_value = false;
let mut constructor = FlipperRef::new(initial_value);

let contract = client
.instantiate("runtime-call-contract", &ink_e2e::alice(), &mut constructor)
.submit()
.await
.expect("instantiate failed");

let mut call_builder = contract.call_builder::<Flipper>();
let flip_dry_run = client
.call(&ink_e2e::bob(), &call_builder.flip())
.dry_run()
.await?;
let gas_required = flip_dry_run.exec_result.gas_required;

// call pallet dispatchable
client
.runtime_call(
&ink_e2e::alice(),
"ContractCaller",
"contract_call_flip",
vec![
scale_value::Value::from_bytes(contract.account_id),
scale_value::serde::to_value(frame_support::weights::Weight::from_parts(
gas_required.ref_time(),
gas_required.proof_size(),
))
.unwrap(),
],
)
.await
.expect("runtime call failed");

// now check that the flip was executed via the pallet
let get_result = client
.call(&ink_e2e::alice(), &call_builder.get())
.dry_run()
.await?;

assert_eq!(get_result.return_value(), !initial_value);

Ok(())
}
20 changes: 0 additions & 20 deletions integration-tests/public/runtime-call-contract/flipper/Cargo.toml

This file was deleted.

33 changes: 0 additions & 33 deletions integration-tests/public/runtime-call-contract/flipper/lib.rs

This file was deleted.

88 changes: 32 additions & 56 deletions integration-tests/public/runtime-call-contract/lib.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,36 @@
#[cfg(test)]
mod e2e_tests{
use flipper::{Flipper, FlipperRef};
use ink_e2e::{
ChainBackend,
ContractsBackend,
};

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

/// Just instantiate a contract using non-default runtime.
#[ink_e2e::test(backend(runtime_only(sandbox = sandbox_runtime::ContractCallerSandbox)))]
async fn instantiate_and_get<Client: E2EBackend>(mut client: Client) -> E2EResult<()> {
use flipper_traits::Flip;

let initial_value = false;
let mut constructor = FlipperRef::new(initial_value);

let contract = client
.instantiate("flipper", &ink_e2e::alice(), &mut constructor)
.submit()
.await
.expect("instantiate failed");

let mut call_builder = contract.call_builder::<Flipper>();
let flip_dry_run = client
.call(&ink_e2e::bob(), &call_builder.flip())
.dry_run()
.await?;
let gas_required = flip_dry_run.exec_result.gas_required;

// call pallet dispatchable
client
.runtime_call(
&ink_e2e::alice(),
"ContractCaller",
"contract_call_flip",
vec![
scale_value::Value::from_bytes(contract.account_id),
scale_value::serde::to_value(frame_support::weights::Weight::from_parts(
gas_required.ref_time(),
gas_required.proof_size(),
))
.unwrap(),
],
)
.await
.expect("runtime call failed");
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use flipper::{
Flipper,
FlipperRef,
};

#[ink::contract]
mod flipper {
#[ink(storage)]
pub struct Flipper {
value: bool,
}

// now check that the flip was executed via the pallet
let get_result = client
.call(&ink_e2e::alice(), &call_builder.get())
.dry_run()
.await?;
impl Flipper {
#[ink(constructor)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
}
}

assert_eq!(get_result.return_value(), !initial_value);
impl flipper_traits::Flip for Flipper {
#[ink(message)]
fn flip(&mut self) {
self.value = !self.value;
}

Ok(())
#[ink(message)]
fn get(&self) -> bool {
self.value
}
}
}
}

#[cfg(test)]
mod e2e_tests;

0 comments on commit 68163d5

Please sign in to comment.