cosmrs/tx/
builder.rs

1//! Transaction builder.
2
3use super::Body;
4use crate::Any;
5use tendermint::block;
6
7/// Transaction [`Body`] builder which simplifies incrementally assembling and
8/// signing a transaction.
9#[derive(Clone, Debug, Default)]
10pub struct BodyBuilder {
11    /// Transaction body in-progress.
12    body: Body,
13}
14
15impl BodyBuilder {
16    /// Create a new transaction builder in the default state.
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Add a message to the transaction.
22    pub fn msg(&mut self, msg: impl Into<Any>) -> &mut Self {
23        self.body.messages.push(msg.into());
24        self
25    }
26
27    /// Add multiple messages to the transaction.
28    pub fn msgs(&mut self, msgs: impl IntoIterator<Item = Any>) -> &mut Self {
29        self.body.messages.extend(msgs);
30        self
31    }
32
33    /// Set the transaction memo.
34    pub fn memo(&mut self, memo: impl Into<String>) -> &mut Self {
35        self.body.memo = memo.into();
36        self
37    }
38
39    /// Set the timeout height.
40    pub fn timeout_height(&mut self, height: impl Into<block::Height>) -> &mut Self {
41        self.body.timeout_height = height.into();
42        self
43    }
44
45    /// Add an extension option.
46    pub fn extension_option(&mut self, option: impl Into<Any>) -> &mut Self {
47        self.body.extension_options.push(option.into());
48        self
49    }
50
51    /// Add a non-critical extension option.
52    pub fn non_critical_extension_option(&mut self, option: impl Into<Any>) -> &mut Self {
53        self.body.non_critical_extension_options.push(option.into());
54        self
55    }
56
57    /// Return the finished [`Body`].
58    pub fn finish(&self) -> Body {
59        self.into()
60    }
61}
62
63impl From<BodyBuilder> for Body {
64    fn from(builder: BodyBuilder) -> Body {
65        builder.body
66    }
67}
68
69impl From<&BodyBuilder> for Body {
70    fn from(builder: &BodyBuilder) -> Body {
71        builder.body.clone()
72    }
73}