From 1f211e5647224b4c44c7022997f631f7f98eb950 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Fri, 1 Mar 2024 14:35:05 +0100 Subject: [PATCH] swapclientserver: add listinstantouts --- loopd/perms/perms.go | 4 ++++ loopd/swapclient_server.go | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/loopd/perms/perms.go b/loopd/perms/perms.go index 07eafd8..138ed79 100644 --- a/loopd/perms/perms.go +++ b/loopd/perms/perms.go @@ -108,4 +108,8 @@ var RequiredPermissions = map[string][]bakery.Op{ Entity: "swap", Action: "read", }}, + "/looprpc.SwapClient/ListInstantOuts": {{ + Entity: "swap", + Action: "read", + }}, } diff --git a/loopd/swapclient_server.go b/loopd/swapclient_server.go index 40aeb58..f42e8d5 100644 --- a/loopd/swapclient_server.go +++ b/loopd/swapclient_server.go @@ -1231,6 +1231,47 @@ func (s *swapClientServer) InstantOutQuote(ctx context.Context, }, nil } +// ListInstantOuts returns a list of all currently known instant out swaps and +// their current status. +func (s *swapClientServer) ListInstantOuts(ctx context.Context, + _ *clientrpc.ListInstantOutsRequest) ( + *clientrpc.ListInstantOutsResponse, error) { + + instantOuts, err := s.instantOutManager.ListInstantOuts(ctx) + if err != nil { + return nil, err + } + + rpcSwaps := make([]*clientrpc.InstantOut, 0, len(instantOuts)) + for _, instantOut := range instantOuts { + rpcSwaps = append(rpcSwaps, rpcInstantOut(instantOut)) + } + + return &clientrpc.ListInstantOutsResponse{ + Swaps: rpcSwaps, + }, nil +} + +func rpcInstantOut(instantOut *instantout.InstantOut) *clientrpc.InstantOut { + var sweepTxId string + if instantOut.SweepTxHash != nil { + sweepTxId = instantOut.SweepTxHash.String() + } + + reservations := make([][]byte, len(instantOut.Reservations)) + for i, res := range instantOut.Reservations { + reservations[i] = res.ID[:] + } + + return &clientrpc.InstantOut{ + SwapHash: instantOut.SwapHash[:], + State: string(instantOut.State), + Amount: uint64(instantOut.Value), + SweepTxId: sweepTxId, + ReservationIds: reservations, + } +} + func rpcAutoloopReason(reason liquidity.Reason) (clientrpc.AutoReason, error) { switch reason { case liquidity.ReasonNone: