Skip to content

Commit

Permalink
feat: use discounted liquid transaction size
Browse files Browse the repository at this point in the history
  • Loading branch information
jackstar12 committed Oct 23, 2024
1 parent d68bef2 commit 6bd6081
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 56 deletions.
5 changes: 1 addition & 4 deletions boltz/liquid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/vulpemventures/go-elements/address"
Expand Down Expand Up @@ -79,8 +77,7 @@ func (transaction *LiquidTransaction) VoutValue(vout uint32) (uint64, error) {
}

func (transaction *LiquidTransaction) VSize() uint64 {
witnessSize := transaction.SerializeSize(true, true) - transaction.SerializeSize(false, true)
return uint64(transaction.SerializeSize(false, true)) + uint64(math.Ceil(float64(witnessSize)/4))
return uint64(transaction.DiscountVirtualSize())
}

func liquidTaprootHash(transaction *liquidtx.Transaction, network *Network, outputs []OutputDetails, index int, cooperative bool) []byte {
Expand Down
2 changes: 1 addition & 1 deletion nursery/nursery.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (nursery *Nursery) createTransaction(currency boltz.Currency, outputs []*Ou
return id, err
}

feeSatPerVbyte, err := nursery.onchain.EstimateFee(currency, true)
feeSatPerVbyte, err := nursery.onchain.EstimateFee(currency)
if err != nil {
return handleErr(fmt.Errorf("could not get fee estimation: %w", err))
}
Expand Down
7 changes: 1 addition & 6 deletions onchain/onchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,7 @@ func (onchain *Onchain) GetWallets(checker WalletChecker) []Wallet {
return wallets
}

func (onchain *Onchain) EstimateFee(currency boltz.Currency, allowLowball bool) (float64, error) {
if currency == boltz.CurrencyLiquid && onchain.Network == boltz.MainNet && allowLowball {
if boltzProvider, ok := onchain.Liquid.Tx.(*BoltzTxProvider); ok {
return boltzProvider.GetFeeEstimation(boltz.CurrencyLiquid)
}
}
func (onchain *Onchain) EstimateFee(currency boltz.Currency) (float64, error) {
chain, err := onchain.GetCurrency(currency)
if err != nil {
return 0, err
Expand Down
30 changes: 0 additions & 30 deletions onchain/onchain_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
//go:build !unit

package onchain_test

import (
"github.com/BoltzExchange/boltz-client/boltz"
onchainmock "github.com/BoltzExchange/boltz-client/mocks/github.com/BoltzExchange/boltz-client/onchain"
"github.com/BoltzExchange/boltz-client/onchain"
"github.com/BoltzExchange/boltz-client/onchain/wallet"
"github.com/stretchr/testify/require"
"testing"
)

func TestEstimateLowballFee(t *testing.T) {
boltzApi := &boltz.Api{URL: "https://api.boltz.exchange"}
blocks := onchainmock.NewMockBlockProvider(t)
chain := &onchain.Onchain{
Liquid: &onchain.Currency{
Tx: onchain.NewBoltzTxProvider(boltzApi, boltz.CurrencyLiquid),
Blocks: blocks,
},
Network: boltz.MainNet,
}
blocks.EXPECT().EstimateFee().Return(0.1, nil)

fee, err := chain.EstimateFee(boltz.CurrencyLiquid, true)
require.NoError(t, err)
require.Equal(t, fee, wallet.MinFeeRate)

fee, err = chain.EstimateFee(boltz.CurrencyLiquid, false)
require.NoError(t, err)
require.Greater(t, fee, wallet.MinFeeRate)
}
2 changes: 1 addition & 1 deletion onchain/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/BoltzExchange/boltz-client/boltz"
)

const MinFeeRate = 0.01
const MinFeeRate = 0.1
const MaxInputs = uint64(256)
const DefaultAutoConsolidateThreshold = uint64(200)
const GapLimit = 100
Expand Down
18 changes: 4 additions & 14 deletions rpcserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,6 @@ func (server *routedBoltzServer) createSwap(ctx context.Context, isAuto bool, re
wallet,
swapResponse.Address,
swapResponse.ExpectedAmount,
!request.GetZeroConf(),
submarinePair.Limits.MaximalZeroConfAmount,
request.SatPerVbyte,
)
if err != nil {
Expand All @@ -888,8 +886,6 @@ func (server *routedBoltzServer) createSwap(ctx context.Context, isAuto bool, re
wallet,
swapResponse.Address,
swapResponse.ExpectedAmount,
false,
submarinePair.Limits.MaximalZeroConfAmount,
request.SatPerVbyte,
)
if err != nil {
Expand Down Expand Up @@ -1320,8 +1316,6 @@ func (server *routedBoltzServer) createChainSwap(ctx context.Context, isAuto boo
fromWallet,
from.LockupAddress,
from.Amount,
!request.GetLockupZeroConf(),
chainPair.Limits.MaximalZeroConfAmount,
request.SatPerVbyte,
)
if err != nil {
Expand Down Expand Up @@ -1696,7 +1690,7 @@ func (server *routedBoltzServer) WalletSend(ctx context.Context, request *boltzr
if err != nil {
return nil, err
}
txId, err := server.sendToAddress(sendWallet, request.Address, request.Amount, true, 0, request.SatPerVbyte)
txId, err := server.sendToAddress(sendWallet, request.Address, request.Amount, request.SatPerVbyte)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2153,19 +2147,15 @@ func (server *routedBoltzServer) getPairs(pairId boltz.Pair) (*boltzrpc.Fees, *b
}, nil
}

func (server *routedBoltzServer) sendToAddress(wallet onchain.Wallet, address string, amount uint64, allowLowball bool, maxZeroConfAmount uint64, fee *float64) (string, error) {
if amount > maxZeroConfAmount && !allowLowball {
logger.Infof("Amount %d exceeds maximal zero conf amount %d, allowing lowball", amount, maxZeroConfAmount)
allowLowball = true
}
func (server *routedBoltzServer) sendToAddress(wallet onchain.Wallet, address string, amount uint64, fee *float64) (string, error) {
if fee == nil {
feeSatPerVbyte, err := server.onchain.EstimateFee(wallet.GetWalletInfo().Currency, allowLowball)
feeSatPerVbyte, err := server.onchain.EstimateFee(wallet.GetWalletInfo().Currency)
if err != nil {
return "", err
}
fee = &feeSatPerVbyte
}
logger.Infof("Using fee of %f sat/vbyte (allowLowball: %v)", *fee, allowLowball)
logger.Infof("Using fee of %f sat/vbyte", *fee)
return wallet.SendToAddress(address, amount, *fee, false)
}

Expand Down

0 comments on commit 6bd6081

Please sign in to comment.