agentwallet-sdk 3.1.0 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -10
- package/dist/flash/executor.d.ts +119 -0
- package/dist/flash/executor.d.ts.map +1 -0
- package/dist/flash/executor.js +195 -0
- package/dist/flash/executor.js.map +1 -0
- package/dist/flash/index.d.ts +28 -0
- package/dist/flash/index.d.ts.map +1 -0
- package/dist/flash/index.js +25 -0
- package/dist/flash/index.js.map +1 -0
- package/dist/flash/scanner.d.ts +133 -0
- package/dist/flash/scanner.d.ts.map +1 -0
- package/dist/flash/scanner.js +212 -0
- package/dist/flash/scanner.js.map +1 -0
- package/dist/flash/types.d.ts +136 -0
- package/dist/flash/types.d.ts.map +1 -0
- package/dist/flash/types.js +23 -0
- package/dist/flash/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -1
- package/dist/mev/index.d.ts +4 -0
- package/dist/mev/index.d.ts.map +1 -0
- package/dist/mev/index.js +4 -0
- package/dist/mev/index.js.map +1 -0
- package/dist/mev/protection.d.ts +54 -0
- package/dist/mev/protection.d.ts.map +1 -0
- package/dist/mev/protection.js +185 -0
- package/dist/mev/protection.js.map +1 -0
- package/dist/mev/risk.d.ts +19 -0
- package/dist/mev/risk.d.ts.map +1 -0
- package/dist/mev/risk.js +95 -0
- package/dist/mev/risk.js.map +1 -0
- package/dist/mev/types.d.ts +49 -0
- package/dist/mev/types.d.ts.map +1 -0
- package/dist/mev/types.js +2 -0
- package/dist/mev/types.js.map +1 -0
- package/dist/solver/adapter.d.ts +47 -0
- package/dist/solver/adapter.d.ts.map +1 -0
- package/dist/solver/adapter.js +138 -0
- package/dist/solver/adapter.js.map +1 -0
- package/dist/solver/analyzer.d.ts +48 -0
- package/dist/solver/analyzer.d.ts.map +1 -0
- package/dist/solver/analyzer.js +89 -0
- package/dist/solver/analyzer.js.map +1 -0
- package/dist/solver/builder.d.ts +31 -0
- package/dist/solver/builder.d.ts.map +1 -0
- package/dist/solver/builder.js +60 -0
- package/dist/solver/builder.js.map +1 -0
- package/dist/solver/index.d.ts +22 -0
- package/dist/solver/index.d.ts.map +1 -0
- package/dist/solver/index.js +21 -0
- package/dist/solver/index.js.map +1 -0
- package/dist/solver/types.d.ts +115 -0
- package/dist/solver/types.d.ts.map +1 -0
- package/dist/solver/types.js +10 -0
- package/dist/solver/types.js.map +1 -0
- package/dist/tax/engine.d.ts +131 -0
- package/dist/tax/engine.d.ts.map +1 -0
- package/dist/tax/engine.js +307 -0
- package/dist/tax/engine.js.map +1 -0
- package/dist/tax/index.d.ts +9 -0
- package/dist/tax/index.d.ts.map +1 -0
- package/dist/tax/index.js +8 -0
- package/dist/tax/index.js.map +1 -0
- package/dist/tax/lots.d.ts +60 -0
- package/dist/tax/lots.d.ts.map +1 -0
- package/dist/tax/lots.js +129 -0
- package/dist/tax/lots.js.map +1 -0
- package/dist/tax/types.d.ts +113 -0
- package/dist/tax/types.d.ts.map +1 -0
- package/dist/tax/types.js +18 -0
- package/dist/tax/types.js.map +1 -0
- package/dist/x402/client.d.ts.map +1 -1
- package/dist/x402/client.js +2 -1
- package/dist/x402/client.js.map +1 -1
- package/dist/yield/index.d.ts +26 -0
- package/dist/yield/index.d.ts.map +1 -0
- package/dist/yield/index.js +25 -0
- package/dist/yield/index.js.map +1 -0
- package/dist/yield/rates.d.ts +114 -0
- package/dist/yield/rates.d.ts.map +1 -0
- package/dist/yield/rates.js +351 -0
- package/dist/yield/rates.js.map +1 -0
- package/dist/yield/types.d.ts +134 -0
- package/dist/yield/types.d.ts.map +1 -0
- package/dist/yield/types.js +24 -0
- package/dist/yield/types.js.map +1 -0
- package/dist/yield/vault.d.ts +112 -0
- package/dist/yield/vault.d.ts.map +1 -0
- package/dist/yield/vault.js +264 -0
- package/dist/yield/vault.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tax / P&L Engine — Core Engine
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ DISCLAIMER: This module is for informational purposes only.
|
|
5
|
+
* Tax laws are complex, jurisdiction-specific, and change frequently.
|
|
6
|
+
* Always consult a qualified tax professional before filing. This is NOT tax advice.
|
|
7
|
+
*
|
|
8
|
+
* Key rules modeled (US-centric):
|
|
9
|
+
* - Every token swap = taxable disposal of fromToken + acquisition of toToken
|
|
10
|
+
* - Short-term gain: held < 365 days → taxed as ordinary income
|
|
11
|
+
* - Long-term gain: held ≥ 365 days → preferential capital gains rates
|
|
12
|
+
* - Yield received = ordinary income (full rate)
|
|
13
|
+
* - Gas fees = reduce net gain (treated as selling expense)
|
|
14
|
+
* - Wash sale rule: NOT applicable to crypto as of 2024 (no legislation yet)
|
|
15
|
+
* - Fiat on-ramp = establishes cost basis, NOT a taxable event
|
|
16
|
+
* - Bridge (cross-chain move) = generally NOT taxable (same owner, same asset)
|
|
17
|
+
*/
|
|
18
|
+
import type { TaxEngineConfig, TaxEvent, TaxLot, RealizedGain, TaxSummary } from './types.js';
|
|
19
|
+
export declare class TaxEngine {
|
|
20
|
+
private config;
|
|
21
|
+
private lotManager;
|
|
22
|
+
private events;
|
|
23
|
+
private gains;
|
|
24
|
+
constructor(config?: TaxEngineConfig);
|
|
25
|
+
/**
|
|
26
|
+
* Record a swap — the most common taxable event in DeFi.
|
|
27
|
+
*
|
|
28
|
+
* A swap creates two sub-events:
|
|
29
|
+
* 1. Disposal of fromToken (taxable — creates realized gain/loss)
|
|
30
|
+
* 2. Acquisition of toToken (establishes new cost basis)
|
|
31
|
+
*
|
|
32
|
+
* Returns the RealizedGain record, or null if no lots were tracked
|
|
33
|
+
* (zero cost basis scenario — gain equals full proceeds).
|
|
34
|
+
*/
|
|
35
|
+
recordSwap(params: {
|
|
36
|
+
txHash: string;
|
|
37
|
+
chain: string;
|
|
38
|
+
timestamp: number;
|
|
39
|
+
fromToken: string;
|
|
40
|
+
fromTokenAddress: string;
|
|
41
|
+
fromAmount: bigint;
|
|
42
|
+
fromValueUsd: number;
|
|
43
|
+
toToken: string;
|
|
44
|
+
toTokenAddress: string;
|
|
45
|
+
toAmount: bigint;
|
|
46
|
+
toValueUsd: number;
|
|
47
|
+
gasPaidUsd?: number;
|
|
48
|
+
}): RealizedGain | null;
|
|
49
|
+
/**
|
|
50
|
+
* Record yield received from a DeFi protocol (taxable as ordinary income).
|
|
51
|
+
*
|
|
52
|
+
* Yield is treated as income at the fair market value when received.
|
|
53
|
+
* The cost basis of the new lot = market value at receipt.
|
|
54
|
+
*/
|
|
55
|
+
recordYieldReceived(params: {
|
|
56
|
+
txHash?: string;
|
|
57
|
+
chain: string;
|
|
58
|
+
timestamp: number;
|
|
59
|
+
token: string;
|
|
60
|
+
tokenAddress: string;
|
|
61
|
+
amount: bigint;
|
|
62
|
+
valueUsd: number;
|
|
63
|
+
protocol: string;
|
|
64
|
+
}): void;
|
|
65
|
+
/**
|
|
66
|
+
* Record a fiat on-ramp (buying crypto with fiat currency).
|
|
67
|
+
*
|
|
68
|
+
* This is NOT a taxable event — it establishes the cost basis for future disposals.
|
|
69
|
+
* The cost basis = amount paid in fiat (USD).
|
|
70
|
+
*/
|
|
71
|
+
recordFiatOnRamp(params: {
|
|
72
|
+
txHash?: string;
|
|
73
|
+
chain: string;
|
|
74
|
+
timestamp: number;
|
|
75
|
+
token: string;
|
|
76
|
+
tokenAddress: string;
|
|
77
|
+
amount: bigint;
|
|
78
|
+
paidUsd: number;
|
|
79
|
+
}): void;
|
|
80
|
+
/**
|
|
81
|
+
* Generate an annual tax summary.
|
|
82
|
+
*
|
|
83
|
+
* ⚠️ For informational purposes only. Consult a tax professional.
|
|
84
|
+
*/
|
|
85
|
+
getSummary(year?: number): TaxSummary;
|
|
86
|
+
/**
|
|
87
|
+
* Export to CSV format compatible with TurboTax, Koinly, CoinTracker, and similar tools.
|
|
88
|
+
*
|
|
89
|
+
* ⚠️ For informational purposes only. Consult a tax professional.
|
|
90
|
+
* All USD values are formatted to 2 decimal places to prevent CSV parsing issues.
|
|
91
|
+
*/
|
|
92
|
+
exportCSV(year?: number): string;
|
|
93
|
+
/**
|
|
94
|
+
* Export to JSON (for programmatic use, custom reporting, CPA handoff).
|
|
95
|
+
*
|
|
96
|
+
* ⚠️ For informational purposes only. Consult a tax professional.
|
|
97
|
+
*/
|
|
98
|
+
exportJSON(year?: number): {
|
|
99
|
+
summary: TaxSummary;
|
|
100
|
+
gains: RealizedGain[];
|
|
101
|
+
events: TaxEvent[];
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Export full engine state for persistence across sessions.
|
|
105
|
+
*/
|
|
106
|
+
exportState(): {
|
|
107
|
+
lots: Record<string, TaxLot[]>;
|
|
108
|
+
events: TaxEvent[];
|
|
109
|
+
gains: RealizedGain[];
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Load state from a previous session (e.g., from database or JSON file).
|
|
113
|
+
* After loading, new swaps will correctly build on existing lot inventory.
|
|
114
|
+
*/
|
|
115
|
+
loadState(state: {
|
|
116
|
+
lots: Record<string, TaxLot[]>;
|
|
117
|
+
events: TaxEvent[];
|
|
118
|
+
gains: RealizedGain[];
|
|
119
|
+
}): void;
|
|
120
|
+
/**
|
|
121
|
+
* Determine if a disposal is short-term or long-term.
|
|
122
|
+
*
|
|
123
|
+
* Uses the acquisition timestamp of the earliest lot consumed (conservative
|
|
124
|
+
* for FIFO; oldest lot determines the holding period of the bulk of the disposal).
|
|
125
|
+
*
|
|
126
|
+
* Rule: held ≥ 365 days = long-term; held < 365 days = short-term.
|
|
127
|
+
* If no lots were tracked, defaults to short-term (conservative).
|
|
128
|
+
*/
|
|
129
|
+
private determineGainType;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/tax/engine.ts"],"names":[],"mapp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E,MAAM,CAAC;QACd,YAAY,EAAE,MAAM,CAAC;QACrB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;KACjB,GAAG,IAAI;IAgCR;;;;OAIG;IACH,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU;IAwCrC;;;;;OAKG;IACH,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM;IAoChC;;;;OAIG;IACH,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG;QACzB,OAAO,EAAE,UAAU,CAAC;QACpB,KAAK,EAAE,YAAY,EAAE,CAAC;QACtB,MAAM,EAAE,QAAQ,EAAE,CAAC;KACpB;IAiBD;;OAEG;IACH,WAAW,IAAI;QACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QAC/B,MAAM,EAAE,QAAQ,EAAE,CAAC;QACnB,KAAK,EAAE,YAAY,EAAE,CAAC;KACvB;IAQD;;;OAGG;IACH,SAAS,CAAC,KAAK,EAAE;QACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QAC/B,MAAM,EAAE,QAAQ,EAAE,CAAC;QACnB,KAAK,EAAE,YAAY,EAAE,CAAC;KACvB,GAAG,IAAI;IAQR;;;;;;;;OAQG;IACH,OAAO,CAAC,iBAAiB;CAiB1B"}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tax / P&L Engine — Core Engine
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ DISCLAIMER: This module is for informational purposes only.
|
|
5
|
+
* Tax laws are complex, jurisdiction-specific, and change frequently.
|
|
6
|
+
* Always consult a qualified tax professional before filing. This is NOT tax advice.
|
|
7
|
+
*
|
|
8
|
+
* Key rules modeled (US-centric):
|
|
9
|
+
* - Every token swap = taxable disposal of fromToken + acquisition of toToken
|
|
10
|
+
* - Short-term gain: held < 365 days → taxed as ordinary income
|
|
11
|
+
* - Long-term gain: held ≥ 365 days → preferential capital gains rates
|
|
12
|
+
* - Yield received = ordinary income (full rate)
|
|
13
|
+
* - Gas fees = reduce net gain (treated as selling expense)
|
|
14
|
+
* - Wash sale rule: NOT applicable to crypto as of 2024 (no legislation yet)
|
|
15
|
+
* - Fiat on-ramp = establishes cost basis, NOT a taxable event
|
|
16
|
+
* - Bridge (cross-chain move) = generally NOT taxable (same owner, same asset)
|
|
17
|
+
*/
|
|
18
|
+
import { LotManager } from './lots.js';
|
|
19
|
+
/** One year in milliseconds */
|
|
20
|
+
const ONE_YEAR_MS = 365 * 24 * 60 * 60 * 1000;
|
|
21
|
+
export class TaxEngine {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
this.events = [];
|
|
24
|
+
this.gains = [];
|
|
25
|
+
this.config = {
|
|
26
|
+
method: 'fifo',
|
|
27
|
+
taxYear: new Date().getFullYear(),
|
|
28
|
+
jurisdiction: 'US',
|
|
29
|
+
trackGasFees: true,
|
|
30
|
+
...config,
|
|
31
|
+
};
|
|
32
|
+
this.lotManager = new LotManager();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Record a swap — the most common taxable event in DeFi.
|
|
36
|
+
*
|
|
37
|
+
* A swap creates two sub-events:
|
|
38
|
+
* 1. Disposal of fromToken (taxable — creates realized gain/loss)
|
|
39
|
+
* 2. Acquisition of toToken (establishes new cost basis)
|
|
40
|
+
*
|
|
41
|
+
* Returns the RealizedGain record, or null if no lots were tracked
|
|
42
|
+
* (zero cost basis scenario — gain equals full proceeds).
|
|
43
|
+
*/
|
|
44
|
+
recordSwap(params) {
|
|
45
|
+
// 1. Dispose of fromToken lots using the configured cost basis method
|
|
46
|
+
const disposals = this.lotManager.disposeLots({
|
|
47
|
+
token: params.fromToken,
|
|
48
|
+
chain: params.chain,
|
|
49
|
+
amount: params.fromAmount,
|
|
50
|
+
method: this.config.method,
|
|
51
|
+
});
|
|
52
|
+
const totalCostBasis = disposals.reduce((sum, d) => sum + d.costBasisUsed, 0);
|
|
53
|
+
// 2. Calculate realized gain/loss
|
|
54
|
+
// gain = proceeds − cost basis − gas fees paid
|
|
55
|
+
const gasPaidUsd = this.config.trackGasFees ? (params.gasPaidUsd ?? 0) : 0;
|
|
56
|
+
const realizedGainUsd = params.fromValueUsd - totalCostBasis - gasPaidUsd;
|
|
57
|
+
// 3. Determine hold duration → short-term vs long-term
|
|
58
|
+
const gainType = this.determineGainType(disposals, params.timestamp);
|
|
59
|
+
// 4. Build the RealizedGain record
|
|
60
|
+
const gain = {
|
|
61
|
+
eventId: `swap-${params.txHash}`,
|
|
62
|
+
txHash: params.txHash,
|
|
63
|
+
timestamp: params.timestamp,
|
|
64
|
+
chain: params.chain,
|
|
65
|
+
token: params.fromToken,
|
|
66
|
+
amountDisposed: params.fromAmount,
|
|
67
|
+
proceedsUsd: params.fromValueUsd,
|
|
68
|
+
costBasisUsd: totalCostBasis,
|
|
69
|
+
realizedGainUsd,
|
|
70
|
+
gainType,
|
|
71
|
+
lotsUsed: disposals.map(d => ({
|
|
72
|
+
lotId: d.lotId,
|
|
73
|
+
amountUsed: d.amountUsed,
|
|
74
|
+
costBasisUsed: d.costBasisUsed,
|
|
75
|
+
})),
|
|
76
|
+
};
|
|
77
|
+
this.gains.push(gain);
|
|
78
|
+
// 5. Add new lot for toToken — cost basis = fair market value at acquisition
|
|
79
|
+
this.lotManager.addLot({
|
|
80
|
+
id: `lot-${params.txHash}-${params.toToken}`,
|
|
81
|
+
token: params.toToken,
|
|
82
|
+
tokenAddress: params.toTokenAddress,
|
|
83
|
+
chain: params.chain,
|
|
84
|
+
amount: params.toAmount,
|
|
85
|
+
costBasisUsd: params.toValueUsd,
|
|
86
|
+
costBasisPerUnit: Number(params.toAmount) > 0
|
|
87
|
+
? params.toValueUsd / Number(params.toAmount)
|
|
88
|
+
: 0,
|
|
89
|
+
acquiredAt: params.timestamp,
|
|
90
|
+
acquiredTxHash: params.txHash,
|
|
91
|
+
acquiredFrom: 'swap',
|
|
92
|
+
});
|
|
93
|
+
// 6. Record the TaxEvent
|
|
94
|
+
this.events.push({
|
|
95
|
+
id: `event-${params.txHash}`,
|
|
96
|
+
type: 'swap',
|
|
97
|
+
timestamp: params.timestamp,
|
|
98
|
+
txHash: params.txHash,
|
|
99
|
+
chain: params.chain,
|
|
100
|
+
disposedToken: params.fromToken,
|
|
101
|
+
disposedAmount: params.fromAmount,
|
|
102
|
+
disposedValueUsd: params.fromValueUsd,
|
|
103
|
+
acquiredToken: params.toToken,
|
|
104
|
+
acquiredAmount: params.toAmount,
|
|
105
|
+
acquiredValueUsd: params.toValueUsd,
|
|
106
|
+
gasPaidUsd,
|
|
107
|
+
});
|
|
108
|
+
// Return null if no lots were tracked (pre-existing wallet, zero cost basis)
|
|
109
|
+
return disposals.length > 0 ? gain : null;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Record yield received from a DeFi protocol (taxable as ordinary income).
|
|
113
|
+
*
|
|
114
|
+
* Yield is treated as income at the fair market value when received.
|
|
115
|
+
* The cost basis of the new lot = market value at receipt.
|
|
116
|
+
*/
|
|
117
|
+
recordYieldReceived(params) {
|
|
118
|
+
const lotId = `lot-yield-${params.txHash ?? params.timestamp}-${params.token}`;
|
|
119
|
+
// Add a lot — cost basis = FMV at receipt (income already recognized)
|
|
120
|
+
this.lotManager.addLot({
|
|
121
|
+
id: lotId,
|
|
122
|
+
token: params.token,
|
|
123
|
+
tokenAddress: params.tokenAddress,
|
|
124
|
+
chain: params.chain,
|
|
125
|
+
amount: params.amount,
|
|
126
|
+
costBasisUsd: params.valueUsd,
|
|
127
|
+
costBasisPerUnit: Number(params.amount) > 0
|
|
128
|
+
? params.valueUsd / Number(params.amount)
|
|
129
|
+
: 0,
|
|
130
|
+
acquiredAt: params.timestamp,
|
|
131
|
+
acquiredTxHash: params.txHash,
|
|
132
|
+
acquiredFrom: 'yield-received',
|
|
133
|
+
});
|
|
134
|
+
// Record the event (acquiredValueUsd = income amount for getSummary)
|
|
135
|
+
this.events.push({
|
|
136
|
+
id: `event-yield-${params.txHash ?? params.timestamp}`,
|
|
137
|
+
type: 'yield-received',
|
|
138
|
+
timestamp: params.timestamp,
|
|
139
|
+
txHash: params.txHash,
|
|
140
|
+
chain: params.chain,
|
|
141
|
+
acquiredToken: params.token,
|
|
142
|
+
acquiredAmount: params.amount,
|
|
143
|
+
acquiredValueUsd: params.valueUsd,
|
|
144
|
+
notes: `Yield from ${params.protocol}`,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Record a fiat on-ramp (buying crypto with fiat currency).
|
|
149
|
+
*
|
|
150
|
+
* This is NOT a taxable event — it establishes the cost basis for future disposals.
|
|
151
|
+
* The cost basis = amount paid in fiat (USD).
|
|
152
|
+
*/
|
|
153
|
+
recordFiatOnRamp(params) {
|
|
154
|
+
const lotId = `lot-fiat-${params.txHash ?? params.timestamp}-${params.token}`;
|
|
155
|
+
this.lotManager.addLot({
|
|
156
|
+
id: lotId,
|
|
157
|
+
token: params.token,
|
|
158
|
+
tokenAddress: params.tokenAddress,
|
|
159
|
+
chain: params.chain,
|
|
160
|
+
amount: params.amount,
|
|
161
|
+
costBasisUsd: params.paidUsd,
|
|
162
|
+
costBasisPerUnit: Number(params.amount) > 0
|
|
163
|
+
? params.paidUsd / Number(params.amount)
|
|
164
|
+
: 0,
|
|
165
|
+
acquiredAt: params.timestamp,
|
|
166
|
+
acquiredTxHash: params.txHash,
|
|
167
|
+
acquiredFrom: 'fiat-on-ramp',
|
|
168
|
+
});
|
|
169
|
+
// Record event — not taxable, no gain created
|
|
170
|
+
this.events.push({
|
|
171
|
+
id: `event-fiat-${params.txHash ?? params.timestamp}`,
|
|
172
|
+
type: 'fiat-on-ramp',
|
|
173
|
+
timestamp: params.timestamp,
|
|
174
|
+
txHash: params.txHash,
|
|
175
|
+
chain: params.chain,
|
|
176
|
+
acquiredToken: params.token,
|
|
177
|
+
acquiredAmount: params.amount,
|
|
178
|
+
acquiredValueUsd: params.paidUsd,
|
|
179
|
+
notes: 'Fiat on-ramp — establishes cost basis, not taxable',
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Generate an annual tax summary.
|
|
184
|
+
*
|
|
185
|
+
* ⚠️ For informational purposes only. Consult a tax professional.
|
|
186
|
+
*/
|
|
187
|
+
getSummary(year) {
|
|
188
|
+
const targetYear = year ?? this.config.taxYear;
|
|
189
|
+
const yearStart = new Date(targetYear, 0, 1).getTime();
|
|
190
|
+
const yearEnd = new Date(targetYear + 1, 0, 1).getTime();
|
|
191
|
+
const yearGains = this.gains.filter(g => g.timestamp >= yearStart && g.timestamp < yearEnd);
|
|
192
|
+
const yearEvents = this.events.filter(e => e.timestamp >= yearStart && e.timestamp < yearEnd);
|
|
193
|
+
const shortTermGain = yearGains
|
|
194
|
+
.filter(g => g.gainType === 'short-term')
|
|
195
|
+
.reduce((sum, g) => sum + g.realizedGainUsd, 0);
|
|
196
|
+
const longTermGain = yearGains
|
|
197
|
+
.filter(g => g.gainType === 'long-term')
|
|
198
|
+
.reduce((sum, g) => sum + g.realizedGainUsd, 0);
|
|
199
|
+
const ordinaryIncome = yearEvents
|
|
200
|
+
.filter(e => e.type === 'yield-received')
|
|
201
|
+
.reduce((sum, e) => sum + (e.acquiredValueUsd ?? 0), 0);
|
|
202
|
+
const totalGasPaid = yearEvents
|
|
203
|
+
.reduce((sum, e) => sum + (e.gasPaidUsd ?? 0), 0);
|
|
204
|
+
return {
|
|
205
|
+
year: targetYear,
|
|
206
|
+
totalProceeds: yearGains.reduce((sum, g) => sum + g.proceedsUsd, 0),
|
|
207
|
+
totalCostBasis: yearGains.reduce((sum, g) => sum + g.costBasisUsd, 0),
|
|
208
|
+
shortTermGain,
|
|
209
|
+
longTermGain,
|
|
210
|
+
totalNetGain: shortTermGain + longTermGain,
|
|
211
|
+
ordinaryIncome,
|
|
212
|
+
totalGasPaid,
|
|
213
|
+
taxableEventCount: yearGains.length,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Export to CSV format compatible with TurboTax, Koinly, CoinTracker, and similar tools.
|
|
218
|
+
*
|
|
219
|
+
* ⚠️ For informational purposes only. Consult a tax professional.
|
|
220
|
+
* All USD values are formatted to 2 decimal places to prevent CSV parsing issues.
|
|
221
|
+
*/
|
|
222
|
+
exportCSV(year) {
|
|
223
|
+
const targetYear = year ?? this.config.taxYear;
|
|
224
|
+
const yearStart = new Date(targetYear, 0, 1).getTime();
|
|
225
|
+
const yearEnd = new Date(targetYear + 1, 0, 1).getTime();
|
|
226
|
+
const yearGains = this.gains.filter(g => g.timestamp >= yearStart && g.timestamp < yearEnd);
|
|
227
|
+
const headers = [
|
|
228
|
+
'Date',
|
|
229
|
+
'Type',
|
|
230
|
+
'Asset',
|
|
231
|
+
'Proceeds (USD)',
|
|
232
|
+
'Cost Basis (USD)',
|
|
233
|
+
'Gain/Loss (USD)',
|
|
234
|
+
'Gain Type',
|
|
235
|
+
'Tx Hash',
|
|
236
|
+
'Chain',
|
|
237
|
+
];
|
|
238
|
+
const rows = yearGains.map(g => [
|
|
239
|
+
new Date(g.timestamp).toISOString().split('T')[0],
|
|
240
|
+
'Sale/Swap',
|
|
241
|
+
g.token,
|
|
242
|
+
g.proceedsUsd.toFixed(2),
|
|
243
|
+
g.costBasisUsd.toFixed(2),
|
|
244
|
+
g.realizedGainUsd.toFixed(2),
|
|
245
|
+
g.gainType === 'long-term' ? 'Long-term' : 'Short-term',
|
|
246
|
+
g.txHash ?? '',
|
|
247
|
+
g.chain,
|
|
248
|
+
]);
|
|
249
|
+
return [headers, ...rows].map(row => row.join(',')).join('\n');
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Export to JSON (for programmatic use, custom reporting, CPA handoff).
|
|
253
|
+
*
|
|
254
|
+
* ⚠️ For informational purposes only. Consult a tax professional.
|
|
255
|
+
*/
|
|
256
|
+
exportJSON(year) {
|
|
257
|
+
const summary = this.getSummary(year);
|
|
258
|
+
const targetYear = year ?? this.config.taxYear;
|
|
259
|
+
const yearStart = new Date(targetYear, 0, 1).getTime();
|
|
260
|
+
const yearEnd = new Date(targetYear + 1, 0, 1).getTime();
|
|
261
|
+
return {
|
|
262
|
+
summary,
|
|
263
|
+
gains: this.gains.filter(g => g.timestamp >= yearStart && g.timestamp < yearEnd),
|
|
264
|
+
events: this.events.filter(e => e.timestamp >= yearStart && e.timestamp < yearEnd),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Export full engine state for persistence across sessions.
|
|
269
|
+
*/
|
|
270
|
+
exportState() {
|
|
271
|
+
return {
|
|
272
|
+
lots: this.lotManager.exportState(),
|
|
273
|
+
events: this.events,
|
|
274
|
+
gains: this.gains,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Load state from a previous session (e.g., from database or JSON file).
|
|
279
|
+
* After loading, new swaps will correctly build on existing lot inventory.
|
|
280
|
+
*/
|
|
281
|
+
loadState(state) {
|
|
282
|
+
this.lotManager.loadState(state.lots);
|
|
283
|
+
this.events = state.events;
|
|
284
|
+
this.gains = state.gains;
|
|
285
|
+
}
|
|
286
|
+
// ─── Private Helpers ───
|
|
287
|
+
/**
|
|
288
|
+
* Determine if a disposal is short-term or long-term.
|
|
289
|
+
*
|
|
290
|
+
* Uses the acquisition timestamp of the earliest lot consumed (conservative
|
|
291
|
+
* for FIFO; oldest lot determines the holding period of the bulk of the disposal).
|
|
292
|
+
*
|
|
293
|
+
* Rule: held ≥ 365 days = long-term; held < 365 days = short-term.
|
|
294
|
+
* If no lots were tracked, defaults to short-term (conservative).
|
|
295
|
+
*/
|
|
296
|
+
determineGainType(disposals, disposalTimestamp) {
|
|
297
|
+
if (disposals.length === 0)
|
|
298
|
+
return 'short-term';
|
|
299
|
+
// Find the earliest acquisition date among all lots used in this disposal
|
|
300
|
+
const earliestAcquiredAt = disposals.reduce((earliest, d) => Math.min(earliest, d.acquiredAt), Infinity);
|
|
301
|
+
if (earliestAcquiredAt === Infinity)
|
|
302
|
+
return 'short-term';
|
|
303
|
+
const holdDurationMs = disposalTimestamp - earliestAcquiredAt;
|
|
304
|
+
return holdDurationMs >= ONE_YEAR_MS ? 'long-term' : 'short-term';
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../../src/tax/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAUH,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,+BAA+B;AAC/B,MAAM,WAAW,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE9C,MAAM,OAAO,SAAS;IAMpB,YAAY,SAA0B,EAAE;QAHhC,WAAM,GAAe,EAAE,CAAC;QACxB,UAAK,GAAmB,EAAE,CAAC;QAGjC,IAAI,CAAC,MAAM,GAAG;YACZ,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACjC,YAAY,EAAE,IAAI;YAClB,YAAY,EAAE,IAAI;YAClB,GAAG,MAAM;SACV,CAAC;QACF,IAAI,CAAC,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;IACrC,CAAC;IAED;;;;;;;;;OASG;IACH,UAAU,CAAC,MAaV;QACC,sEAAsE;QACtE,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;YAC5C,KAAK,EAAE,MAAM,CAAC,SAAS;YACvB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,MAAM,EAAE,MAAM,CAAC,UAAU;YACzB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;SAC3B,CAAC,CAAC;QAEH,MAAM,cAAc,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,GAA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AO,CACvD;SACF,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,WAAW;QAKT,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE;YACnC,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,KAAK,EAAE,IAAI,CAAC,KAAK;SAClB,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,SAAS,CAAC,KAIT;QACC,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;IAC3B,CAAC;IAED,0BAA0B;IAE1B;;;;;;;;OAQG;IACK,iBAAiB,CACvB,SAAkG,EAClG,iBAAyB;QAEzB,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,YAAY,CAAC;QAEhD,0EAA0E;QAC1E,MAAM,kBAAkB,GAAG,SAAS,CAAC,MAAM,CACzC,CAAC,QAAQ,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC,EACjD,QAAQ,CACT,CAAC;QAEF,IAAI,kBAAkB,KAAK,QAAQ;YAAE,OAAO,YAAY,CAAC;QAEzD,MAAM,cAAc,GAAG,iBAAiB,GAAG,kBAAkB,CAAC;QAC9D,OAAO,cAAc,IAAI,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC;IACpE,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tax / P&L Engine — Barrel Export
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ DISCLAIMER: For informational purposes only. Consult a tax professional.
|
|
5
|
+
*/
|
|
6
|
+
export type { TaxEventType, CostBasisMethod, GainType, TaxLot, TaxEvent, RealizedGain, TaxSummary, TaxExportRow, TaxEngineConfig, } from './types.js';
|
|
7
|
+
export { LotManager } from './lots.js';
|
|
8
|
+
export { TaxEngine } from './engine.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tax/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EACV,YAAY,EACZ,eAAe,EACf,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,GAChB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tax/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAcH,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tax / P&L Engine — Cost Basis Lot Manager
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ DISCLAIMER: For informational purposes only. Consult a tax professional.
|
|
5
|
+
*
|
|
6
|
+
* Manages tax lots (individual purchase/acquisition records) for each token.
|
|
7
|
+
* Supports FIFO, LIFO, and Specific Identification disposal ordering.
|
|
8
|
+
*/
|
|
9
|
+
import type { TaxLot, CostBasisMethod } from './types.js';
|
|
10
|
+
export declare class LotManager {
|
|
11
|
+
private lots;
|
|
12
|
+
private getLotKey;
|
|
13
|
+
/**
|
|
14
|
+
* Add a new tax lot (e.g., after buying or receiving a token).
|
|
15
|
+
*/
|
|
16
|
+
addLot(lot: TaxLot): void;
|
|
17
|
+
/**
|
|
18
|
+
* Get all lots for a token on a specific chain.
|
|
19
|
+
*/
|
|
20
|
+
getLots(token: string, chain: string): TaxLot[];
|
|
21
|
+
/**
|
|
22
|
+
* Get a specific lot by ID (used by determineGainType for acquisition date lookup).
|
|
23
|
+
*/
|
|
24
|
+
getLotById(lotId: string): TaxLot | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Dispose of lots using FIFO / LIFO / Specific Identification.
|
|
27
|
+
* Updates the lot inventory in place and returns an audit trail of disposals.
|
|
28
|
+
*
|
|
29
|
+
* If no lots are tracked (pre-existing wallet), returns [] with zero cost basis.
|
|
30
|
+
*/
|
|
31
|
+
disposeLots(params: {
|
|
32
|
+
token: string;
|
|
33
|
+
chain: string;
|
|
34
|
+
amount: bigint;
|
|
35
|
+
method: CostBasisMethod;
|
|
36
|
+
specificLotIds?: string[];
|
|
37
|
+
}): Array<{
|
|
38
|
+
lotId: string;
|
|
39
|
+
amountUsed: bigint;
|
|
40
|
+
costBasisUsed: number;
|
|
41
|
+
acquiredAt: number;
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* Get total holdings across all lots for a token on a chain.
|
|
45
|
+
*/
|
|
46
|
+
getTotalHolding(token: string, chain: string): {
|
|
47
|
+
amount: bigint;
|
|
48
|
+
costBasisUsd: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Export lot state for persistence (e.g., to database or JSON file).
|
|
52
|
+
* Note: bigint values will need special serialization if persisting to JSON.
|
|
53
|
+
*/
|
|
54
|
+
exportState(): Record<string, TaxLot[]>;
|
|
55
|
+
/**
|
|
56
|
+
* Load lot state from a previous session.
|
|
57
|
+
*/
|
|
58
|
+
loadState(state: Record<string, TaxLot[]>): void;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=lots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lots.d.ts","sourceRoot":"","sources":["../../src/tax/lots.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,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