From d9dac3a1905c1ebec4d67d893b7e22b22101ea30 Mon Sep 17 00:00:00 2001 From: Tadeo hepperle Date: Tue, 20 Feb 2024 18:49:13 +0100 Subject: [PATCH] adjust signed extension examples --- Cargo.lock | 1 + subxt/Cargo.toml | 2 +- subxt/examples/rpc_legacy.rs | 4 ++-- subxt/examples/setup_config_custom.rs | 15 ++++++++++----- subxt/examples/setup_config_signed_extension.rs | 8 ++------ subxt/src/config/default_extrinsic_params.rs | 6 ++++++ 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e82bf79b6..6fe5bf2575 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4531,6 +4531,7 @@ dependencies = [ "subxt-lightclient", "subxt-macro", "subxt-metadata", + "subxt-signer", "thiserror", "tokio", "tokio-stream", diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index e910eff407..9d0f2afd56 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -115,7 +115,7 @@ sp-core = { workspace = true } sp-keyring = { workspace = true } sp-runtime = { workspace = true } assert_matches = { workspace = true } -# subxt-signer = { path = "../signer" } // todo!() revert this, but rust analyzer blows up on cyclic dep +subxt-signer = { path = "../signer" } # Tracing subscriber is useful for light-client examples to ensure that # the `bootNodes` and chain spec are configured correctly. If all is fine, then # the light-client wlll emit INFO logs with diff --git a/subxt/examples/rpc_legacy.rs b/subxt/examples/rpc_legacy.rs index f0b831c3b6..25bde6d1e3 100644 --- a/subxt/examples/rpc_legacy.rs +++ b/subxt/examples/rpc_legacy.rs @@ -40,7 +40,7 @@ async fn main() -> Result<(), Box> { .await?; let current_header = rpc.chain_get_header(None).await?.unwrap(); - let ext_params = Params::new().mortal(¤t_header, 8).build(); + let ext_params = Params::new().mortal(¤t_header, 8).nonce(current_nonce).build(); let balance_transfer = polkadot::tx() .balances() @@ -48,7 +48,7 @@ async fn main() -> Result<(), Box> { let ext_hash = api .tx() - .create_signed_with_nonce(&balance_transfer, &alice, current_nonce, ext_params)? + .create_signed(&balance_transfer, &alice, ext_params).await? .submit() .await?; diff --git a/subxt/examples/setup_config_custom.rs b/subxt/examples/setup_config_custom.rs index 1c556971b9..fee6b01cd4 100644 --- a/subxt/examples/setup_config_custom.rs +++ b/subxt/examples/setup_config_custom.rs @@ -1,7 +1,7 @@ #![allow(missing_docs)] use codec::Encode; use subxt::client::OfflineClientT; -use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder, ExtrinsicParamsError}; +use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder, ExtrinsicParamsError, BaseParams, FromBaseParams}; use subxt_signer::sr25519::dev; #[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale")] @@ -51,18 +51,23 @@ impl CustomExtrinsicParamsBuilder { } } +impl FromBaseParams for CustomExtrinsicParamsBuilder{ + fn from_base_params(params: &BaseParams) -> Self { + Default::default() + } +} + // Describe how to fetch and then encode the params: impl ExtrinsicParams for CustomExtrinsicParams { type Params = CustomExtrinsicParamsBuilder; // Gather together all of the params we will need to encode: - fn new>( - _nonce: u64, - client: Client, + fn new( + base_params: &BaseParams, other_params: Self::Params, ) -> Result { Ok(Self { - genesis_hash: client.genesis_hash(), + genesis_hash: base_params.client.genesis_hash(), tip: other_params.tip, foo: other_params.foo, }) diff --git a/subxt/examples/setup_config_signed_extension.rs b/subxt/examples/setup_config_signed_extension.rs index e1f7e33acf..df77323cee 100644 --- a/subxt/examples/setup_config_signed_extension.rs +++ b/subxt/examples/setup_config_signed_extension.rs @@ -6,7 +6,7 @@ use subxt::client::OfflineClientT; use subxt::config::signed_extensions; use subxt::config::{ Config, DefaultExtrinsicParamsBuilder, ExtrinsicParams, ExtrinsicParamsEncoder, - ExtrinsicParamsError, + ExtrinsicParamsError, BaseParams }; use subxt_signer::sr25519::dev; @@ -60,11 +60,7 @@ impl signed_extensions::SignedExtension for CustomSignedExtension impl ExtrinsicParams for CustomSignedExtension { type Params = (); - fn new>( - _nonce: u64, - _client: Client, - _other_params: Self::Params, - ) -> Result { + fn new( base_params: &BaseParams, _params: Self::Params) -> Result { Ok(CustomSignedExtension) } } diff --git a/subxt/src/config/default_extrinsic_params.rs b/subxt/src/config/default_extrinsic_params.rs index 26531964ff..c39f1f2c6f 100644 --- a/subxt/src/config/default_extrinsic_params.rs +++ b/subxt/src/config/default_extrinsic_params.rs @@ -103,6 +103,12 @@ impl DefaultExtrinsicParamsBuilder { self } + /// Provide a specific nonce for the submitter of the extrinsic + pub fn nonce(mut self, nonce: u64) -> Self { + self.nonce = Some(nonce); + self + } + /// Provide a tip to the block author using the token denominated by the `asset_id` provided. This /// is not applicable on chains which don't use the `ChargeAssetTxPayment` signed extension; in this /// case, no tip will be given.