Files
pezkuwi-subxt/subxt/tests/integration/frame/contracts.rs
T
Alexandru Vasile 08369f3e43 Reference key storage api (#447)
* codegen: Update polkadot.rs

polkadot commit-hash: d96d3bea85
polkadot tag: v0.9.16-rc2

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Reference key storage api

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Regenerate polkadot.rs with reference api

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Update tests with reference interface

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Fix polkadot.rs license check

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Update polkadot.rs with copyright

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "codegen: Update polkadot.rs with copyright"

This reverts commit 2970d0573dc0b11d01072b270a525ad497992ddf.

Revert "cli: Fix polkadot.rs license check"

This reverts commit 6fe8818582ae39669c059c1ed0424b6606620295.

* codegen: Implement AccountData trait in the expected order

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Store implementation of StorageEntry

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Generate AccountDefaultData wrapper struct

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Allow `Account` references

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Update polkadot.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Utilize AccountDefaultData instead of Account

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Update polkadot.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Update tests to utilize `Account` reference

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Rename AccountDefaultData to AccountOwned

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Add comments for wrapper account

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Obtain vector type parameter for TypePath::Type

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Use slices instead of `& std::vec` in storage API

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Update polkadot.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Fix documentation

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Remove extra reference

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Add staking example to exercise storage API

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Update polkadot.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Update storage tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix cargo clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Simplify vec_type_param

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Rename staking_details.rs to fetch_staking_details.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Remove dummy variable

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Update polkadot version

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Apply rust-fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Regenerate polkadot.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Remove comment

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
2022-03-02 17:13:43 +02:00

231 lines
6.6 KiB
Rust

// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is part of subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
use sp_keyring::AccountKeyring;
use crate::{
node_runtime::{
self,
contracts::{
calls::TransactionApi,
events,
storage,
},
system,
DefaultAccountData,
DispatchError,
},
test_context,
NodeRuntimeSignedExtra,
TestContext,
};
use sp_core::sr25519::Pair;
use sp_runtime::MultiAddress;
use subxt::{
Client,
Config,
DefaultConfig,
Error,
PairSigner,
TransactionProgress,
};
struct ContractsTestContext {
cxt: TestContext,
signer: PairSigner<DefaultConfig, NodeRuntimeSignedExtra, Pair>,
}
type Hash = <DefaultConfig as Config>::Hash;
type AccountId = <DefaultConfig as Config>::AccountId;
impl ContractsTestContext {
async fn init() -> Self {
let cxt = test_context().await;
let signer = PairSigner::new(AccountKeyring::Alice.pair());
Self { cxt, signer }
}
fn client(&self) -> &Client<DefaultConfig> {
self.cxt.client()
}
fn contracts_tx(
&self,
) -> TransactionApi<DefaultConfig, NodeRuntimeSignedExtra, DefaultAccountData> {
self.cxt.api.tx().contracts()
}
async fn instantiate_with_code(
&self,
) -> Result<(Hash, AccountId), Error<DispatchError>> {
log::info!("instantiate_with_code:");
const CONTRACT: &str = r#"
(module
(func (export "call"))
(func (export "deploy"))
)
"#;
let code = wabt::wat2wasm(CONTRACT).expect("invalid wabt");
let events = self
.cxt
.api
.tx()
.contracts()
.instantiate_with_code(
100_000_000_000_000_000, // endowment
500_000_000_000, // gas_limit
None, // storage_deposit_limit
code,
vec![], // data
vec![], // salt
)
.sign_and_submit_then_watch(&self.signer)
.await?
.wait_for_finalized_success()
.await?;
let code_stored = events
.find_first::<events::CodeStored>()?
.ok_or_else(|| Error::Other("Failed to find a CodeStored event".into()))?;
let instantiated = events
.find_first::<events::Instantiated>()?
.ok_or_else(|| Error::Other("Failed to find a Instantiated event".into()))?;
let _extrinsic_success = events
.find_first::<system::events::ExtrinsicSuccess>()?
.ok_or_else(|| {
Error::Other("Failed to find a ExtrinsicSuccess event".into())
})?;
log::info!(" Block hash: {:?}", events.block_hash());
log::info!(" Code hash: {:?}", code_stored.code_hash);
log::info!(" Contract address: {:?}", instantiated.contract);
Ok((code_stored.code_hash, instantiated.contract))
}
async fn instantiate(
&self,
code_hash: Hash,
data: Vec<u8>,
salt: Vec<u8>,
) -> Result<AccountId, Error<DispatchError>> {
// call instantiate extrinsic
let result = self
.contracts_tx()
.instantiate(
100_000_000_000_000_000, // endowment
500_000_000_000, // gas_limit
None, // storage_deposit_limit
code_hash,
data,
salt,
)
.sign_and_submit_then_watch(&self.signer)
.await?
.wait_for_finalized_success()
.await?;
log::info!("Instantiate result: {:?}", result);
let instantiated = result
.find_first::<events::Instantiated>()?
.ok_or_else(|| Error::Other("Failed to find a Instantiated event".into()))?;
Ok(instantiated.contract)
}
async fn call(
&self,
contract: AccountId,
input_data: Vec<u8>,
) -> Result<
TransactionProgress<'_, DefaultConfig, DispatchError, node_runtime::Event>,
Error<DispatchError>,
> {
log::info!("call: {:?}", contract);
let result = self
.contracts_tx()
.call(
MultiAddress::Id(contract),
0, // value
500_000_000, // gas_limit
None, // storage_deposit_limit
input_data,
)
.sign_and_submit_then_watch(&self.signer)
.await?;
log::info!("Call result: {:?}", result);
Ok(result)
}
}
#[async_std::test]
async fn tx_instantiate_with_code() {
let ctx = ContractsTestContext::init().await;
let result = ctx.instantiate_with_code().await;
assert!(
result.is_ok(),
"Error calling instantiate_with_code and receiving CodeStored and Instantiated Events: {:?}",
result
);
}
#[async_std::test]
async fn tx_instantiate() {
let ctx = ContractsTestContext::init().await;
let (code_hash, _) = ctx.instantiate_with_code().await.unwrap();
let instantiated = ctx.instantiate(code_hash, vec![], vec![1u8]).await;
assert!(
instantiated.is_ok(),
"Error instantiating contract: {:?}",
instantiated
);
}
#[async_std::test]
async fn tx_call() {
let cxt = ContractsTestContext::init().await;
let (_, contract) = cxt.instantiate_with_code().await.unwrap();
let contract_info = cxt
.cxt
.api
.storage()
.contracts()
.contract_info_of(&contract, None)
.await;
assert!(contract_info.is_ok());
let keys = cxt
.client()
.storage()
.fetch_keys::<storage::ContractInfoOf>(5, None, None)
.await
.unwrap()
.iter()
.map(|key| hex::encode(&key.0))
.collect::<Vec<_>>();
println!("keys post: {:?}", keys);
let executed = cxt.call(contract, vec![]).await;
assert!(executed.is_ok(), "Error calling contract: {:?}", executed);
}