Skip to content

Commit

Permalink
[WIP] cosmrs::tx::Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-iqlusion committed Jul 22, 2022
1 parent 2ed647e commit cb1e374
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
3 changes: 1 addition & 2 deletions cosmrs/src/crypto/public_key.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Public keys
use crate::{prost_ext::MessageExt, proto, AccountId, Error, ErrorReport, Result};
use crate::{prost_ext::MessageExt, proto, AccountId, Error, ErrorReport, Result, Any};
use eyre::WrapErr;
use prost::Message;
use prost_types::Any;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use subtle_encoding::base64;
Expand Down
2 changes: 1 addition & 1 deletion cosmrs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum Error {
/// Expected type URL.
expected: &'static str,

/// Actual type URL found in the [`prost_types::Any`] message.
/// Actual type URL found in the [`crate::Any`] message.
found: String,
},

Expand Down
2 changes: 2 additions & 0 deletions cosmrs/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub mod mode_info;

mod auth_info;
mod body;
mod builder;
mod fee;
mod msg;
mod raw;
Expand All @@ -115,6 +116,7 @@ mod signer_info;
pub use self::{
auth_info::AuthInfo,
body::Body,
builder::Builder,
fee::Fee,
mode_info::ModeInfo,
msg::{Msg, MsgProto},
Expand Down
9 changes: 7 additions & 2 deletions cosmrs/src/tx/body.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Transaction bodies.
use crate::{prost_ext::MessageExt, proto, ErrorReport, Result};
use prost_types::Any;
use crate::{prost_ext::MessageExt, proto, ErrorReport, Result, Any};
use tendermint::block;

/// [`Body`] of a transaction that all signers sign over.
Expand Down Expand Up @@ -67,6 +66,12 @@ impl Body {
}
}

impl Default for Body {
fn default() -> Body {
Self::new([], "", 0u8)
}
}

impl From<Body> for proto::cosmos::tx::v1beta1::TxBody {
fn from(body: Body) -> proto::cosmos::tx::v1beta1::TxBody {
proto::cosmos::tx::v1beta1::TxBody {
Expand Down
70 changes: 70 additions & 0 deletions cosmrs/src/tx/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Transaction builder.
use super::Body;
use crate::Any;
use tendermint::block;

/// Transaction [`Body`] builder which simplifies incrementally assembling and
/// signing a transaction.
#[derive(Clone, Debug, Default)]
pub struct Builder {
/// Transaction body in-progress.
body: Body,
}

impl Builder {
/// Create a new transaction builder in the default state.
pub fn new() -> Self {
Self::default()
}

/// Add a message to the transaction.
pub fn msg(&mut self, msg: impl Into<Any>) -> &mut Self {
self.body.messages.push(msg.into());
self
}

/// Add multiple messages to the transaction.
pub fn msgs(&mut self, msgs: impl IntoIterator<Item = Any>) -> &mut Self {
self.body.messages.extend(msgs);
self
}

/// Set the transaction memo.
// TODO(tarcieri): error if the memo is already set?
pub fn memo(&mut self, memo: impl Into<String>) -> &mut Self {
self.body.memo = memo.into();
self
}

/// Set the timeout height.
// TODO(tarcieri): error if the timeout height is already set?
pub fn timeout_height(&mut self, height: impl Into<block::Height>) -> &mut Self {
self.body.timeout_height = height.into();
self
}

/// Add an extension option.
pub fn extension_option(&mut self, option: impl Into<Any>) -> &mut Self {
self.body.extension_options.push(option.into());
self
}

/// Add a non-critical extension option.
pub fn non_critical_extension_option(&mut self, option: impl Into<Any>) -> &mut Self {
self.body.extension_options.push(option.into());
self
}
}

impl From<Builder> for Body {
fn from(builder: Builder) -> Body {
builder.body
}
}

impl From<&Builder> for Body {
fn from(builder: &Builder) -> Body {
builder.body.clone()
}
}

0 comments on commit cb1e374

Please sign in to comment.