Skip to content

Commit

Permalink
Add TransactionParams trait (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmauro authored May 30, 2023
1 parent 2295994 commit 3b29215
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions algonaut_algod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2018"
algonaut_crypto = { path = "../algonaut_crypto", version = "0.4.2" }
algonaut_encoding = { path = "../algonaut_encoding", version = "0.4.2" }
algonaut_model = { path = "../algonaut_model", version = "0.4.2" }
algonaut_transaction = { path = "../algonaut_transaction", version = "0.4.2" }
serde = "^1.0"
serde_derive = "^1.0"
serde_json = "^1.0"
Expand Down
19 changes: 19 additions & 0 deletions algonaut_algod/src/models/transaction_params_200_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

use algonaut_crypto::{deserialize_hash, HashDigest};
use algonaut_transaction::builder::TransactionParams;

/// TransactionParams200Response : TransactionParams contains the parameters that help a client construct a new transaction.
Expand Down Expand Up @@ -54,3 +55,21 @@ impl TransactionParams200Response {
}
}
}

impl TransactionParams for TransactionParams200Response {
fn last_round(&self) -> u64 {
self.last_round
}

fn min_fee(&self) -> u64 {
self.min_fee
}

fn genesis_hash(&self) -> HashDigest {
self.genesis_hash
}

fn genesis_id(&self) -> &String {
&self.genesis_id
}
}
1 change: 0 additions & 1 deletion algonaut_transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ repository = "https://github.com/manuelmauro/algonaut"
version = "0.4.2"

[dependencies]
algonaut_algod = { path = "../algonaut_algod", version = "0.4.2" }
algonaut_core = { path = "../algonaut_core", version = "0.4.2" }
algonaut_crypto = { path = "../algonaut_crypto", version = "0.4.2" }
algonaut_encoding = { path = "../algonaut_encoding", version = "0.4.2" }
Expand Down
22 changes: 14 additions & 8 deletions algonaut_transaction/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ use crate::{
TransactionType,
},
};
use algonaut_algod::models::TransactionParams200Response;
use algonaut_core::{Address, CompiledTeal, MicroAlgos, Round, VotePk, VrfPk};
use algonaut_crypto::HashDigest;

pub trait TransactionParams {
fn last_round(&self) -> u64;
fn min_fee(&self) -> u64;
fn genesis_hash(&self) -> HashDigest;
fn genesis_id(&self) -> &String;
}

/// A builder for [Transaction].
pub struct TxnBuilder {
fee: MicroAlgos,
Expand All @@ -29,26 +35,26 @@ impl TxnBuilder {
/// Convenience to initialize builder with suggested transaction params
///
/// The txn fee is estimated, based on params. To set the fee manually, use [with_fee](Self::with_fee) or [new](Self::new).
pub fn with(params: &TransactionParams200Response, txn_type: TransactionType) -> Self {
Self::with_fee(params, MicroAlgos(params.min_fee), txn_type)
pub fn with(params: &impl TransactionParams, txn_type: TransactionType) -> Self {
Self::with_fee(params, MicroAlgos(params.min_fee()), txn_type)
}

/// Convenience to initialize builder with suggested transaction params, and set the fee manually (ignoring the fee fields in params).
///
/// Useful e.g. in txns groups where one txn pays the fee for others.
pub fn with_fee(
params: &TransactionParams200Response,
params: &impl TransactionParams,
fee: MicroAlgos,
txn_type: TransactionType,
) -> Self {
Self::new(
fee,
Round(params.last_round),
Round(params.last_round + 1000),
params.genesis_hash,
Round(params.last_round()),
Round(params.last_round() + 1000),
params.genesis_hash(),
txn_type,
)
.genesis_id(params.genesis_id.clone())
.genesis_id(params.genesis_id().clone())
}

pub fn new(
Expand Down

0 comments on commit 3b29215

Please sign in to comment.