Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[E2E] Add create_call_builder for testing on-chain contracts #2075

Merged
merged 26 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Custom signature topic in Events - [#2031](https://github.com/paritytech/ink/pull/2031)
- Linter: `non_fallible_api` lint - [#2004](https://github.com/paritytech/ink/pull/2004)
- Linter: Publish the linting crates on crates.io - [#2060](https://github.com/paritytech/ink/pull/2060)
- [E2E] Added `create_call_builder` for testing existing contracts - [#2075](https://github.com/paritytech/ink/pull/2075)

### Fixed
- Fix the `StorageVec` type by excluding the `len_cached` field from its type info - [#2052](https://github.com/paritytech/ink/pull/2052)
Expand Down
20 changes: 20 additions & 0 deletions crates/e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ pub use {
drink_client::Client as DrinkClient,
};

use ink_env::{
call::FromAccountId,
ContractEnv,
Environment,
};
use pallet_contracts_primitives::{
ContractExecResult,
ContractInstantiateResult,
Expand Down Expand Up @@ -128,6 +133,21 @@ pub fn account_id(account: AccountKeyring) -> ink_primitives::AccountId {
.expect("account keyring has a valid account id")
}

/// Creates a call builder builder for `Contract`, based on an account id.
pub fn create_call_builder<Contract>(
acc_id: <<Contract as ContractEnv>::Env as Environment>::AccountId,
) -> <Contract as ContractCallBuilder>::Type
where
<Contract as ContractEnv>::Env: Environment,
Contract: ContractCallBuilder,
Contract: ContractEnv,
Contract::Type: FromAccountId<<Contract as ContractEnv>::Env>,
{
<<Contract as ContractCallBuilder>::Type as FromAccountId<
<Contract as ContractEnv>::Env,
>>::from_account_id(acc_id)
}

/// Builds a contract and imports its scaffolded structure as a module.
#[macro_export]
macro_rules! build {
Expand Down
1 change: 1 addition & 0 deletions integration-tests/flipper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ink = { path = "../../crates/ink", default-features = false }

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }
hex-literal = { version = "0.4.1" }

[lib]
path = "lib.rs"
Expand Down
45 changes: 45 additions & 0 deletions integration-tests/flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,50 @@ pub mod flipper {

Ok(())
}

/// This test illustrates how to test an existing on-chain contract.
///
/// You can utilize this to e.g. create a snapshot of a production chain
/// and run the E2E tests against a deployed contract there.
/// This process is explained [here](https://use.ink/5.x/basics/contract-testing/chain-snapshot).
/// It requires a node to be run in the background, before executing the test.
///
/// The test is run like this:
///
/// ```
/// # The env variable needs to be set, otherwise `ink_e2e` will spawn a new
/// # node process for each test.
/// $ export CONTRACTS_NODE_URL=ws://127.0.0.1:9944
///
/// $ cargo test --features e2e-tests e2e_test_deployed_contract
/// ```
#[ink_e2e::test]
#[ignore]
async fn e2e_test_deployed_contract<Client: E2EBackend>(
mut client: Client,
) -> E2EResult<()> {
// given
// You can take a SS58 address and convert it to hex using the `subkey` tool:
//
// ```
// subkey inspect 5D4zzvxGZq4wx3SYuaDomSMSAS1h1Knm35gz62UAk4Tqscey
// ...
// Public key (hex): 0x2c75f0aa09dbfbfd49e6286a0f2edd3b4913f04a58b13391c79e96782f5713e3
// ```
let acc_id = hex_literal::hex!(
"2c75f0aa09dbfbfd49e6286a0f2edd3b4913f04a58b13391c79e96782f5713e3"
);
let acc_id = AccountId::from(acc_id);

// when
// Invoke `Flipper::get()` from Bob
let call_builder = ink_e2e::create_call_builder::<Flipper>(acc_id);
let get = call_builder.get();
let get_res = client.call(&ink_e2e::bob(), &get).dry_run().await?;

// then
assert!(matches!(get_res.return_value(), true));
Ok(())
}
}
}
Loading