Skip to content

Commit

Permalink
feat: allow manual refunds to wallets (#167)
Browse files Browse the repository at this point in the history
* feat: manual refunds to wallets

* refactor: use pointers instead of values in db functions

* test: run manual refund tests from btc instead of lbtc

wallet balance check can be flaky since gdk takes a bit to sync

* chore: cleanup
  • Loading branch information
jackstar12 committed Jul 7, 2024
1 parent e027b7c commit 798ed3e
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 136 deletions.
12 changes: 11 additions & 1 deletion boltz/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package boltz

import (
"errors"

"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/vulpemventures/go-elements/address"
)
Expand All @@ -21,3 +21,13 @@ func ValidateAddress(network *Network, rawAddress string, currency Currency) err
}
return err
}

func GetAddressCurrency(network *Network, address string) (Currency, error) {
if err := ValidateAddress(network, address, CurrencyBtc); err == nil {
return CurrencyBtc, nil
}
if err := ValidateAddress(network, address, CurrencyLiquid); err == nil {
return CurrencyLiquid, nil
}
return "", fmt.Errorf("invalid address: %s", address)
}
53 changes: 47 additions & 6 deletions boltzrpc/boltzrpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion boltzrpc/boltzrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,10 @@ message ListSwapsResponse {
}
message RefundSwapRequest {
string id = 1;
string address = 2;
oneof destination {
string address = 2;
uint64 wallet_id = 3;
}
}

message GetSwapInfoRequest {
Expand Down
4 changes: 2 additions & 2 deletions boltzrpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (boltz *Boltz) ListSwaps(request *boltzrpc.ListSwapsRequest) (*boltzrpc.Lis
return boltz.Client.ListSwaps(boltz.Ctx, request)
}

func (boltz *Boltz) RefundSwap(id string, address string) (*boltzrpc.GetSwapInfoResponse, error) {
return boltz.Client.RefundSwap(boltz.Ctx, &boltzrpc.RefundSwapRequest{Id: id, Address: address})
func (boltz *Boltz) RefundSwap(request *boltzrpc.RefundSwapRequest) (*boltzrpc.GetSwapInfoResponse, error) {
return boltz.Client.RefundSwap(boltz.Ctx, request)
}

func (boltz *Boltz) GetSwapInfo(id string) (*boltzrpc.GetSwapInfoResponse, error) {
Expand Down
32 changes: 15 additions & 17 deletions cmd/boltzcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,17 +940,6 @@ var createChainSwapCommand = &cli.Command{
},
}

func checkAddress(network *boltz.Network, address string) (boltzrpc.Currency, error) {
if err := boltz.ValidateAddress(network, address, boltz.CurrencyBtc); err == nil {
return boltzrpc.Currency_BTC, nil
}
if err := boltz.ValidateAddress(network, address, boltz.CurrencyLiquid); err == nil {
return boltzrpc.Currency_LBTC, nil
}
return boltzrpc.Currency_BTC, fmt.Errorf("invalid address: %s", address)

}

func createChainSwap(ctx *cli.Context) error {
client := getClient(ctx)
var amount uint64
Expand Down Expand Up @@ -986,18 +975,20 @@ func createChainSwap(ctx *cli.Context) error {
network, _ := boltz.ParseChain(info.Network)

if toAddress := ctx.String("to-address"); toAddress != "" {
pair.To, err = checkAddress(network, toAddress)
to, err := boltz.GetAddressCurrency(network, toAddress)
if err != nil {
return err
}
pair.To = utils.SerializeCurrency(to)
request.ToAddress = &toAddress
}

if refundAddress := ctx.String("refund-address"); refundAddress != "" {
pair.From, err = checkAddress(network, refundAddress)
from, err := boltz.GetAddressCurrency(network, refundAddress)
if err != nil {
return err
}
pair.From = utils.SerializeCurrency(from)
request.RefundAddress = &refundAddress
}

Expand Down Expand Up @@ -1064,16 +1055,23 @@ func createChainSwap(ctx *cli.Context) error {
var refundSwapCommand = &cli.Command{
Name: "refundswap",
Category: "Swaps",
Usage: "Refund a chain-to-x swap manually",
ArgsUsage: "id addresss",
Usage: "Refund a chain-to-x swap manually to an onchain address or internal wallet",
ArgsUsage: "id address|wallet",
Action: requireNArgs(2, refundSwap),
}

func refundSwap(ctx *cli.Context) error {
client := getClient(ctx)
id := ctx.Args().First()
address := ctx.Args().Get(1)
swap, err := client.RefundSwap(id, address)
destination := ctx.Args().Get(1)
request := &boltzrpc.RefundSwapRequest{Id: id}
walletId, err := getWalletId(ctx, destination)
if err == nil {
request.Destination = &boltzrpc.RefundSwapRequest_WalletId{WalletId: *walletId}
} else {
request.Destination = &boltzrpc.RefundSwapRequest_Address{Address: destination}
}
swap, err := client.RefundSwap(request)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 798ed3e

Please sign in to comment.