Skip to content

Commit 9313bd4

Browse files
authored
Merge pull request #231 from YusukeShimizu/shared-serialization
Use the peerswaprpc proto file for shared serialization
2 parents 913b8ef + e658ee5 commit 9313bd4

File tree

6 files changed

+284
-253
lines changed

6 files changed

+284
-253
lines changed

clightning/clightning_commands.go

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (g *LiquidGetAddress) Call() (jrpc2.Result, error) {
5757
return nil, err
5858
}
5959
log.Infof("[Wallet] Getting lbtc address %s", res)
60-
return &GetAddressResponse{LiquidAddress: res}, nil
60+
return &peerswaprpc.GetAddressResponse{Address: res}, nil
6161
}
6262

6363
func (g *LiquidGetAddress) Description() string {
@@ -74,10 +74,6 @@ func (g *LiquidGetAddress) Get(client *ClightningClient) jrpc2.ServerMethod {
7474
}
7575
}
7676

77-
type GetAddressResponse struct {
78-
LiquidAddress string `json:"lbtc_address"`
79-
}
80-
8177
// GetBalance returns the liquid balance
8278
type LiquidGetBalance struct {
8379
cl *ClightningClient
@@ -105,8 +101,8 @@ func (g *LiquidGetBalance) Call() (jrpc2.Result, error) {
105101
if err != nil {
106102
return nil, err
107103
}
108-
return &GetBalanceResponse{
109-
res,
104+
return &peerswaprpc.GetBalanceResponse{
105+
SatAmount: res,
110106
}, nil
111107
}
112108

@@ -124,10 +120,6 @@ func (g *LiquidGetBalance) Get(client *ClightningClient) jrpc2.ServerMethod {
124120
}
125121
}
126122

127-
type GetBalanceResponse struct {
128-
LiquidBalance uint64 `json:"lbtc_balance_sat"`
129-
}
130-
131123
// LiquidSendToAddress sends
132124
type LiquidSendToAddress struct {
133125
Address string `json:"address"`
@@ -170,11 +162,7 @@ func (s *LiquidSendToAddress) Call() (jrpc2.Result, error) {
170162
log.Infof("Error sending to address %v", err)
171163
return nil, err
172164
}
173-
return &SendToAddressResponse{TxId: res}, nil
174-
}
175-
176-
type SendToAddressResponse struct {
177-
TxId string `json:"txid"`
165+
return &peerswaprpc.SendToAddressResponse{TxId: res}, nil
178166
}
179167

180168
func (s *LiquidSendToAddress) Description() string {
@@ -580,7 +568,7 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
580568
return nil, err
581569
}
582570

583-
peerSwappers := []*PeerSwapPeer{}
571+
peerSwappers := []*peerswaprpc.PeerSwapPeer{}
584572
for _, peer := range peers {
585573
if p, ok := polls[peer.Id]; ok {
586574
swaps, err := l.cl.swaps.ListSwapsByPeer(peer.Id)
@@ -616,42 +604,66 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
616604
}
617605
}
618606

619-
peerSwapPeer := &PeerSwapPeer{
607+
peerSwapPeer := &peerswaprpc.PeerSwapPeer{
620608
NodeId: peer.Id,
621609
SwapsAllowed: p.PeerAllowed,
622610
SupportedAssets: p.Assets,
623-
AsSender: &SwapStats{
611+
AsSender: &peerswaprpc.SwapStats{
624612
SwapsOut: SenderSwapsOut,
625613
SwapsIn: SenderSwapsIn,
626614
SatsOut: SenderSatsOut,
627615
SatsIn: SenderSatsIn,
628616
},
629-
AsReceiver: &SwapStats{
617+
AsReceiver: &peerswaprpc.SwapStats{
630618
SwapsOut: ReceiverSwapsOut,
631619
SwapsIn: ReceiverSwapsIn,
632620
SatsOut: ReceiverSatsOut,
633621
SatsIn: ReceiverSatsIn,
634622
},
635623
PaidFee: paidFees,
636-
PeerPremium: &Premium{
637-
BTCSwapInPremiumRatePPM: p.BTCSwapInPremiumRatePPM,
638-
BTCSwapOutPremiumRatePPM: p.BTCSwapOutPremiumRatePPM,
639-
LBTCSwapInPremiumRatePPM: p.LBTCSwapInPremiumRatePPM,
640-
LBTCSwapOutPremiumRatePPM: p.LBTCSwapOutPremiumRatePPM,
624+
PeerPremium: &peerswaprpc.PeerPremium{
625+
NodeId: peer.Id,
626+
Rates: []*peerswaprpc.PremiumRate{
627+
{
628+
Asset: peerswaprpc.AssetType_BTC,
629+
Operation: peerswaprpc.OperationType_SWAP_IN,
630+
PremiumRatePpm: p.BTCSwapInPremiumRatePPM,
631+
},
632+
{
633+
Asset: peerswaprpc.AssetType_BTC,
634+
Operation: peerswaprpc.OperationType_SWAP_OUT,
635+
PremiumRatePpm: p.BTCSwapOutPremiumRatePPM,
636+
},
637+
{
638+
Asset: peerswaprpc.AssetType_LBTC,
639+
Operation: peerswaprpc.OperationType_SWAP_IN,
640+
PremiumRatePpm: p.LBTCSwapInPremiumRatePPM,
641+
},
642+
{
643+
Asset: peerswaprpc.AssetType_LBTC,
644+
Operation: peerswaprpc.OperationType_SWAP_OUT,
645+
PremiumRatePpm: p.LBTCSwapOutPremiumRatePPM,
646+
},
647+
},
641648
},
642649
}
643650
channels, err := l.cl.glightning.ListChannelsBySource(peer.Id)
644651
if err != nil {
645652
return nil, err
646653
}
647-
peerSwapPeerChannels := []*PeerSwapPeerChannel{}
654+
peerSwapPeerChannels := []*peerswaprpc.PeerSwapPeerChannel{}
648655
for _, channel := range channels {
649656
if c, ok := fundingChannels[channel.ShortChannelId]; ok {
650-
peerSwapPeerChannels = append(peerSwapPeerChannels, &PeerSwapPeerChannel{
651-
ChannelId: c.ShortChannelId,
652-
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
653-
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
654-
State: c.State,
657+
scid, err := peerswaprpc.NewScidFromString(c.ShortChannelId)
658+
if err != nil {
659+
return nil, err
660+
}
661+
peerSwapPeerChannels = append(peerSwapPeerChannels, &peerswaprpc.PeerSwapPeerChannel{
662+
ChannelId: scid.ToUint64(),
663+
ShortChannelId: c.ShortChannelId,
664+
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
665+
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
666+
Active: channelActive(c.State),
655667
})
656668
}
657669
}
@@ -663,6 +675,10 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
663675
return peerSwappers, nil
664676
}
665677

678+
func channelActive(state string) bool {
679+
return state == "CHANNELD_NORMAL"
680+
}
681+
666682
type GetSwap struct {
667683
SwapId string `json:"swap_id"`
668684
cl *ClightningClient

0 commit comments

Comments
 (0)