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 Mar 1, 2022
1 parent 3a1156f commit d74f723
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -572,14 +572,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("is_greedy", false)) {
greedy_index = addressee_index;
}
}
++addressee_index;
}
}

Expand Down Expand Up @@ -745,6 +751,14 @@ namespace sdk {

change = total - required_with_fee;

if (change != 0 && greedy_index != NO_CHANGE_INDEX) {
// If a 'greedy' addressee exists send change there
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
5 changes: 4 additions & 1 deletion src/transaction_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ 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()) {
// This check only applies when the amount has been set explicitly, so send all
// and greedy outputs are ignored as the amount is calculated later
if (!result.value("send_all", false) && !addressee.value("is_greedy", false)
&& satoshi.value() < session.get_dust_threshold()) {
set_tx_error(result, res::id_invalid_amount);
}

Expand Down

0 comments on commit d74f723

Please sign in to comment.