Development of Crypto Tax Calculation System
Crypto tax calculation is a multi-step process: classify events → determine cost basis → apply correct method → account for jurisdiction-specific rules (holding period, tax rates, specific rules). System should do this automatically and be configurable for different jurisdictions.
Tax Events and Their Treatment
const TAX_TREATMENT: Record<string, Record<string, TaxTreatment>> = {
US: {
swap: { type: "capital_gain", description: "Crypto-to-crypto swap is taxable disposal" },
staking_reward: { type: "ordinary_income", description: "Taxable when received (FMV)" },
mining_reward: { type: "ordinary_income" },
airdrop: { type: "ordinary_income", description: "If can be freely traded" },
hard_fork: { type: "ordinary_income" },
nft_sale: { type: "capital_gain" },
defi_liquidity: { type: "complex", description: "Depends on structure" },
},
UK: {
swap: { type: "capital_gain" },
staking_reward: { type: "miscellaneous_income" },
mining_reward: { type: "trading_income_or_miscellaneous" },
airdrop: { type: "capital_gain", description: "Not income unless received for service" },
},
DE: {
swap: { type: "capital_gain", exemptAfterHolding: 365 }, // Germany: tax free after 1 year
staking_reward: { type: "other_income", exemptAmount: 256 }, // €256/year exempt
mining_reward: { type: "business_income" },
},
};
Tax Liability Calculation
class TaxCalculationEngine {
async calculateTaxYear(
userId: string,
taxYear: number,
jurisdiction: string,
method: CostBasisMethod
): Promise<TaxYearSummary> {
const events = await this.db.getTaxEvents(userId, taxYear);
const jurisdictionRules = TAX_TREATMENT[jurisdiction];
let shortTermGains = 0;
let longTermGains = 0;
let ordinaryIncome = 0;
const processedEvents: ProcessedTaxEvent[] = [];
for (const event of events) {
const treatment = jurisdictionRules[event.type];
if (!treatment) continue;
if (treatment.type === "capital_gain") {
const { costBasis, isLongTerm } = await this.getCostBasis(
userId, event, method, jurisdiction
);
const gain = event.proceedsUSD - costBasis;
// German exemption: after 1 year hold, tax free
if (treatment.exemptAfterHolding && isLongTerm) {
processedEvents.push({ ...event, gain, tax: 0, reason: "exempt_long_term" });
continue;
}
if (isLongTerm) longTermGains += gain;
else shortTermGains += gain;
processedEvents.push({ ...event, costBasis, gain, isLongTerm });
} else if (treatment.type === "ordinary_income" || treatment.type === "miscellaneous_income") {
const income = event.usdValueAtReceipt;
ordinaryIncome += income;
processedEvents.push({ ...event, income });
}
}
// Apply tax rates
const taxLiability = this.applyTaxRates(
jurisdiction, taxYear, shortTermGains, longTermGains, ordinaryIncome
);
return {
taxYear,
jurisdiction,
method,
shortTermGains,
longTermGains,
ordinaryIncome,
taxLiability,
events: processedEvents,
};
}
private applyTaxRates(
jurisdiction: string,
year: number,
shortTerm: number,
longTerm: number,
income: number
): TaxLiability {
const rates = TAX_RATES[jurisdiction][year];
switch (jurisdiction) {
case "US":
return {
shortTermTax: shortTerm > 0 ? shortTerm * rates.shortTermRate : 0,
longTermTax: longTerm > 0 ? this.calculateLTCGTax(longTerm, rates) : 0,
incomeTax: income * rates.ordinaryRate,
};
case "DE":
// Germany: Abgeltungsteuer 25% on capital gains
const capitalGains = shortTerm + longTerm;
return {
capitalGainsTax: capitalGains > 0 ? capitalGains * 0.25 * 1.055 : 0, // 25% + Soli 5.5%
incomeTax: income * rates.incomeRate,
};
default:
return { estimatedTax: (shortTerm + longTerm + income) * 0.2 };
}
}
}
Tax Optimization
System can suggest tax-loss harvesting strategies:
async function identifyTaxLossHarvestingOpportunities(
userId: string,
currentYear: number
): Promise<HarvestingOpportunity[]> {
const currentGains = await calculateYTDGains(userId, currentYear);
const openPositions = await getOpenPositions(userId);
const opportunities: HarvestingOpportunity[] = [];
for (const position of openPositions) {
const currentValue = await getCurrentValue(position);
const unrealizedLoss = currentValue - position.costBasis;
if (unrealizedLoss < -100) { // minimum loss $100
opportunities.push({
asset: position.asset,
amount: position.amount,
costBasis: position.costBasis,
currentValue,
potentialLoss: unrealizedLoss,
taxSavings: Math.abs(unrealizedLoss) * getEffectiveTaxRate(userId),
washSaleRisk: await checkWashSaleRule(userId, position.asset), // US specific
});
}
}
return opportunities.sort((a, b) => b.taxSavings - a.taxSavings);
}
Crypto tax calculation system with support for US/UK/DE/AU jurisdictions, automatic event classification, and tax optimization suggestions — 3-5 weeks development.







