Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the peerswaprpc proto file for shared serialization #231

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 23 additions & 50 deletions clightning/clightning_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (g *LiquidGetAddress) Call() (jrpc2.Result, error) {
return nil, err
}
log.Infof("[Wallet] Getting lbtc address %s", res)
return &GetAddressResponse{LiquidAddress: res}, nil
return &peerswaprpc.GetAddressResponse{Address: res}, nil
}

func (g *LiquidGetAddress) Description() string {
Expand All @@ -70,10 +70,6 @@ func (g *LiquidGetAddress) Get(client *ClightningClient) jrpc2.ServerMethod {
}
}

type GetAddressResponse struct {
LiquidAddress string `json:"lbtc_address"`
}

// GetBalance returns the liquid balance
type LiquidGetBalance struct {
cl *ClightningClient
Expand Down Expand Up @@ -101,8 +97,8 @@ func (g *LiquidGetBalance) Call() (jrpc2.Result, error) {
if err != nil {
return nil, err
}
return &GetBalanceResponse{
res,
return &peerswaprpc.GetBalanceResponse{
SatAmount: res,
}, nil
}

Expand All @@ -120,10 +116,6 @@ func (g *LiquidGetBalance) Get(client *ClightningClient) jrpc2.ServerMethod {
}
}

type GetBalanceResponse struct {
LiquidBalance uint64 `json:"lbtc_balance_sat"`
}

// LiquidSendToAddress sends
type LiquidSendToAddress struct {
Address string `json:"address"`
Expand Down Expand Up @@ -166,11 +158,7 @@ func (s *LiquidSendToAddress) Call() (jrpc2.Result, error) {
log.Infof("Error sending to address %v", err)
return nil, err
}
return &SendToAddressResponse{TxId: res}, nil
}

type SendToAddressResponse struct {
TxId string `json:"txid"`
return &peerswaprpc.SendToAddressResponse{TxId: res}, nil
}

func (s *LiquidSendToAddress) Description() string {
Expand Down Expand Up @@ -596,7 +584,7 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
return nil, err
}

peerSwappers := []*PeerSwapPeer{}
peerSwappers := []*peerswaprpc.PeerSwapPeer{}
for _, peer := range peers {
if p, ok := polls[peer.Id]; ok {
swaps, err := l.cl.swaps.ListSwapsByPeer(peer.Id)
Expand Down Expand Up @@ -632,17 +620,17 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
}
}

peerSwapPeer := &PeerSwapPeer{
peerSwapPeer := &peerswaprpc.PeerSwapPeer{
NodeId: peer.Id,
SwapsAllowed: p.PeerAllowed,
SupportedAssets: p.Assets,
AsSender: &SwapStats{
AsSender: &peerswaprpc.SwapStats{
SwapsOut: SenderSwapsOut,
SwapsIn: SenderSwapsIn,
SatsOut: SenderSatsOut,
SatsIn: SenderSatsIn,
},
AsReceiver: &SwapStats{
AsReceiver: &peerswaprpc.SwapStats{
SwapsOut: ReceiverSwapsOut,
SwapsIn: ReceiverSwapsIn,
SatsOut: ReceiverSatsOut,
Expand All @@ -651,14 +639,19 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
PaidFee: paidFees,
}

peerSwapPeerChannels := []*PeerSwapPeerChannel{}
peerSwapPeerChannels := []*peerswaprpc.PeerSwapPeerChannel{}
for _, channel := range peer.Channels {
if c, ok := fundingChannels[channel.ShortChannelId]; ok {
peerSwapPeerChannels = append(peerSwapPeerChannels, &PeerSwapPeerChannel{
ChannelId: c.ShortChannelId,
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
State: c.State,
scid, err := peerswaprpc.NewScidFromString(c.ShortChannelId)
if err != nil {
return nil, err
}
peerSwapPeerChannels = append(peerSwapPeerChannels, &peerswaprpc.PeerSwapPeerChannel{
ChannelId: scid.ToUint64(),
ShortChannelId: c.ShortChannelId,
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
Active: channelActive(c.State),
})
}
}
Expand All @@ -670,6 +663,10 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
return peerSwappers, nil
}

func channelActive(state string) bool {
return state == "CHANNELD_NORMAL"
}

type GetSwap struct {
SwapId string `json:"swap_id"`
cl *ClightningClient
Expand Down Expand Up @@ -1115,30 +1112,6 @@ func (c ListConfig) LongDescription() string {
return c.Description()
}

type PeerSwapPeerChannel struct {
ChannelId string `json:"short_channel_id"`
LocalBalance uint64 `json:"local_balance"`
RemoteBalance uint64 `json:"remote_balance"`
State string `json:"state"`
}

type SwapStats struct {
SwapsOut uint64 `json:"total_swaps_out"`
SwapsIn uint64 `json:"total_swaps_in"`
SatsOut uint64 `json:"total_sats_swapped_out"`
SatsIn uint64 `json:"total_sats_swapped_in"`
}

type PeerSwapPeer struct {
NodeId string `json:"nodeid"`
SwapsAllowed bool `json:"swaps_allowed"`
SupportedAssets []string `json:"supported_assets"`
Channels []*PeerSwapPeerChannel `json:"channels"`
AsSender *SwapStats `json:"sent,omitempty"`
AsReceiver *SwapStats `json:"received,omitempty"`
PaidFee uint64 `json:"total_fee_paid,omitempty"`
}

// checkFeatures checks if a node runs the peerswap Plugin
func checkFeatures(features []byte, featureBit int64) bool {
featuresInt := big.NewInt(0)
Expand Down
Loading
Loading