Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions accounting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ type CommonConfig struct {
// data api may be down.
DisableFiat bool

// FiatBackend is the backend API to be used to for any fiat related
// queries.
FiatBackend fiat.PriceBackend

// Granularity specifies the level of granularity with which we want to
// get fiat prices.
Granularity *fiat.Granularity
// PriceSourceCfg is the config to be used for initialising the
// PriceSource used for fiat related queries.
PriceSourceCfg *fiat.PriceSourceConfig

// Categories is a set of custom categories which should be added to the
// report.
Expand All @@ -104,7 +100,7 @@ type CommonConfig struct {
// that fee lookups are not possible in certain cases.
func NewOnChainConfig(ctx context.Context, lnd lndclient.LndServices, startTime,
endTime time.Time, disableFiat bool, txLookup fees.GetDetailsFunc,
fiatBackend fiat.PriceBackend, granularity *fiat.Granularity,
priceCfg *fiat.PriceSourceConfig,
categories []CustomCategory) *OnChainConfig {

var getFee func(chainhash.Hash) (btcutil.Amount, error)
Expand All @@ -131,12 +127,11 @@ func NewOnChainConfig(ctx context.Context, lnd lndclient.LndServices, startTime,
return lnd.WalletKit.ListSweeps(ctx)
},
CommonConfig: CommonConfig{
StartTime: startTime,
EndTime: endTime,
DisableFiat: disableFiat,
FiatBackend: fiatBackend,
Granularity: granularity,
Categories: categories,
StartTime: startTime,
EndTime: endTime,
DisableFiat: disableFiat,
Categories: categories,
PriceSourceCfg: priceCfg,
},
GetFee: getFee,
}
Expand All @@ -148,7 +143,7 @@ func NewOnChainConfig(ctx context.Context, lnd lndclient.LndServices, startTime,
func NewOffChainConfig(ctx context.Context, lnd lndclient.LndServices,
maxInvoices, maxPayments, maxForwards uint64, ownPubkey route.Vertex,
startTime, endTime time.Time, disableFiat bool,
fiatBackend fiat.PriceBackend, granularity *fiat.Granularity,
priceCfg *fiat.PriceSourceConfig,
categories []CustomCategory) *OffChainConfig {

return &OffChainConfig{
Expand Down Expand Up @@ -177,12 +172,11 @@ func NewOffChainConfig(ctx context.Context, lnd lndclient.LndServices,
},
OwnPubKey: ownPubkey,
CommonConfig: CommonConfig{
StartTime: startTime,
EndTime: endTime,
DisableFiat: disableFiat,
FiatBackend: fiatBackend,
Granularity: granularity,
Categories: categories,
StartTime: startTime,
EndTime: endTime,
DisableFiat: disableFiat,
Categories: categories,
PriceSourceCfg: priceCfg,
},
}
}
15 changes: 7 additions & 8 deletions accounting/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/lightninglabs/faraday/utils"
)

// usdPrice is a function which gets the USD price of bitcoin at a given time.
type usdPrice func(timestamp time.Time) (*fiat.USDPrice, error)
// fiatPrice is a function which gets the fiat price of bitcoin at a given time.
type fiatPrice func(timestamp time.Time) (*fiat.Price, error)

// satsToMsat converts an amount expressed in sats to msat.
func satsToMsat(sats btcutil.Amount) int64 {
Expand All @@ -33,14 +33,13 @@ func invertMsat(msat int64) int64 {
// of price data and returns a convert function which can be used to get
// individual price points from this data.
func getConversion(ctx context.Context, startTime, endTime time.Time,
disableFiat bool, fiatBackend fiat.PriceBackend,
granularity *fiat.Granularity) (usdPrice, error) {
disableFiat bool, priceCfg *fiat.PriceSourceConfig) (fiatPrice, error) {

// If we don't want fiat values, just return a price which will yield
// a zero price and timestamp.
if disableFiat {
return func(_ time.Time) (*fiat.USDPrice, error) {
return &fiat.USDPrice{}, nil
return func(_ time.Time) (*fiat.Price, error) {
return &fiat.Price{}, nil
}, nil
}

Expand All @@ -49,7 +48,7 @@ func getConversion(ctx context.Context, startTime, endTime time.Time,
return nil, err
}

fiatClient, err := fiat.NewPriceSource(fiatBackend, granularity)
fiatClient, err := fiat.NewPriceSource(priceCfg)
if err != nil {
return nil, err
}
Expand All @@ -64,7 +63,7 @@ func getConversion(ctx context.Context, startTime, endTime time.Time,

// Create a wrapper function which can be used to get individual price
// points from our set of price data as we create our report.
return func(ts time.Time) (*fiat.USDPrice, error) {
return func(ts time.Time) (*fiat.Price, error) {
return fiat.GetPrice(prices, ts)
}, nil
}
4 changes: 2 additions & 2 deletions accounting/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type entryUtils struct {
// may be nil.
getFee getFeeFunc

// getFiat provides a USD price for the btc value provided at its
// getFiat provides a fiat price for the btc value provided at its
// timestamp.
getFiat usdPrice
getFiat fiatPrice

// customCategories is a set of custom categories which are set for the
// report.
Expand Down
30 changes: 15 additions & 15 deletions accounting/entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ var (

mockPriceTimestamp = time.Unix(1594306589, 0)

mockBTCPrice = &fiat.USDPrice{
mockBTCPrice = &fiat.Price{
Timestamp: mockPriceTimestamp,
Price: decimal.NewFromInt(100000),
}
Expand All @@ -178,7 +178,7 @@ var (
)

// mockPrice is a mocked price function which returns mockPrice * amount.
func mockPrice(_ time.Time) (*fiat.USDPrice, error) {
func mockPrice(_ time.Time) (*fiat.Price, error) {
return mockBTCPrice, nil
}

Expand Down Expand Up @@ -213,7 +213,7 @@ func TestChannelOpenEntry(t *testing.T) {
return &HarmonyEntry{
Timestamp: transactionTimestamp,
Amount: amtMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, amtMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, amtMsat),
TxID: openChannelTx,
Reference: fmt.Sprintf("%v", channelID),
Note: note,
Expand All @@ -232,7 +232,7 @@ func TestChannelOpenEntry(t *testing.T) {
feeEntry := &HarmonyEntry{
Timestamp: transactionTimestamp,
Amount: msatAmt,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, msatAmt),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, msatAmt),
TxID: openChannelTx,
Reference: FeeReference(openChannelTx),
Note: channelOpenFeeNote(channelID),
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestChannelCloseEntry(t *testing.T) {
chanEntry := &HarmonyEntry{
Timestamp: closeTimestamp,
Amount: amtMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, amtMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, amtMsat),
TxID: closeTx,
Reference: closeTx,
Note: note,
Expand All @@ -332,7 +332,7 @@ func TestChannelCloseEntry(t *testing.T) {
feeEntry := &HarmonyEntry{
Timestamp: closeTimestamp,
Amount: mockFeeMSat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, mockFeeMSat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, mockFeeMSat),
TxID: closeTx,
Reference: FeeReference(closeTx),
Note: "",
Expand Down Expand Up @@ -432,7 +432,7 @@ func TestSweepEntry(t *testing.T) {
sweepEntry := &HarmonyEntry{
Timestamp: onChainTimestamp,
Amount: amtMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, amtMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, amtMsat),
TxID: onChainTxID,
Reference: onChainTxID,
Note: "",
Expand All @@ -447,7 +447,7 @@ func TestSweepEntry(t *testing.T) {
{
Timestamp: onChainTimestamp,
Amount: mockFeeMSat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, mockFeeMSat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, mockFeeMSat),
TxID: onChainTxID,
Reference: FeeReference(onChainTxID),
Note: "",
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestOnChainEntry(t *testing.T) {
entry := &HarmonyEntry{
Timestamp: onChainTimestamp,
Amount: amtMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, amtMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, amtMsat),
TxID: onChainTxID,
Reference: onChainTxID,
Note: label,
Expand All @@ -548,7 +548,7 @@ func TestOnChainEntry(t *testing.T) {
feeEntry := &HarmonyEntry{
Timestamp: onChainTimestamp,
Amount: feeMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, feeMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, feeMsat),
TxID: onChainTxID,
Reference: FeeReference(onChainTxID),
Note: "",
Expand Down Expand Up @@ -646,7 +646,7 @@ func TestInvoiceEntry(t *testing.T) {
expectedEntry := &HarmonyEntry{
Timestamp: invoiceSettleTime,
Amount: invoiceOverpaidAmt,
FiatValue: fiat.MsatToUSD(
FiatValue: fiat.MsatToFiat(
mockBTCPrice.Price, invoiceOverpaidAmt,
),
TxID: invoiceHash,
Expand Down Expand Up @@ -713,7 +713,7 @@ func TestPaymentEntry(t *testing.T) {
paymentEntry := &HarmonyEntry{
Timestamp: paymentTime,
Amount: amtMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, amtMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, amtMsat),
TxID: paymentHash,
Reference: paymentRef,
Note: paymentNote(&otherPubkey),
Expand All @@ -728,7 +728,7 @@ func TestPaymentEntry(t *testing.T) {
feeEntry := &HarmonyEntry{
Timestamp: paymentTime,
Amount: feeMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, feeMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, feeMsat),
TxID: paymentHash,
Reference: FeeReference(paymentRef),
Note: paymentNote(&otherPubkey),
Expand Down Expand Up @@ -789,7 +789,7 @@ func TestForwardingEntry(t *testing.T) {
fwdEntry := &HarmonyEntry{
Timestamp: forwardTs,
Amount: 0,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, 0),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, 0),
TxID: txid,
Reference: "",
Note: note,
Expand All @@ -802,7 +802,7 @@ func TestForwardingEntry(t *testing.T) {
feeEntry := &HarmonyEntry{
Timestamp: forwardTs,
Amount: fwdFeeMsat,
FiatValue: fiat.MsatToUSD(mockBTCPrice.Price, fwdFeeMsat),
FiatValue: fiat.MsatToFiat(mockBTCPrice.Price, fwdFeeMsat),
TxID: txid,
Reference: "",
Note: "",
Expand Down
4 changes: 2 additions & 2 deletions accounting/off_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func OffChainReport(ctx context.Context, cfg *OffChainConfig) (Report, error) {
// or a no-op function if we do not want prices.
getPrice, err := getConversion(
ctx, cfg.StartTime, cfg.EndTime, cfg.DisableFiat,
cfg.FiatBackend, cfg.Granularity,
cfg.PriceSourceCfg,
)
if err != nil {
return nil, err
Expand All @@ -57,7 +57,7 @@ func OffChainReport(ctx context.Context, cfg *OffChainConfig) (Report, error) {
// offChainReportWithPrices produces off chain reports using the getPrice
// function provided. This allows testing of our report creation without calling
// the actual price API.
func offChainReportWithPrices(cfg *OffChainConfig, getPrice usdPrice) (Report,
func offChainReportWithPrices(cfg *OffChainConfig, getPrice fiatPrice) (Report,
error) {

invoices, err := cfg.ListInvoices()
Expand Down
4 changes: 2 additions & 2 deletions accounting/on_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func OnChainReport(ctx context.Context, cfg *OnChainConfig) (Report, error) {
// or a no-op function if we do not want prices.
getPrice, err := getConversion(
ctx, cfg.StartTime, cfg.EndTime, cfg.DisableFiat,
cfg.FiatBackend, cfg.Granularity,
cfg.PriceSourceCfg,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,7 +77,7 @@ func newChannelInfo(id lnwire.ShortChannelID, chanPoint *wire.OutPoint,
// getOnChainInfo queries lnd for all transactions relevant to our on chain
// transactions, and produces the set of information that we will need to create
// an on chain report.
func getOnChainInfo(cfg *OnChainConfig, getPrice usdPrice) (*onChainInformation,
func getOnChainInfo(cfg *OnChainConfig, getPrice fiatPrice) (*onChainInformation,
error) {

// Create an info struct to hold all the elements we need.
Expand Down
6 changes: 3 additions & 3 deletions accounting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type HarmonyEntry struct {

// BTCPrice is the timestamped bitcoin price we used to get our fiat
// value.
BTCPrice *fiat.USDPrice
BTCPrice *fiat.Price
}

// newHarmonyEntry produces a harmony entry. If provided with a negative amount,
Expand All @@ -63,7 +63,7 @@ type HarmonyEntry struct {
// a credit.
func newHarmonyEntry(ts time.Time, amountMsat int64, e EntryType, txid,
reference, note, category string, onChain bool,
convert usdPrice) (*HarmonyEntry,
convert fiatPrice) (*HarmonyEntry,

error) {

Expand All @@ -86,7 +86,7 @@ func newHarmonyEntry(ts time.Time, amountMsat int64, e EntryType, txid,
return &HarmonyEntry{
Timestamp: ts,
Amount: amtMsat,
FiatValue: fiat.MsatToUSD(btcPrice.Price, amtMsat),
FiatValue: fiat.MsatToFiat(btcPrice.Price, amtMsat),
TxID: txid,
Reference: reference,
Note: note,
Expand Down
Loading