-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
96 lines (89 loc) · 2.91 KB
/
calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const {
TO_DATE,
FROM_DATE,
FILENAME_IN,
FILENAME_OUT,
PRICE_FILE_NAME
} = require('./config')
var fs = require('fs');
var getPriceForTime = require('./prices');
const BigNumber = require('bignumber.js');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: `./${FILENAME_OUT}`,
header: [
{id: 'type', title: 'Type'},
{id: 'buy', title: 'Buy'},
{id: 'buycur', title: 'Cur.'},
{id: 'sell', title: 'Sell'},
{id: 'sellcur', title: 'Cur.'},
{id: 'fee', title: 'Fee'},
{id: 'feecur', title: 'Cur.'},
{id: 'exchange', title: 'Exchange'},
{id: 'group', title: 'Group'},
{id: 'comment', title: 'Comment'},
{id: 'date', title: 'Date'},
{id: 'usdvalue', title: 'Value in USD'}
]
});
console.log("Loading prices...")
fs.readFile(PRICE_FILE_NAME, 'utf8', (err, priceData) => {
if (err) {
console.warn("Error reading price data")
console.warn(err); return 0
}
if (!priceData) {console.warn("No price data"); return 0}
console.log("Prices loaded.")
const prices = JSON.parse(priceData)
console.log("Loading transactions...")
fs.readFile(FILENAME_IN, 'utf8', (err, txData) => {
if (err) {
console.warn("Error reading tx data")
console.warn(err); return 0
}
if (!txData) {console.warn("No tx data"); return 0}
console.log("Transactions loaded.")
const transactions = JSON.parse(txData)
let results = []
let totalVrsc = 0
let totalUsd = 0
console.log("\nSaving Coinbase Txs...")
transactions.forEach((tx) => {
if ((tx.category === 'generate' || tx.category === 'mint')
&& Number(tx.blocktime) < TO_DATE
&& Number(tx.blocktime) > FROM_DATE) {
let txDate = new Date(tx.blocktime*1000)
let toPush = {
type: "Mining",
buy: tx.amount,
buycur: "VRSC",
sell: "",
sellcur: "",
fee: "",
feecur: "",
exchange: "",
group: "",
comment: tx.category === "mint" ? "Staking" : "Mining",
date: `${txDate.getUTCFullYear()}-${
txDate.getUTCMonth() + 1
}-${txDate.getUTCDate()} ${txDate.getUTCHours()}:${txDate.getUTCMinutes()}:${txDate.getUTCSeconds()}`,
usdvalue: (BigNumber(getPriceForTime(prices, tx.blocktime)).multipliedBy(BigNumber(tx.amount))).toNumber(),
};
totalUsd += toPush.usdvalue
totalVrsc += tx.amount
results.push(toPush)
}
})
csvWriter.writeRecords(results)
.then(() => {
console.log(`Recorded ${results.length} staking/mining transaction(s)!`)
console.log(`\nTotal VRSC: ${totalVrsc}`)
console.log(`Total USD: ${totalUsd}`)
console.log(`Wrote detailed outputs to ${FILENAME_OUT}\n`)
})
.catch(err => {
console.log("\nError writing file:")
console.log(err.message + '\n')
})
});
})