Skip to content

Commit

Permalink
lwkwallet: refactor fee calculations
Browse files Browse the repository at this point in the history
Variable and function names have been corrected
to eliminate confusion with fee calculations.
Test cases have also been refactored.
  • Loading branch information
YusukeShimizu committed Jun 30, 2024
1 parent 3729fbd commit dfb4967
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
38 changes: 19 additions & 19 deletions lwk/lwkwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ import (
// Satoshi represents a Satoshi value.
type Satoshi = uint64

// SatPerVByte represents a fee rate in sat/vb.
type SatPerVByte float64

const (
// 1 kb = 1000 bytes
kb = 1000
btcToSatoshiExp = 8
// TODO: Basically, the inherited ctx should be used
// and there is no need to specify a timeout here.
// Set up here because ctx is not inherited throughout the current codebase.
defaultContextTimeout = time.Second * 5
minimumSatPerByte SatPerKVByte = 0.1
defaultContextTimeout = time.Second * 5
minimumFee SatPerVByte = 0.1
supportedCLIVersion = "0.5.1"
)

// SatPerKVByte represents a fee rate in sat/kb.
type SatPerKVByte float64

func SatPerKVByteFromFeeBTCPerKb(feeBTCPerKb float64) SatPerKVByte {
s := SatPerKVByte(feeBTCPerKb * math.Pow10(btcToSatoshiExp) / kb)
if s < minimumSatPerByte {
log.Debugf("Using minimum fee rate of %v sat/kw",
minimumSatPerByte)
return minimumSatPerByte
func SatPerVByteFromFeeBTCPerKb(feeBTCPerKb float64) SatPerVByte {
s := SatPerVByte(feeBTCPerKb * math.Pow10(btcToSatoshiExp) / kb)
if s < minimumFee {
log.Debugf("using minimum fee rate of %v sat/vbyte",
minimumFee)
return minimumFee
}
return s
}

func (s SatPerKVByte) GetSatPerKVByte() float64 {
func (s SatPerVByte) getValue() float64 {
return float64(s)
}

func (s SatPerKVByte) GetFee(txSize int64) Satoshi {
return Satoshi(s.GetSatPerKVByte() * float64(txSize))
func (s SatPerVByte) GetFee(txSizeBytes int64) Satoshi {
return Satoshi(s.getValue() * float64(txSizeBytes))
}

// LWKRpcWallet uses the elementsd rpc wallet
Expand Down Expand Up @@ -159,7 +159,7 @@ func (r *LWKRpcWallet) CreateAndBroadcastTransaction(swapParams *swap.OpeningPar
asset []byte) (txid, rawTx string, fee Satoshi, err error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
feerate := float64(r.getFeePerKb(ctx)) * kb
feerate := r.getFeeSatPerVByte(ctx).getValue()
fundedTx, err := r.lwkClient.send(ctx, &sendRequest{
Addressees: []*unvalidatedAddressee{
{
Expand Down Expand Up @@ -263,18 +263,18 @@ func (r *LWKRpcWallet) SendRawTx(txHex string) (string, error) {
return res, nil
}

func (r *LWKRpcWallet) getFeePerKb(ctx context.Context) SatPerKVByte {
func (r *LWKRpcWallet) getFeeSatPerVByte(ctx context.Context) SatPerVByte {
feeBTCPerKb, err := r.electrumClient.GetFee(ctx, wallet.LiquidTargetBlocks)
if err != nil {
log.Infof("error getting fee: %v.", err)
}
return SatPerKVByteFromFeeBTCPerKb(float64(feeBTCPerKb))
return SatPerVByteFromFeeBTCPerKb(float64(feeBTCPerKb))
}

func (r *LWKRpcWallet) GetFee(txSize int64) (Satoshi, error) {
func (r *LWKRpcWallet) GetFee(txSizeBytes int64) (Satoshi, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
return r.getFeePerKb(ctx).GetFee(txSize), nil
return r.getFeeSatPerVByte(ctx).GetFee(txSizeBytes), nil
}

func (r *LWKRpcWallet) SetLabel(txID, address, label string) error {
Expand Down
36 changes: 28 additions & 8 deletions lwk/lwkwallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,44 @@ import (

func TestSatPerKVByteFromFeeBTCPerKb(t *testing.T) {
t.Parallel()
t.Run("below minimum minimumSatPerByte", func(t *testing.T) {
t.Run("above minimumSatPerByte", func(t *testing.T) {
t.Parallel()
var (
txsize int64 = 1000
FeeBTCPerKb = 0.0000001
FeeBTCPerKb = 0.0001
)
got := lwk.SatPerKVByteFromFeeBTCPerKb(FeeBTCPerKb).GetFee(txsize)
want := lwk.Satoshi(100)
got := lwk.SatPerVByteFromFeeBTCPerKb(FeeBTCPerKb)
assert.Equal(t, 10.0, float64(got))
})
t.Run("below minimumSatPerByte", func(t *testing.T) {
t.Parallel()
var (
FeeBTCPerKb = 0.0000002
)
got := lwk.SatPerVByteFromFeeBTCPerKb(FeeBTCPerKb)
assert.Equal(t, 0.1, float64(got))
})
}

func TestGetFee(t *testing.T) {
t.Parallel()
t.Run("above minimumSatPerByte", func(t *testing.T) {
t.Parallel()
var (
txsize int64 = 250
FeeBTCPerKb = 0.0001
)
got := lwk.SatPerVByteFromFeeBTCPerKb(FeeBTCPerKb).GetFee(txsize)
want := lwk.Satoshi(2500)
assert.Equal(t, want, got)
})
t.Run("above minimum minimumSatPerByte", func(t *testing.T) {
t.Parallel()
var (
txsize int64 = 1000
FeeBTCPerKb = 0.000002
FeeBTCPerKb = 0.0000002
)
got := lwk.SatPerKVByteFromFeeBTCPerKb(FeeBTCPerKb).GetFee(txsize)
want := lwk.Satoshi(200)
got := lwk.SatPerVByteFromFeeBTCPerKb(FeeBTCPerKb).GetFee(txsize)
want := lwk.Satoshi(100)
assert.Equal(t, want, got)
})
}

0 comments on commit dfb4967

Please sign in to comment.