forked from use-ink/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.rs
More file actions
161 lines (149 loc) · 5.51 KB
/
backend.rs
File metadata and controls
161 lines (149 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::Keypair;
use crate::{
builders::CreateBuilderPartial,
CallBuilderFinal,
CallDryRunResult,
CallResult,
InstantiationResult,
UploadResult,
};
use ink_env::{
DefaultEnvironment,
Environment,
};
use jsonrpsee::core::async_trait;
use pallet_contracts_primitives::ContractInstantiateResult;
use subxt::dynamic::Value;
/// Full E2E testing backend: combines general chain API and contract-specific operations.
#[async_trait]
pub trait E2EBackend<E: Environment = DefaultEnvironment>:
ChainBackend + ContractsBackend<E>
{
}
/// General chain operations useful in contract testing.
#[async_trait]
pub trait ChainBackend {
/// Account type.
type AccountId;
/// Balance type.
type Balance: Send;
/// Error type.
type Error;
/// Event log type.
type EventLog;
/// Generate a new account and fund it with the given `amount` of tokens from the
/// `origin`.
async fn create_and_fund_account(
&mut self,
origin: &Keypair,
amount: Self::Balance,
) -> Keypair;
/// Returns the balance of `actor`.
async fn balance(
&mut self,
account: Self::AccountId,
) -> Result<Self::Balance, Self::Error>;
/// Executes a runtime call `call_name` for the `pallet_name`.
/// The `call_data` is a `Vec<Value>`.
///
/// Note:
/// - `pallet_name` must be in camel case, for example `Balances`.
/// - `call_name` must be snake case, for example `force_transfer`.
/// - `call_data` is a `Vec<subxt::dynamic::Value>` that holds a representation of
/// some value.
///
/// Returns when the transaction is included in a block. The return value contains all
/// events that are associated with this transaction.
async fn runtime_call<'a>(
&mut self,
origin: &Keypair,
pallet_name: &'a str,
call_name: &'a str,
call_data: Vec<Value>,
) -> Result<Self::EventLog, Self::Error>;
}
/// Contract-specific operations.
#[async_trait]
pub trait ContractsBackend<E: Environment> {
/// Error type.
type Error;
/// Event log type.
type EventLog;
/// The function subsequently uploads and instantiates an instance of the contract.
///
/// This function extracts the metadata of the contract at the file path
/// `target/ink/$contract_name.contract`.
///
/// Calling this function multiple times should be idempotent, the contract is
/// newly instantiated each time using a unique salt. No existing contract
/// instance is reused!
async fn instantiate<Contract, Args: Send + scale::Encode, R>(
&mut self,
contract_name: &str,
caller: &Keypair,
constructor: CreateBuilderPartial<E, Contract, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<InstantiationResult<E, Self::EventLog>, Self::Error>;
/// Dry run contract instantiation.
async fn instantiate_dry_run<Contract, Args: Send + scale::Encode, R>(
&mut self,
contract_name: &str,
caller: &Keypair,
constructor: CreateBuilderPartial<E, Contract, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> ContractInstantiateResult<E::AccountId, E::Balance, ()>;
/// The function subsequently uploads and instantiates an instance of the contract.
///
/// This function extracts the Wasm of the contract for the specified contract.
///
/// Calling this function multiple times should be idempotent, the contract is
/// newly instantiated each time using a unique salt. No existing contract
/// instance is reused!
async fn upload(
&mut self,
contract_name: &str,
caller: &Keypair,
storage_deposit_limit: Option<E::Balance>,
) -> Result<UploadResult<E, Self::EventLog>, Self::Error>;
/// Executes a `call` for the contract at `account_id`.
///
/// Returns when the transaction is included in a block. The return value
/// contains all events that are associated with this transaction.
async fn call<Args: Sync + scale::Encode, RetType: Send + scale::Decode>(
&mut self,
caller: &Keypair,
message: &CallBuilderFinal<E, Args, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<CallResult<E, RetType, Self::EventLog>, Self::Error>
where
CallBuilderFinal<E, Args, RetType>: Clone;
/// Executes a dry-run `call`.
///
/// Returns the result of the dry run, together with the decoded return value of the
/// invoked message.
async fn call_dry_run<Args: Sync + scale::Encode, RetType: Send + scale::Decode>(
&mut self,
caller: &Keypair,
message: &CallBuilderFinal<E, Args, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> CallDryRunResult<E, RetType>
where
CallBuilderFinal<E, Args, RetType>: Clone;
}