fix: swap logic

This commit is contained in:
im-adithya 2025-04-17 19:50:54 +05:30
parent 490c0e07b4
commit fab261494a
6 changed files with 37 additions and 15 deletions

View file

@ -286,7 +286,7 @@ func (cs *CashuService) UpdateChannel(ctx context.Context, updateChannelRequest
}
func (cs *CashuService) EnableAutoSwap(balanceThreshold uint64, destination string) error {
return lnclient.StartAutoSwap(cs.ctx, balanceThreshold, destination, cs.GetBalances)
return lnclient.StartAutoSwap(cs.ctx, balanceThreshold, destination, cs.GetBalances, cs.SendPaymentSync)
}
func (cs *CashuService) GetBalances(ctx context.Context, includeInactiveChannels bool) (*lnclient.BalancesResponse, error) {

View file

@ -1714,7 +1714,7 @@ func (ls *LDKService) GetBalances(ctx context.Context, includeInactiveChannels b
}
func (ls *LDKService) EnableAutoSwap(balanceThreshold uint64, destination string) error {
return lnclient.StartAutoSwap(ls.ctx, balanceThreshold, destination, ls.GetBalances)
return lnclient.StartAutoSwap(ls.ctx, balanceThreshold, destination, ls.GetBalances, ls.SendPaymentSync)
}
func (ls *LDKService) GetStorageDir() (string, error) {

View file

@ -1154,7 +1154,7 @@ func (svc *LNDService) GetBalances(ctx context.Context, includeInactiveChannels
}
func (svc *LNDService) EnableAutoSwap(balanceThreshold uint64, destination string) error {
return lnclient.StartAutoSwap(svc.ctx, balanceThreshold, destination, svc.GetBalances)
return lnclient.StartAutoSwap(svc.ctx, balanceThreshold, destination, svc.GetBalances, svc.SendPaymentSync)
}
func (svc *LNDService) GetStorageDir() (string, error) {

View file

@ -130,7 +130,7 @@ func (svc *PhoenixService) GetBalances(ctx context.Context, includeInactiveChann
}
func (svc *PhoenixService) EnableAutoSwap(balanceThreshold uint64, destination string) error {
return lnclient.StartAutoSwap(svc.ctx, balanceThreshold, destination, svc.GetBalances)
return lnclient.StartAutoSwap(svc.ctx, balanceThreshold, destination, svc.GetBalances, svc.SendPaymentSync)
}
func (svc *PhoenixService) ListTransactions(ctx context.Context, from, until, limit, offset uint64, unpaid bool, invoiceType string) (transactions []lnclient.Transaction, err error) {

View file

@ -13,9 +13,10 @@ import (
"github.com/sirupsen/logrus"
)
type GetBalancesFn func(context.Context, bool) (*BalancesResponse, error)
type getBalancesFn func(context.Context, bool) (*BalancesResponse, error)
type sendPaymentFn func(context.Context, string, *uint64) (*PayInvoiceResponse, error)
func StartAutoSwap(ctx context.Context, balanceThreshold uint64, swapAmount uint64, destination string, getBalancesFn GetBalancesFn) error {
func StartAutoSwap(ctx context.Context, balanceThreshold uint64, destination string, getBalances getBalancesFn, sendPayment sendPaymentFn) error {
go func() {
// TODO: Do we want to check every hour?
ticker := time.NewTicker(1 * time.Hour)
@ -23,15 +24,25 @@ func StartAutoSwap(ctx context.Context, balanceThreshold uint64, swapAmount uint
select {
case <-ticker.C:
logger.Logger.Info("Checking to see if we can swap")
balance, err := getBalancesFn(ctx, false)
balance, err := getBalances(ctx, false)
if err != nil {
logger.Logger.WithError(err).Error("Failed to get balance")
return
}
lightningBalance := uint64(balance.Lightning.TotalSpendable)
if lightningBalance >= balanceThreshold {
logger.Logger.Info("Initiating swap")
ReverseSwap(swapAmount, destination)
balanceThresholdMilliSats := balanceThreshold * 1000
if lightningBalance >= balanceThresholdMilliSats {
// TODO: Change this calcuation
amount := lightningBalance - balanceThresholdMilliSats
logger.Logger.WithFields(logrus.Fields{
"amount": amount,
"destination": destination,
}).Info("Initiating swap")
// TODO: Should we ourselves add a check that the amount is < 50000
err := ReverseSwap(ctx, amount/1000, destination, sendPayment)
if err != nil {
logger.Logger.WithError(err).Error("Failed to swap")
}
}
case <-ctx.Done():
return
@ -41,10 +52,10 @@ func StartAutoSwap(ctx context.Context, balanceThreshold uint64, swapAmount uint
return nil
}
func ReverseSwap(amount uint64, destination string) error {
func ReverseSwap(ctx context.Context, amount uint64, destination string, sendPayment sendPaymentFn) error {
// TODO: Make these configurable from env or using network env var
const endpoint = "wss://api.testnet.boltz.exchange/v2/ws"
var network = boltz.MainNet
const endpoint = "https://api.testnet.boltz.exchange"
var network = boltz.TestNet
ourKeys, err := btcec.NewPrivateKey()
if err != nil {
@ -77,7 +88,7 @@ func ReverseSwap(amount uint64, destination string) error {
}
tree := swap.SwapTree.Deserialize()
if err := tree.Init(boltz.CurrencyBtc, false, ourKeys, boltzPubKey); err != nil {
if err := tree.Init(boltz.CurrencyBtc, true, ourKeys, boltzPubKey); err != nil {
return err
}
@ -104,7 +115,15 @@ func ReverseSwap(amount uint64, destination string) error {
logger.Logger.WithFields(logrus.Fields{
"swap": swap,
"update": update,
}).Info("Swap created, waiting for invoice to be paid")
}).Info("Swap created, paying the invoice")
// TODO: Use transaction service method here
_, err := sendPayment(ctx, swap.Invoice, nil)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"swap": swap,
"update": update,
}).Error("Error paying the invoice")
}
break
case boltz.TransactionMempool:

View file

@ -175,6 +175,9 @@ func (mln *MockLn) GetNodeStatus(ctx context.Context) (nodeStatus *lnclient.Node
func (mln *MockLn) GetNetworkGraph(ctx context.Context, nodeIds []string) (lnclient.NetworkGraphResponse, error) {
return nil, nil
}
func (mln *MockLn) EnableAutoSwap(uint64, string) error {
return nil
}
func (mln *MockLn) UpdateLastWalletSyncRequest() {}