@mnemopay/sdk 0.7.5 → 0.9.0
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 +182 -211
- package/dist/cli/dashboard.d.ts +9 -0
- package/dist/cli/dashboard.d.ts.map +1 -0
- package/dist/cli/dashboard.js +78 -0
- package/dist/cli/dashboard.js.map +1 -0
- package/dist/client.d.ts +182 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +177 -0
- package/dist/client.js.map +1 -0
- package/dist/commerce.d.ts +225 -0
- package/dist/commerce.d.ts.map +1 -0
- package/dist/commerce.js +420 -0
- package/dist/commerce.js.map +1 -0
- package/dist/fraud.d.ts +75 -1
- package/dist/fraud.d.ts.map +1 -1
- package/dist/fraud.js +247 -26
- package/dist/fraud.js.map +1 -1
- package/dist/identity.d.ts +154 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +269 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +44 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +196 -37
- package/dist/index.js.map +1 -1
- package/dist/ledger.d.ts +137 -0
- package/dist/ledger.d.ts.map +1 -0
- package/dist/ledger.js +250 -0
- package/dist/ledger.js.map +1 -0
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +298 -6
- package/dist/mcp/server.js.map +1 -1
- package/dist/network.d.ts +155 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +263 -0
- package/dist/network.js.map +1 -0
- package/dist/rails/index.d.ts +2 -0
- package/dist/rails/index.d.ts.map +1 -1
- package/dist/rails/index.js +19 -4
- package/dist/rails/index.js.map +1 -1
- package/dist/rails/paystack.d.ts +157 -0
- package/dist/rails/paystack.d.ts.map +1 -0
- package/dist/rails/paystack.js +366 -0
- package/dist/rails/paystack.js.map +1 -0
- package/package.json +34 -17
package/dist/ledger.d.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Double-Entry Ledger for MnemoPay
|
|
3
|
+
*
|
|
4
|
+
* Every financial operation records two entries (debit + credit) that always
|
|
5
|
+
* balance to zero. This is the accounting standard required by regulators,
|
|
6
|
+
* bank partners, and auditors.
|
|
7
|
+
*
|
|
8
|
+
* Account types:
|
|
9
|
+
* - agent:{agentId} → Agent wallet (available funds)
|
|
10
|
+
* - escrow:{agentId} → Funds held in escrow pending settlement
|
|
11
|
+
* - platform:revenue → Platform fee income
|
|
12
|
+
* - platform:float → Funds in transit / settlement buffer
|
|
13
|
+
* - counterparty:{agentId} → Funds owed to/from counterparty agents
|
|
14
|
+
*/
|
|
15
|
+
export type AccountType = "agent" | "escrow" | "revenue" | "float" | "counterparty";
|
|
16
|
+
export type EntryDirection = "debit" | "credit";
|
|
17
|
+
export type Currency = "USD" | "USDC" | "BTC" | "EUR" | "NGN";
|
|
18
|
+
export interface LedgerEntry {
|
|
19
|
+
id: string;
|
|
20
|
+
/** Links debit+credit pair */
|
|
21
|
+
txRef: string;
|
|
22
|
+
/** Full account identifier (e.g., "agent:agent-123") */
|
|
23
|
+
account: string;
|
|
24
|
+
/** Account type for querying */
|
|
25
|
+
accountType: AccountType;
|
|
26
|
+
/** Debit amount (money going out of this account) */
|
|
27
|
+
debit: number;
|
|
28
|
+
/** Credit amount (money coming into this account) */
|
|
29
|
+
credit: number;
|
|
30
|
+
/** Currency code */
|
|
31
|
+
currency: Currency;
|
|
32
|
+
/** Human-readable description */
|
|
33
|
+
description: string;
|
|
34
|
+
/** Related MnemoPay transaction ID */
|
|
35
|
+
relatedTxId?: string;
|
|
36
|
+
/** Counterparty account for cross-referencing */
|
|
37
|
+
counterAccount?: string;
|
|
38
|
+
/** ISO timestamp */
|
|
39
|
+
createdAt: string;
|
|
40
|
+
/** Sequence number for ordering (monotonically increasing) */
|
|
41
|
+
seq: number;
|
|
42
|
+
}
|
|
43
|
+
export interface AccountBalance {
|
|
44
|
+
account: string;
|
|
45
|
+
accountType: AccountType;
|
|
46
|
+
currency: Currency;
|
|
47
|
+
/** Total credits minus total debits */
|
|
48
|
+
balance: number;
|
|
49
|
+
/** Sum of all credits */
|
|
50
|
+
totalCredits: number;
|
|
51
|
+
/** Sum of all debits */
|
|
52
|
+
totalDebits: number;
|
|
53
|
+
/** Number of entries */
|
|
54
|
+
entryCount: number;
|
|
55
|
+
}
|
|
56
|
+
export interface LedgerSummary {
|
|
57
|
+
/** Total debits across all accounts (should equal totalCredits) */
|
|
58
|
+
totalDebits: number;
|
|
59
|
+
/** Total credits across all accounts (should equal totalCredits) */
|
|
60
|
+
totalCredits: number;
|
|
61
|
+
/** Must be zero if ledger is balanced */
|
|
62
|
+
imbalance: number;
|
|
63
|
+
/** Whether the ledger balances */
|
|
64
|
+
balanced: boolean;
|
|
65
|
+
/** Number of entries */
|
|
66
|
+
entryCount: number;
|
|
67
|
+
/** All account balances */
|
|
68
|
+
accounts: AccountBalance[];
|
|
69
|
+
}
|
|
70
|
+
export interface TransferResult {
|
|
71
|
+
entries: [LedgerEntry, LedgerEntry];
|
|
72
|
+
txRef: string;
|
|
73
|
+
}
|
|
74
|
+
export declare class Ledger {
|
|
75
|
+
private entries;
|
|
76
|
+
private seq;
|
|
77
|
+
constructor(existingEntries?: LedgerEntry[]);
|
|
78
|
+
/**
|
|
79
|
+
* Record a double-entry transfer. Debits one account, credits another.
|
|
80
|
+
* The ledger always balances: every debit has an equal credit.
|
|
81
|
+
*/
|
|
82
|
+
transfer(fromAccount: string, toAccount: string, amount: number, currency: Currency, description: string, relatedTxId?: string): TransferResult;
|
|
83
|
+
/**
|
|
84
|
+
* CHARGE: Move funds from agent's available balance into escrow.
|
|
85
|
+
* Debit agent → Credit escrow
|
|
86
|
+
*/
|
|
87
|
+
recordCharge(agentId: string, amount: number, txId: string, currency?: Currency): TransferResult;
|
|
88
|
+
/**
|
|
89
|
+
* SETTLE: Release escrow, deduct platform fee, credit counterparty.
|
|
90
|
+
* Returns 2-3 transfer results (escrow→float, float→revenue for fee, float→counterparty for net).
|
|
91
|
+
*/
|
|
92
|
+
recordSettlement(agentId: string, txId: string, grossAmount: number, feeAmount: number, netAmount: number, counterpartyId?: string, currency?: Currency): TransferResult[];
|
|
93
|
+
/**
|
|
94
|
+
* REFUND: Reverse a completed transaction.
|
|
95
|
+
* Credits back the agent, debits counterparty/revenue.
|
|
96
|
+
*/
|
|
97
|
+
recordRefund(agentId: string, txId: string, netAmount: number, counterpartyId?: string, currency?: Currency): TransferResult[];
|
|
98
|
+
/**
|
|
99
|
+
* CANCEL: Release escrow back to agent (charge was never settled).
|
|
100
|
+
*/
|
|
101
|
+
recordCancellation(agentId: string, amount: number, txId: string, currency?: Currency): TransferResult;
|
|
102
|
+
/**
|
|
103
|
+
* FUND: External funds coming into an agent's wallet (top-up, deposit).
|
|
104
|
+
*/
|
|
105
|
+
recordFunding(agentId: string, amount: number, source: string, currency?: Currency): TransferResult;
|
|
106
|
+
/**
|
|
107
|
+
* Get balance for a specific account.
|
|
108
|
+
* Balance = SUM(credits) - SUM(debits)
|
|
109
|
+
*/
|
|
110
|
+
getBalance(account: string, currency?: Currency): number;
|
|
111
|
+
/**
|
|
112
|
+
* Get detailed balance info for an account.
|
|
113
|
+
*/
|
|
114
|
+
getAccountBalance(account: string, currency?: Currency): AccountBalance;
|
|
115
|
+
/**
|
|
116
|
+
* Get all entries for a specific transaction.
|
|
117
|
+
*/
|
|
118
|
+
getEntriesForTransaction(txId: string): LedgerEntry[];
|
|
119
|
+
/**
|
|
120
|
+
* Get all entries for a specific account, optionally filtered by date range.
|
|
121
|
+
*/
|
|
122
|
+
getAccountHistory(account: string, limit?: number, offset?: number): LedgerEntry[];
|
|
123
|
+
/**
|
|
124
|
+
* Verify the entire ledger balances (total debits = total credits).
|
|
125
|
+
*/
|
|
126
|
+
verify(): LedgerSummary;
|
|
127
|
+
/**
|
|
128
|
+
* Export all entries for persistence.
|
|
129
|
+
*/
|
|
130
|
+
serialize(): LedgerEntry[];
|
|
131
|
+
/**
|
|
132
|
+
* Get entry count.
|
|
133
|
+
*/
|
|
134
|
+
get size(): number;
|
|
135
|
+
private parseAccountType;
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=ledger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../src/ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,MAAM,MAAM,WAAW,GACnB,OAAO,GACP,QAAQ,GACR,SAAS,GACT,OAAO,GACP,cAAc,CAAC;AAEnB,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEhD,MAAM,MAAM,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE9D,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,wDAAwD;IACxD,OAAO,EAAE,MAAM,CAAC;IAChB,gCAAgC;IAChC,WAAW,EAAE,WAAW,CAAC;IACzB,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAC;IACf,oBAAoB;IACpB,QAAQ,EAAE,QAAQ,CAAC;IACnB,iCAAiC;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,sCAAsC;IACtC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iDAAiD;IACjD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oBAAoB;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,8DAA8D;IAC9D,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,WAAW,CAAC;IACzB,QAAQ,EAAE,QAAQ,CAAC;IACnB,uCAAuC;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,yBAAyB;IACzB,YAAY,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V,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,cAAc,CAAC,EAAE,MAAM,EACvB,QAAQ,GAAE,QAAgB,GACzB,cAAc,EAAE;IA4BnB;;OAEG;IACH,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,QAAgB,GAAG,cAAc;IAW7G;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,QAAgB,GAAG,cAAc;IAY1G;;;OAGG;IACH,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAE,QAAgB,GAAG,MAAM;IAY/D;;OAEG;IACH,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAE,QAAgB,GAAG,cAAc;IAsB9E;;OAEG;IACH,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,EAAE;IAIrD;;OAEG;IACH,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,SAAK,EAAE,MAAM,SAAI,GAAG,WAAW,EAAE;IAOzE;;OAEG;IACH,MAAM,IAAI,aAAa;IA2CvB;;OAEG;IACH,SAAS,IAAI,WAAW,EAAE;IAI1B;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAID,OAAO,CAAC,gBAAgB;CAWzB"}
|
package/dist/ledger.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Double-Entry Ledger for MnemoPay
|
|
4
|
+
*
|
|
5
|
+
* Every financial operation records two entries (debit + credit) that always
|
|
6
|
+
* balance to zero. This is the accounting standard required by regulators,
|
|
7
|
+
* bank partners, and auditors.
|
|
8
|
+
*
|
|
9
|
+
* Account types:
|
|
10
|
+
* - agent:{agentId} → Agent wallet (available funds)
|
|
11
|
+
* - escrow:{agentId} → Funds held in escrow pending settlement
|
|
12
|
+
* - platform:revenue → Platform fee income
|
|
13
|
+
* - platform:float → Funds in transit / settlement buffer
|
|
14
|
+
* - counterparty:{agentId} → Funds owed to/from counterparty agents
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Ledger = void 0;
|
|
18
|
+
// ─── Ledger ─────────────────────────────────────────────────────────────────
|
|
19
|
+
class Ledger {
|
|
20
|
+
entries = [];
|
|
21
|
+
seq = 0;
|
|
22
|
+
constructor(existingEntries) {
|
|
23
|
+
if (existingEntries) {
|
|
24
|
+
this.entries = [...existingEntries];
|
|
25
|
+
this.seq = existingEntries.length > 0
|
|
26
|
+
? Math.max(...existingEntries.map(e => e.seq)) + 1
|
|
27
|
+
: 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// ── Core: Double-Entry Transfer ──────────────────────────────────────────
|
|
31
|
+
/**
|
|
32
|
+
* Record a double-entry transfer. Debits one account, credits another.
|
|
33
|
+
* The ledger always balances: every debit has an equal credit.
|
|
34
|
+
*/
|
|
35
|
+
transfer(fromAccount, toAccount, amount, currency, description, relatedTxId) {
|
|
36
|
+
if (!Number.isFinite(amount) || amount <= 0)
|
|
37
|
+
throw new Error("Ledger transfer amount must be a positive finite number");
|
|
38
|
+
if (!fromAccount || !toAccount)
|
|
39
|
+
throw new Error("Both fromAccount and toAccount are required");
|
|
40
|
+
if (fromAccount === toAccount)
|
|
41
|
+
throw new Error("Cannot transfer to the same account");
|
|
42
|
+
const txRef = crypto.randomUUID();
|
|
43
|
+
const now = new Date().toISOString();
|
|
44
|
+
const debitEntry = {
|
|
45
|
+
id: crypto.randomUUID(),
|
|
46
|
+
txRef,
|
|
47
|
+
account: fromAccount,
|
|
48
|
+
accountType: this.parseAccountType(fromAccount),
|
|
49
|
+
debit: amount,
|
|
50
|
+
credit: 0,
|
|
51
|
+
currency,
|
|
52
|
+
description,
|
|
53
|
+
relatedTxId,
|
|
54
|
+
counterAccount: toAccount,
|
|
55
|
+
createdAt: now,
|
|
56
|
+
seq: this.seq++,
|
|
57
|
+
};
|
|
58
|
+
const creditEntry = {
|
|
59
|
+
id: crypto.randomUUID(),
|
|
60
|
+
txRef,
|
|
61
|
+
account: toAccount,
|
|
62
|
+
accountType: this.parseAccountType(toAccount),
|
|
63
|
+
debit: 0,
|
|
64
|
+
credit: amount,
|
|
65
|
+
currency,
|
|
66
|
+
description,
|
|
67
|
+
relatedTxId,
|
|
68
|
+
counterAccount: fromAccount,
|
|
69
|
+
createdAt: now,
|
|
70
|
+
seq: this.seq++,
|
|
71
|
+
};
|
|
72
|
+
this.entries.push(debitEntry, creditEntry);
|
|
73
|
+
return { entries: [debitEntry, creditEntry], txRef };
|
|
74
|
+
}
|
|
75
|
+
// ── Payment Flow Methods ─────────────────────────────────────────────────
|
|
76
|
+
/**
|
|
77
|
+
* CHARGE: Move funds from agent's available balance into escrow.
|
|
78
|
+
* Debit agent → Credit escrow
|
|
79
|
+
*/
|
|
80
|
+
recordCharge(agentId, amount, txId, currency = "USD") {
|
|
81
|
+
return this.transfer(`agent:${agentId}`, `escrow:${agentId}`, amount, currency, `Hold for pending charge`, txId);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* SETTLE: Release escrow, deduct platform fee, credit counterparty.
|
|
85
|
+
* Returns 2-3 transfer results (escrow→float, float→revenue for fee, float→counterparty for net).
|
|
86
|
+
*/
|
|
87
|
+
recordSettlement(agentId, txId, grossAmount, feeAmount, netAmount, counterpartyId, currency = "USD") {
|
|
88
|
+
const results = [];
|
|
89
|
+
// 1. Escrow → Platform float (full gross amount)
|
|
90
|
+
results.push(this.transfer(`escrow:${agentId}`, `platform:float`, grossAmount, currency, `Settlement release from escrow`, txId));
|
|
91
|
+
// 2. Platform float → Platform revenue (fee portion)
|
|
92
|
+
if (feeAmount > 0) {
|
|
93
|
+
results.push(this.transfer(`platform:float`, `platform:revenue`, feeAmount, currency, `Platform fee (${((feeAmount / grossAmount) * 100).toFixed(1)}%)`, txId));
|
|
94
|
+
}
|
|
95
|
+
// 3. Platform float → Counterparty or back to agent (net amount)
|
|
96
|
+
const destination = counterpartyId
|
|
97
|
+
? `counterparty:${counterpartyId}`
|
|
98
|
+
: `agent:${agentId}`;
|
|
99
|
+
results.push(this.transfer(`platform:float`, destination, netAmount, currency, `Net settlement payout`, txId));
|
|
100
|
+
return results;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* REFUND: Reverse a completed transaction.
|
|
104
|
+
* Credits back the agent, debits counterparty/revenue.
|
|
105
|
+
*/
|
|
106
|
+
recordRefund(agentId, txId, netAmount, counterpartyId, currency = "USD") {
|
|
107
|
+
const results = [];
|
|
108
|
+
if (counterpartyId) {
|
|
109
|
+
// Reverse: counterparty returns net to agent
|
|
110
|
+
results.push(this.transfer(`counterparty:${counterpartyId}`, `agent:${agentId}`, netAmount, currency, `Refund — reversal of net settlement`, txId));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// No counterparty: reverse the float→agent payout from settlement
|
|
114
|
+
results.push(this.transfer(`agent:${agentId}`, `platform:float`, netAmount, currency, `Refund — reversal of net settlement`, txId));
|
|
115
|
+
}
|
|
116
|
+
return results;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* CANCEL: Release escrow back to agent (charge was never settled).
|
|
120
|
+
*/
|
|
121
|
+
recordCancellation(agentId, amount, txId, currency = "USD") {
|
|
122
|
+
return this.transfer(`escrow:${agentId}`, `agent:${agentId}`, amount, currency, `Cancelled — escrow released`, txId);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* FUND: External funds coming into an agent's wallet (top-up, deposit).
|
|
126
|
+
*/
|
|
127
|
+
recordFunding(agentId, amount, source, currency = "USD") {
|
|
128
|
+
return this.transfer(`platform:float`, `agent:${agentId}`, amount, currency, `Wallet funding from ${source}`);
|
|
129
|
+
}
|
|
130
|
+
// ── Queries ──────────────────────────────────────────────────────────────
|
|
131
|
+
/**
|
|
132
|
+
* Get balance for a specific account.
|
|
133
|
+
* Balance = SUM(credits) - SUM(debits)
|
|
134
|
+
*/
|
|
135
|
+
getBalance(account, currency = "USD") {
|
|
136
|
+
let balance = 0;
|
|
137
|
+
for (const entry of this.entries) {
|
|
138
|
+
if (entry.account === account && entry.currency === currency) {
|
|
139
|
+
balance += entry.credit - entry.debit;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// Round to appropriate precision: 2 decimals for fiat, 8 for crypto
|
|
143
|
+
const precision = (currency === "BTC") ? 1e8 : 100;
|
|
144
|
+
return Math.round(balance * precision) / precision;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get detailed balance info for an account.
|
|
148
|
+
*/
|
|
149
|
+
getAccountBalance(account, currency = "USD") {
|
|
150
|
+
let totalCredits = 0;
|
|
151
|
+
let totalDebits = 0;
|
|
152
|
+
let entryCount = 0;
|
|
153
|
+
for (const entry of this.entries) {
|
|
154
|
+
if (entry.account === account && entry.currency === currency) {
|
|
155
|
+
totalCredits += entry.credit;
|
|
156
|
+
totalDebits += entry.debit;
|
|
157
|
+
entryCount++;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
account,
|
|
162
|
+
accountType: this.parseAccountType(account),
|
|
163
|
+
currency,
|
|
164
|
+
balance: Math.round((totalCredits - totalDebits) * 100) / 100,
|
|
165
|
+
totalCredits: Math.round(totalCredits * 100) / 100,
|
|
166
|
+
totalDebits: Math.round(totalDebits * 100) / 100,
|
|
167
|
+
entryCount,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Get all entries for a specific transaction.
|
|
172
|
+
*/
|
|
173
|
+
getEntriesForTransaction(txId) {
|
|
174
|
+
return this.entries.filter(e => e.relatedTxId === txId);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Get all entries for a specific account, optionally filtered by date range.
|
|
178
|
+
*/
|
|
179
|
+
getAccountHistory(account, limit = 50, offset = 0) {
|
|
180
|
+
return this.entries
|
|
181
|
+
.filter(e => e.account === account)
|
|
182
|
+
.sort((a, b) => b.seq - a.seq)
|
|
183
|
+
.slice(offset, offset + limit);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Verify the entire ledger balances (total debits = total credits).
|
|
187
|
+
*/
|
|
188
|
+
verify() {
|
|
189
|
+
const accountMap = new Map();
|
|
190
|
+
let totalDebits = 0;
|
|
191
|
+
let totalCredits = 0;
|
|
192
|
+
for (const entry of this.entries) {
|
|
193
|
+
totalDebits += entry.debit;
|
|
194
|
+
totalCredits += entry.credit;
|
|
195
|
+
const key = `${entry.account}:${entry.currency}`;
|
|
196
|
+
if (!accountMap.has(key)) {
|
|
197
|
+
accountMap.set(key, {
|
|
198
|
+
account: entry.account,
|
|
199
|
+
accountType: entry.accountType,
|
|
200
|
+
currency: entry.currency,
|
|
201
|
+
balance: 0,
|
|
202
|
+
totalCredits: 0,
|
|
203
|
+
totalDebits: 0,
|
|
204
|
+
entryCount: 0,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
const acct = accountMap.get(key);
|
|
208
|
+
acct.totalCredits += entry.credit;
|
|
209
|
+
acct.totalDebits += entry.debit;
|
|
210
|
+
acct.balance = Math.round((acct.totalCredits - acct.totalDebits) * 100) / 100;
|
|
211
|
+
acct.entryCount++;
|
|
212
|
+
}
|
|
213
|
+
const imbalance = Math.round((totalDebits - totalCredits) * 100) / 100;
|
|
214
|
+
return {
|
|
215
|
+
totalDebits: Math.round(totalDebits * 100) / 100,
|
|
216
|
+
totalCredits: Math.round(totalCredits * 100) / 100,
|
|
217
|
+
imbalance,
|
|
218
|
+
balanced: imbalance === 0,
|
|
219
|
+
entryCount: this.entries.length,
|
|
220
|
+
accounts: Array.from(accountMap.values()),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
// ── Serialization ────────────────────────────────────────────────────────
|
|
224
|
+
/**
|
|
225
|
+
* Export all entries for persistence.
|
|
226
|
+
*/
|
|
227
|
+
serialize() {
|
|
228
|
+
return [...this.entries];
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Get entry count.
|
|
232
|
+
*/
|
|
233
|
+
get size() {
|
|
234
|
+
return this.entries.length;
|
|
235
|
+
}
|
|
236
|
+
// ── Internal ─────────────────────────────────────────────────────────────
|
|
237
|
+
parseAccountType(account) {
|
|
238
|
+
const prefix = account.split(":")[0];
|
|
239
|
+
switch (prefix) {
|
|
240
|
+
case "agent": return "agent";
|
|
241
|
+
case "escrow": return "escrow";
|
|
242
|
+
case "platform":
|
|
243
|
+
return account.includes("revenue") ? "revenue" : "float";
|
|
244
|
+
case "counterparty": return "counterparty";
|
|
245
|
+
default: return "agent";
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
exports.Ledger = Ledger;
|
|
250
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../src/ledger.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AA2EH,+EAA+E;AAE/E,MAAa,MAAM;IACT,OAAO,GAAkB,EAAE,CAAC;IAC5B,GAAG,GAAG,CAAC,CAAC;IAEhB,YAAY,eAA+B;QACzC,IAAI,eAAe,EAAE,CAAC;YACpB,IAAI,CAAC,OAAO,GAAG,CAAC,GAAG,eAAe,CAAC,CAAC;YACpC,IAAI,CAAC,GAAG,GAAG,eAAe,CAAC,MAAM,GAAG,CAAC;gBACnC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;gBAClD,CAAC,CAAC,CAAC,CAAC;QACR,CAAC;IACH,CAAC;IAED,4EAA4E;IAE5E;;;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CAAC,CAAC,CAAC,CAAC,CAAC;QACrC,QAAQ,MAAM,EAAE,CAAC;YACf,KAAK,OAAO,CAAC,CAAC,OAAO,OAAO,CAAC;YAC7B,KAAK,QAAQ,CAAC,CAAC,OAAO,QAAQ,CAAC;YAC/B,KAAK,UAAU;gBACb,OAAO,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;YAC3D,KAAK,cAAc,CAAC,CAAC,OAAO,cAAc,CAAC;YAC3C,OAAO,CAAC,CAAC,OAAO,OAAO,CAAC;QAC1B,CAAC;IACH,CAAC;CACF;AAhVD,wBAgVC"}
|
package/dist/mcp/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/mcp/server.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/mcp/server.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAygBnE,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAwdjD;AAID,MAAM,CAAC,OAAO,UAAU,mBAAmB,IAAI,MAAM,CAgBpD"}
|