update for 2024

This commit is contained in:
2024-12-15 20:40:15 -07:00
parent ac86e46258
commit 26b9c7f0d5
4 changed files with 94 additions and 94 deletions

View File

@@ -3,15 +3,15 @@ const converter = require('json-2-csv')
const fs = require('fs')
const BN = require('bignumber.js')
const { PDFDocument } = require('pdf-lib')
const year = 2023
const year = 2024
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,
@@ -21,17 +21,17 @@ const handleBuy = (tx, i) => {
})
}
const createDisposition = (tx, utxo, amount, st) => {
const costBasis = amount.times(utxo.Price)
const createDisposition = (tx, lot, amount, st) => {
const costBasis = amount.times(lot.Price)
const proceeds = amount.times(tx.Price)
const gainLoss = proceeds.minus(costBasis)
return {
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,29 +41,29 @@ const createDisposition = (tx, utxo, amount, st) => {
}
}
const consumeUtxos = (tx) => {
const consumeLots = (tx) => {
const copy = []
for (i = 0; i < utxos.length; i++) {
copy[i] = utxos[i];
for (i = 0; i < lots.length; i++) {
copy[i] = lots[i];
}
const sorted = copy
.filter(utxo => utxo.Asset === tx.Asset && !utxo.Remaining.isZero())
.filter(lot => lot.Asset === tx.Asset && !lot.Remaining.isZero())
.sort((a, b) => new BN(b.Price).minus(a.Price).toNumber())
const txDate = new Date(tx.Date).valueOf()
const stUtxo = sorted.find(utxo => txDate - new Date(utxo.Date).valueOf() < 31556926000)
const ltUtxo = sorted.find(utxo => txDate - new Date(utxo.Date).valueOf() >= 31556926000)
const stLot = sorted.find(lot => txDate - new Date(lot.Date).valueOf() < 31556926000)
const ltLot = sorted.find(lot => txDate - new Date(lot.Date).valueOf() >= 31556926000)
// find the best utxo
let utxo
// find the best lot
let lot
// decide st vs lt
let st
if (!stUtxo && !ltUtxo) {
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 (!stLot && !ltLot) {
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-01T12:00:00',
Id: -1,
Asset: tx.Asset,
@@ -71,53 +71,53 @@ const consumeUtxos = (tx) => {
Price: '0',
Remaining: tx.Remaining,
}
} else if (!stUtxo) {
utxo = utxos.find(u => u.Id === ltUtxo.Id)
} else if (!stLot) {
lot = lots.find(u => u.Id === ltLot.Id)
st = false
} else if (!ltUtxo) {
utxo = utxos.find(u => u.Id === stUtxo.Id)
} else if (!ltLot) {
lot = lots.find(u => u.Id === stLot.Id)
st = true
} else {
const { Tax: stTax } = getProvisional(tx, stUtxo, true)
const { Tax: ltTax } = getProvisional(tx, ltUtxo, false)
const { Tax: stTax } = getProvisional(tx, stLot, true)
const { Tax: ltTax } = getProvisional(tx, ltLot, false)
if (new BN(stTax).lt(ltTax)) {
utxo = utxos.find(u => u.Id === stUtxo.Id)
lot = lots.find(u => u.Id === stLot.Id)
st = true
} else {
utxo = utxos.find(u => u.Id === ltUtxo.Id)
lot = lots.find(u => u.Id === ltLot.Id)
st = false
}
}
if (utxo.Remaining.gte(tx.Remaining)) {
const disposition = createDisposition(tx, utxo, tx.Remaining, st)
if (lot.Remaining.gte(tx.Remaining)) {
const disposition = createDisposition(tx, lot, tx.Remaining, st)
dispositions.push(disposition)
utxo.Remaining = utxo.Remaining.minus(tx.Remaining)
lot.Remaining = lot.Remaining.minus(tx.Remaining)
} else {
const disposition = createDisposition(tx, utxo, utxo.Remaining, st)
const disposition = createDisposition(tx, lot, lot.Remaining, st)
dispositions.push(disposition)
tx.Remaining = tx.Remaining.minus(utxo.Remaining)
utxo.Remaining = new BN(0)
consumeUtxos(tx)
tx.Remaining = tx.Remaining.minus(lot.Remaining)
lot.Remaining = new BN(0)
consumeLots(tx)
}
}
const getProvisional = (tx, utxo, st) => {
if (utxo.Remaining.gte(tx.Remaining)) {
return createDisposition(tx, utxo, tx.Remaining, st)
const getProvisional = (tx, lot, st) => {
if (lot.Remaining.gte(tx.Remaining)) {
return createDisposition(tx, lot, tx.Remaining, st)
} else {
return createDisposition(tx, utxo, utxo.Remaining, st)
return createDisposition(tx, lot, lot.Remaining, st)
}
}
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)
@@ -127,7 +127,7 @@ let balances = utxos.reduce((prev, curr) => {
return prev
}, {})
utxos = utxos.map(u => ({
lots = lots.map(u => ({
...u,
Remaining: u.Remaining.toFixed(8),
}))
@@ -147,8 +147,8 @@ converter.json2csv(balances).then(csv =>
fs.appendFileSync(`./balances.csv`, csv)
)
converter.json2csv(utxos).then(csv =>
fs.appendFileSync(`./utxos.csv`, csv)
converter.json2csv(lots).then(csv =>
fs.appendFileSync(`./lots.csv`, csv)
)
// **** 8949 ****