diff --git a/interface.go b/interface.go index d2e2cd1..94d709b 100644 --- a/interface.go +++ b/interface.go @@ -66,6 +66,10 @@ type OutRequest struct { // LoopOutChannel optionally specifies the short channel id of the // channel to loop out. LoopOutChannel *uint64 + + // SwapPublicationDeadline can be set by the client to allow the server + // delaying publication of the swap HTLC to save on chain fees. + SwapPublicationDeadline time.Time } // Out contains the full details of a loop out request. This includes things diff --git a/loopout.go b/loopout.go index 635f6d1..82b0f92 100644 --- a/loopout.go +++ b/loopout.go @@ -79,29 +79,36 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, // the server revocation key and the swap and prepay invoices. log.Infof("Initiating swap request at height %v", currentHeight) - swapResp, err := cfg.server.NewLoopOutSwap(globalCtx, swapHash, - request.Amount, receiverKey, + // The swap deadline will be given to the server for it to use as the + // latest swap publication time. + swapResp, err := cfg.server.NewLoopOutSwap( + globalCtx, swapHash, request.Amount, receiverKey, + request.SwapPublicationDeadline, ) if err != nil { return nil, fmt.Errorf("cannot initiate swap: %v", err) } - err = validateLoopOutContract(cfg.lnd, currentHeight, request, swapHash, swapResp) + err = validateLoopOutContract( + cfg.lnd, currentHeight, request, swapHash, swapResp, + ) if err != nil { return nil, err } - // Instantiate a struct that contains all required data to start the swap. + // Instantiate a struct that contains all required data to start the + // swap. initiationTime := time.Now() contract := loopdb.LoopOutContract{ - SwapInvoice: swapResp.swapInvoice, - DestAddr: request.DestAddr, - MaxSwapRoutingFee: request.MaxSwapRoutingFee, - SweepConfTarget: request.SweepConfTarget, - UnchargeChannel: request.LoopOutChannel, - PrepayInvoice: swapResp.prepayInvoice, - MaxPrepayRoutingFee: request.MaxPrepayRoutingFee, + SwapInvoice: swapResp.swapInvoice, + DestAddr: request.DestAddr, + MaxSwapRoutingFee: request.MaxSwapRoutingFee, + SweepConfTarget: request.SweepConfTarget, + UnchargeChannel: request.LoopOutChannel, + PrepayInvoice: swapResp.prepayInvoice, + MaxPrepayRoutingFee: request.MaxPrepayRoutingFee, + SwapPublicationDeadline: request.SwapPublicationDeadline, SwapContract: loopdb.SwapContract{ InitiationHeight: currentHeight, InitiationTime: initiationTime, diff --git a/server_mock_test.go b/server_mock_test.go index 9afc7c4..5629904 100644 --- a/server_mock_test.go +++ b/server_mock_test.go @@ -53,7 +53,7 @@ func newServerMock() *serverMock { func (s *serverMock) NewLoopOutSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, - receiverKey [33]byte) ( + receiverKey [33]byte, _ time.Time) ( *newLoopOutResponse, error) { _, senderKey := test.CreateKey(100) diff --git a/swap_server_client.go b/swap_server_client.go index 527cc0d..fde9e4f 100644 --- a/swap_server_client.go +++ b/swap_server_client.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "fmt" + "time" "github.com/lightninglabs/loop/looprpc" "github.com/lightningnetwork/lnd/lntypes" @@ -31,7 +32,8 @@ type swapServerClient interface { NewLoopOutSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, - receiverKey [33]byte) ( + receiverKey [33]byte, + swapPublicationDeadline time.Time) ( *newLoopOutResponse, error) NewLoopInSwap(ctx context.Context, @@ -153,15 +155,17 @@ func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context, func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, - receiverKey [33]byte) (*newLoopOutResponse, error) { + receiverKey [33]byte, swapPublicationDeadline time.Time) ( + *newLoopOutResponse, error) { rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout) defer rpcCancel() swapResp, err := s.server.NewLoopOutSwap(rpcCtx, &looprpc.ServerLoopOutRequest{ - SwapHash: swapHash[:], - Amt: uint64(amount), - ReceiverKey: receiverKey[:], + SwapHash: swapHash[:], + Amt: uint64(amount), + ReceiverKey: receiverKey[:], + SwapPublicationDeadline: swapPublicationDeadline.Unix(), }, ) if err != nil {