From dc5b87fc3d54e8269ab1faa0d6aea90d3a8152f4 Mon Sep 17 00:00:00 2001 From: YusukeShimizu Date: Sat, 23 Dec 2023 14:09:02 +0900 Subject: [PATCH] test: integration tests for premium This commit is to modify and add integration tests for the addition of premium. --- peerswaprpc/server.go | 15 +- test/bitcoin_cln_test.go | 868 +++++++++++++++++++++++++++------------ test/bitcoin_lnd_test.go | 593 ++++++++++++++------------ test/liquid_cln_test.go | 438 ++++++++++++-------- test/liquid_lnd_test.go | 481 ++++++++++++---------- test/setup.go | 7 +- test/testcases.go | 100 +++-- test/wumbo_test.go | 2 +- 8 files changed, 1539 insertions(+), 965 deletions(-) diff --git a/peerswaprpc/server.go b/peerswaprpc/server.go index 2fd8fe89..022cfeed 100644 --- a/peerswaprpc/server.go +++ b/peerswaprpc/server.go @@ -144,7 +144,7 @@ func (p *PeerswapServer) SwapOut(ctx context.Context, request *SwapOutRequest) ( return nil, fmt.Errorf("peer is not connected") } - swapOut, err := p.swaps.SwapOut(peerId, request.Asset, shortId.String(), pk, request.SwapAmount) + swapOut, err := p.swaps.SwapOut(peerId, request.Asset, shortId.String(), pk, request.SwapAmount, request.GetPremiumLimit()) if err != nil { return nil, err } @@ -268,7 +268,7 @@ func (p *PeerswapServer) SwapIn(ctx context.Context, request *SwapInRequest) (*S return nil, fmt.Errorf("peer is not connected") } - swapIn, err := p.swaps.SwapIn(peerId, request.Asset, shortId.String(), pk, request.SwapAmount) + swapIn, err := p.swaps.SwapIn(peerId, request.Asset, shortId.String(), pk, request.SwapAmount, request.GetPremiumLimit()) if err != nil { return nil, err } @@ -376,10 +376,12 @@ func (p *PeerswapServer) ListPeers(ctx context.Context, request *ListPeersReques } peerSwapPeers = append(peerSwapPeers, &PeerSwapPeer{ - NodeId: v.PubKey, - SwapsAllowed: poll.PeerAllowed, - SupportedAssets: poll.Assets, - Channels: getPeerSwapChannels(v.PubKey, channelRes.Channels), + NodeId: v.PubKey, + SwapsAllowed: poll.PeerAllowed, + SwapInPremiumRatePpm: poll.SwapInPremiumRatePPM, + SwapOutPremiumRatePpm: poll.SwapOutPremiumRatePPM, + SupportedAssets: poll.Assets, + Channels: getPeerSwapChannels(v.PubKey, channelRes.Channels), AsSender: &SwapStats{ SwapsOut: SenderSwapsOut, SwapsIn: SenderSwapsIn, @@ -571,6 +573,7 @@ func PrettyprintFromServiceSwap(swap *swap.SwapStateMachine) *PrettyPrintSwap { ClaimTxId: swap.Data.ClaimTxId, CancelMessage: swap.Data.GetCancelMessage(), LndChanId: lnd_chan_id, + PremiumAmount: swap.Data.GetPremium(), } } diff --git a/test/bitcoin_cln_test.go b/test/bitcoin_cln_test.go index e1c162fc..b1508e27 100644 --- a/test/bitcoin_cln_test.go +++ b/test/bitcoin_cln_test.go @@ -9,6 +9,7 @@ import ( "github.com/elementsproject/peerswap/clightning" "github.com/elementsproject/peerswap/peerswaprpc" + "github.com/elementsproject/peerswap/policy" "github.com/elementsproject/peerswap/swap" "github.com/elementsproject/peerswap/testframework" "github.com/stretchr/testify/assert" @@ -59,21 +60,24 @@ func Test_OnlyOneActiveSwapPerChannelCln(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 5, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 5, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -86,7 +90,11 @@ func Test_OnlyOneActiveSwapPerChannelCln(t *testing.T) { go func(n int) { defer wg.Done() var response map[string]interface{} - err := lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err := lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) t.Logf("[%d] Response: %v", n, response) if err != nil { t.Logf("[%d] Err: %s", n, err.Error()) @@ -148,29 +156,35 @@ func Test_ClnCln_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" // Do swap. go func() { var response map[string]interface{} - lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) - + lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -214,28 +228,36 @@ func Test_ClnCln_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" // Do swap. go func() { var response map[string]interface{} - lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err := lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) + require.NoError(err) }() coopClaimTest(t, params) @@ -280,28 +302,35 @@ func Test_ClnCln_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" // Do swap. go func() { var response map[string]interface{} - lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) @@ -352,21 +381,24 @@ func Test_ClnCln_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -374,7 +406,11 @@ func Test_ClnCln_Bitcoin_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -418,21 +454,24 @@ func Test_ClnCln_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -440,7 +479,11 @@ func Test_ClnCln_Bitcoin_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) }) @@ -484,21 +527,24 @@ func Test_ClnCln_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -506,7 +552,11 @@ func Test_ClnCln_Bitcoin_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) }) @@ -559,21 +609,24 @@ func Test_ClnLnd_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -581,7 +634,11 @@ func Test_ClnLnd_Bitcoin_SwapIn(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[1].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -628,21 +685,24 @@ func Test_ClnLnd_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -650,7 +710,11 @@ func Test_ClnLnd_Bitcoin_SwapIn(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[1].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) }) @@ -697,21 +761,24 @@ func Test_ClnLnd_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -719,11 +786,14 @@ func Test_ClnLnd_Bitcoin_SwapIn(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[1].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) }) - } func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { @@ -773,21 +843,24 @@ func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -795,7 +868,11 @@ func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -842,21 +919,24 @@ func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -864,7 +944,11 @@ func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) }) @@ -911,21 +995,24 @@ func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -933,7 +1020,11 @@ func Test_ClnLnd_Bitcoin_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].(*testframework.CLightningNode).Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) }) @@ -982,34 +1073,41 @@ func Test_ClnCln_ExcessiveAmount(t *testing.T) { } params := &testParams{ - swapAmt: 2 * channelBalances[0], - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: 2 * channelBalances[0], + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" // Swap out should fail as the swap_amt is to high. var response map[string]interface{} - err := lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err := lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) assert.Error(t, err) // Swap in should fail as the swap_amt is to high. - err = lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err = lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset, PremiumLimit: params.premiumLimit}, &response) assert.Error(t, err) }) - t.Run("swapout", func(t *testing.T) { + t.Run("maxhtlc_swapout", func(t *testing.T) { t.Parallel() require := require.New(t) @@ -1049,21 +1147,24 @@ func Test_ClnCln_ExcessiveAmount(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -1071,10 +1172,14 @@ func Test_ClnCln_ExcessiveAmount(t *testing.T) { assert.NoError(t, err) // Swap out should fail as the swap_amt is to high. var response map[string]interface{} - err = lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err = lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) assert.Error(t, err) }) - t.Run("swapin", func(t *testing.T) { + t.Run("maxhtlc_swapin", func(t *testing.T) { t.Parallel() require := require.New(t) @@ -1114,21 +1219,24 @@ func Test_ClnCln_ExcessiveAmount(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -1136,7 +1244,243 @@ func Test_ClnCln_ExcessiveAmount(t *testing.T) { assert.NoError(t, err) // Swap in should fail as the swap_amt is to high. var response map[string]interface{} - err = lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err = lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) + assert.Error(t, err) + }) + +} + +func Test_Cln_Premium(t *testing.T) { + IsIntegrationTest(t) + t.Parallel() + + t.Run("negative_swapin", func(t *testing.T) { + t.Parallel() + require := require.New(t) + + bitcoind, lightningds, scid := clnclnSetupWithConfig(t, uint64(math.Pow10(9)), 0, []string{ + "--dev-bitcoind-poll=1", + "--dev-fast-gossip", + "--large-channels", + }, + []byte("accept_all_peers=1\nswap_in_premium_rate_ppm=-10000\n"), + ) + defer func() { + if t.Failed() { + filter := os.Getenv("PEERSWAP_TEST_FILTER") + pprintFail( + tailableProcess{ + p: bitcoind.DaemonProcess, + lines: defaultLines, + }, + tailableProcess{ + p: lightningds[0].DaemonProcess, + filter: filter, + lines: defaultLines, + }, + tailableProcess{ + p: lightningds[1].DaemonProcess, + filter: filter, + lines: defaultLines, + }, + ) + } + }() + + var channelBalances []uint64 + var walletBalances []uint64 + for _, lightningd := range lightningds { + b, err := lightningd.GetBtcBalanceSat() + require.NoError(err) + walletBalances = append(walletBalances, b) + + b, err = lightningd.GetChannelBalanceSat(scid) + require.NoError(err) + channelBalances = append(channelBalances, b) + } + + params := &testParams{ + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: -10000, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, + } + asset := "btc" + + var response map[string]interface{} + err := lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) + assert.NoError(t, err) + + preimageClaimTest(t, params) + }) + + t.Run("negative_swapout", func(t *testing.T) { + t.Parallel() + require := require.New(t) + + bitcoind, lightningds, scid := clnclnSetupWithConfig(t, uint64(math.Pow10(9)), 0, []string{ + "--dev-bitcoind-poll=1", + "--dev-fast-gossip", + "--large-channels", + }, + []byte("accept_all_peers=1\nswap_out_premium_rate_ppm=-10000\n"), + ) + defer func() { + if t.Failed() { + filter := os.Getenv("PEERSWAP_TEST_FILTER") + pprintFail( + tailableProcess{ + p: bitcoind.DaemonProcess, + lines: defaultLines, + }, + tailableProcess{ + p: lightningds[0].DaemonProcess, + filter: filter, + lines: defaultLines, + }, + tailableProcess{ + p: lightningds[1].DaemonProcess, + filter: filter, + lines: defaultLines, + }, + ) + } + }() + + var channelBalances []uint64 + var walletBalances []uint64 + for _, lightningd := range lightningds { + b, err := lightningd.GetBtcBalanceSat() + require.NoError(err) + walletBalances = append(walletBalances, b) + + b, err = lightningd.GetChannelBalanceSat(scid) + require.NoError(err) + channelBalances = append(channelBalances, b) + } + + params := &testParams{ + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: -10000, + } + asset := "btc" + + // Swap out should fail as the premium is to high. + var response map[string]interface{} + err := lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) + assert.Error(t, err) + }) + + t.Run("exceed_limit", func(t *testing.T) { + t.Parallel() + require := require.New(t) + + bitcoind, lightningds, scid := clnclnSetup(t, uint64(math.Pow10(9))) + defer func() { + if t.Failed() { + filter := os.Getenv("PEERSWAP_TEST_FILTER") + pprintFail( + tailableProcess{ + p: bitcoind.DaemonProcess, + lines: defaultLines, + }, + tailableProcess{ + p: lightningds[0].DaemonProcess, + filter: filter, + lines: defaultLines, + }, + tailableProcess{ + p: lightningds[1].DaemonProcess, + filter: filter, + lines: defaultLines, + }, + ) + } + }() + + var channelBalances []uint64 + var walletBalances []uint64 + for _, lightningd := range lightningds { + b, err := lightningd.GetBtcBalanceSat() + require.NoError(err) + walletBalances = append(walletBalances, b) + + b, err = lightningd.GetChannelBalanceSat(scid) + require.NoError(err) + channelBalances = append(channelBalances, b) + } + + params := &testParams{ + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: 1, + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, + } + asset := "btc" + + // Swap in should fail as the premium is to high. + var response map[string]interface{} + err := lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) assert.Error(t, err) }) @@ -1157,7 +1501,7 @@ func Test_ClnCln_StuckChannels(t *testing.T) { "--dev-fast-gossip", "--large-channels", "--min-capacity-sat=1000", - }) + }, []byte("accept_all_peers=1\n")) defer func() { if t.Failed() { diff --git a/test/bitcoin_lnd_test.go b/test/bitcoin_lnd_test.go index ef35fd2d..4173b93d 100644 --- a/test/bitcoin_lnd_test.go +++ b/test/bitcoin_lnd_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/elementsproject/peerswap/peerswaprpc" + "github.com/elementsproject/peerswap/policy" "github.com/elementsproject/peerswap/swap" "github.com/elementsproject/peerswap/testframework" "github.com/stretchr/testify/assert" @@ -69,20 +70,23 @@ func Test_OnlyOneActiveSwapPerChannelLnd(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 5, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 5, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -97,9 +101,10 @@ func Test_OnlyOneActiveSwapPerChannelLnd(t *testing.T) { go func(n int) { defer wg.Done() res, err := peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) t.Logf("[%d] Response: %v", n, res) if err != nil { @@ -181,20 +186,23 @@ func Test_LndLnd_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -203,9 +211,10 @@ func Test_LndLnd_Bitcoin_SwapIn(t *testing.T) { defer cancel() go func() { peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -260,20 +269,23 @@ func Test_LndLnd_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -282,9 +294,10 @@ func Test_LndLnd_Bitcoin_SwapIn(t *testing.T) { defer cancel() go func() { peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() coopClaimTest(t, params) @@ -339,21 +352,24 @@ func Test_LndLnd_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -362,9 +378,10 @@ func Test_LndLnd_Bitcoin_SwapIn(t *testing.T) { defer cancel() go func() { peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -425,21 +442,24 @@ func Test_LndLnd_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -449,9 +469,10 @@ func Test_LndLnd_Bitcoin_SwapOut(t *testing.T) { defer cancel() go func() { peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -506,21 +527,24 @@ func Test_LndLnd_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -530,9 +554,10 @@ func Test_LndLnd_Bitcoin_SwapOut(t *testing.T) { defer cancel() go func() { peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() coopClaimTest(t, params) @@ -587,21 +612,24 @@ func Test_LndLnd_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -610,9 +638,10 @@ func Test_LndLnd_Bitcoin_SwapOut(t *testing.T) { defer cancel() go func() { peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -671,21 +700,24 @@ func Test_LndCln_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -694,9 +726,10 @@ func Test_LndCln_Bitcoin_SwapIn(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -749,21 +782,24 @@ func Test_LndCln_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -772,9 +808,10 @@ func Test_LndCln_Bitcoin_SwapIn(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() @@ -828,21 +865,24 @@ func Test_LndCln_Bitcoin_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*testframework.CLightningNode).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -851,9 +891,10 @@ func Test_LndCln_Bitcoin_SwapIn(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -912,21 +953,24 @@ func Test_LndCln_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -935,9 +979,10 @@ func Test_LndCln_Bitcoin_SwapOut(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -990,21 +1035,24 @@ func Test_LndCln_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -1013,9 +1061,10 @@ func Test_LndCln_Bitcoin_SwapOut(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() coopClaimTest(t, params) @@ -1068,21 +1117,24 @@ func Test_LndCln_Bitcoin_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*testframework.CLightningNode).DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -1091,9 +1143,10 @@ func Test_LndCln_Bitcoin_SwapOut(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -1153,21 +1206,24 @@ func Test_LndLnd_ExcessiveAmount(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - csv: BitcoinCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + csv: BitcoinCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" @@ -1178,9 +1234,10 @@ func Test_LndLnd_ExcessiveAmount(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() _, err = peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) assert.Error(t, err) }) @@ -1234,20 +1291,23 @@ func Test_LndLnd_ExcessiveAmount(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: bitcoind.RpcProxy, - chaind: bitcoind, - confirms: BitcoinConfirms, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: bitcoind.RpcProxy, + chaind: bitcoind, + confirms: BitcoinConfirms, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "btc" _, err = lightningds[0].SetHtlcMaximumMilliSatoshis(scid, channelBalances[0]*1000/2-1) @@ -1257,9 +1317,10 @@ func Test_LndLnd_ExcessiveAmount(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() _, err = peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) assert.Error(t, err) }) diff --git a/test/liquid_cln_test.go b/test/liquid_cln_test.go index c4672def..197175b1 100644 --- a/test/liquid_cln_test.go +++ b/test/liquid_cln_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/elementsproject/peerswap/clightning" + "github.com/elementsproject/peerswap/policy" "github.com/elementsproject/peerswap/swap" "github.com/stretchr/testify/require" ) @@ -58,28 +59,37 @@ func Test_ClnCln_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" // Do swap. go func() { var response map[string]interface{} - lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].Rpc.Request( + &clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit, + }, &response) }() preimageClaimTest(t, params) @@ -128,28 +138,35 @@ func Test_ClnCln_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" // Do swap. go func() { var response map[string]interface{} - lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) @@ -198,28 +215,35 @@ func Test_ClnCln_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" // Do swap. go func() { var response map[string]interface{} - lightningds[1].Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) @@ -274,21 +298,24 @@ func Test_ClnCln_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -296,7 +323,11 @@ func Test_ClnCln_Liquid_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -344,21 +375,24 @@ func Test_ClnCln_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -366,7 +400,11 @@ func Test_ClnCln_Liquid_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) }) @@ -414,21 +452,24 @@ func Test_ClnCln_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].DaemonProcess, - makerPeerswap: lightningds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].DaemonProcess, + makerPeerswap: lightningds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -436,7 +477,11 @@ func Test_ClnCln_Liquid_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) }) @@ -494,21 +539,24 @@ func Test_ClnLnd_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -516,7 +564,11 @@ func Test_ClnLnd_Liquid_SwapIn(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[1].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -590,7 +642,11 @@ func Test_ClnLnd_Liquid_SwapIn(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[1].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[1].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) }) @@ -642,21 +698,24 @@ func Test_ClnLnd_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -664,7 +723,11 @@ func Test_ClnLnd_Liquid_SwapIn(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - err := lightningds[1].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapIn{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + err := lightningds[1].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapIn{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) require.NoError(err) }() csvClaimTest(t, params) @@ -723,21 +786,24 @@ func Test_ClnLnd_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -745,7 +811,11 @@ func Test_ClnLnd_Liquid_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() preimageClaimTest(t, params) }) @@ -796,21 +866,24 @@ func Test_ClnLnd_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -818,7 +891,11 @@ func Test_ClnLnd_Liquid_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() coopClaimTest(t, params) }) @@ -869,21 +946,24 @@ func Test_ClnLnd_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -891,7 +971,11 @@ func Test_ClnLnd_Liquid_SwapOut(t *testing.T) { go func() { // We need to run this in a go routine as the Request call is blocking and sometimes does not return. var response map[string]interface{} - lightningds[0].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapOut{SatAmt: params.swapAmt, ShortChannelId: params.scid, Asset: asset}, &response) + lightningds[0].(*CLightningNodeWithLiquid).Rpc.Request(&clightning.SwapOut{ + SatAmt: params.swapAmt, + ShortChannelId: params.scid, + Asset: asset, + PremiumLimit: params.premiumLimit}, &response) }() csvClaimTest(t, params) }) diff --git a/test/liquid_lnd_test.go b/test/liquid_lnd_test.go index e9b76517..d043986c 100644 --- a/test/liquid_lnd_test.go +++ b/test/liquid_lnd_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/elementsproject/peerswap/peerswaprpc" + "github.com/elementsproject/peerswap/policy" "github.com/elementsproject/peerswap/swap" "github.com/stretchr/testify/require" ) @@ -69,21 +70,24 @@ func Test_LndLnd_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -92,9 +96,10 @@ func Test_LndLnd_Liquid_SwapIn(t *testing.T) { defer cancel() go func() { peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -153,21 +158,24 @@ func Test_LndLnd_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -176,9 +184,10 @@ func Test_LndLnd_Liquid_SwapIn(t *testing.T) { defer cancel() go func() { peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() coopClaimTest(t, params) @@ -237,21 +246,24 @@ func Test_LndLnd_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -260,9 +272,10 @@ func Test_LndLnd_Liquid_SwapIn(t *testing.T) { defer cancel() go func() { peerswapds[1].PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -327,21 +340,24 @@ func Test_LndLnd_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -351,9 +367,10 @@ func Test_LndLnd_Liquid_SwapOut(t *testing.T) { defer cancel() go func() { peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -412,21 +429,24 @@ func Test_LndLnd_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -436,9 +456,10 @@ func Test_LndLnd_Liquid_SwapOut(t *testing.T) { defer cancel() go func() { peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() coopClaimTest(t, params) @@ -497,21 +518,24 @@ func Test_LndLnd_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapds[0].DaemonProcess, - makerPeerswap: peerswapds[1].DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapds[0].DaemonProcess, + makerPeerswap: peerswapds[1].DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -521,9 +545,10 @@ func Test_LndLnd_Liquid_SwapOut(t *testing.T) { defer cancel() go func() { peerswapds[0].PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -586,21 +611,24 @@ func Test_LndCln_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -609,9 +637,10 @@ func Test_LndCln_Liquid_SwapIn(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -668,21 +697,24 @@ func Test_LndCln_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -691,9 +723,10 @@ func Test_LndCln_Liquid_SwapIn(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: int64(maxPaymentSize), }) }() coopClaimTest(t, params) @@ -750,21 +783,24 @@ func Test_LndCln_Liquid_SwapIn(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, - makerPeerswap: peerswapd.DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_IN, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: lightningds[0].(*CLightningNodeWithLiquid).DaemonProcess, + makerPeerswap: peerswapd.DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_IN, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -773,9 +809,10 @@ func Test_LndCln_Liquid_SwapIn(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapIn(ctx, &peerswaprpc.SwapInRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) @@ -838,21 +875,24 @@ func Test_LndCln_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -861,9 +901,10 @@ func Test_LndCln_Liquid_SwapOut(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() preimageClaimTest(t, params) @@ -920,21 +961,24 @@ func Test_LndCln_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -943,9 +987,10 @@ func Test_LndCln_Liquid_SwapOut(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() coopClaimTest(t, params) @@ -1002,21 +1047,24 @@ func Test_LndCln_Liquid_SwapOut(t *testing.T) { } params := &testParams{ - swapAmt: channelBalances[0] / 2, - scid: scid, - origTakerWallet: walletBalances[0], - origMakerWallet: walletBalances[1], - origTakerBalance: channelBalances[0], - origMakerBalance: channelBalances[1], - takerNode: lightningds[0], - makerNode: lightningds[1], - takerPeerswap: peerswapd.DaemonProcess, - makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, - chainRpc: liquidd.RpcProxy, - chaind: liquidd, - confirms: LiquidConfirms, - csv: LiquidCsv, - swapType: swap.SWAPTYPE_OUT, + swapAmt: channelBalances[0] / 2, + scid: scid, + origTakerWallet: walletBalances[0], + origMakerWallet: walletBalances[1], + origTakerBalance: channelBalances[0], + origMakerBalance: channelBalances[1], + takerNode: lightningds[0], + makerNode: lightningds[1], + takerPeerswap: peerswapd.DaemonProcess, + makerPeerswap: lightningds[1].(*CLightningNodeWithLiquid).DaemonProcess, + chainRpc: liquidd.RpcProxy, + chaind: liquidd, + confirms: LiquidConfirms, + csv: LiquidCsv, + swapType: swap.SWAPTYPE_OUT, + premiumLimit: int64(channelBalances[0] / 100), + swapInPremiumRate: policy.DefaultPolicy().SwapInPremiumRatePPM, + swapOutPremiumRate: policy.DefaultPolicy().SwapOutPremiumRatePPM, } asset := "lbtc" @@ -1025,9 +1073,10 @@ func Test_LndCln_Liquid_SwapOut(t *testing.T) { defer cancel() go func() { peerswapd.PeerswapClient.SwapOut(ctx, &peerswaprpc.SwapOutRequest{ - ChannelId: lcid, - SwapAmount: params.swapAmt, - Asset: asset, + ChannelId: lcid, + SwapAmount: params.swapAmt, + Asset: asset, + PremiumLimit: params.premiumLimit, }) }() csvClaimTest(t, params) diff --git a/test/setup.go b/test/setup.go index 024a11fb..cd4c8646 100644 --- a/test/setup.go +++ b/test/setup.go @@ -35,10 +35,10 @@ func clnclnSetup(t *testing.T, fundAmt uint64) (*testframework.BitcoinNode, []*t "--dev-bitcoind-poll=1", "--dev-fast-gossip", "--large-channels", - }) + }, []byte("accept_all_peers=1\nmin_swap_amount_msat=1\n")) } -func clnclnSetupWithConfig(t *testing.T, fundAmt, pushAmt uint64, clnConf []string) (*testframework.BitcoinNode, []*testframework.CLightningNode, string) { +func clnclnSetupWithConfig(t *testing.T, fundAmt, pushAmt uint64, clnConf []string, policyConf []byte) (*testframework.BitcoinNode, []*testframework.CLightningNode, string) { // Get PeerSwap plugin path and test dir _, filename, _, _ := runtime.Caller(0) pathToPlugin := filepath.Join(filename, "..", "..", "out", "test-builds", "peerswap") @@ -65,8 +65,7 @@ func clnclnSetupWithConfig(t *testing.T, fundAmt, pushAmt uint64, clnConf []stri if err != nil { t.Fatal("could not create dir", err) } - err = os.WriteFile(filepath.Join(lightningd.GetDataDir(), "peerswap", "policy.conf"), - []byte("accept_all_peers=1\nmin_swap_amount_msat=1\n"), os.ModePerm) + err = os.WriteFile(filepath.Join(lightningd.GetDataDir(), "peerswap", "policy.conf"), policyConf, os.ModePerm) if err != nil { t.Fatal("could not create policy file", err) } diff --git a/test/testcases.go b/test/testcases.go index b2603fb3..e0dc0942 100644 --- a/test/testcases.go +++ b/test/testcases.go @@ -11,21 +11,35 @@ import ( ) type testParams struct { - swapAmt uint64 - scid string - origTakerWallet uint64 - origMakerWallet uint64 - origTakerBalance uint64 - origMakerBalance uint64 - takerNode testframework.LightningNode - makerNode testframework.LightningNode - takerPeerswap *testframework.DaemonProcess - makerPeerswap *testframework.DaemonProcess - chainRpc *testframework.RpcProxy - chaind ChainNode - confirms int - csv int - swapType swap.SwapType + swapAmt uint64 + scid string + origTakerWallet uint64 + origMakerWallet uint64 + origTakerBalance uint64 + origMakerBalance uint64 + takerNode testframework.LightningNode + makerNode testframework.LightningNode + takerPeerswap *testframework.DaemonProcess + makerPeerswap *testframework.DaemonProcess + chainRpc *testframework.RpcProxy + chaind ChainNode + confirms int + csv int + swapType swap.SwapType + premiumLimit int64 + swapInPremiumRate int64 + swapOutPremiumRate int64 +} + +func (p *testParams) premium() int64 { + switch p.swapType { + case swap.SWAPTYPE_IN: + return swap.ComputePremium(p.swapAmt, p.swapInPremiumRate) + case swap.SWAPTYPE_OUT: + return swap.ComputePremium(p.swapAmt, p.swapOutPremiumRate) + default: + return 0 + } } func coopClaimTest(t *testing.T, params *testParams) { @@ -48,6 +62,9 @@ func coopClaimTest(t *testing.T, params *testParams) { require.NoError(err) moveAmt := (params.origTakerBalance - feeInvoiceAmt - params.swapAmt) + 1 + if params.swapType == swap.SWAPTYPE_OUT { + moveAmt = uint64(int64(moveAmt) - params.premium()) + } inv, err := params.makerNode.AddInvoice(moveAmt, "shift balance", "") require.NoError(err) @@ -59,7 +76,11 @@ func coopClaimTest(t *testing.T, params *testParams) { err = testframework.WaitFor(func() bool { setTakerFunds, err = params.takerNode.GetChannelBalanceSat(params.scid) require.NoError(err) - return setTakerFunds == params.swapAmt-1 + expectTakerFunds := params.swapAmt - 1 + if params.swapType == swap.SWAPTYPE_OUT { + expectTakerFunds = uint64(int64(expectTakerFunds) + params.premium()) + } + return setTakerFunds == expectTakerFunds }, testframework.TIMEOUT) require.NoError(err) @@ -123,16 +144,16 @@ func coopClaimTest(t *testing.T, params *testParams) { func preimageClaimTest(t *testing.T, params *testParams) { require := require.New(t) - var premium uint64 + var feeInvoiceAmt uint64 if params.swapType == swap.SWAPTYPE_OUT { // Wait for channel balance to change, this means the invoice was payed. testframework.AssertWaitForBalanceChange(t, params.takerNode, params.scid, params.origTakerBalance, testframework.TIMEOUT) testframework.AssertWaitForBalanceChange(t, params.makerNode, params.scid, params.origMakerBalance, testframework.TIMEOUT) - // Get premium from difference. + // Get fee from difference. newBalance, err := params.takerNode.GetChannelBalanceSat(params.scid) require.NoError(err) - premium = params.origTakerBalance - newBalance + feeInvoiceAmt = params.origTakerBalance - newBalance } // Wait for opening tx being broadcasted. @@ -156,12 +177,19 @@ func preimageClaimTest(t *testing.T, params *testParams) { } // Check channel balances match. - // premium is only !=0 when swap type is swap_out. - expected := float64(params.origTakerBalance - params.swapAmt - premium) - require.True(testframework.AssertWaitForChannelBalance(t, params.takerNode, params.scid, expected, 1., testframework.TIMEOUT)) - - expected = float64(params.origMakerBalance + params.swapAmt + premium) - require.True(testframework.AssertWaitForChannelBalance(t, params.makerNode, params.scid, expected, 1., testframework.TIMEOUT)) + // fee invoice amount is only !=0 when swap type is swap_out. + expectedTakerChannelBalance := float64(int64(params.origTakerBalance - params.swapAmt - feeInvoiceAmt)) + if params.swapType == swap.SWAPTYPE_OUT { + // taker pay premium by invoice being paid. + expectedTakerChannelBalance -= float64(params.premium()) + } + require.True(testframework.AssertWaitForChannelBalance(t, params.takerNode, params.scid, expectedTakerChannelBalance, 1., testframework.TIMEOUT)) + expectedMakerChannelBalance := float64(params.origMakerBalance + params.swapAmt + feeInvoiceAmt) + if params.swapType == swap.SWAPTYPE_OUT { + // maker receive premium by invoice being paid. + expectedMakerChannelBalance += float64(params.premium()) + } + require.True(testframework.AssertWaitForChannelBalance(t, params.makerNode, params.scid, expectedMakerChannelBalance, 1., testframework.TIMEOUT)) // Wait for claim tx being broadcasted. // Get claim fee. @@ -182,19 +210,25 @@ func preimageClaimTest(t *testing.T, params *testParams) { t.Fatal("unknown role") } - // Check Wallet balance. - // Expect: (WITHOUT PREMIUM) + // Check Wallet takerBalance. + // Expect: // - taker -> before - claim_fee + swapamt // - maker -> before - commitment_fee - swapamt - balance, err := params.takerNode.GetBtcBalanceSat() + takerBalance, err := params.takerNode.GetBtcBalanceSat() require.NoError(err) - require.InDelta(params.origTakerWallet-claimFee+params.swapAmt, float64(balance), 1., "expected %d, got %d", - params.origTakerWallet-claimFee+params.swapAmt, balance) + expectTakerBalance := int64(params.origTakerWallet - claimFee + params.swapAmt) + if swap.SWAPTYPE_IN == params.swapType { + expectTakerBalance += params.premium() + } + require.InDelta(expectTakerBalance, float64(takerBalance), 1., "expected %d, got %d", expectTakerBalance, takerBalance) - balance, err = params.makerNode.GetBtcBalanceSat() + makerBalance, err := params.makerNode.GetBtcBalanceSat() require.NoError(err) - require.InDelta((params.origMakerWallet - commitFee - params.swapAmt), float64(balance), 1., "expected %d, got %d", - (params.origMakerWallet - commitFee - params.swapAmt), balance) + expectMakerBalance := int64(params.origMakerWallet - commitFee - params.swapAmt) + if swap.SWAPTYPE_IN == params.swapType { + expectMakerBalance -= params.premium() + } + require.InDelta(expectMakerBalance, float64(makerBalance), 1., "expected %d, got %d", expectMakerBalance, makerBalance) // Check latest invoice memo should be of the form "swap-in btc claim " bolt11, err := params.makerNode.GetLatestInvoice() diff --git a/test/wumbo_test.go b/test/wumbo_test.go index 07764234..d71ae70f 100644 --- a/test/wumbo_test.go +++ b/test/wumbo_test.go @@ -108,7 +108,7 @@ func Test_Wumbo(t *testing.T) { } // Test Swap-out - bitcoind, lightningds, scid := clnclnSetupWithConfig(t, maxChanSize, 0, options) + bitcoind, lightningds, scid := clnclnSetupWithConfig(t, maxChanSize, 0, options, []byte("accept_all_peers=1\n")) defer func() { if t.Failed() { filter := os.Getenv("PEERSWAP_TEST_FILTER")