mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 09:08:18 +00:00
a2b8dde5e6
* Add Static type which defers to Encode/Decode and impls EncodeAsType/DecodeAsType * rename to static_type and impl Deref/Mut * Improve type substitution in codegen so that concrete types can be swapped in * A couple of comment tweaks and no need for a macro export * Extend type substitution logic to work recursively on destination type * cargo fmt * Fix a couple of comments * update ui test outpuot * Add docs and missing_docs lint * Add test for replacing multiple of Ident * Update codegen/src/error.rs Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com> * update copyright year and fix ui test * simplify another error --------- Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
86 lines
3.1 KiB
Rust
86 lines
3.1 KiB
Rust
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
|
|
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
|
|
// see LICENSE for license details.
|
|
|
|
//! To run this example, a local polkadot node should be running. Example verified against polkadot v0.9.28-9ffe6e9e3da.
|
|
//!
|
|
//! E.g.
|
|
//! ```bash
|
|
//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.28/polkadot" --output /usr/local/bin/polkadot --location
|
|
//! polkadot --dev --tmp
|
|
//! ```
|
|
|
|
// This example showcases working with dynamic values rather than those that are generated via the subxt proc macro.
|
|
|
|
use sp_keyring::AccountKeyring;
|
|
use subxt::{dynamic::Value, tx::PairSigner, OnlineClient, PolkadotConfig};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let api = OnlineClient::<PolkadotConfig>::new().await?;
|
|
|
|
// 1. Dynamic Balance Transfer (the dynamic equivalent to the balance_transfer example).
|
|
|
|
let signer = PairSigner::new(AccountKeyring::Alice.pair());
|
|
let dest = AccountKeyring::Bob.to_account_id();
|
|
|
|
// Create a transaction to submit:
|
|
let tx = subxt::dynamic::tx(
|
|
"Balances",
|
|
"transfer",
|
|
vec![
|
|
// A value representing a MultiAddress<AccountId32, _>. We want the "Id" variant, and that
|
|
// will ultimately contain the bytes for our destination address (there is a new type wrapping
|
|
// the address, but our encoding will happily ignore such things and do it's best to line up what
|
|
// we provide with what it needs).
|
|
Value::unnamed_variant("Id", [Value::from_bytes(&dest)]),
|
|
// A value representing the amount we'd like to transfer.
|
|
Value::u128(123_456_789_012_345),
|
|
],
|
|
);
|
|
|
|
// submit the transaction with default params:
|
|
let hash = api.tx().sign_and_submit_default(&tx, &signer).await?;
|
|
println!("Balance transfer extrinsic submitted: {hash}");
|
|
|
|
// 2. Dynamic constant access (the dynamic equivalent to the fetch_constants example).
|
|
|
|
let constant_address = subxt::dynamic::constant("Balances", "ExistentialDeposit");
|
|
let existential_deposit = api.constants().at(&constant_address)?.to_value()?;
|
|
println!("Existential Deposit: {existential_deposit}");
|
|
|
|
// 3. Dynamic storage access
|
|
|
|
let storage_address = subxt::dynamic::storage(
|
|
"System",
|
|
"Account",
|
|
vec![
|
|
// Something that encodes to an AccountId32 is what we need for the map key here:
|
|
Value::from_bytes(&dest),
|
|
],
|
|
);
|
|
let account = api
|
|
.storage()
|
|
.at(None)
|
|
.await?
|
|
.fetch_or_default(&storage_address)
|
|
.await?
|
|
.to_value()?;
|
|
println!("Bob's account details: {account}");
|
|
|
|
// 4. Dynamic storage iteration (the dynamic equivalent to the fetch_all_accounts example).
|
|
|
|
let storage_address = subxt::dynamic::storage_root("System", "Account");
|
|
let mut iter = api
|
|
.storage()
|
|
.at(None)
|
|
.await?
|
|
.iter(storage_address, 10)
|
|
.await?;
|
|
while let Some((key, account)) = iter.next().await? {
|
|
println!("{}: {}", hex::encode(key), account.to_value()?);
|
|
}
|
|
|
|
Ok(())
|
|
}
|