2019-03-07 02:20:51 +00:00
|
|
|
package swap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2019-03-07 02:20:51 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2019-07-31 08:46:51 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2021-12-01 15:50:54 +00:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-03-07 02:20:51 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// DecodeInvoice gets the destination, hash and the amount of an invoice.
|
2019-07-31 08:46:51 +00:00
|
|
|
// 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) {
|
2019-03-07 02:20:51 +00:00
|
|
|
|
|
|
|
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
|
2019-03-07 02:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if swapPayReq.MilliSat == nil {
|
2021-12-01 16:04:40 +00:00
|
|
|
return route.Vertex{}, nil, lntypes.Hash{}, 0,
|
2021-12-01 15:50:54 +00:00
|
|
|
errors.New("no amount in invoice")
|
2019-03-07 02:20:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:46:51 +00:00
|
|
|
var hash lntypes.Hash
|
|
|
|
copy(hash[:], swapPayReq.PaymentHash[:])
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
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
|
2019-03-07 02:20:51 +00:00
|
|
|
}
|