Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,18 @@ func (api *api) RefundSwap(refundSwapRequest *RefundSwapRequest) error {
}

func (api *api) GetAutoSwapConfig() (*GetAutoSwapConfigResponse, error) {
if api.svc.GetSwapsService() == nil {
return nil, errors.New("SwapsService not started")
}

swapOutBalanceThresholdStr, _ := api.cfg.Get(config.AutoSwapBalanceThresholdKey, "")
swapOutAmountStr, _ := api.cfg.Get(config.AutoSwapAmountKey, "")
swapOutDestination, _ := api.cfg.Get(config.AutoSwapDestinationKey, "")

if xpub := api.svc.GetSwapsService().GetDecryptedAutoSwapXpub(); xpub != "" {
swapOutDestination = xpub
}

swapOutEnabled := swapOutBalanceThresholdStr != "" && swapOutAmountStr != ""
var swapOutBalanceThreshold, swapOutAmount uint64
if swapOutEnabled {
Expand Down Expand Up @@ -984,6 +992,30 @@ func (api *api) InitiateSwapIn(ctx context.Context, initiateSwapInRequest *Initi
}

func (api *api) EnableAutoSwapOut(ctx context.Context, enableAutoSwapsRequest *EnableAutoSwapRequest) error {
if api.svc.GetSwapsService() == nil {
return errors.New("SwapsService not started")
}

encryptionKey := ""
if enableAutoSwapsRequest.Destination != "" {
switch enableAutoSwapsRequest.DestinationType {
case "address":
if err := api.svc.GetSwapsService().ValidateAddress(enableAutoSwapsRequest.Destination); err != nil {
return err
}
case "xpub":
if !api.cfg.CheckUnlockPassword(enableAutoSwapsRequest.UnlockPassword) {
return errors.New("invalid unlock password")
}
if err := api.svc.GetSwapsService().ValidateXpub(enableAutoSwapsRequest.Destination); err != nil {
return err
}
encryptionKey = enableAutoSwapsRequest.UnlockPassword
default:
return errors.New("destination type must be address or xpub")
}
}

err := api.cfg.SetUpdate(config.AutoSwapBalanceThresholdKey, strconv.FormatUint(enableAutoSwapsRequest.BalanceThreshold, 10), "")
if err != nil {
logger.Logger.WithError(err).Error("Failed to save autoswap balance threshold to config")
Expand All @@ -996,16 +1028,13 @@ func (api *api) EnableAutoSwapOut(ctx context.Context, enableAutoSwapsRequest *E
return err
}

err = api.cfg.SetUpdate(config.AutoSwapDestinationKey, enableAutoSwapsRequest.Destination, "")
err = api.cfg.SetUpdate(config.AutoSwapDestinationKey, enableAutoSwapsRequest.Destination, encryptionKey)
if err != nil {
logger.Logger.WithError(err).Error("Failed to save autoswap destination to config")
return err
}

if api.svc.GetSwapsService() == nil {
return errors.New("SwapsService not started")
}
return api.svc.GetSwapsService().EnableAutoSwapOut()
return api.svc.GetSwapsService().EnableAutoSwapOut(enableAutoSwapsRequest.UnlockPassword)
}

func (api *api) DisableAutoSwap() error {
Expand Down
2 changes: 2 additions & 0 deletions api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ type EnableAutoSwapRequest struct {
BalanceThreshold uint64 `json:"balanceThreshold"`
SwapAmount uint64 `json:"swapAmount"`
Destination string `json:"destination"`
DestinationType string `json:"destinationType"`
UnlockPassword string `json:"unlockPassword"`
}

type GetAutoSwapConfigResponse struct {
Expand Down
Loading
Loading