114 lines
2.8 KiB
JavaScript
114 lines
2.8 KiB
JavaScript
const csvToJson = require('convert-csv-to-json')
|
|
const converter = require('json-2-csv')
|
|
const fs = require('fs')
|
|
const BN = require('bignumber.js')
|
|
|
|
const txs = csvToJson.fieldDelimiter(',').getJsonFromCsv(`../Ledger.csv`)
|
|
let lots = []
|
|
let dispositions = []
|
|
|
|
const handleBuy = (tx, i) => {
|
|
const { Date: date, Asset, Received, Price } = tx
|
|
lots.push({
|
|
Date: date,
|
|
Id: i,
|
|
Asset,
|
|
Amount: Received,
|
|
Price,
|
|
Remaining: new BN(Received),
|
|
})
|
|
}
|
|
|
|
const createDisposition = (tx, lot, amount) => {
|
|
const st = new Date(tx.Date).valueOf() - new Date(lot.Date).valueOf() < 31556926000
|
|
const costBasis = amount.times(lot.Price)
|
|
const proceeds = amount.times(tx.Price)
|
|
const gainLoss = proceeds.minus(costBasis)
|
|
const disposition = {
|
|
Date: tx.Date,
|
|
Asset: tx.Asset,
|
|
Lot: lot.Id,
|
|
Amount: amount.toFixed(8),
|
|
PurchasePrice: lot.Price,
|
|
DateAcquired: lot.Date,
|
|
SalePrice: tx.Price,
|
|
CostBasis: costBasis.toFixed(2),
|
|
Proceeds: proceeds.toFixed(2),
|
|
GainLoss: gainLoss.toFixed(2),
|
|
ShortTerm: st ? 'true' : '',
|
|
Tax: gainLoss.gt(0) ? gainLoss.times(st ? .24 : .15).toFixed(2) : 0
|
|
}
|
|
dispositions.push(disposition)
|
|
}
|
|
|
|
const consumeLots = (tx) => {
|
|
for (let i = 0; i < lots.length; i++) {
|
|
let lot = lots[i]
|
|
|
|
if (!lot) {
|
|
console.error(`WARNING: No lot available! Assuming $0 cost basis:\n-Date:${tx.Date}\n-Asset: ${tx.Asset}\n-Sent: ${tx.Sent}\n-Remaining: ${tx.Remaining.toFixed(8)}\n`)
|
|
lot = {
|
|
Date: '2017-01-01',
|
|
Id: -1,
|
|
Asset: tx.Asset,
|
|
Amount: tx.Remaining.toFixed(8),
|
|
Price: '0',
|
|
Remaining: tx.Remaining,
|
|
}
|
|
}
|
|
|
|
if (lot.Asset !== tx.Asset || lot.Remaining.isZero()) continue
|
|
|
|
if (lot.Remaining.gte(tx.Remaining)) {
|
|
createDisposition(tx, lot, tx.Remaining)
|
|
lot.Remaining = lot.Remaining.minus(tx.Remaining)
|
|
return
|
|
} else {
|
|
createDisposition(tx, lot, lot.Remaining)
|
|
tx.Remaining = tx.Remaining.minus(lot.Remaining)
|
|
lot.Remaining = new BN(0)
|
|
return consumeLots(tx)
|
|
}
|
|
}
|
|
}
|
|
|
|
txs.forEach((tx, i) => {
|
|
if (tx.Received) return handleBuy(tx, i)
|
|
tx.Remaining = new BN(tx.Sent)
|
|
return consumeLots(tx)
|
|
})
|
|
|
|
let balances = lots.reduce((prev, curr) => {
|
|
const asset = curr.Asset
|
|
if (prev[asset]) {
|
|
prev[asset] = prev[asset].plus(curr.Remaining)
|
|
} else {
|
|
prev[asset] = curr.Remaining
|
|
}
|
|
return prev
|
|
}, {})
|
|
|
|
lots = lots.map(u => ({
|
|
...u,
|
|
Remaining: u.Remaining.toFixed(8),
|
|
}))
|
|
|
|
balances = Object.keys(balances)
|
|
.filter(asset => !balances[asset].isZero())
|
|
.map(asset => ({
|
|
Asset: asset,
|
|
balance: balances[asset].toFixed(8)
|
|
}))
|
|
|
|
converter.json2csv(lots).then(csv =>
|
|
fs.appendFileSync(`./lots.csv`, csv)
|
|
)
|
|
|
|
converter.json2csv(dispositions).then(csv =>
|
|
fs.appendFileSync(`./dispositions.csv`, csv)
|
|
)
|
|
|
|
converter.json2csv(balances).then(csv =>
|
|
fs.appendFileSync(`./balances.csv`, csv)
|
|
)
|