Skip to content

Commit

Permalink
Use hardcoded estimated tx size, improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ivnsch committed Apr 11, 2022
1 parent ca44147 commit 07329cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion algonaut_transaction/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl TxnBuilder {
min_fee: MicroAlgos,
) -> Result<Transaction, TransactionError> {
let mut txn = self.build_tx(MicroAlgos(0));
txn.fee = txn.estimate_fee(fee_per_byte, min_fee)?;
txn.fee = txn.estimate_basic_sig_fee(fee_per_byte, min_fee)?;
Ok(txn)
}

Expand Down
17 changes: 7 additions & 10 deletions algonaut_transaction/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::account::Account;
use crate::error::TransactionError;
use algonaut_core::CompiledTeal;
use algonaut_core::SignedLogic;
Expand Down Expand Up @@ -98,27 +97,25 @@ impl Transaction {
}

/// Estimates the size of the encoded transaction, used in calculating the fee.
// TODO Consider calculating this in a way that doesn't cause a `clone` to be necessary.
pub fn estimate_size(&self) -> Result<u64, TransactionError> {
let account = Account::generate();
let signed_transaction = account.sign_transaction(self.clone())?;
Ok(signed_transaction.to_msg_pack()?.len() as u64)
pub fn estimate_basic_sig_size(&self) -> Result<u64, TransactionError> {
// 75 is the number of bytes added to a txn after signing it
Ok(self.to_msg_pack()?.len() as u64 + 75)
}

pub fn estimate_fee(
pub fn estimate_basic_sig_fee(
&self,
fee_per_byte: MicroAlgos,
min_fee: MicroAlgos,
) -> Result<MicroAlgos, TransactionError> {
let total_fee = fee_per_byte * self.estimate_size()?;
let total_fee = fee_per_byte * self.estimate_basic_sig_size()?;
Ok(min_fee.max(total_fee))
}

pub fn estimate_fee_with_params(
pub fn estimate_basic_sig_fee_with_params(
&self,
params: &SuggestedTransactionParams,
) -> Result<MicroAlgos, TransactionError> {
self.estimate_fee(params.fee_per_byte, params.min_fee)
self.estimate_basic_sig_fee(params.fee_per_byte, params.min_fee)
}

/// The address of the account that signs and pays the fee.
Expand Down

0 comments on commit 07329cd

Please sign in to comment.