@davidosborn/crypto-tax-calculator
Version:
A tool to calculate the capital gains of cryptocurrency assets for Canadian taxes
83 lines (68 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _stream = _interopRequireDefault(require("stream"));
var _assets = _interopRequireDefault(require("./assets"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* A transaction.
* @typedef {object} Transaction
* @property {string} exchange The exchange on which the transaction was executed.
* @property {string} asset The asset.
* @property {number} amount The amount of assets.
* @property {number} time The time of the transaction, as a UNIX timestamp.
* @property {number} value The value of the transaction, in Canadian dollars.
* @property {number} fee The transaction fee, in Canadian dollars.
*/
/**
* A stream that breaks up assets trades into currency transactions.
*/
class TradeSeparateStream extends _stream.default.Transform {
constructor() {
super({
objectMode: true
});
}
/**
* Breaks up an asset trade into currency transactions.
* @param {Trade} chunk The trade.
* @param {string} encoding The encoding type (always 'Buffer').
* @param {function} callback A callback for when the transformation is complete.
*/
async _transform(chunk, encoding, callback) {
let baseChunk = {
exchange: chunk.exchange,
asset: chunk.baseAsset,
amount: chunk.baseAmount,
time: chunk.time,
value: chunk.value,
fee: 0
};
let quoteChunk = {
exchange: chunk.exchange,
asset: chunk.quoteAsset,
amount: chunk.quoteAmount,
time: chunk.time,
value: chunk.value,
fee: 0
};
let chunks = [baseChunk, quoteChunk];
if (chunk.sell) {
quoteChunk.amount = -quoteChunk.amount;
quoteChunk.fee = chunk.fee;
chunks.reverse();
} else {
baseChunk.amount = -baseChunk.amount;
baseChunk.fee = chunk.fee;
} // Drop the chunks that represent fiat currencies.
// TODO: This is questionable.
for (let i = 0; i < chunks.length; ++i) if (_assets.default.getPriority(chunks[i].asset) === 0) chunks.splice(i--, 1);
for (let chunk of chunks) this.push(chunk);
callback();
}
}
function _default(...args) {
return new TradeSeparateStream(...args);
}