Skip to content

Commit

Permalink
Manual backports of #1795 and #1855 to update subxt
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones committed Aug 23, 2023
1 parent 949c00a commit 26c1e50
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 91 deletions.
3 changes: 2 additions & 1 deletion crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ tokio = { version = "1.18.2", features = ["rt-multi-thread"] }
log = { version = "0.4" }
env_logger = { version = "0.10" }
scale = { package = "parity-scale-codec", version = "3.4", default-features = false, features = ["derive"] }
subxt = "0.28.0"
subxt = "0.31.0"
subxt-signer = { version = "0.31.0", features = ["subxt", "sr25519"] }

# Substrate
pallet-contracts-primitives = "24.0.0"
Expand Down
68 changes: 34 additions & 34 deletions crates/e2e/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ use super::{
ContractExecResult,
ContractInstantiateResult,
ContractsApi,
Signer,
Keypair,
};
use ink_env::Environment;
use ink_primitives::MessageResult;
use pallet_contracts_primitives::ExecReturnValue;
use sp_core::Pair;
#[cfg(feature = "std")]
use std::{
collections::BTreeMap,
Expand All @@ -52,7 +51,8 @@ use subxt::{
ValueDef,
},
},
tx::PairSigner,
tx::Signer,
Config,
};

/// Result of a contract instantiation.
Expand All @@ -61,7 +61,7 @@ pub struct InstantiationResult<C: subxt::Config, E: Environment> {
pub account_id: E::AccountId,
/// The result of the dry run, contains debug messages
/// if there were any.
pub dry_run: ContractInstantiateResult<C::AccountId, E::Balance>,
pub dry_run: ContractInstantiateResult<C::AccountId, E::Balance, ()>,
/// Events that happened with the contract instantiation.
pub events: ExtrinsicEvents<C>,
}
Expand Down Expand Up @@ -191,7 +191,7 @@ where
pub struct CallDryRunResult<E: Environment, V> {
/// The result of the dry run, contains debug messages
/// if there were any.
pub exec_result: ContractExecResult<E::Balance>,
pub exec_result: ContractExecResult<E::Balance, ()>,
_marker: PhantomData<V>,
}

Expand Down Expand Up @@ -271,15 +271,15 @@ where
/// No contract with the given name found in scope.
ContractNotFound(String),
/// The `instantiate_with_code` dry run failed.
InstantiateDryRun(ContractInstantiateResult<C::AccountId, E::Balance>),
InstantiateDryRun(ContractInstantiateResult<C::AccountId, E::Balance, ()>),
/// The `instantiate_with_code` extrinsic failed.
InstantiateExtrinsic(subxt::error::DispatchError),
/// The `upload` dry run failed.
UploadDryRun(CodeUploadResult<E::Hash, E::Balance>),
/// The `upload` extrinsic failed.
UploadExtrinsic(subxt::error::DispatchError),
/// The `call` dry run failed.
CallDryRun(ContractExecResult<E::Balance>),
CallDryRun(ContractExecResult<E::Balance, ()>),
/// The `call` extrinsic failed.
CallExtrinsic(subxt::error::DispatchError),
/// Error fetching account balance.
Expand Down Expand Up @@ -383,12 +383,11 @@ where
impl<C, E> Client<C, E>
where
C: subxt::Config,
C::AccountId: From<sp_runtime::AccountId32>
+ scale::Codec
+ serde::de::DeserializeOwned
+ Debug,
C::AccountId:
From<sr25519::PublicKey> + scale::Codec + serde::de::DeserializeOwned + Debug,
C::Address: From<sr25519::PublicKey>,
C::Signature: From<sr25519::Signature>,
<C::ExtrinsicParams as ExtrinsicParams<C::Index, C::Hash>>::OtherParams: Default,
<C::ExtrinsicParams as ExtrinsicParams<C::Hash>>::OtherParams: Default,

E: Environment,
E::AccountId: Debug,
Expand Down Expand Up @@ -424,37 +423,38 @@ where
/// number of times.
pub async fn create_and_fund_account(
&self,
origin: &Signer<C>,
origin: &Keypair,
amount: E::Balance,
) -> Signer<C>
) -> Keypair
where
E::Balance: Clone,
C::AccountId: Clone + core::fmt::Display + Debug,
C::AccountId: From<sp_core::crypto::AccountId32>,
{
let (pair, _, _) = <sr25519::Pair as Pair>::generate_with_phrase(None);
let pair_signer = PairSigner::<C, _>::new(pair);
let account_id = pair_signer.account_id().to_owned();
let (_, phrase, _) =
<sp_core::sr25519::Pair as sp_core::Pair>::generate_with_phrase(None);
let phrase =
subxt_signer::bip39::Mnemonic::parse(phrase).expect("valid phrase expected");
let keypair = Keypair::from_phrase(&phrase, None).expect("valid phrase expected");
let account_id = <Keypair as Signer<C>>::account_id(&keypair);
let origin_account_id = origin.public_key().to_account_id();

self.api
.try_transfer_balance(origin, account_id.clone(), amount)
.await
.unwrap_or_else(|err| {
panic!(
"transfer from {} to {} failed with {:?}",
origin.account_id(),
account_id,
err
origin_account_id, account_id, err
)
});

log_info(&format!(
"transfer from {} to {} succeeded",
origin.account_id(),
account_id,
origin_account_id, account_id,
));

pair_signer
keypair
}

/// This function extracts the metadata of the contract at the file path
Expand All @@ -468,7 +468,7 @@ where
pub async fn instantiate<ContractRef, Args, R>(
&mut self,
contract_name: &str,
signer: &Signer<C>,
signer: &Keypair,
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
Expand All @@ -488,11 +488,11 @@ where
pub async fn instantiate_dry_run<ContractRef, Args, R>(
&mut self,
contract_name: &str,
signer: &Signer<C>,
signer: &Keypair,
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> ContractInstantiateResult<C::AccountId, E::Balance>
) -> ContractInstantiateResult<C::AccountId, E::Balance, ()>
where
Args: scale::Encode,
{
Expand Down Expand Up @@ -535,7 +535,7 @@ where
/// Executes an `instantiate_with_code` call and captures the resulting events.
async fn exec_instantiate<ContractRef, Args, R>(
&mut self,
signer: &Signer<C>,
signer: &Keypair,
code: Vec<u8>,
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
value: E::Balance,
Expand Down Expand Up @@ -646,7 +646,7 @@ where
pub async fn upload(
&mut self,
contract_name: &str,
signer: &Signer<C>,
signer: &Keypair,
storage_deposit_limit: Option<E::Balance>,
) -> Result<UploadResult<C, E>, Error<C, E>> {
let code = self.load_code(contract_name);
Expand All @@ -660,7 +660,7 @@ where
/// Executes an `upload` call and captures the resulting events.
pub async fn exec_upload(
&mut self,
signer: &Signer<C>,
signer: &Keypair,
code: Vec<u8>,
storage_deposit_limit: Option<E::Balance>,
) -> Result<UploadResult<C, E>, Error<C, E>> {
Expand Down Expand Up @@ -731,7 +731,7 @@ where
/// contains all events that are associated with this transaction.
pub async fn call<RetType>(
&mut self,
signer: &Signer<C>,
signer: &Keypair,
message: Message<E, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
Expand Down Expand Up @@ -793,7 +793,7 @@ where
/// contains all events that are associated with this transaction.
pub async fn runtime_call<'a>(
&mut self,
signer: &Signer<C>,
signer: &Keypair,
pallet_name: &'a str,
call_name: &'a str,
call_data: Vec<Value>,
Expand Down Expand Up @@ -828,7 +828,7 @@ where
/// invoked message.
pub async fn call_dry_run<RetType>(
&mut self,
signer: &Signer<C>,
signer: &Keypair,
message: &Message<E, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
Expand All @@ -839,7 +839,7 @@ where
let exec_result = self
.api
.call_dry_run(
Signer::account_id(signer).clone(),
Signer::<C>::account_id(signer),
message,
value,
storage_deposit_limit,
Expand Down Expand Up @@ -940,6 +940,6 @@ where
}

/// Returns true if the give event is System::Extrinsic failed.
fn is_extrinsic_failed_event(event: &EventDetails) -> bool {
fn is_extrinsic_failed_event<C: Config>(event: &EventDetails<C>) -> bool {
event.pallet_name() == "System" && event.variant_name() == "ExtrinsicFailed"
}
41 changes: 5 additions & 36 deletions crates/e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

mod builders;
mod client;
mod default_accounts;
mod node_proc;
mod xts;

Expand All @@ -37,7 +36,6 @@ pub use client::{
InstantiationResult,
UploadResult,
};
pub use default_accounts::*;
pub use env_logger;
pub use ink_e2e_macro::test;
pub use node_proc::{
Expand All @@ -46,9 +44,11 @@ pub use node_proc::{
};
pub use sp_core::H256;
pub use sp_keyring::AccountKeyring;
pub use subxt::{
pub use subxt;
pub use subxt_signer::sr25519::{
self,
tx::PairSigner,
dev::*,
Keypair,
};
pub use tokio;

Expand All @@ -57,44 +57,13 @@ use pallet_contracts_primitives::{
ContractExecResult,
ContractInstantiateResult,
};
use sp_core::sr25519;
use std::{
cell::RefCell,
sync::Once,
};
use xts::ContractsApi;

/// Default set of commonly used types by Substrate runtimes.
#[cfg(feature = "std")]
pub enum SubstrateConfig {}

#[cfg(feature = "std")]
impl subxt::Config for SubstrateConfig {
type Index = u32;
type Hash = sp_core::H256;
type Hasher = subxt::config::substrate::BlakeTwo256;
type AccountId = subxt::config::substrate::AccountId32;
type Address = sp_runtime::MultiAddress<Self::AccountId, u32>;
type Header = subxt::config::substrate::SubstrateHeader<
u32,
subxt::config::substrate::BlakeTwo256,
>;
type Signature = sp_runtime::MultiSignature;
type ExtrinsicParams = subxt::config::substrate::SubstrateExtrinsicParams<Self>;
}

/// Default set of commonly used types by Polkadot nodes.
#[cfg(feature = "std")]
pub type PolkadotConfig = subxt::config::WithExtrinsicParams<
SubstrateConfig,
subxt::config::polkadot::PolkadotExtrinsicParams<SubstrateConfig>,
>;

/// Signer that is used throughout the E2E testing.
///
/// The E2E testing can only be used with nodes that support `sr25519`
/// cryptography.
pub type Signer<C> = PairSigner<C, sr25519::Pair>;
pub use subxt::PolkadotConfig;

/// We use this to only initialize `env_logger` once.
pub static INIT: Once = Once::new();
Expand Down
Loading

0 comments on commit 26c1e50

Please sign in to comment.