mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-08 21:52:41 +02:00
lnd: modify sendcoin rpc impl for select utxos
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
This commit is contained in:
committed by
yyforyongyu
parent
13bad2c20c
commit
b50a1e6360
41
rpcserver.go
41
rpcserver.go
@@ -1067,7 +1067,8 @@ func allowCORS(handler http.Handler, origins []string) http.Handler {
|
|||||||
// address to a specified output value to be sent to that address.
|
// address to a specified output value to be sent to that address.
|
||||||
func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
|
func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
|
||||||
feeRate chainfee.SatPerKWeight, minConfs int32, label string,
|
feeRate chainfee.SatPerKWeight, minConfs int32, label string,
|
||||||
strategy wallet.CoinSelectionStrategy) (*chainhash.Hash, error) {
|
strategy wallet.CoinSelectionStrategy,
|
||||||
|
selectedUtxos fn.Set[wire.OutPoint]) (*chainhash.Hash, error) {
|
||||||
|
|
||||||
outputs, err := addrPairsToOutputs(paymentMap, r.cfg.ActiveNetParams.Params)
|
outputs, err := addrPairsToOutputs(paymentMap, r.cfg.ActiveNetParams.Params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1077,7 +1078,7 @@ func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
|
|||||||
// We first do a dry run, to sanity check we won't spend our wallet
|
// We first do a dry run, to sanity check we won't spend our wallet
|
||||||
// balance below the reserved amount.
|
// balance below the reserved amount.
|
||||||
authoredTx, err := r.server.cc.Wallet.CreateSimpleTx(
|
authoredTx, err := r.server.cc.Wallet.CreateSimpleTx(
|
||||||
nil, outputs, feeRate, minConfs, strategy, true,
|
selectedUtxos, outputs, feeRate, minConfs, strategy, true,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1098,7 +1099,7 @@ func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
|
|||||||
// If that checks out, we're fairly confident that creating sending to
|
// If that checks out, we're fairly confident that creating sending to
|
||||||
// these outputs will keep the wallet balance above the reserve.
|
// these outputs will keep the wallet balance above the reserve.
|
||||||
tx, err := r.server.cc.Wallet.SendOutputs(
|
tx, err := r.server.cc.Wallet.SendOutputs(
|
||||||
nil, outputs, feeRate, minConfs, label, strategy,
|
selectedUtxos, outputs, feeRate, minConfs, label, strategy,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1290,9 +1291,9 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
rpcsLog.Infof("[sendcoins] addr=%v, amt=%v, sat/kw=%v, min_confs=%v, "+
|
rpcsLog.Infof("[sendcoins] addr=%v, amt=%v, sat/kw=%v, min_confs=%v, "+
|
||||||
"send_all=%v",
|
"send_all=%v, select_outpoints=%v",
|
||||||
in.Addr, btcutil.Amount(in.Amount), int64(feePerKw), minConfs,
|
in.Addr, btcutil.Amount(in.Amount), int64(feePerKw), minConfs,
|
||||||
in.SendAll)
|
in.SendAll, len(in.Outpoints))
|
||||||
|
|
||||||
// Decode the address receiving the coins, we need to check whether the
|
// Decode the address receiving the coins, we need to check whether the
|
||||||
// address is valid for this network.
|
// address is valid for this network.
|
||||||
@@ -1337,6 +1338,22 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
|||||||
wallet := r.server.cc.Wallet
|
wallet := r.server.cc.Wallet
|
||||||
maxFeeRate := r.cfg.Sweeper.MaxFeeRate.FeePerKWeight()
|
maxFeeRate := r.cfg.Sweeper.MaxFeeRate.FeePerKWeight()
|
||||||
|
|
||||||
|
var selectOutpoints fn.Set[wire.OutPoint]
|
||||||
|
if len(in.Outpoints) != 0 {
|
||||||
|
wireOutpoints, err := toWireOutpoints(in.Outpoints)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("can't create outpoints "+
|
||||||
|
"%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fn.HasDuplicates(wireOutpoints) {
|
||||||
|
return nil, fmt.Errorf("selected outpoints contain " +
|
||||||
|
"duplicate values")
|
||||||
|
}
|
||||||
|
|
||||||
|
selectOutpoints = fn.NewSet(wireOutpoints...)
|
||||||
|
}
|
||||||
|
|
||||||
// If the send all flag is active, then we'll attempt to sweep all the
|
// If the send all flag is active, then we'll attempt to sweep all the
|
||||||
// coins in the wallet in a single transaction (if possible),
|
// coins in the wallet in a single transaction (if possible),
|
||||||
// otherwise, we'll respect the amount, and attempt a regular 2-output
|
// otherwise, we'll respect the amount, and attempt a regular 2-output
|
||||||
@@ -1363,7 +1380,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
|||||||
sweepTxPkg, err := sweep.CraftSweepAllTx(
|
sweepTxPkg, err := sweep.CraftSweepAllTx(
|
||||||
feePerKw, maxFeeRate, uint32(bestHeight), nil,
|
feePerKw, maxFeeRate, uint32(bestHeight), nil,
|
||||||
targetAddr, wallet, wallet, wallet.WalletController,
|
targetAddr, wallet, wallet, wallet.WalletController,
|
||||||
r.server.cc.Signer, minConfs, nil,
|
r.server.cc.Signer, minConfs, selectOutpoints,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1417,7 +1434,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
|||||||
feePerKw, maxFeeRate, uint32(bestHeight),
|
feePerKw, maxFeeRate, uint32(bestHeight),
|
||||||
outputs, targetAddr, wallet, wallet,
|
outputs, targetAddr, wallet, wallet,
|
||||||
wallet.WalletController,
|
wallet.WalletController,
|
||||||
r.server.cc.Signer, minConfs, nil,
|
r.server.cc.Signer, minConfs, selectOutpoints,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1443,7 +1460,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rpcsLog.Debugf("Sweeping all coins from wallet to addr=%v, "+
|
rpcsLog.Debugf("Sweeping coins from wallet to addr=%v, "+
|
||||||
"with tx=%v", in.Addr, spew.Sdump(sweepTxPkg.SweepTx))
|
"with tx=%v", in.Addr, spew.Sdump(sweepTxPkg.SweepTx))
|
||||||
|
|
||||||
// As our sweep transaction was created, successfully, we'll
|
// As our sweep transaction was created, successfully, we'll
|
||||||
@@ -1468,8 +1485,8 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
|||||||
paymentMap := map[string]int64{targetAddr.String(): in.Amount}
|
paymentMap := map[string]int64{targetAddr.String(): in.Amount}
|
||||||
err := wallet.WithCoinSelectLock(func() error {
|
err := wallet.WithCoinSelectLock(func() error {
|
||||||
newTXID, err := r.sendCoinsOnChain(
|
newTXID, err := r.sendCoinsOnChain(
|
||||||
paymentMap, feePerKw, minConfs,
|
paymentMap, feePerKw, minConfs, label,
|
||||||
label, coinSelectionStrategy,
|
coinSelectionStrategy, selectOutpoints,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -1540,8 +1557,8 @@ func (r *rpcServer) SendMany(ctx context.Context,
|
|||||||
wallet := r.server.cc.Wallet
|
wallet := r.server.cc.Wallet
|
||||||
err = wallet.WithCoinSelectLock(func() error {
|
err = wallet.WithCoinSelectLock(func() error {
|
||||||
sendManyTXID, err := r.sendCoinsOnChain(
|
sendManyTXID, err := r.sendCoinsOnChain(
|
||||||
in.AddrToAmount, feePerKw, minConfs,
|
in.AddrToAmount, feePerKw, minConfs, label,
|
||||||
label, coinSelectionStrategy,
|
coinSelectionStrategy, nil,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user