ink_e2e/
lib.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Module for the logic behind ink!'s End-to-End testing framework.
16
17#![doc(
18    html_logo_url = "https://use.ink/img/crate-docs/logo.png",
19    html_favicon_url = "https://use.ink/crate-docs/favicon.png"
20)]
21
22mod backend;
23mod backend_calls;
24mod builders;
25mod client_utils;
26mod contract_build;
27mod contract_results;
28mod error;
29pub mod events;
30mod node_proc;
31#[cfg(feature = "sandbox")]
32mod sandbox_client;
33mod subxt_client;
34mod xts;
35
36pub use crate::contract_build::build_root_and_contract_dependencies;
37pub use backend::{
38    BuilderClient,
39    ChainBackend,
40    ContractsBackend,
41    E2EBackend,
42};
43pub use backend_calls::{
44    CallBuilder,
45    InstantiateBuilder,
46};
47pub use client_utils::ContractsRegistry;
48pub use contract_results::{
49    CallDryRunResult,
50    CallResult,
51    InstantiateDryRunResult,
52    InstantiationResult,
53    UploadResult,
54};
55pub use ink_e2e_macro::test;
56pub use node_proc::{
57    TestNodeProcess,
58    TestNodeProcessBuilder,
59};
60pub use pallet_revive::evm::CallTrace;
61#[cfg(feature = "sandbox")]
62pub use sandbox_client::{
63    preset,
64    Client as SandboxClient,
65};
66pub use sp_keyring::Sr25519Keyring;
67pub use subxt::{
68    self,
69    backend::rpc::RpcClient,
70};
71pub use subxt_client::{
72    CallBuilderFinal,
73    Client,
74    Error,
75};
76pub use subxt_signer::{
77    self,
78    sr25519::{
79        self,
80        dev::*,
81        Keypair,
82    },
83};
84pub use tokio;
85pub use tracing_subscriber;
86
87#[cfg(feature = "sandbox")]
88pub use ink_sandbox::DefaultSandbox;
89
90use ink::codegen::ContractCallBuilder;
91use ink_env::{
92    call::FromAddr,
93    ContractEnv,
94    Environment,
95};
96use ink_primitives::{
97    Address,
98    DepositLimit,
99    H256,
100};
101pub use sp_weights::Weight;
102use std::{
103    cell::RefCell,
104    sync::Once,
105};
106use xts::ReviveApi;
107
108use ink_primitives::types::AccountIdMapper;
109pub use subxt::PolkadotConfig;
110
111/// We use this to only initialize `env_logger` once.
112pub static INIT: Once = Once::new();
113
114// We save the name of the currently executing test here as a mean
115// of prefixing log entries to make it easier pinning them to tests.
116thread_local! {
117    /// This prefix will be used for log output. It is set by each
118    /// `#[ink_e2e::test]` with the function name as String.
119    /// This way it is possible to distinguish the lines in stdout
120    /// and stderr, to still know which line belongs to which test.
121    pub static LOG_PREFIX: RefCell<String> = RefCell::new(String::from("no prefix set"));
122}
123
124/// Returns the name of the test which is currently executed.
125pub fn log_prefix() -> String {
126    LOG_PREFIX.with(|log_prefix| log_prefix.borrow().clone())
127}
128
129/// Writes `msg` to stdout.
130pub fn log_info(msg: &str) {
131    tracing::info!("[{}] {}", log_prefix(), msg);
132}
133
134/// Writes `msg` to stderr.
135pub fn log_error(msg: &str) {
136    tracing::error!("[{}] {}", log_prefix(), msg);
137}
138
139/// Get an ink! [`ink_primitives::AccountId`] for a given keyring account.
140pub fn account_id(account: Sr25519Keyring) -> ink_primitives::AccountId {
141    ink_primitives::AccountId::try_from(account.to_account_id().as_ref())
142        .expect("account keyring has a valid account id")
143}
144
145/// Returns the [`ink::Address`] for a given keyring account.
146///
147/// # Developer Note
148///
149/// We take the `AccountId` and return only the first twenty bytes, this
150/// is what `pallet-revive` does as well.
151pub fn address<E: Environment>(account: Sr25519Keyring) -> Address {
152    AccountIdMapper::to_address(account.to_account_id().as_ref())
153}
154
155/// Creates a call builder for `Contract`, based on an account id.
156pub fn create_call_builder<Contract>(
157    acc_id: Address,
158) -> <Contract as ContractCallBuilder>::Type
159where
160    <Contract as ContractEnv>::Env: Environment,
161    Contract: ContractCallBuilder,
162    Contract: ContractEnv,
163    Contract::Type: FromAddr,
164{
165    <<Contract as ContractCallBuilder>::Type as FromAddr>::from_addr(acc_id)
166}
167
168fn balance_to_deposit_limit<E: Environment>(
169    b: <E as Environment>::Balance,
170) -> DepositLimit<<E as Environment>::Balance> {
171    DepositLimit::Balance(b)
172}
173
174fn deposit_limit_to_balance<E: Environment>(
175    l: DepositLimit<<E as Environment>::Balance>,
176) -> <E as Environment>::Balance {
177    match l {
178        DepositLimit::Balance(l) => l,
179        // todo
180        DepositLimit::Unchecked => panic!("`Unchecked` is not supported"),
181    }
182}