Files
pezkuwi-subxt/subxt/examples/setup_config_custom.rs
T
Alexandru Vasile 14b71279ba cargo: Switch to workspace lints (#1299)
* subxt: Remove unstable lints that cause compile warnings

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

* cargo: Switch to workspace lints

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

* cargo: Fix codec package at root level

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

* cargo: Move profiles to the root level

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

* Fix lightclient and metadata crates

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

* Revert "cargo: Fix codec package at root level"

This reverts commit cdf9e1628d708a972673eb3a9e967b6896edbd73.

* Fix complexity clippy

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

* cargo: Remove lints to be replaced by `cargo machete`

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

* cargo: Remove unused dependencies (detected by machete)

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

* ci: Add machete step

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

* cargo: Bump rust version

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

* ci: Rename machete step

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

* ci: Rename cargo machete step

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

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
2023-12-04 13:07:23 +02:00

111 lines
3.6 KiB
Rust

#![allow(missing_docs)]
use codec::Encode;
use subxt::client::OfflineClientT;
use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder, ExtrinsicParamsError};
use subxt_signer::sr25519::dev;
#[subxt::subxt(
runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
derive_for_type(path = "xcm::v2::multilocation::MultiLocation", derive = "Clone"),
derive_for_type(path = "xcm::v2::multilocation::Junctions", derive = "Clone"),
derive_for_type(path = "xcm::v2::junction::Junction", derive = "Clone"),
derive_for_type(path = "xcm::v2::NetworkId", derive = "Clone"),
derive_for_type(path = "xcm::v2::BodyId", derive = "Clone"),
derive_for_type(path = "xcm::v2::BodyPart", derive = "Clone"),
derive_for_type(
path = "bounded_collections::weak_bounded_vec::WeakBoundedVec",
derive = "Clone"
)
)]
pub mod runtime {}
use runtime::runtime_types::xcm::v2::multilocation::MultiLocation;
// We don't need to construct this at runtime,
// so an empty enum is appropriate:
pub enum CustomConfig {}
impl Config for CustomConfig {
type Hash = subxt::utils::H256;
type AccountId = subxt::utils::AccountId32;
type Address = subxt::utils::MultiAddress<Self::AccountId, ()>;
type Signature = subxt::utils::MultiSignature;
type Hasher = subxt::config::substrate::BlakeTwo256;
type Header = subxt::config::substrate::SubstrateHeader<u32, Self::Hasher>;
type ExtrinsicParams = CustomExtrinsicParams<Self>;
type AssetId = MultiLocation;
}
// This represents some arbitrary (and nonsensical) custom parameters that
// will be attached to transaction extra and additional payloads:
pub struct CustomExtrinsicParams<T: Config> {
genesis_hash: T::Hash,
tip: u128,
foo: bool,
}
// We can provide a "pretty" interface to allow users to provide these:
#[derive(Default)]
pub struct CustomExtrinsicParamsBuilder {
tip: u128,
foo: bool,
}
impl CustomExtrinsicParamsBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn tip(mut self, value: u128) -> Self {
self.tip = value;
self
}
pub fn enable_foo(mut self) -> Self {
self.foo = true;
self
}
}
// Describe how to fetch and then encode the params:
impl<T: Config> ExtrinsicParams<T> for CustomExtrinsicParams<T> {
type OtherParams = CustomExtrinsicParamsBuilder;
// Gather together all of the params we will need to encode:
fn new<Client: OfflineClientT<T>>(
_nonce: u64,
client: Client,
other_params: Self::OtherParams,
) -> Result<Self, ExtrinsicParamsError> {
Ok(Self {
genesis_hash: client.genesis_hash(),
tip: other_params.tip,
foo: other_params.foo,
})
}
}
// Encode the relevant params when asked:
impl<T: Config> ExtrinsicParamsEncoder for CustomExtrinsicParams<T> {
fn encode_extra_to(&self, v: &mut Vec<u8>) {
(self.tip, self.foo).encode_to(v);
}
fn encode_additional_to(&self, v: &mut Vec<u8>) {
self.genesis_hash.encode_to(v)
}
}
#[tokio::main]
async fn main() {
// With the config defined, it can be handed to Subxt as follows:
let client = subxt::OnlineClient::<CustomConfig>::new().await.unwrap();
let tx_payload = runtime::tx().system().remark(b"Hello".to_vec());
// Build your custom "OtherParams":
let tx_config = CustomExtrinsicParamsBuilder::new().tip(1234).enable_foo();
// And provide them when submitting a transaction:
let _ = client
.tx()
.sign_and_submit_then_watch(&tx_payload, &dev::alice(), tx_config)
.await;
}