update for 2024
This commit is contained in:
@@ -4,12 +4,12 @@ const fs = require('fs')
|
||||
const BN = require('bignumber.js')
|
||||
|
||||
const txs = csvToJson.fieldDelimiter(',').getJsonFromCsv(`../Ledger.csv`)
|
||||
let utxos = []
|
||||
let lots = []
|
||||
let dispositions = []
|
||||
|
||||
const handleBuy = (tx, i) => {
|
||||
const { Date: date, Asset, Received, Price } = tx
|
||||
utxos.push({
|
||||
lots.push({
|
||||
Date: date,
|
||||
Id: i,
|
||||
Asset,
|
||||
@@ -19,18 +19,18 @@ const handleBuy = (tx, i) => {
|
||||
})
|
||||
}
|
||||
|
||||
const createDisposition = (tx, utxo, amount) => {
|
||||
const st = new Date(tx.Date).valueOf() - new Date(utxo.Date).valueOf() < 31556926000
|
||||
const costBasis = amount.times(utxo.Price)
|
||||
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,
|
||||
Utxo: utxo.Id,
|
||||
Lot: lot.Id,
|
||||
Amount: amount.toFixed(8),
|
||||
PurchasePrice: utxo.Price,
|
||||
DateAcquired: utxo.Date,
|
||||
PurchasePrice: lot.Price,
|
||||
DateAcquired: lot.Date,
|
||||
SalePrice: tx.Price,
|
||||
CostBasis: costBasis.toFixed(2),
|
||||
Proceeds: proceeds.toFixed(2),
|
||||
@@ -41,13 +41,13 @@ const createDisposition = (tx, utxo, amount) => {
|
||||
dispositions.push(disposition)
|
||||
}
|
||||
|
||||
const consumeUtxos = (tx) => {
|
||||
for (let i = 0; i < utxos.length; i++) {
|
||||
let utxo = utxos[i]
|
||||
const consumeLots = (tx) => {
|
||||
for (let i = 0; i < lots.length; i++) {
|
||||
let lot = lots[i]
|
||||
|
||||
if (!utxo) {
|
||||
console.error(`WARNING: No utxo available! Assuming $0 cost basis:\n-Date:${tx.Date}\n-Asset: ${tx.Asset}\n-Sent: ${tx.Sent}\n-Remaining: ${tx.Remaining.toFixed(8)}\n`)
|
||||
utxo = {
|
||||
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,
|
||||
@@ -57,17 +57,17 @@ const consumeUtxos = (tx) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (utxo.Asset !== tx.Asset || utxo.Remaining.isZero()) continue
|
||||
if (lot.Asset !== tx.Asset || lot.Remaining.isZero()) continue
|
||||
|
||||
if (utxo.Remaining.gte(tx.Remaining)) {
|
||||
createDisposition(tx, utxo, tx.Remaining)
|
||||
utxo.Remaining = utxo.Remaining.minus(tx.Remaining)
|
||||
if (lot.Remaining.gte(tx.Remaining)) {
|
||||
createDisposition(tx, lot, tx.Remaining)
|
||||
lot.Remaining = lot.Remaining.minus(tx.Remaining)
|
||||
return
|
||||
} else {
|
||||
createDisposition(tx, utxo, utxo.Remaining)
|
||||
tx.Remaining = tx.Remaining.minus(utxo.Remaining)
|
||||
utxo.Remaining = new BN(0)
|
||||
return consumeUtxos(tx)
|
||||
createDisposition(tx, lot, lot.Remaining)
|
||||
tx.Remaining = tx.Remaining.minus(lot.Remaining)
|
||||
lot.Remaining = new BN(0)
|
||||
return consumeLots(tx)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,10 +75,10 @@ const consumeUtxos = (tx) => {
|
||||
txs.forEach((tx, i) => {
|
||||
if (tx.Received) return handleBuy(tx, i)
|
||||
tx.Remaining = new BN(tx.Sent)
|
||||
return consumeUtxos(tx)
|
||||
return consumeLots(tx)
|
||||
})
|
||||
|
||||
let balances = utxos.reduce((prev, curr) => {
|
||||
let balances = lots.reduce((prev, curr) => {
|
||||
const asset = curr.Asset
|
||||
if (prev[asset]) {
|
||||
prev[asset] = prev[asset].plus(curr.Remaining)
|
||||
@@ -88,7 +88,7 @@ let balances = utxos.reduce((prev, curr) => {
|
||||
return prev
|
||||
}, {})
|
||||
|
||||
utxos = utxos.map(u => ({
|
||||
lots = lots.map(u => ({
|
||||
...u,
|
||||
Remaining: u.Remaining.toFixed(8),
|
||||
}))
|
||||
@@ -100,8 +100,8 @@ balances = Object.keys(balances)
|
||||
balance: balances[asset].toFixed(8)
|
||||
}))
|
||||
|
||||
converter.json2csv(utxos).then(csv =>
|
||||
fs.appendFileSync(`./utxos.csv`, csv)
|
||||
converter.json2csv(lots).then(csv =>
|
||||
fs.appendFileSync(`./lots.csv`, csv)
|
||||
)
|
||||
|
||||
converter.json2csv(dispositions).then(csv =>
|
||||
|
||||
Reference in New Issue
Block a user