Skip to content

Commit

Permalink
chore: rm lowball code
Browse files Browse the repository at this point in the history
  • Loading branch information
jackstar12 committed Jan 3, 2025
1 parent 8dfcc13 commit 50ee5f7
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 782 deletions.
6 changes: 3 additions & 3 deletions boltzrpc/autoswaprpc/autoswaprpc.pb.go

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

1,275 changes: 638 additions & 637 deletions boltzrpc/boltzrpc.pb.go

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions boltzrpc/boltzrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,7 @@ message CreateSwapRequest {
// when connected to a lightning node, a new invoice for `amount` sats will be fetched
// the `amount` field has to be populated in case of a lnurl and lnaddress
optional string invoice = 7;
// Boltz does not accept 0-conf for Liquid transactions with a fee of 0.01 sat/vByte;
// when `zero_conf` is enabled, a fee of 0.1 sat/vByte will be used for Liquid lockup transactions
optional bool zero_conf = 8;
optional bool zero_conf = 8 [deprecated = true];
// Fee rate to use when sending from internal wallet
optional double sat_per_vbyte = 9;
// Rates to accept for the swap. Queries latest from boltz otherwise
Expand Down Expand Up @@ -628,9 +626,7 @@ message CreateChainSwapRequest {
optional bool accept_zero_conf = 7;
// If set, the daemon will not pay the swap from an internal wallet.
optional bool external_pay = 8;
// Boltz does not accept 0-conf for Liquid transactions with a fee of 0.01 sat/vByte;
// when `lockup_zero_conf` is enabled, a fee of 0.1 sat/vByte will be used for Liquid lockup transactions
optional bool lockup_zero_conf = 9;
optional bool lockup_zero_conf = 9 [deprecated = true];
// Fee rate to use when sending from internal wallet
optional double sat_per_vbyte = 10;
// Rates to accept for the swap. Queries latest from boltz otherwise
Expand Down
8 changes: 4 additions & 4 deletions docs/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ Channel creations are an optional extension to a submarine swap in the data type
| `to_wallet_id` | [`uint64`](#uint64) | optional | Wallet where the the funds will go if the swap succeeds. |
| `accept_zero_conf` | [`bool`](#bool) | optional | Whether the daemon should broadcast the claim transaction immediately after the lockup transaction is in the mempool. Should only be used for smaller amounts as it involves trust in Boltz. |
| `external_pay` | [`bool`](#bool) | optional | If set, the daemon will not pay the swap from an internal wallet. |
| `lockup_zero_conf` | [`bool`](#bool) | optional | Boltz does not accept 0-conf for Liquid transactions with a fee of 0.01 sat/vByte; when `lockup_zero_conf` is enabled, a fee of 0.1 sat/vByte will be used for Liquid lockup transactions |
| `lockup_zero_conf` | [`bool`](#bool) | optional | **Deprecated.** |
| `sat_per_vbyte` | [`double`](#double) | optional | Fee rate to use when sending from internal wallet |
| `accepted_pair` | [`PairInfo`](#pairinfo) | optional | Rates to accept for the swap. Queries latest from boltz otherwise The recommended way to use this is to pass a user approved value from a previous `GetPairInfo` call |

Expand Down Expand Up @@ -625,7 +625,7 @@ Channel creations are an optional extension to a submarine swap in the data type
| `refund_address` | [`string`](#string) | optional | address where the coins should go if the swap fails. Refunds will go to any of the daemons wallets otherwise. |
| `wallet_id` | [`uint64`](#uint64) | optional | wallet to pay swap from. only used if `send_from_internal` is set to true |
| `invoice` | [`string`](#string) | optional | bolt11 invoice, lnurl, or lnaddress to use for the swap. required in standalone mode. when connected to a lightning node, a new invoice for `amount` sats will be fetched the `amount` field has to be populated in case of a lnurl and lnaddress |
| `zero_conf` | [`bool`](#bool) | optional | Boltz does not accept 0-conf for Liquid transactions with a fee of 0.01 sat/vByte; when `zero_conf` is enabled, a fee of 0.1 sat/vByte will be used for Liquid lockup transactions |
| `zero_conf` | [`bool`](#bool) | optional | **Deprecated.** |
| `sat_per_vbyte` | [`double`](#double) | optional | Fee rate to use when sending from internal wallet |
| `accepted_pair` | [`PairInfo`](#pairinfo) | optional | Rates to accept for the swap. Queries latest from boltz otherwise The recommended way to use this is to pass a user approved value from a previous `GetPairInfo` call |

Expand Down Expand Up @@ -1746,9 +1746,9 @@ Reloads the configuration from disk.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `swap` | [`ChainSwap`](#chainswap) | optional | Populated when a swap is recommended based on the current balance of the configured `from_wallet` |
| `swap` | [`ChainSwap`](#chainswap) | optional | Populated when a swap is recommended based on the configured `wallet_balance` of the configured `from_wallet` exceeds the currently configured `max_balance` |
| `wallet_balance` | [`boltzrpc.Balance`](#boltzrpc.balance) | | |
| `max_balance` | [`uint64`](#uint64) | | Currently configured max_balance |
| `max_balance` | [`uint64`](#uint64) | | |



Expand Down
42 changes: 13 additions & 29 deletions onchain/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/BoltzExchange/boltz-client/v2/boltz"
)

const MinFeeRate = 0.1
const MaxInputs = uint64(255) // TODO: change back to 256 when gdk is fixed
const DefaultAutoConsolidateThreshold = uint64(200)
const GapLimit = 100
Expand Down Expand Up @@ -116,7 +115,6 @@ type Wallet struct {
session Session
connected bool
syncedAccounts []uint64
txProvider onchain.TxProvider
spentOutputs map[string]bool
spentOutputsLock sync.RWMutex
txNotifier <-chan TransactionNotification
Expand Down Expand Up @@ -342,9 +340,7 @@ func (wallet *Wallet) Connect() error {
}

params := map[string]any{
// gdk uses sat/kVB
"min_fee_rate": MinFeeRate * 1000,
"gap_limit": GapLimit,
"gap_limit": GapLimit,
}
var electrum onchain.ElectrumOptions
if wallet.Currency == boltz.CurrencyBtc {
Expand Down Expand Up @@ -568,10 +564,6 @@ func GenerateMnemonic() (string, error) {
return C.GoString(mnemonic), nil
}

func (wallet *Wallet) SetTxProvider(txProvider onchain.TxProvider) {
wallet.txProvider = txProvider
}

func (wallet *Wallet) Disconnect() error {
if !wallet.connected {
return nil
Expand Down Expand Up @@ -822,33 +814,25 @@ func (wallet *Wallet) SendToAddress(address string, amount uint64, satPerVbyte f
return "", fmt.Errorf("could not sign: %s", signedTx.Error)
}

if wallet.txProvider != nil {
tx, err = wallet.txProvider.BroadcastTransaction(signedTx.Transaction)
} else {
params, free = toJson(result)
var sendTx struct {
TxHash string `json:"txhash"`
Error string `json:"error"`
}
if err := withAuthHandler(C.GA_send_transaction(wallet.session, params, handler), handler, &sendTx); err != nil {
return "", err
}
free()

if sendTx.Error != "" {
err = errors.New(sendTx.Error)
}
tx = sendTx.TxHash
params, free = toJson(result)
var sendTx struct {
TxHash string `json:"txhash"`
Error string `json:"error"`
}
if err != nil {
return "", fmt.Errorf("failed to broadcast: %w", err)
if err := withAuthHandler(C.GA_send_transaction(wallet.session, params, handler), handler, &sendTx); err != nil {
return "", err
}
free()

if sendTx.Error != "" {
return "", fmt.Errorf("failed to broadcast: %s", sendTx.Error)
}

for _, input := range signedTx.TransactionInputs {
wallet.spentOutputs[input.TxId] = true
}

return tx, nil
return sendTx.TxHash, nil
}

func (wallet *Wallet) SetSpentOutputs(outputs []string) {
Expand Down
27 changes: 11 additions & 16 deletions onchain/wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ package wallet_test
import (
"github.com/BoltzExchange/boltz-client/v2/boltz"
"github.com/BoltzExchange/boltz-client/v2/logger"
onchainmock "github.com/BoltzExchange/boltz-client/v2/mocks/github.com/BoltzExchange/boltz-client/v2/onchain"
"github.com/BoltzExchange/boltz-client/v2/onchain"
onchainWallet "github.com/BoltzExchange/boltz-client/v2/onchain/wallet"
"github.com/BoltzExchange/boltz-client/v2/test"
"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"os"
"testing"
Expand Down Expand Up @@ -49,34 +47,31 @@ func TestSend(t *testing.T) {
test.MineBlock()
})

minFeeRate := 1.0

t.Run("SendFee", func(t *testing.T) {
amount, fee, err := wallet.GetSendFee(addr, 0, onchainWallet.MinFeeRate, true)
amount, fee, err := wallet.GetSendFee(addr, 0, minFeeRate, true)
require.NoError(t, err)

balance, err := wallet.GetBalance()
require.NoError(t, err)
require.Equal(t, balance.Confirmed, amount+fee)
})

t.Run("TxProvider", func(t *testing.T) {
t.Run("SpentOutputs", func(t *testing.T) {
t.Cleanup(func() {
wallet.SetTxProvider(nil)
wallet.SetSpentOutputs(nil)
})

txProvider := onchainmock.NewMockTxProvider(t)
txProvider.EXPECT().BroadcastTransaction(mock.Anything).RunAndReturn(func(txHex string) (string, error) {
require.NotEmpty(t, txHex)
return "txid", nil
})
wallet.SetTxProvider(txProvider)

txid, err := wallet.SendToAddress(addr, 0, onchainWallet.MinFeeRate, true)
txes, err := wallet.GetTransactions(0, 0)
require.NoError(t, err)
require.Equal(t, "txid", txid)
require.NotEmpty(t, txes)
var spent []string
for _, tx := range txes {
spent = append(spent, tx.Id)
}
wallet.SetSpentOutputs(spent)

_, err = wallet.SendToAddress(addr, 0, onchainWallet.MinFeeRate, true)
require.Error(t, err)
// all outputs will now be marked as spent internally, so no funds should be available
balance, err := wallet.GetBalance()
require.NoError(t, err)
Expand Down
55 changes: 12 additions & 43 deletions rpcserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,10 @@ func (server *routedBoltzServer) checkMagicRoutingHint(decoded *lightning.Decode
return nil, nil
}

func checkInvoiceExpiry(request *boltzrpc.CreateSwapRequest, invoice *lightning.DecodedInvoice) {
func checkInvoiceExpiry(request *boltzrpc.CreateSwapRequest, invoice *lightning.DecodedInvoice) bool {
expiryLeft := time.Until(invoice.Expiry.Add(-10 * time.Second))
currency := request.Pair.GetFrom()
if expiryLeft.Minutes() < boltz.GetBlockTime(serializers.ParseCurrency(&currency)) {
zeroConf := true
request.ZeroConf = &zeroConf
}
return expiryLeft.Minutes() > boltz.GetBlockTime(serializers.ParseCurrency(&currency))
}

// TODO: custom refund address
Expand Down Expand Up @@ -725,7 +722,9 @@ func (server *routedBoltzServer) createSwap(ctx context.Context, isAuto bool, re
if err != nil {
return nil, err
}
checkInvoiceExpiry(request, decoded)
if !checkInvoiceExpiry(request, decoded) {
return nil, status.Errorf(codes.InvalidArgument, "invoice is about to expire")
}
preimageHash = decoded.PaymentHash[:]
createSwap.Invoice = invoice
// set amount for balance check
Expand Down Expand Up @@ -758,14 +757,7 @@ func (server *routedBoltzServer) createSwap(ctx context.Context, isAuto bool, re
createSwap.PreimageHash = preimageHash
}

feeRate, err := server.estimateFee(
request.GetSatPerVbyte(),
pair.From,
request.Amount,
// swapResponse will be populated if we found a magic routing hint, so we can't use lowball
swapResponse == nil && !request.GetZeroConf(),
request.AcceptedPair.Limits.MaximalZeroConfAmount,
)
feeRate, err := server.estimateFee(request.GetSatPerVbyte(), pair.From)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1219,13 +1211,7 @@ func (server *routedBoltzServer) createChainSwap(ctx context.Context, isAuto boo
}
}

feeRate, err := server.estimateFee(
request.GetSatPerVbyte(),
pair.From,
amount,
!request.GetLockupZeroConf(),
request.AcceptedPair.Limits.MaximalZeroConfAmount,
)
feeRate, err := server.estimateFee(request.GetSatPerVbyte(), pair.From)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1388,19 +1374,6 @@ func (server *routedBoltzServer) createChainSwap(ctx context.Context, isAuto boo
return serializeChainSwap(&chainSwap), nil
}

func (server *routedBoltzServer) loginWallet(credentials *wallet.Credentials) (*wallet.Wallet, error) {
chain, err := server.onchain.GetCurrency(credentials.Currency)
if err != nil {
return nil, err
}
result, err := wallet.Login(credentials)
if err != nil {
return nil, err
}
result.SetTxProvider(chain.Tx)
return result, nil
}

func (server *routedBoltzServer) importWallet(ctx context.Context, credentials *wallet.Credentials, password string) error {
decryptWalletCredentials, err := server.decryptWalletCredentials(password)
if err != nil {
Expand All @@ -1416,7 +1389,7 @@ func (server *routedBoltzServer) importWallet(ctx context.Context, credentials *
}
}

wallet, err := server.loginWallet(credentials)
wallet, err := wallet.Login(credentials)
if err != nil {
return errors.New("could not login: " + err.Error())
}
Expand Down Expand Up @@ -1767,7 +1740,7 @@ func (server *routedBoltzServer) WalletSend(ctx context.Context, request *boltzr
if err != nil {
return nil, err
}
feeRate, err := server.estimateFee(request.GetSatPerVbyte(), sendWallet.GetWalletInfo().Currency, request.Amount, false, 0)
feeRate, err := server.estimateFee(request.GetSatPerVbyte(), sendWallet.GetWalletInfo().Currency)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1905,7 +1878,7 @@ func (server *routedBoltzServer) unlock(password string) error {
creds := creds
go func() {
defer wg.Done()
wallet, err := server.loginWallet(creds)
wallet, err := wallet.Login(creds)
if err != nil {
logger.Errorf("could not login to wallet: %v", err)
} else {
Expand Down Expand Up @@ -2231,17 +2204,13 @@ func (server *routedBoltzServer) getPairs(pairId boltz.Pair) (*boltzrpc.Fees, *b
}, nil
}

func (server *routedBoltzServer) estimateFee(requested float64, currency boltz.Currency, amount uint64, allowLowball bool, maxZeroConfAmount uint64) (float64, error) {
func (server *routedBoltzServer) estimateFee(requested float64, currency boltz.Currency) (float64, error) {
if requested == 0 {
if amount > maxZeroConfAmount && !allowLowball {
logger.Infof("Amount %d exceeds maximal zero conf amount %d, allowing lowball", amount, maxZeroConfAmount)
allowLowball = true
}
feeSatPerVbyte, err := server.onchain.EstimateFee(currency)
if err != nil {
return 0, err
}
logger.Infof("Using fee of %f sat/vbyte (allowLowball: %v)", feeSatPerVbyte, allowLowball)
logger.Infof("Using fee of %f sat/vbyte", feeSatPerVbyte)
return feeSatPerVbyte, nil
}
return requested, nil
Expand Down
Loading

0 comments on commit 50ee5f7

Please sign in to comment.