Skip to content

Commit abf0e42

Browse files
committed
staticaddr: remove hardcoded states
1 parent 356b673 commit abf0e42

File tree

5 files changed

+513
-1063
lines changed

5 files changed

+513
-1063
lines changed

cmd/loop/staticaddr.go

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -287,48 +287,9 @@ func listDeposits(ctx *cli.Context) error {
287287
}
288288
defer cleanup()
289289

290-
var filterState looprpc.DepositState
291-
switch ctx.String("filter") {
292-
case "":
293-
// If no filter is specified, we'll default to showing all.
294-
295-
case "deposited":
296-
filterState = looprpc.DepositState_DEPOSITED
297-
298-
case "withdrawing":
299-
filterState = looprpc.DepositState_WITHDRAWING
300-
301-
case "withdrawn":
302-
filterState = looprpc.DepositState_WITHDRAWN
303-
304-
case "looping_in":
305-
filterState = looprpc.DepositState_LOOPING_IN
306-
307-
case "looped_in":
308-
filterState = looprpc.DepositState_LOOPED_IN
309-
310-
case "publish_expired_deposit":
311-
filterState = looprpc.DepositState_PUBLISH_EXPIRED
312-
313-
case "sweep_htlc_timeout":
314-
filterState = looprpc.DepositState_SWEEP_HTLC_TIMEOUT
315-
316-
case "htlc_timeout_swept":
317-
filterState = looprpc.DepositState_HTLC_TIMEOUT_SWEPT
318-
319-
case "wait_for_expiry_sweep":
320-
filterState = looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
321-
322-
case "expired":
323-
filterState = looprpc.DepositState_EXPIRED
324-
325-
default:
326-
filterState = looprpc.DepositState_UNKNOWN_STATE
327-
}
328-
329290
resp, err := client.ListStaticAddressDeposits(
330291
ctxb, &looprpc.ListStaticAddressDepositsRequest{
331-
StateFilter: filterState,
292+
StateFilter: ctx.String("filter"),
332293
},
333294
)
334295
if err != nil {
@@ -474,7 +435,7 @@ func staticAddressLoopIn(ctx *cli.Context) error {
474435
// Get the amount we need to quote for.
475436
depositList, err := client.ListStaticAddressDeposits(
476437
ctxb, &looprpc.ListStaticAddressDepositsRequest{
477-
StateFilter: looprpc.DepositState_DEPOSITED,
438+
StateFilter: string(deposit.Deposited),
478439
},
479440
)
480441
if err != nil {

loopd/swapclient_server.go

Lines changed: 8 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
14671467
*looprpc.ListStaticAddressDepositsResponse, error) {
14681468

14691469
outpoints := req.Outpoints
1470-
if req.StateFilter != looprpc.DepositState_UNKNOWN_STATE &&
1470+
if req.StateFilter != "" &&
14711471
len(outpoints) > 0 {
14721472

14731473
return nil, fmt.Errorf("can either filter by state or " +
@@ -1498,13 +1498,7 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
14981498
}
14991499
} else {
15001500
f := func(d *deposit.Deposit) bool {
1501-
if req.StateFilter == looprpc.DepositState_UNKNOWN_STATE {
1502-
// Per default, we return deposits in all
1503-
// states.
1504-
return true
1505-
}
1506-
1507-
return d.IsInState(toServerState(req.StateFilter))
1501+
return d.IsInState(fsm.StateType(req.StateFilter))
15081502
}
15091503
filteredDeposits = filter(allDeposits, f)
15101504
}
@@ -1558,11 +1552,9 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
15581552
"%v", err)
15591553
}
15601554
swap := &looprpc.StaticAddressLoopInSwap{
1561-
SwapHash: swp.SwapHash[:],
1562-
DepositOutpoints: swp.DepositOutpoints,
1563-
State: toClientStaticAddressLoopInState(
1564-
swp.GetState(),
1565-
),
1555+
SwapHash: swp.SwapHash[:],
1556+
DepositOutpoints: swp.DepositOutpoints,
1557+
State: string(swp.GetState()),
15661558
SwapAmountSatoshis: int64(swp.TotalDepositAmount()),
15671559
PaymentRequestAmountSatoshis: int64(
15681560
swapPayReq.MilliSat.ToSatoshis(),
@@ -1719,10 +1711,9 @@ func filter(deposits []*deposit.Deposit, f filterFunc) []*looprpc.Deposit {
17191711
hash := d.Hash
17201712
outpoint := wire.NewOutPoint(&hash, d.Index).String()
17211713
deposit := &looprpc.Deposit{
1722-
Id: d.ID[:],
1723-
State: toClientDepositState(
1724-
d.GetState(),
1725-
),
1714+
Id: d.ID[:],
1715+
State: string(d.GetState()),
1716+
17261717
Outpoint: outpoint,
17271718
Value: int64(d.Value),
17281719
ConfirmationHeight: d.ConfirmationHeight,
@@ -1734,125 +1725,6 @@ func filter(deposits []*deposit.Deposit, f filterFunc) []*looprpc.Deposit {
17341725
return clientDeposits
17351726
}
17361727

1737-
func toClientDepositState(state fsm.StateType) looprpc.DepositState {
1738-
switch state {
1739-
case deposit.Deposited:
1740-
return looprpc.DepositState_DEPOSITED
1741-
1742-
case deposit.Withdrawing:
1743-
return looprpc.DepositState_WITHDRAWING
1744-
1745-
case deposit.Withdrawn:
1746-
return looprpc.DepositState_WITHDRAWN
1747-
1748-
case deposit.PublishExpirySweep:
1749-
return looprpc.DepositState_PUBLISH_EXPIRED
1750-
1751-
case deposit.LoopingIn:
1752-
return looprpc.DepositState_LOOPING_IN
1753-
1754-
case deposit.LoopedIn:
1755-
return looprpc.DepositState_LOOPED_IN
1756-
1757-
case deposit.SweepHtlcTimeout:
1758-
return looprpc.DepositState_SWEEP_HTLC_TIMEOUT
1759-
1760-
case deposit.HtlcTimeoutSwept:
1761-
return looprpc.DepositState_HTLC_TIMEOUT_SWEPT
1762-
1763-
case deposit.WaitForExpirySweep:
1764-
return looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
1765-
1766-
case deposit.Expired:
1767-
return looprpc.DepositState_EXPIRED
1768-
1769-
default:
1770-
return looprpc.DepositState_UNKNOWN_STATE
1771-
}
1772-
}
1773-
1774-
func toClientStaticAddressLoopInState(
1775-
state fsm.StateType) looprpc.StaticAddressLoopInSwapState {
1776-
1777-
switch state {
1778-
case loopin.InitHtlcTx:
1779-
return looprpc.StaticAddressLoopInSwapState_INIT_HTLC
1780-
1781-
case loopin.SignHtlcTx:
1782-
return looprpc.StaticAddressLoopInSwapState_SIGN_HTLC_TX
1783-
1784-
case loopin.MonitorInvoiceAndHtlcTx:
1785-
return looprpc.StaticAddressLoopInSwapState_MONITOR_INVOICE_HTLC_TX
1786-
1787-
case loopin.PaymentReceived:
1788-
return looprpc.StaticAddressLoopInSwapState_PAYMENT_RECEIVED
1789-
1790-
case loopin.SweepHtlcTimeout:
1791-
return looprpc.StaticAddressLoopInSwapState_SWEEP_STATIC_ADDRESS_HTLC_TIMEOUT
1792-
1793-
case loopin.MonitorHtlcTimeoutSweep:
1794-
return looprpc.StaticAddressLoopInSwapState_MONITOR_HTLC_TIMEOUT_SWEEP
1795-
1796-
case loopin.HtlcTimeoutSwept:
1797-
return looprpc.StaticAddressLoopInSwapState_HTLC_STATIC_ADDRESS_TIMEOUT_SWEPT
1798-
1799-
case loopin.FetchSignPushSweeplessSweepTx:
1800-
return looprpc.StaticAddressLoopInSwapState_FETCH_SIGN_PUSH_SWEEPLESS_SWEEP_TX
1801-
1802-
case loopin.Succeeded:
1803-
return looprpc.StaticAddressLoopInSwapState_SUCCEEDED
1804-
1805-
case loopin.SucceededSweeplessSigFailed:
1806-
return looprpc.StaticAddressLoopInSwapState_SUCCEEDED_SWEEPLESS_SIG_FAILED
1807-
1808-
case loopin.UnlockDeposits:
1809-
return looprpc.StaticAddressLoopInSwapState_UNLOCK_DEPOSITS
1810-
1811-
case loopin.Failed:
1812-
return looprpc.StaticAddressLoopInSwapState_FAILED_STATIC_ADDRESS_SWAP
1813-
1814-
default:
1815-
return looprpc.StaticAddressLoopInSwapState_UNKNOWN_STATIC_ADDRESS_SWAP_STATE
1816-
}
1817-
}
1818-
1819-
func toServerState(state looprpc.DepositState) fsm.StateType {
1820-
switch state {
1821-
case looprpc.DepositState_DEPOSITED:
1822-
return deposit.Deposited
1823-
1824-
case looprpc.DepositState_WITHDRAWING:
1825-
return deposit.Withdrawing
1826-
1827-
case looprpc.DepositState_WITHDRAWN:
1828-
return deposit.Withdrawn
1829-
1830-
case looprpc.DepositState_PUBLISH_EXPIRED:
1831-
return deposit.PublishExpirySweep
1832-
1833-
case looprpc.DepositState_LOOPING_IN:
1834-
return deposit.LoopingIn
1835-
1836-
case looprpc.DepositState_LOOPED_IN:
1837-
return deposit.LoopedIn
1838-
1839-
case looprpc.DepositState_SWEEP_HTLC_TIMEOUT:
1840-
return deposit.SweepHtlcTimeout
1841-
1842-
case looprpc.DepositState_HTLC_TIMEOUT_SWEPT:
1843-
return deposit.HtlcTimeoutSwept
1844-
1845-
case looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
1846-
return deposit.WaitForExpirySweep
1847-
1848-
case looprpc.DepositState_EXPIRED:
1849-
return deposit.Expired
1850-
1851-
default:
1852-
return fsm.EmptyState
1853-
}
1854-
}
1855-
18561728
func toServerOutpoints(outpoints []*looprpc.OutPoint) ([]wire.OutPoint,
18571729
error) {
18581730

0 commit comments

Comments
 (0)