core-lightning/common/wallet_tx.c
Mark Beckwith 7767b68ee9 Removed redundancies in withdraw and fundchannel.
No new functionality, just a continuation of my work toward completing #665.

I removed the common members of `struct withdrawal` and `struct fund_channel`
and placed them in a new `struct wallet_tx`.  Then it was fairly straightforward
to reimplement the existing code in terms of `wallet_tx`.

Since I made some structural changes I wanted to get this approved before I
go any farther.

Added 'all' to fundchannel help message.
2018-05-03 18:20:20 +02:00

49 lines
1.2 KiB
C

#include <common/wallet_tx.h>
#include <inttypes.h>
#include <lightningd/jsonrpc_errors.h>
#include <wallet/wallet.h>
void wtx_init(struct command *cmd, struct wallet_tx * wtx)
{
wtx->cmd = cmd;
wtx->amount = 0;
wtx->change_key_index = 0;
wtx->utxos = NULL;
wtx->all_funds = false;
}
bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
size_t out_len)
{
u64 fee_estimate;
if (tx->all_funds) {
tx->utxos = wallet_select_all(tx->cmd, tx->cmd->ld->wallet,
fee_rate_per_kw, out_len,
&tx->amount,
&fee_estimate);
if (!tx->utxos || tx->amount < 546) {
command_fail(tx->cmd, "Cannot afford fee %"PRIu64,
fee_estimate);
return false;
}
tx->change = 0;
} else {
tx->utxos = wallet_select_coins(tx->cmd, tx->cmd->ld->wallet,
tx->amount,
fee_rate_per_kw, out_len,
&fee_estimate, &tx->change);
if (!tx->utxos || tx->amount < 546) {
command_fail(tx->cmd,
"Cannot afford funding transaction");
return false;
}
if (tx->change < 546) {
tx->change = 0;
tx->change_key_index = 0;
} else {
tx->change_key_index = wallet_get_newindex(tx->cmd->ld);
}
}
return true;
}