Skip to content

Commit

Permalink
fixup! fixup! fixup! lwk: lwk package
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeShimizu committed Mar 29, 2024
1 parent 0466594 commit 8603558
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
5 changes: 3 additions & 2 deletions lwk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ type unvalidatedAddressee struct {

type sendRequest struct {
Addressees []*unvalidatedAddressee `json:"addressees"`
FeeRate *float64 `json:"fee_rate,omitempty"`
WalletName string `json:"name"`
// Optional fee rate in sat/vb
FeeRate *float64 `json:"fee_rate,omitempty"`
WalletName string `json:"name"`
}

type sendResponse struct {
Expand Down
48 changes: 25 additions & 23 deletions lwk/lwkwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"log"
"math"

"github.com/checksum0/go-electrum/electrum"

Expand All @@ -13,9 +14,14 @@ import (
"github.com/vulpemventures/go-elements/psetv2"
)

// Satoshi represents a satoshi value.
type Satoshi = uint64

// SatPerKVByte represents a fee rate in sat/kb.
type SatPerKVByte = uint64

const (
kiloByte = 1000
minimumSatPerByte = 0.2
minimumSatPerByte = 200
)

// LWKRpcWallet uses the elementsd rpc wallet
Expand Down Expand Up @@ -67,16 +73,9 @@ func (r *LWKRpcWallet) setupWallet() error {

// CreateFundedTransaction takes a tx with outputs and adds inputs in order to spend the tx
func (r *LWKRpcWallet) CreateAndBroadcastTransaction(swapParams *swap.OpeningParams,
asset []byte) (txid, rawTx string, fee uint64, err error) {
asset []byte) (txid, rawTx string, fee SatPerKVByte, err error) {
ctx := context.Background()
feeRes, err := r.electrumClient.GetFee(ctx, wallet.LiquidTargetBlocks)
satPerByte := float64(feeRes) / float64(kiloByte)
if err != nil {
satPerByte = minimumSatPerByte
}
if satPerByte < minimumSatPerByte {
satPerByte = minimumSatPerByte
}
feerate := float64(r.getFeePerKb(ctx))
fundedTx, err := r.lwkClient.send(ctx, &sendRequest{
Addressees: []*unvalidatedAddressee{
{
Expand All @@ -85,7 +84,7 @@ func (r *LWKRpcWallet) CreateAndBroadcastTransaction(swapParams *swap.OpeningPar
},
},
WalletName: r.walletName,
FeeRate: &satPerByte,
FeeRate: &feerate,
})
if err != nil {
return "", "", 0, err
Expand Down Expand Up @@ -120,11 +119,11 @@ func (r *LWKRpcWallet) CreateAndBroadcastTransaction(swapParams *swap.OpeningPar
if err != nil {
return "", "", 0, err
}
return broadcasted.Txid, txhex, 0, nil
return broadcasted.Txid, txhex, SatPerKVByte(feerate), nil
}

// GetBalance returns the balance in sats
func (r *LWKRpcWallet) GetBalance() (uint64, error) {
func (r *LWKRpcWallet) GetBalance() (Satoshi, error) {
ctx := context.Background()
balance, err := r.lwkClient.balance(ctx, &balanceRequest{
WalletName: r.walletName,
Expand All @@ -147,7 +146,7 @@ func (r *LWKRpcWallet) GetAddress() (string, error) {
}

// SendToAddress sends an amount to an address
func (r *LWKRpcWallet) SendToAddress(address string, amount uint64) (string, error) {
func (r *LWKRpcWallet) SendToAddress(address string, amount Satoshi) (string, error) {
ctx := context.Background()
sendres, err := r.lwkClient.send(ctx, &sendRequest{
WalletName: r.walletName,
Expand Down Expand Up @@ -188,21 +187,24 @@ func (r *LWKRpcWallet) SendRawTx(txHex string) (string, error) {
return res, nil
}

func (r *LWKRpcWallet) GetFee(txSize int64) (uint64, error) {
ctx := context.Background()
feeRes, err := r.electrumClient.GetFee(ctx, wallet.LiquidTargetBlocks)

satPerByte := float64(feeRes) / float64(kiloByte)
func (r *LWKRpcWallet) getFeePerKb(ctx context.Context) SatPerKVByte {
feeBTCPerKb, err := r.electrumClient.GetFee(ctx, wallet.LiquidTargetBlocks)
// convert to sat per byte
satPerByte := uint64(float64(feeBTCPerKb) * math.Pow10(int(8)))
if satPerByte < minimumSatPerByte {
satPerByte = minimumSatPerByte
}
if err != nil {
satPerByte = minimumSatPerByte
}
// assume largest witness
fee := satPerByte * float64(txSize)
return satPerByte
}

return uint64(fee), nil
func (r *LWKRpcWallet) GetFee(txSize int64) (Satoshi, error) {
ctx := context.Background()
// assume largest witness
fee := r.getFeePerKb(ctx) * uint64(txSize)
return fee, nil
}

func (r *LWKRpcWallet) SetLabel(txID, address, label string) error {
Expand Down

0 comments on commit 8603558

Please sign in to comment.