Skip to content

Commit

Permalink
Support greedy addressees
Browse files Browse the repository at this point in the history
Setting the 'greedy' flag on an addressee means it will consume any
change instead of it going to a change output.
  • Loading branch information
jkauffman1 committed Dec 2, 2021
1 parent cec8139 commit 26427ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/ga_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,20 @@ namespace sdk {

// Add all outputs and compute the total amount of satoshi to be sent
amount required_total{ 0 };
uint32_t greedy_index = NO_CHANGE_INDEX;

if (num_addressees) {
int addressee_index = 0;
for (auto& addressee : *addressees_p) {
const auto addressee_asset_id = asset_id_from_json(net_params, addressee);
if (addressee_asset_id == asset_id) {
required_total += add_tx_addressee(session, net_params, result, tx, addressee);
reordered_addressees.push_back(addressee);
if (addressee.value("greedy", false)) {
greedy_index = addressee_index;
}
}
++addressee_index;
}
}

Expand Down Expand Up @@ -730,6 +736,14 @@ namespace sdk {

change = total - required_with_fee;

// If a 'greedy' addressee exists send change there
if (change > 0 && greedy_index != NO_CHANGE_INDEX) {
set_tx_output_value(net_params, tx, greedy_index, asset_id, change.value());
addressees_p->at(greedy_index)["satoshi"] = change.value();
required_total += change;
continue;
}

if ((!have_change_output && change < dust_threshold)
|| (have_change_output && change >= dust_threshold)) {
// We don't have a change output, and have only dust left over, or
Expand Down
3 changes: 2 additions & 1 deletion src/transaction_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ namespace sdk {

// Transactions with outputs below the dust threshold (except OP_RETURN)
// are not relayed by network nodes
if (!result.value("send_all", false) && satoshi.value() < session.get_dust_threshold()) {
if (!result.value("send_all", false) && !addressee.value("greedy", false)
&& satoshi.value() < session.get_dust_threshold()) {
set_tx_error(result, res::id_invalid_amount);
}

Expand Down

0 comments on commit 26427ca

Please sign in to comment.