2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-09 19:10:47 +00:00
loop/swap/fees.go

58 lines
1.6 KiB
Go
Raw Normal View History

package swap
import (
"errors"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/zpay32"
)
const (
// FeeRateTotalParts defines the granularity of the fee rate.
// Throughout the codebase, we'll use fix based arithmetic to compute
// fees.
FeeRateTotalParts = 1e6
)
// CalcFee returns the swap fee for a given swap amount.
func CalcFee(amount, feeBase btcutil.Amount, feeRate int64) btcutil.Amount {
return feeBase + amount*btcutil.Amount(feeRate)/
btcutil.Amount(FeeRateTotalParts)
}
// FeeRateAsPercentage converts a feerate to a percentage.
func FeeRateAsPercentage(feeRate int64) float64 {
return float64(feeRate) / (FeeRateTotalParts / 100)
}
// DecodeInvoice gets the destination, hash and the amount of an invoice.
// It requires an amount to be specified.
2021-12-01 16:04:40 +00:00
func DecodeInvoice(params *chaincfg.Params, payReq string) (route.Vertex,
[][]zpay32.HopHint, lntypes.Hash, btcutil.Amount, error) {
swapPayReq, err := zpay32.Decode(
payReq, params,
)
if err != nil {
2021-12-01 16:04:40 +00:00
return route.Vertex{}, nil, lntypes.Hash{}, 0, err
}
if swapPayReq.MilliSat == nil {
2021-12-01 16:04:40 +00:00
return route.Vertex{}, nil, lntypes.Hash{}, 0,
errors.New("no amount in invoice")
}
var hash lntypes.Hash
copy(hash[:], swapPayReq.PaymentHash[:])
var destination route.Vertex
destPubKey := swapPayReq.Destination.SerializeCompressed()
copy(destination[:], destPubKey)
2021-12-01 16:04:40 +00:00
return destination, swapPayReq.RouteHints, hash,
swapPayReq.MilliSat.ToSatoshis(), nil
}