2019-03-06 20:13:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/lightninglabs/loop"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// view prints all swaps currently in the database.
|
2019-03-07 10:37:28 +00:00
|
|
|
func view(config *config) error {
|
|
|
|
chainParams, err := swap.ChainParamsFromNetwork(config.Network)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-07 10:37:28 +00:00
|
|
|
lnd, err := getLnd(config.Network, config.Lnd)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lnd.Close()
|
|
|
|
|
2019-03-07 10:37:28 +00:00
|
|
|
swapClient, cleanup, err := getClient(
|
|
|
|
config.Network, config.SwapServer, config.Insecure, &lnd.LndServices,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
|
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 {
|
2019-04-11 07:06:59 +00:00
|
|
|
swaps, err := swapClient.Store.FetchLoopOutSwaps()
|
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 {
|
2019-03-07 04:32:24 +00:00
|
|
|
htlc, err := swap.NewHtlc(
|
2019-03-06 20:13:50 +00:00
|
|
|
s.Contract.CltvExpiry,
|
|
|
|
s.Contract.SenderKey,
|
|
|
|
s.Contract.ReceiverKey,
|
2019-04-04 10:20:45 +00:00
|
|
|
s.Hash, swap.HtlcP2WSH, 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)
|
2019-04-04 10:20:45 +00:00
|
|
|
fmt.Printf(" Htlc address: %v\n", htlc.Address)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
unchargeChannel := "any"
|
|
|
|
if s.Contract.UnchargeChannel != nil {
|
|
|
|
unchargeChannel = strconv.FormatUint(
|
|
|
|
*s.Contract.UnchargeChannel, 10,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
fmt.Printf(" Uncharge channel: %v\n", unchargeChannel)
|
|
|
|
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 {
|
|
|
|
fmt.Printf(" Update %v, Time %v, State: %v\n",
|
|
|
|
i, e.Time, e.State,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-04 10:20:45 +00:00
|
|
|
|
|
|
|
func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
|
2019-04-11 07:06:59 +00:00
|
|
|
swaps, err := swapClient.Store.FetchLoopInSwaps()
|
2019-04-04 10:20:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range swaps {
|
|
|
|
htlc, err := swap.NewHtlc(
|
|
|
|
s.Contract.CltvExpiry,
|
|
|
|
s.Contract.SenderKey,
|
|
|
|
s.Contract.ReceiverKey,
|
|
|
|
s.Hash, swap.HtlcNP2WSH, chainParams,
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
fmt.Printf(" Htlc address: %v\n", htlc.Address)
|
|
|
|
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
|
|
|
|
}
|