cmd/loop: add cli clock and hook

This commit is contained in:
Boris Nagaev 2026-02-04 16:30:10 -05:00
parent 3e9a99dd55
commit cdec0cacef
No known key found for this signature in database
3 changed files with 18 additions and 4 deletions

View file

@ -248,9 +248,9 @@ func loopOut(ctx context.Context, cmd *cli.Command) error {
// Set our maximum swap wait time. If a fast swap is requested we set
// it to now, otherwise to 30 minutes in the future.
fast := cmd.Bool("fast")
swapDeadline := time.Now()
swapDeadline := cliClock.Now()
if !fast {
swapDeadline = time.Now().Add(defaultSwapWaitTime)
swapDeadline = cliClock.Now().Add(defaultSwapWaitTime)
}
sweepConfTarget := int32(cmd.Uint64("conf_target"))

View file

@ -19,6 +19,7 @@ import (
"github.com/lightninglabs/loop/loopd"
"github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
@ -99,6 +100,9 @@ var (
instantOutCommand, listInstantOutsCommand, stopCommand,
printManCommand, printMarkdownCommand,
}
// cliClock provides the time source used by CLI commands.
cliClock clock.Clock = clock.NewDefaultClock()
)
const (
@ -264,6 +268,16 @@ func getClientWithConn(cmd *cli.Command) (looprpc.SwapClientClient,
return loopClient, conn, cleanup, nil
}
// hookClock overrides cliClock until the returned callback is called.
func hookClock(c clock.Clock) func() {
prev := cliClock
cliClock = c
return func() {
cliClock = prev
}
}
func getMaxRoutingFee(amt btcutil.Amount) btcutil.Amount {
return swap.CalcFee(amt, maxRoutingFeeBase, maxRoutingFeeRate)
}

View file

@ -206,9 +206,9 @@ func quoteOut(ctx context.Context, cmd *cli.Command) error {
defer cleanup()
fast := cmd.Bool("fast")
swapDeadline := time.Now()
swapDeadline := cliClock.Now()
if !fast {
swapDeadline = time.Now().Add(defaultSwapWaitTime)
swapDeadline = cliClock.Now().Add(defaultSwapWaitTime)
}
quoteReq := &looprpc.QuoteRequest{