2020-01-03 13:01:31 +00:00
|
|
|
package loopd
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-16 15:40:52 +00:00
|
|
|
"context"
|
2019-03-06 20:13:50 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-04-04 10:20:45 +00:00
|
|
|
"github.com/lightninglabs/loop"
|
2019-05-15 12:01:27 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2024-01-18 14:18:36 +00:00
|
|
|
"github.com/lightninglabs/loop/sweepbatcher"
|
2023-11-14 00:52:39 +00:00
|
|
|
"github.com/lightninglabs/loop/utils"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// view prints all swaps currently in the database.
|
2021-11-12 09:14:47 +00:00
|
|
|
func view(config *Config, lisCfg *ListenerCfg) error {
|
2020-06-17 20:25:57 +00:00
|
|
|
network := lndclient.Network(config.Network)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-06-17 20:25:57 +00:00
|
|
|
lnd, err := lisCfg.getLnd(network, config.Lnd)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lnd.Close()
|
|
|
|
|
2023-10-12 15:04:09 +00:00
|
|
|
chainParams, err := network.ChainParams()
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-18 14:18:36 +00:00
|
|
|
swapDb, baseDb, err := openDatabase(config, chainParams)
|
2023-10-12 15:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-19 03:41:37 +00:00
|
|
|
sweeperDb := sweepbatcher.NewSQLStore(
|
|
|
|
loopdb.NewTypedStore[sweepbatcher.Querier](baseDb),
|
|
|
|
chainParams,
|
|
|
|
)
|
2024-01-18 14:18:36 +00:00
|
|
|
|
|
|
|
swapClient, cleanup, err := getClient(
|
|
|
|
config, swapDb, sweeperDb, &lnd.LndServices,
|
|
|
|
)
|
2020-06-17 20:25:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-12 15:04:09 +00:00
|
|
|
defer cleanup()
|
2020-06-17 20:25:57 +00:00
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
if err := viewOut(swapClient, chainParams); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := viewIn(swapClient, chainParams); err != nil {
|
2019-03-06 20:13:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
|
2023-05-16 15:40:52 +00:00
|
|
|
swaps, err := swapClient.Store.FetchLoopOutSwaps(context.Background())
|
2019-04-04 10:20:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-07 10:37:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
for _, s := range swaps {
|
2023-09-28 11:16:01 +00:00
|
|
|
s := s
|
|
|
|
|
2023-11-14 00:52:39 +00:00
|
|
|
htlc, err := utils.GetHtlc(
|
2022-11-16 18:01:28 +00:00
|
|
|
s.Hash, &s.Contract.SwapContract, chainParams,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
fmt.Printf("OUT %v\n", s.Hash)
|
2019-03-06 20:13:50 +00:00
|
|
|
fmt.Printf(" Created: %v (height %v)\n",
|
|
|
|
s.Contract.InitiationTime, s.Contract.InitiationHeight,
|
|
|
|
)
|
|
|
|
fmt.Printf(" Preimage: %v\n", s.Contract.Preimage)
|
2022-11-16 18:01:28 +00:00
|
|
|
fmt.Printf(" Htlc address (%s): %v\n", htlc.OutputType,
|
|
|
|
htlc.Address)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-05-19 07:54:27 +00:00
|
|
|
fmt.Printf(" Uncharge channels: %v\n",
|
|
|
|
s.Contract.OutgoingChanSet)
|
2019-03-06 20:13:50 +00:00
|
|
|
fmt.Printf(" Dest: %v\n", s.Contract.DestAddr)
|
|
|
|
fmt.Printf(" Amt: %v, Expiry: %v\n",
|
|
|
|
s.Contract.AmountRequested, s.Contract.CltvExpiry,
|
|
|
|
)
|
|
|
|
for i, e := range s.Events {
|
2019-05-15 12:01:27 +00:00
|
|
|
fmt.Printf(" Update %v, Time %v, State: %v",
|
2019-03-06 20:13:50 +00:00
|
|
|
i, e.Time, e.State,
|
|
|
|
)
|
2019-05-15 12:01:27 +00:00
|
|
|
if e.State.Type() != loopdb.StateTypePending {
|
|
|
|
fmt.Printf(", Cost: server=%v, onchain=%v, "+
|
|
|
|
"offchain=%v",
|
|
|
|
e.Cost.Server,
|
|
|
|
e.Cost.Onchain,
|
|
|
|
e.Cost.Offchain,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-04 10:20:45 +00:00
|
|
|
|
|
|
|
func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
|
2023-05-16 15:40:52 +00:00
|
|
|
swaps, err := swapClient.Store.FetchLoopInSwaps(context.Background())
|
2019-04-04 10:20:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range swaps {
|
2023-09-28 11:16:01 +00:00
|
|
|
s := s
|
|
|
|
|
2023-11-14 00:52:39 +00:00
|
|
|
htlc, err := utils.GetHtlc(
|
2022-11-16 18:01:28 +00:00
|
|
|
s.Hash, &s.Contract.SwapContract, chainParams,
|
2019-04-04 10:20:45 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("IN %v\n", s.Hash)
|
|
|
|
fmt.Printf(" Created: %v (height %v)\n",
|
|
|
|
s.Contract.InitiationTime, s.Contract.InitiationHeight,
|
|
|
|
)
|
|
|
|
fmt.Printf(" Preimage: %v\n", s.Contract.Preimage)
|
2022-11-16 18:01:28 +00:00
|
|
|
fmt.Printf(" Htlc address (%s): %v\n", htlc.OutputType,
|
|
|
|
htlc.Address)
|
2019-04-04 10:20:45 +00:00
|
|
|
fmt.Printf(" Amt: %v, Expiry: %v\n",
|
|
|
|
s.Contract.AmountRequested, s.Contract.CltvExpiry,
|
|
|
|
)
|
|
|
|
for i, e := range s.Events {
|
|
|
|
fmt.Printf(" Update %v, Time %v, State: %v\n",
|
|
|
|
i, e.Time, e.State,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|