2020-05-20 15:18:46 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2021-06-09 14:19:59 +02:00
|
|
|
"encoding/csv"
|
|
|
|
|
"errors"
|
2020-05-20 15:18:46 +02:00
|
|
|
"fmt"
|
2021-06-09 14:19:59 +02:00
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2020-05-20 15:18:46 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/lightninglabs/faraday/frdrpc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CSVHeaders returns the headers used for harmony csv records.
|
2021-06-09 12:13:03 +02:00
|
|
|
var CSVHeaders = "Timestamp,OnChain,Type,Category,Amount(Msat),Amount(%v),TxID,Reference,BTCPrice,BTCTimestamp,Note"
|
2020-05-20 15:18:46 +02:00
|
|
|
|
2021-06-09 14:12:10 +02:00
|
|
|
// writeToCSV returns a csv string of the values contained in a rpc entry. For ease
|
2020-05-20 15:18:46 +02:00
|
|
|
// of use, the credit field is used to set a negative sign (-) on the amount
|
|
|
|
|
// of an entry when it decreases our balance (credit=false).
|
2021-06-09 14:12:10 +02:00
|
|
|
func writeToCSV(e *frdrpc.ReportEntry) string {
|
2020-05-20 15:18:46 +02:00
|
|
|
amountPrefix := ""
|
|
|
|
|
if !e.Credit {
|
|
|
|
|
amountPrefix = "-"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts := time.Unix(int64(e.Timestamp), 0)
|
|
|
|
|
|
2020-10-15 11:29:27 +02:00
|
|
|
return fmt.Sprintf("%v,%v,%v,%v,%v%v,%v%v,%v,%v,%v,%v,%v",
|
|
|
|
|
ts, e.OnChain, e.Type, e.CustomCategory, amountPrefix, e.Amount,
|
|
|
|
|
amountPrefix, e.Fiat, e.Txid, e.Reference, e.BtcPrice.Price,
|
2020-07-09 17:15:57 +02:00
|
|
|
e.BtcPrice.PriceTimestamp, e.Note)
|
2020-05-20 15:18:46 +02:00
|
|
|
}
|
2021-06-09 14:19:59 +02:00
|
|
|
|
|
|
|
|
// parsePricesFromCSV reads price point data from the csv at the specified path.
|
|
|
|
|
// This function expects the first csv line to be headers and expects the rest
|
|
|
|
|
// of the lines to be tuples of the following format:
|
|
|
|
|
// 'unix tx seconds, price of 1 BTC in chosen currency'.
|
|
|
|
|
func parsePricesFromCSV(path, currency string) ([]*frdrpc.BitcoinPrice, error) {
|
|
|
|
|
if path == "" || currency == "" {
|
|
|
|
|
return nil, errors.New("custom price csv path and " +
|
|
|
|
|
"currency must both be specified")
|
|
|
|
|
}
|
|
|
|
|
csvFile, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer csvFile.Close()
|
|
|
|
|
|
|
|
|
|
csvLines, err := csv.NewReader(csvFile).ReadAll()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(csvLines) < 2 {
|
|
|
|
|
return nil, errors.New("no price points found in CSV")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip the first line in the CSV file since we expect this line
|
|
|
|
|
// to contain column headers.
|
|
|
|
|
csvLines = csvLines[1:]
|
|
|
|
|
|
|
|
|
|
prices := make([]*frdrpc.BitcoinPrice, len(csvLines))
|
|
|
|
|
|
|
|
|
|
for i, line := range csvLines {
|
|
|
|
|
if len(line) != 2 {
|
|
|
|
|
return nil, errors.New("incorrect csv format. " +
|
|
|
|
|
"Two columns items are expected per row")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timestamp, err := strconv.ParseInt(line[0], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prices[i] = &frdrpc.BitcoinPrice{
|
|
|
|
|
PriceTimestamp: uint64(timestamp),
|
|
|
|
|
Price: line[1],
|
|
|
|
|
Currency: currency,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return prices, nil
|
|
|
|
|
}
|