Module tx

Module tx 

Source
Expand description

Construct and sign transactions.

§Example

use subxt_signer::sr25519::dev;
use subxt_macro::subxt;
use subxt_core::config::{PolkadotConfig, HashFor};
use subxt_core::config::DefaultExtrinsicParamsBuilder as Params;
use subxt_core::tx;
use subxt_core::utils::H256;
use subxt_core::metadata;

// If we generate types without `subxt`, we need to point to `::subxt_core`:
#[subxt(
    crate = "::subxt_core",
    runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale",
)]
pub mod polkadot {}

// Gather some other information about the chain that we'll need to construct valid extrinsics:
let state = tx::ClientState::<PolkadotConfig> {
    metadata: {
        let metadata_bytes = include_bytes!("../../../artifacts/polkadot_metadata_small.scale");
        metadata::decode_from(&metadata_bytes[..]).unwrap()
    },
    genesis_hash: {
        let h = "91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3";
        let bytes = hex::decode(h).unwrap();
        H256::from_slice(&bytes)
    },
    runtime_version: tx::RuntimeVersion {
        spec_version: 9370,
        transaction_version: 20,
    }
};

// Now we can build a balance transfer extrinsic.
let dest = dev::bob().public_key().into();
let call = polkadot::tx().balances().transfer_allow_death(dest, 10_000);
let params = Params::new().tip(1_000).nonce(0).build();

// We can validate that this lines up with the given metadata:
tx::validate(&call, &state.metadata).unwrap();

// We can build a signed transaction:
let signed_call = tx::create_v4_signed(&call, &state, params)
    .unwrap()
    .sign(&dev::alice());

// And log it:
println!("Tx: 0x{}", hex::encode(signed_call.encoded()));

Re-exports§

pub use crate::client::ClientState;
pub use crate::client::RuntimeVersion;

Modules§

payload
This module contains the trait and types used to represent transactions that can be submitted.
signer
A library to submit extrinsics to a substrate node via RPC.

Structs§

PartialTransactionV4
A partially constructed V4 extrinsic, ready to be signed.
PartialTransactionV5
A partially constructed V5 general extrinsic, ready to be signed or emitted as-is.
Transaction
This represents a signed transaction that’s ready to be submitted. Use Transaction::encoded() or Transaction::into_encoded() to get the bytes for it, or Transaction::hash_with() to hash the transaction given an instance of Config::Hasher.

Enums§

TransactionVersion
The transaction versions supported by Subxt.

Functions§

call_data
Return the SCALE encoded bytes representing the call data of the transaction.
create_v4_signed
Construct a v4 extrinsic, ready to be signed.
create_v4_unsigned
Creates a V4 “unsigned” transaction without submitting it.
create_v5_bare
Creates a V5 “bare” transaction without submitting it.
create_v5_general
Construct a v5 “general” extrinsic, ready to be signed or emitted as is.
suggested_version
Returns the suggested transaction versions to build for a given chain, or an error if Subxt doesn’t support any version expected by the chain.
validate
Run the validation logic against some extrinsic you’d like to submit. Returns Ok(()) if the call is valid (or if it’s not possible to check since the call has no validation hash). Return an error if the call was not valid or something went wrong trying to validate it (ie the pallet or call in question do not exist at all).