Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update subxt version #1750

Merged
merged 14 commits into from
Apr 17, 2023
12 changes: 6 additions & 6 deletions crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ 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.27.0"
subxt = "0.28.0"

# Substrate
pallet-contracts-primitives = "18.0.0"
sp-core = "16.0.0"
sp-keyring = "18.0.0"
sp-runtime = "18.0.0"
sp-weights = "14.0.0"
pallet-contracts-primitives = "23.0.0"
sp-core = "20.0.0"
sp-keyring = "23.0.0"
sp-runtime = "23.0.0"
sp-weights = "19.0.0"

[dev-dependencies]
# Required for the doctest of `MessageBuilder::call`
Expand Down
70 changes: 46 additions & 24 deletions crates/e2e/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use ink_env::Environment;
use ink_primitives::MessageResult;
use pallet_contracts_primitives::ExecReturnValue;
use sp_core::Pair;
#[cfg(feature = "std")]
ascjones marked this conversation as resolved.
Show resolved Hide resolved
use std::{
collections::BTreeMap,
fmt::Debug,
Expand All @@ -42,10 +43,14 @@ use subxt::{
blocks::ExtrinsicEvents,
config::ExtrinsicParams,
events::EventDetails,
ext::scale_value::{
Composite,
Value,
ValueDef,
ext::{
scale_decode,
scale_encode,
scale_value::{
Composite,
Value,
ValueDef,
},
},
tx::PairSigner,
};
Expand Down Expand Up @@ -279,6 +284,8 @@ where
CallExtrinsic(subxt::error::DispatchError),
/// Error fetching account balance.
Balance(String),
/// Decoding failed.
Decoding(subxt::Error),
}

// We implement a custom `Debug` here, as to avoid requiring the trait
Expand Down Expand Up @@ -307,12 +314,21 @@ where
Error::CallDryRun(_) => f.write_str("CallDryRun"),
Error::CallExtrinsic(_) => f.write_str("CallExtrinsic"),
Error::Balance(msg) => write!(f, "Balance: {msg}"),
Error::Decoding(err) => write!(f, "Decoding: {err}"),
}
}
}

/// A contract was successfully instantiated.
#[derive(Debug, scale::Decode, scale::Encode)]
#[derive(
Debug,
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
struct ContractInstantiatedEvent<E: Environment> {
/// Account id of the deployer.
pub deployer: E::AccountId,
Expand All @@ -329,7 +345,15 @@ where
}

/// Code with the specified hash has been stored.
#[derive(Debug, scale::Decode, scale::Encode)]
#[derive(
Debug,
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
struct CodeStoredEvent<E: Environment> {
/// Hash under which the contract code was stored.
pub code_hash: E::Hash,
Expand Down Expand Up @@ -580,10 +604,9 @@ where
// multiple accounts as part of its constructor!
} else if is_extrinsic_failed_event(&evt) {
let metadata = self.api.client.metadata();
let dispatch_error = subxt::error::DispatchError::decode_from(
evt.field_bytes(),
&metadata,
);
let dispatch_error =
subxt::error::DispatchError::decode_from(evt.field_bytes(), metadata)
.map_err(Error::Decoding)?;
log_error(&format!(
"extrinsic for instantiate failed: {dispatch_error:?}"
));
Expand Down Expand Up @@ -672,10 +695,10 @@ where
break
} else if is_extrinsic_failed_event(&evt) {
let metadata = self.api.client.metadata();
let dispatch_error = subxt::error::DispatchError::decode_from(
evt.field_bytes(),
&metadata,
);
let dispatch_error =
subxt::error::DispatchError::decode_from(evt.field_bytes(), metadata)
.map_err(Error::Decoding)?;

log_error(&format!("extrinsic for upload failed: {dispatch_error:?}"));
return Err(Error::UploadExtrinsic(dispatch_error))
}
Expand Down Expand Up @@ -727,7 +750,7 @@ where
let tx_events = self
.api
.call(
sp_runtime::MultiAddress::Id(message.account_id().clone()),
subxt::utils::MultiAddress::Id(message.account_id().clone()),
value,
dry_run.exec_result.gas_required,
storage_deposit_limit,
Expand All @@ -743,10 +766,9 @@ where

if is_extrinsic_failed_event(&evt) {
let metadata = self.api.client.metadata();
let dispatch_error = subxt::error::DispatchError::decode_from(
evt.field_bytes(),
&metadata,
);
let dispatch_error =
subxt::error::DispatchError::decode_from(evt.field_bytes(), metadata)
.map_err(Error::Decoding)?;
log_error(&format!("extrinsic for call failed: {dispatch_error:?}"));
return Err(Error::CallExtrinsic(dispatch_error))
}
Expand Down Expand Up @@ -788,10 +810,10 @@ where

if is_extrinsic_failed_event(&evt) {
let metadata = self.api.client.metadata();
let dispatch_error = subxt::error::DispatchError::decode_from(
evt.field_bytes(),
&metadata,
);
let dispatch_error =
subxt::error::DispatchError::decode_from(evt.field_bytes(), metadata)
.map_err(Error::Decoding)?;

log_error(&format!("extrinsic for call failed: {dispatch_error:?}"));
return Err(Error::CallExtrinsic(dispatch_error))
}
Expand Down Expand Up @@ -857,7 +879,7 @@ where
.api
.client
.storage()
.at(None)
.at_latest()
.await
.unwrap_or_else(|err| {
panic!("unable to fetch balance: {err:?}");
Expand Down
78 changes: 44 additions & 34 deletions crates/e2e/src/xts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,58 @@ use sp_weights::Weight;
use subxt::{
blocks::ExtrinsicEvents,
config::ExtrinsicParams,
ext::scale_encode,
rpc_params,
tx,
utils::MultiAddress,
OnlineClient,
};

/// A raw call to `pallet-contracts`'s `instantiate_with_code`.
#[derive(Debug, scale::Encode, scale::Decode)]
pub struct InstantiateWithCode<B> {
#[derive(Debug, scale::Encode, scale::Decode, scale_encode::EncodeAsType)]
#[encode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_encode")]
pub struct InstantiateWithCode<E: Environment> {
#[codec(compact)]
value: B,
gas_limit: Weight,
storage_deposit_limit: Option<B>,
value: E::Balance,
gas_limit: subxt::utils::Static<Weight>,
storage_deposit_limit: Option<E::Balance>,
code: Vec<u8>,
data: Vec<u8>,
salt: Vec<u8>,
}

/// A raw call to `pallet-contracts`'s `call`.
#[derive(Debug, scale::Encode, scale::Decode)]
pub struct Call<E: Environment, B> {
dest: sp_runtime::MultiAddress<E::AccountId, ()>,
#[derive(Debug, scale::Decode, scale::Encode, scale_encode::EncodeAsType)]
#[encode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_encode")]
pub struct Call<E: Environment> {
dest: MultiAddress<E::AccountId, ()>,
#[codec(compact)]
value: B,
gas_limit: Weight,
storage_deposit_limit: Option<B>,
value: E::Balance,
gas_limit: subxt::utils::Static<Weight>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weight is a super simple struct, so for a little added robustness/simplicity I wonder whether it's worth just writing your own version that impls EncodeAsType et al

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but sp_weight::Weight comes with pretty broad API and I don't know whether it would be convenient in the end to copy just the struct (copying the whole API seems to be an overkill); I'm leaving the decision to one of this repo maintainers @ascjones ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with a local copy of the type with a minimal API, it is likely to be highly stable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

storage_deposit_limit: Option<E::Balance>,
data: Vec<u8>,
}

/// A raw call to `pallet-contracts`'s `call`.
#[derive(Debug, scale::Encode, scale::Decode)]
#[derive(Debug, scale::Decode, scale::Encode, scale_encode::EncodeAsType)]
#[encode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_encode")]
pub struct Transfer<E: Environment, C: subxt::Config> {
dest: C::Address,
dest: subxt::utils::Static<C::Address>,
#[codec(compact)]
value: E::Balance,
}

#[derive(
Debug, Clone, Copy, scale::Encode, scale::Decode, PartialEq, Eq, serde::Serialize,
Debug,
Clone,
Copy,
PartialEq,
Eq,
serde::Serialize,
scale::Decode,
scale::Encode,
scale_encode::EncodeAsType,
)]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub enum Determinism {
/// The execution should be deterministic and hence no indeterministic instructions
/// are allowed.
Expand All @@ -90,10 +103,11 @@ pub enum Determinism {
}

/// A raw call to `pallet-contracts`'s `upload`.
#[derive(Debug, scale::Encode, scale::Decode)]
pub struct UploadCode<B> {
#[derive(Debug, scale::Encode, scale::Decode, scale_encode::EncodeAsType)]
#[encode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_encode")]
pub struct UploadCode<E: Environment> {
code: Vec<u8>,
storage_deposit_limit: Option<B>,
storage_deposit_limit: Option<E::Balance>,
determinism: Determinism,
}

Expand Down Expand Up @@ -183,14 +197,13 @@ where
dest: C::AccountId,
value: E::Balance,
) -> Result<(), subxt::Error> {
let call = subxt::tx::StaticTxPayload::new(
let call = subxt::tx::Payload::new(
"Balances",
"transfer",
Transfer::<E, C> {
dest: dest.into(),
dest: subxt::utils::Static(dest.into()),
value,
},
Default::default(),
)
.unvalidated();

Expand Down Expand Up @@ -249,7 +262,7 @@ where
signer: &Signer<C>,
) -> ExtrinsicEvents<C>
where
Call: tx::TxPayload,
Call: subxt::tx::TxPayload,
{
self.client
.tx()
Expand Down Expand Up @@ -292,18 +305,17 @@ where
salt: Vec<u8>,
signer: &Signer<C>,
) -> ExtrinsicEvents<C> {
let call = subxt::tx::StaticTxPayload::new(
let call = subxt::tx::Payload::new(
"Contracts",
"instantiate_with_code",
InstantiateWithCode::<E::Balance> {
InstantiateWithCode::<E> {
value,
gas_limit,
gas_limit: gas_limit.into(),
storage_deposit_limit,
code,
data,
salt,
},
Default::default(),
)
.unvalidated();

Expand Down Expand Up @@ -347,15 +359,14 @@ where
code: Vec<u8>,
storage_deposit_limit: Option<E::Balance>,
) -> ExtrinsicEvents<C> {
let call = subxt::tx::StaticTxPayload::new(
let call = subxt::tx::Payload::new(
"Contracts",
"upload_code",
UploadCode::<E::Balance> {
UploadCode::<E> {
code,
storage_deposit_limit,
determinism: Determinism::Deterministic,
},
Default::default(),
)
.unvalidated();

Expand Down Expand Up @@ -398,24 +409,23 @@ where
/// contains all events that are associated with this transaction.
pub async fn call(
&self,
contract: sp_runtime::MultiAddress<E::AccountId, ()>,
contract: MultiAddress<E::AccountId, ()>,
value: E::Balance,
gas_limit: Weight,
storage_deposit_limit: Option<E::Balance>,
data: Vec<u8>,
signer: &Signer<C>,
) -> ExtrinsicEvents<C> {
let call = subxt::tx::StaticTxPayload::new(
let call = subxt::tx::Payload::new(
"Contracts",
"call",
Call::<E, E::Balance> {
Call::<E> {
dest: contract,
value,
gas_limit,
gas_limit: gas_limit.into(),
storage_deposit_limit,
data,
},
Default::default(),
)
.unvalidated();

Expand Down
4 changes: 4 additions & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ secp256k1 = { version = "0.27.0", features = ["recovery", "global-context"], opt
#
# Sadly couldn't be marked as dev-dependency.
# Never use this crate outside the off-chain environment!
scale-decode = { version = "0.5.0", default-features = false, optional = true }
scale-encode = { version = "0.1.0", default-features = false, optional = true }
ascjones marked this conversation as resolved.
Show resolved Hide resolved
scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
Expand All @@ -61,6 +63,8 @@ std = [
"ink_storage_traits/std",
"ink_engine/std",
"scale/std",
"scale-decode",
"scale-encode",
"scale-info/std",
"secp256k1",
"num-traits/std",
Expand Down
Loading