Skip to content

Commit

Permalink
Allow tx payloads to be boxed (#1690)
Browse files Browse the repository at this point in the history
* Allow tx payloads to be boxed

* Add example of boxed payloads

* explicit box import

* box import at top

* cargo fmt
  • Loading branch information
jsdw authored Jul 29, 2024
1 parent 99fa8ee commit 83ef8a9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/src/tx/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::error::MetadataError;
use crate::metadata::Metadata;
use crate::Error;
use alloc::borrow::{Cow, ToOwned};
use alloc::boxed::Box;
use alloc::string::String;

use alloc::vec::Vec;
Expand Down Expand Up @@ -38,6 +39,32 @@ pub trait Payload {
}
}

macro_rules! boxed_payload {
($ty:path) => {
impl<T: Payload + ?Sized> Payload for $ty {
fn encode_call_data_to(
&self,
metadata: &Metadata,
out: &mut Vec<u8>,
) -> Result<(), Error> {
self.as_ref().encode_call_data_to(metadata, out)
}
fn encode_call_data(&self, metadata: &Metadata) -> Result<Vec<u8>, Error> {
self.as_ref().encode_call_data(metadata)
}
fn validation_details(&self) -> Option<ValidationDetails<'_>> {
self.as_ref().validation_details()
}
}
};
}

boxed_payload!(Box<T>);
#[cfg(feature = "std")]
boxed_payload!(std::sync::Arc<T>);
#[cfg(feature = "std")]
boxed_payload!(std::rc::Rc<T>);

/// Details required to validate the shape of a transaction payload against some metadata.
pub struct ValidationDetails<'a> {
/// The pallet name.
Expand Down
43 changes: 43 additions & 0 deletions subxt/examples/tx_boxed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![allow(missing_docs)]
use subxt::{OnlineClient, PolkadotConfig};
use subxt_signer::sr25519::dev;

#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
pub mod polkadot {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = OnlineClient::<PolkadotConfig>::new().await?;

// Prepare some extrinsics. These are boxed so that they can live alongside each other.
let txs = [dynamic_remark(), balance_transfer(), remark()];

for tx in txs {
let from = dev::alice();
api.tx()
.sign_and_submit_then_watch_default(&tx, &from)
.await?
.wait_for_finalized_success()
.await?;

println!("Submitted tx");
}

Ok(())
}

fn balance_transfer() -> Box<dyn subxt::tx::Payload> {
let dest = dev::bob().public_key().into();
Box::new(polkadot::tx().balances().transfer_allow_death(dest, 10_000))
}

fn remark() -> Box<dyn subxt::tx::Payload> {
Box::new(polkadot::tx().system().remark(vec![1, 2, 3, 4, 5]))
}

fn dynamic_remark() -> Box<dyn subxt::tx::Payload> {
use subxt::dynamic::{tx, Value};
let tx_payload = tx("System", "remark", vec![Value::from_bytes("Hello")]);

Box::new(tx_payload)
}
8 changes: 8 additions & 0 deletions subxt/src/book/usage/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@
//! This example doesn't wait for the transaction to be included in a block; it just submits it and
//! hopes for the best!
//!
//! ### Boxing transaction payloads
//!
//! Transaction payloads can be boxed so that they all share a common type and can be stored together.
//!
//! ```rust,ignore
#![doc = include_str!("../../../examples/tx_boxed.rs")]
//! ```
//!
//! ### Custom handling of transaction status updates
//!
//! If you'd like more control or visibility over exactly which status updates are being emitted for
Expand Down

0 comments on commit 83ef8a9

Please sign in to comment.