mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-12-03 20:33:59 +01:00
Implement fee inclusive transactions (#657)
* Add amount_includes_fee option in TX building * Add --amount_includes_fee CLI option * Implement send 'max' amount Co-authored-by: cliik <cliik@example.com>
This commit is contained in:
@@ -508,6 +508,7 @@ where
|
||||
&mut *w,
|
||||
keychain_mask,
|
||||
args.amount,
|
||||
args.amount_includes_fee.unwrap_or(false),
|
||||
args.minimum_confirmations,
|
||||
args.max_outputs as usize,
|
||||
args.num_change_outputs as usize,
|
||||
@@ -543,6 +544,7 @@ where
|
||||
&parent_key_id,
|
||||
true,
|
||||
use_test_rng,
|
||||
args.amount_includes_fee.unwrap_or(false),
|
||||
)?
|
||||
};
|
||||
|
||||
@@ -696,6 +698,7 @@ where
|
||||
&parent_key_id,
|
||||
false,
|
||||
use_test_rng,
|
||||
false,
|
||||
)?;
|
||||
|
||||
let keychain = w.keychain(keychain_mask)?;
|
||||
@@ -823,6 +826,7 @@ where
|
||||
parent_key_id.clone(),
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
)?;
|
||||
|
||||
// Add inputs and outputs to original context
|
||||
|
||||
@@ -40,6 +40,8 @@ pub struct InitTxArgs {
|
||||
#[serde(with = "secp_ser::string_or_u64")]
|
||||
/// The amount to send, in nanogrins. (`1 G = 1_000_000_000nG`)
|
||||
pub amount: u64,
|
||||
/// Does the amount include the fee, or will fees be spent in addition to the amount?
|
||||
pub amount_includes_fee: Option<bool>,
|
||||
#[serde(with = "secp_ser::string_or_u64")]
|
||||
/// The minimum number of confirmations an output
|
||||
/// should have in order to be included in the transaction.
|
||||
@@ -105,6 +107,7 @@ impl Default for InitTxArgs {
|
||||
InitTxArgs {
|
||||
src_acct_name: None,
|
||||
amount: 0,
|
||||
amount_includes_fee: None,
|
||||
minimum_confirmations: 10,
|
||||
max_outputs: 500,
|
||||
num_change_outputs: 1,
|
||||
|
||||
@@ -51,6 +51,7 @@ pub fn build_send_tx<'a, T: ?Sized, C, K>(
|
||||
parent_key_id: Identifier,
|
||||
use_test_nonce: bool,
|
||||
is_initiator: bool,
|
||||
amount_includes_fee: bool,
|
||||
) -> Result<Context, Error>
|
||||
where
|
||||
T: WalletBackend<'a, C, K>,
|
||||
@@ -61,6 +62,7 @@ where
|
||||
wallet,
|
||||
keychain_mask,
|
||||
slate.amount,
|
||||
amount_includes_fee,
|
||||
current_height,
|
||||
minimum_confirmations,
|
||||
max_outputs,
|
||||
@@ -69,6 +71,14 @@ where
|
||||
&parent_key_id,
|
||||
false,
|
||||
)?;
|
||||
if amount_includes_fee {
|
||||
slate.amount = slate
|
||||
.amount
|
||||
.checked_sub(fee)
|
||||
.ok_or(ErrorKind::GenericError(
|
||||
format!("Transaction amount is too small to include fee").into(),
|
||||
))?;
|
||||
};
|
||||
|
||||
if fixed_fee.map(|f| fee != f).unwrap_or(false) {
|
||||
return Err(ErrorKind::Fee("The initially selected fee is not sufficient".into()).into());
|
||||
@@ -312,6 +322,7 @@ pub fn select_send_tx<'a, T: ?Sized, C, K, B>(
|
||||
wallet: &mut T,
|
||||
keychain_mask: Option<&SecretKey>,
|
||||
amount: u64,
|
||||
amount_includes_fee: bool,
|
||||
current_height: u64,
|
||||
minimum_confirmations: u64,
|
||||
max_outputs: usize,
|
||||
@@ -337,6 +348,7 @@ where
|
||||
let (coins, _total, amount, fee) = select_coins_and_fee(
|
||||
wallet,
|
||||
amount,
|
||||
amount_includes_fee,
|
||||
current_height,
|
||||
minimum_confirmations,
|
||||
max_outputs,
|
||||
@@ -363,6 +375,7 @@ where
|
||||
pub fn select_coins_and_fee<'a, T: ?Sized, C, K>(
|
||||
wallet: &mut T,
|
||||
amount: u64,
|
||||
amount_includes_fee: bool,
|
||||
current_height: u64,
|
||||
minimum_confirmations: u64,
|
||||
max_outputs: usize,
|
||||
@@ -401,7 +414,10 @@ where
|
||||
// First attempt to spend without change
|
||||
let mut fee = tx_fee(coins.len(), 1, 1);
|
||||
let mut total: u64 = coins.iter().map(|c| c.value).sum();
|
||||
let mut amount_with_fee = amount + fee;
|
||||
let mut amount_with_fee = match amount_includes_fee {
|
||||
true => amount,
|
||||
false => amount + fee,
|
||||
};
|
||||
|
||||
if total == 0 {
|
||||
return Err(ErrorKind::NotEnoughFunds {
|
||||
@@ -429,7 +445,10 @@ where
|
||||
// We need to add a change address or amount with fee is more than total
|
||||
if total != amount_with_fee {
|
||||
fee = tx_fee(coins.len(), num_outputs, 1);
|
||||
amount_with_fee = amount + fee;
|
||||
amount_with_fee = match amount_includes_fee {
|
||||
true => amount,
|
||||
false => amount + fee,
|
||||
};
|
||||
|
||||
// Here check if we have enough outputs for the amount including fee otherwise
|
||||
// look for other outputs and check again
|
||||
@@ -458,10 +477,21 @@ where
|
||||
.1;
|
||||
fee = tx_fee(coins.len(), num_outputs, 1);
|
||||
total = coins.iter().map(|c| c.value).sum();
|
||||
amount_with_fee = amount + fee;
|
||||
amount_with_fee = match amount_includes_fee {
|
||||
true => amount,
|
||||
false => amount + fee,
|
||||
};
|
||||
}
|
||||
}
|
||||
Ok((coins, total, amount, fee))
|
||||
// If original amount includes fee, the new amount should
|
||||
// be reduced, to accommodate the fee.
|
||||
let new_amount = match amount_includes_fee {
|
||||
true => amount.checked_sub(fee).ok_or(ErrorKind::GenericError(
|
||||
format!("Transaction amount is too small to include fee").into(),
|
||||
))?,
|
||||
false => amount,
|
||||
};
|
||||
Ok((coins, total, new_amount, fee))
|
||||
}
|
||||
|
||||
/// Selects inputs and change for a transaction
|
||||
|
||||
@@ -96,6 +96,7 @@ pub fn estimate_send_tx<'a, T: ?Sized, C, K>(
|
||||
wallet: &mut T,
|
||||
keychain_mask: Option<&SecretKey>,
|
||||
amount: u64,
|
||||
amount_includes_fee: bool,
|
||||
minimum_confirmations: u64,
|
||||
max_outputs: usize,
|
||||
num_change_outputs: usize,
|
||||
@@ -128,6 +129,7 @@ where
|
||||
let (_coins, total, _amount, fee) = selection::select_coins_and_fee(
|
||||
wallet,
|
||||
amount,
|
||||
amount_includes_fee,
|
||||
current_height,
|
||||
minimum_confirmations,
|
||||
max_outputs,
|
||||
@@ -151,6 +153,7 @@ pub fn add_inputs_to_slate<'a, T: ?Sized, C, K>(
|
||||
parent_key_id: &Identifier,
|
||||
is_initiator: bool,
|
||||
use_test_rng: bool,
|
||||
amount_includes_fee: bool,
|
||||
) -> Result<Context, Error>
|
||||
where
|
||||
T: WalletBackend<'a, C, K>,
|
||||
@@ -181,6 +184,7 @@ where
|
||||
parent_key_id.clone(),
|
||||
use_test_rng,
|
||||
is_initiator,
|
||||
amount_includes_fee,
|
||||
)?;
|
||||
|
||||
// Generate a kernel offset and subtract from our context's secret key. Store
|
||||
@@ -270,6 +274,7 @@ where
|
||||
let (_coins, _total, _amount, fee) = selection::select_coins_and_fee(
|
||||
wallet,
|
||||
init_tx_args.amount,
|
||||
init_tx_args.amount_includes_fee.unwrap_or(false),
|
||||
current_height,
|
||||
init_tx_args.minimum_confirmations,
|
||||
init_tx_args.max_outputs as usize,
|
||||
|
||||
Reference in New Issue
Block a user