Skip to content

Commit 4ed8e37

Browse files
authored
Fix up contracts pallet tests (paritytech#163)
* Revert contracts put_code test to pure code (not using the macro) * Test contract instantiate * Fmt
1 parent 2829d7d commit 4ed8e37

File tree

3 files changed

+115
-37
lines changed

3 files changed

+115
-37
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
1919
[features]
2020
client = ["substrate-subxt-client"]
2121

22+
# enable this feature to run tests which require a local dev chain node
23+
integration-tests = []
24+
2225
[dependencies]
2326
log = "0.4.11"
2427
thiserror = "1.0.20"

src/frame/contracts.rs

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,43 +117,85 @@ pub struct InstantiatedEvent<T: Contracts> {
117117

118118
#[cfg(test)]
119119
mod tests {
120+
use sp_keyring::AccountKeyring;
121+
120122
use super::*;
123+
use crate::{
124+
ClientBuilder,
125+
ContractsTemplateRuntime,
126+
PairSigner,
127+
};
128+
129+
fn contract_wasm() -> Vec<u8> {
130+
const CONTRACT: &str = r#"
131+
(module
132+
(func (export "call"))
133+
(func (export "deploy"))
134+
)
135+
"#;
136+
wabt::wat2wasm(CONTRACT).expect("invalid wabt")
137+
}
138+
139+
#[async_std::test]
140+
#[cfg(feature = "integration-tests")]
141+
async fn tx_put_code() {
142+
env_logger::try_init().ok();
143+
144+
let signer = PairSigner::new(AccountKeyring::Alice.pair());
145+
let client = ClientBuilder::<ContractsTemplateRuntime>::new()
146+
.build()
147+
.await
148+
.unwrap();
149+
150+
let code = contract_wasm();
151+
let result = client.put_code_and_watch(&signer, &code).await.unwrap();
152+
let code_stored = result.code_stored().unwrap();
153+
154+
assert!(
155+
code_stored.is_some(),
156+
format!(
157+
"Error calling put_code and receiving CodeStored Event: {:?}",
158+
code_stored
159+
)
160+
);
161+
}
162+
163+
#[async_std::test]
164+
#[cfg(feature = "integration-tests")]
165+
async fn tx_instantiate() {
166+
env_logger::try_init().ok();
167+
let signer = PairSigner::new(AccountKeyring::Bob.pair());
168+
let client = ClientBuilder::<ContractsTemplateRuntime>::new()
169+
.build()
170+
.await
171+
.unwrap();
172+
173+
// call put_code extrinsic
174+
let code = contract_wasm();
175+
let result = client.put_code_and_watch(&signer, &code).await.unwrap();
176+
let code_stored = result.code_stored().unwrap();
177+
let code_hash = code_stored.unwrap().code_hash;
178+
179+
log::info!("Code hash: {:?}", code_hash);
180+
181+
// call instantiate extrinsic
182+
let result = client
183+
.instantiate_and_watch(
184+
&signer,
185+
100_000_000_000_000, // endowment
186+
500_000_000, // gas_limit
187+
&code_hash,
188+
&[], // data
189+
)
190+
.await
191+
.unwrap();
192+
193+
log::info!("Instantiate result: {:?}", result);
194+
let event = result.instantiated().unwrap();
121195

122-
subxt_test!({
123-
name: test_put_code_and_instantiate,
124-
prelude: {
125-
const CONTRACT: &str = r#"
126-
(module
127-
(func (export "call"))
128-
(func (export "deploy"))
129-
)
130-
"#;
131-
let wasm = wabt::wat2wasm(CONTRACT).expect("invalid wabt");
132-
let code_hash;
133-
},
134-
step: {
135-
call: PutCodeCall {
136-
_runtime: PhantomData,
137-
code: &wasm,
138-
},
139-
event: CodeStoredEvent {
140-
code_hash: {
141-
code_hash = event.code_hash.clone();
142-
event.code_hash.clone()
143-
},
144-
},
145-
},
146-
step: {
147-
call: InstantiateCall {
148-
endowment: 100_000_000_000_000,
149-
gas_limit: 500_000_000,
150-
code_hash: &code_hash,
151-
data: &[],
152-
},
153-
event: InstantiatedEvent {
154-
caller: alice.clone(),
155-
contract: event.contract.clone(),
156-
},
157-
},
158-
});
196+
assert!(
197+
event.is_some(),
198+
format!("Error instantiating contract: {:?}", result)
199+
);
200+
}
159201
}

src/runtimes.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,39 @@ impl Balances for NodeTemplateRuntime {
116116

117117
impl Sudo for NodeTemplateRuntime {}
118118

119+
/// Concrete type definitions compatible with the node template, with the
120+
/// contracts pallet enabled.
121+
///
122+
/// Inherits types from [`NodeTemplateRuntime`], but adds an implementation for
123+
/// the contracts pallet trait.
124+
#[derive(Debug, Clone, Eq, PartialEq)]
125+
pub struct ContractsTemplateRuntime;
126+
127+
impl Runtime for ContractsTemplateRuntime {
128+
type Signature = <NodeTemplateRuntime as Runtime>::Signature;
129+
type Extra = DefaultExtra<Self>;
130+
}
131+
132+
impl System for ContractsTemplateRuntime {
133+
type Index = <NodeTemplateRuntime as System>::Index;
134+
type BlockNumber = <NodeTemplateRuntime as System>::BlockNumber;
135+
type Hash = <NodeTemplateRuntime as System>::Hash;
136+
type Hashing = <NodeTemplateRuntime as System>::Hashing;
137+
type AccountId = <NodeTemplateRuntime as System>::AccountId;
138+
type Address = <NodeTemplateRuntime as System>::Address;
139+
type Header = <NodeTemplateRuntime as System>::Header;
140+
type Extrinsic = <NodeTemplateRuntime as System>::Extrinsic;
141+
type AccountData = <NodeTemplateRuntime as System>::AccountData;
142+
}
143+
144+
impl Balances for ContractsTemplateRuntime {
145+
type Balance = <NodeTemplateRuntime as Balances>::Balance;
146+
}
147+
148+
impl Contracts for ContractsTemplateRuntime {}
149+
150+
impl Sudo for ContractsTemplateRuntime {}
151+
119152
/// Concrete type definitions compatible with those for kusama, v0.7
120153
///
121154
/// # Note

0 commit comments

Comments
 (0)