@umpledger/sdk 2.0.0-alpha.1
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/dist/index.d.mts +839 -0
- package/dist/index.d.ts +839 -0
- package/dist/index.js +1520 -0
- package/dist/index.mjs +1466 -0
- package/examples/multi-agent-marketplace.ts +140 -0
- package/examples/quickstart.ts +107 -0
- package/package.json +58 -0
- package/src/core/agent-manager.ts +200 -0
- package/src/core/audit-trail.ts +91 -0
- package/src/core/index.ts +3 -0
- package/src/core/wallet-manager.ts +257 -0
- package/src/index.ts +188 -0
- package/src/pricing/engine.ts +311 -0
- package/src/pricing/index.ts +3 -0
- package/src/pricing/templates.ts +237 -0
- package/src/settlement/bus.ts +253 -0
- package/src/settlement/index.ts +2 -0
- package/src/terms/contract-manager.ts +142 -0
- package/src/terms/index.ts +2 -0
- package/src/terms/metering.ts +106 -0
- package/src/types.ts +417 -0
- package/src/utils/errors.ts +91 -0
- package/src/utils/id.ts +35 -0
- package/src/utils/index.ts +2 -0
- package/tests/ump.test.ts +525 -0
- package/tsconfig.json +28 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════
|
|
2
|
+
// UMP v2.0 — Core Type Definitions
|
|
3
|
+
// The Universal Monetization Protocol type system
|
|
4
|
+
// ═══════════════════════════════════════════════════════════════
|
|
5
|
+
|
|
6
|
+
// ── Agent Identity Types ──────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
export type AgentType = 'HUMAN' | 'ORGANIZATION' | 'AI_AGENT' | 'SERVICE' | 'COMPOSITE';
|
|
9
|
+
export type AgentStatus = 'ACTIVE' | 'SUSPENDED' | 'REVOKED' | 'EXPIRED';
|
|
10
|
+
|
|
11
|
+
export interface AuthorityScope {
|
|
12
|
+
maxPerTransaction: number;
|
|
13
|
+
maxPerDay: number;
|
|
14
|
+
maxPerMonth: number;
|
|
15
|
+
allowedServices?: string[];
|
|
16
|
+
allowedCounterparties?: string[];
|
|
17
|
+
requiresApprovalAbove?: number;
|
|
18
|
+
autoRevokeAfter?: number; // ms
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface VerificationProof {
|
|
22
|
+
publicKey: string;
|
|
23
|
+
issuingAuthority: string;
|
|
24
|
+
expiresAt: Date;
|
|
25
|
+
revocationEndpoint?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AgentIdentity {
|
|
29
|
+
agentId: string;
|
|
30
|
+
agentType: AgentType;
|
|
31
|
+
parentId: string | null;
|
|
32
|
+
displayName: string;
|
|
33
|
+
capabilities: string[];
|
|
34
|
+
authorityScope: AuthorityScope;
|
|
35
|
+
verification: VerificationProof;
|
|
36
|
+
metadata: Record<string, unknown>;
|
|
37
|
+
status: AgentStatus;
|
|
38
|
+
createdAt: Date;
|
|
39
|
+
updatedAt: Date;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface CreateAgentOptions {
|
|
43
|
+
name: string;
|
|
44
|
+
type: AgentType;
|
|
45
|
+
parentId?: string;
|
|
46
|
+
capabilities?: string[];
|
|
47
|
+
authority: Partial<AuthorityScope> & {
|
|
48
|
+
maxPerTransaction: number | string;
|
|
49
|
+
maxPerDay: number | string;
|
|
50
|
+
};
|
|
51
|
+
metadata?: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ── Wallet & Value Types ──────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
export type ValueUnitType =
|
|
57
|
+
| 'FIAT'
|
|
58
|
+
| 'AI_TOKEN'
|
|
59
|
+
| 'COMPUTE_CREDIT'
|
|
60
|
+
| 'OUTCOME_SCORE'
|
|
61
|
+
| 'PLATFORM_CREDIT'
|
|
62
|
+
| 'ENVIRONMENTAL';
|
|
63
|
+
|
|
64
|
+
export interface Balance {
|
|
65
|
+
valueUnitType: ValueUnitType;
|
|
66
|
+
currency?: string; // e.g. 'USD', 'EUR' for FIAT
|
|
67
|
+
amount: number;
|
|
68
|
+
reserved: number;
|
|
69
|
+
available: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface FundingConfig {
|
|
73
|
+
type: 'LINKED_BANK' | 'PARENT_DRAWDOWN' | 'PREPAID';
|
|
74
|
+
sourceId: string;
|
|
75
|
+
autoTopup?: AutoTopupRule;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface AutoTopupRule {
|
|
79
|
+
threshold: number;
|
|
80
|
+
amount: number;
|
|
81
|
+
maxPerDay: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface LedgerEntry {
|
|
85
|
+
entryId: string;
|
|
86
|
+
timestamp: Date;
|
|
87
|
+
type: 'DEBIT' | 'CREDIT' | 'RESERVE' | 'RELEASE' | 'TOPUP';
|
|
88
|
+
amount: number;
|
|
89
|
+
valueUnitType: ValueUnitType;
|
|
90
|
+
currency?: string;
|
|
91
|
+
counterpartyAgentId?: string;
|
|
92
|
+
transactionId?: string;
|
|
93
|
+
description: string;
|
|
94
|
+
balanceAfter: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface Wallet {
|
|
98
|
+
walletId: string;
|
|
99
|
+
ownerAgentId: string;
|
|
100
|
+
balances: Balance[];
|
|
101
|
+
fundingSource?: FundingConfig;
|
|
102
|
+
frozen: boolean;
|
|
103
|
+
createdAt: Date;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface FundWalletOptions {
|
|
107
|
+
amount: number | string;
|
|
108
|
+
source?: string;
|
|
109
|
+
valueUnitType?: ValueUnitType;
|
|
110
|
+
currency?: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── Pricing Types ─────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
export type PricingPrimitive =
|
|
116
|
+
| 'FIXED'
|
|
117
|
+
| 'UNIT_RATE'
|
|
118
|
+
| 'TIERED'
|
|
119
|
+
| 'PERCENTAGE'
|
|
120
|
+
| 'THRESHOLD'
|
|
121
|
+
| 'TIME_WINDOW'
|
|
122
|
+
| 'CONDITIONAL'
|
|
123
|
+
| 'COMPOSITE';
|
|
124
|
+
|
|
125
|
+
export type CompositeOperator = 'ADD' | 'MAX' | 'MIN' | 'FIRST_MATCH';
|
|
126
|
+
|
|
127
|
+
export interface PricingRuleBase {
|
|
128
|
+
ruleId: string;
|
|
129
|
+
name: string;
|
|
130
|
+
description?: string;
|
|
131
|
+
primitive: PricingPrimitive;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface FixedRule extends PricingRuleBase {
|
|
135
|
+
primitive: 'FIXED';
|
|
136
|
+
amount: number;
|
|
137
|
+
period?: 'PER_EVENT' | 'HOURLY' | 'DAILY' | 'MONTHLY' | 'YEARLY';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface UnitRateRule extends PricingRuleBase {
|
|
141
|
+
primitive: 'UNIT_RATE';
|
|
142
|
+
rate: number;
|
|
143
|
+
unit: string; // 'TOKEN', 'API_CALL', 'GPU_SECOND', etc.
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface TierBand {
|
|
147
|
+
from: number;
|
|
148
|
+
to: number | null; // null = unlimited
|
|
149
|
+
rate: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface TieredRule extends PricingRuleBase {
|
|
153
|
+
primitive: 'TIERED';
|
|
154
|
+
tiers: TierBand[];
|
|
155
|
+
mode: 'GRADUATED' | 'VOLUME'; // graduated = each unit in tier at tier rate; volume = all units at qualifying tier rate
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface PercentageRule extends PricingRuleBase {
|
|
159
|
+
primitive: 'PERCENTAGE';
|
|
160
|
+
percentage: number; // 0-1
|
|
161
|
+
referenceField: string; // what the percentage is of
|
|
162
|
+
min?: number;
|
|
163
|
+
max?: number;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface ThresholdRule extends PricingRuleBase {
|
|
167
|
+
primitive: 'THRESHOLD';
|
|
168
|
+
threshold: number;
|
|
169
|
+
belowRate: number;
|
|
170
|
+
aboveRate: number;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface TimeWindowBand {
|
|
174
|
+
dayOfWeek?: number[]; // 0=Sun, 6=Sat
|
|
175
|
+
startHour: number;
|
|
176
|
+
endHour: number;
|
|
177
|
+
rate: number;
|
|
178
|
+
label?: string; // 'peak', 'off-peak', etc.
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface TimeWindowRule extends PricingRuleBase {
|
|
182
|
+
primitive: 'TIME_WINDOW';
|
|
183
|
+
windows: TimeWindowBand[];
|
|
184
|
+
defaultRate: number;
|
|
185
|
+
timezone: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface ConditionalBranch {
|
|
189
|
+
condition: string; // expression, e.g. "outcome == 'SUCCESS'"
|
|
190
|
+
rule: PricingRule;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface ConditionalRule extends PricingRuleBase {
|
|
194
|
+
primitive: 'CONDITIONAL';
|
|
195
|
+
field: string;
|
|
196
|
+
branches: ConditionalBranch[];
|
|
197
|
+
fallback?: PricingRule;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface CompositeRule extends PricingRuleBase {
|
|
201
|
+
primitive: 'COMPOSITE';
|
|
202
|
+
operator: CompositeOperator;
|
|
203
|
+
rules: PricingRule[];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export type PricingRule =
|
|
207
|
+
| FixedRule
|
|
208
|
+
| UnitRateRule
|
|
209
|
+
| TieredRule
|
|
210
|
+
| PercentageRule
|
|
211
|
+
| ThresholdRule
|
|
212
|
+
| TimeWindowRule
|
|
213
|
+
| ConditionalRule
|
|
214
|
+
| CompositeRule;
|
|
215
|
+
|
|
216
|
+
// ── Contract Types ────────────────────────────────────────────
|
|
217
|
+
|
|
218
|
+
export type ContractMode = 'PRE_NEGOTIATED' | 'TEMPLATE' | 'DYNAMIC';
|
|
219
|
+
export type ContractStatus = 'DRAFT' | 'PROPOSED' | 'ACTIVE' | 'EXPIRED' | 'TERMINATED';
|
|
220
|
+
|
|
221
|
+
export interface Contract {
|
|
222
|
+
contractId: string;
|
|
223
|
+
mode: ContractMode;
|
|
224
|
+
status: ContractStatus;
|
|
225
|
+
parties: { sourceAgentId: string; targetAgentId: string };
|
|
226
|
+
pricingRules: PricingRule[];
|
|
227
|
+
effectiveFrom: Date;
|
|
228
|
+
effectiveUntil?: Date;
|
|
229
|
+
metadata: Record<string, unknown>;
|
|
230
|
+
createdAt: Date;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface CreateContractOptions {
|
|
234
|
+
mode?: ContractMode;
|
|
235
|
+
targetAgentId: string;
|
|
236
|
+
pricingRules: Omit<PricingRule, 'ruleId'>[];
|
|
237
|
+
effectiveFrom?: Date;
|
|
238
|
+
effectiveUntil?: Date;
|
|
239
|
+
metadata?: Record<string, unknown>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ── Metering & Usage Types ────────────────────────────────────
|
|
243
|
+
|
|
244
|
+
export interface UsageEvent {
|
|
245
|
+
eventId: string;
|
|
246
|
+
sourceAgentId: string;
|
|
247
|
+
targetAgentId: string;
|
|
248
|
+
contractId: string;
|
|
249
|
+
serviceId: string;
|
|
250
|
+
timestamp: Date;
|
|
251
|
+
quantity: number;
|
|
252
|
+
unit: string;
|
|
253
|
+
dimensions: Record<string, string | number>;
|
|
254
|
+
outcome?: OutcomeAttestation;
|
|
255
|
+
signature?: string;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export type OutcomeType =
|
|
259
|
+
| 'TASK_COMPLETION'
|
|
260
|
+
| 'METRIC_IMPROVEMENT'
|
|
261
|
+
| 'REVENUE_GENERATED'
|
|
262
|
+
| 'COST_SAVED'
|
|
263
|
+
| 'CUSTOM';
|
|
264
|
+
|
|
265
|
+
export type VerificationMethod =
|
|
266
|
+
| 'SELF_REPORTED'
|
|
267
|
+
| 'BILATERAL_AGREEMENT'
|
|
268
|
+
| 'THIRD_PARTY_ORACLE'
|
|
269
|
+
| 'AUTOMATED_TEST';
|
|
270
|
+
|
|
271
|
+
export type AttestationStatus = 'CLAIMED' | 'VERIFIED' | 'DISPUTED' | 'EXPIRED';
|
|
272
|
+
|
|
273
|
+
export interface OutcomeAttestation {
|
|
274
|
+
outcomeId: string;
|
|
275
|
+
outcomeType: OutcomeType;
|
|
276
|
+
claimedBy: string;
|
|
277
|
+
evidence: Evidence[];
|
|
278
|
+
verificationMethod: VerificationMethod;
|
|
279
|
+
verifiedBy?: string;
|
|
280
|
+
confidenceScore: number; // 0-1
|
|
281
|
+
attestationStatus: AttestationStatus;
|
|
282
|
+
disputeWindow: number; // ms
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface Evidence {
|
|
286
|
+
type: 'LOG' | 'METRIC' | 'SCREENSHOT' | 'THIRD_PARTY' | 'TEST_RESULT';
|
|
287
|
+
uri: string;
|
|
288
|
+
hash: string;
|
|
289
|
+
description?: string;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ── Settlement Types ──────────────────────────────────────────
|
|
293
|
+
|
|
294
|
+
export type SettlementPattern =
|
|
295
|
+
| 'INSTANT_DRAWDOWN'
|
|
296
|
+
| 'ESCROW_RELEASE'
|
|
297
|
+
| 'WATERFALL_SPLIT'
|
|
298
|
+
| 'NET_SETTLEMENT'
|
|
299
|
+
| 'CONDITIONAL_RELEASE'
|
|
300
|
+
| 'CROSS_CURRENCY_ATOMIC';
|
|
301
|
+
|
|
302
|
+
export type SettlementStatus = 'PENDING' | 'PROCESSING' | 'SETTLED' | 'FAILED' | 'REVERSED';
|
|
303
|
+
|
|
304
|
+
export interface RatedRecord {
|
|
305
|
+
ratedRecordId: string;
|
|
306
|
+
usageEventId: string;
|
|
307
|
+
contractId: string;
|
|
308
|
+
pricingRuleId: string;
|
|
309
|
+
quantity: number;
|
|
310
|
+
rate: number;
|
|
311
|
+
amount: number;
|
|
312
|
+
currency: string;
|
|
313
|
+
ratedAt: Date;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export interface Settlement {
|
|
317
|
+
settlementId: string;
|
|
318
|
+
pattern: SettlementPattern;
|
|
319
|
+
status: SettlementStatus;
|
|
320
|
+
sourceAgentId: string;
|
|
321
|
+
targetAgentId: string;
|
|
322
|
+
ratedRecords: RatedRecord[];
|
|
323
|
+
totalAmount: number;
|
|
324
|
+
currency: string;
|
|
325
|
+
settledAt?: Date;
|
|
326
|
+
auditId: string;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// ── Transaction Types ─────────────────────────────────────────
|
|
330
|
+
|
|
331
|
+
export interface TransactOptions {
|
|
332
|
+
from: string;
|
|
333
|
+
to: string;
|
|
334
|
+
service: string;
|
|
335
|
+
payload?: Record<string, unknown>;
|
|
336
|
+
maxCost?: number | string;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export interface TransactionResult {
|
|
340
|
+
transactionId: string;
|
|
341
|
+
cost: number;
|
|
342
|
+
currency: string;
|
|
343
|
+
outcome?: OutcomeAttestation;
|
|
344
|
+
auditId: string;
|
|
345
|
+
settledAt: Date;
|
|
346
|
+
duration: number; // ms
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ── Audit Types ───────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
export interface AuditRecord {
|
|
352
|
+
auditId: string;
|
|
353
|
+
what: { operation: string; entityType: string; entityId: string; amount?: number };
|
|
354
|
+
who: { sourceAgentId: string; targetAgentId: string; humanOwnerId?: string };
|
|
355
|
+
when: Date;
|
|
356
|
+
why: { contractId?: string; pricingRuleId?: string; justification?: string };
|
|
357
|
+
how: { policiesEvaluated: string[]; policiesPassed: string[]; overrides?: string[] };
|
|
358
|
+
result: { balanceBefore?: number; balanceAfter?: number; settlementAmount?: number; status: string };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ── Dispute Types ─────────────────────────────────────────────
|
|
362
|
+
|
|
363
|
+
export type DisputeStage =
|
|
364
|
+
| 'AUTOMATED_RECONCILIATION'
|
|
365
|
+
| 'AGENT_NEGOTIATION'
|
|
366
|
+
| 'ARBITRATION_ORACLE'
|
|
367
|
+
| 'HUMAN_ESCALATION';
|
|
368
|
+
|
|
369
|
+
export type DisputeStatus = 'OPEN' | 'IN_PROGRESS' | 'RESOLVED' | 'ESCALATED' | 'CLOSED';
|
|
370
|
+
|
|
371
|
+
export interface Dispute {
|
|
372
|
+
disputeId: string;
|
|
373
|
+
transactionId: string;
|
|
374
|
+
openedBy: string;
|
|
375
|
+
respondent: string;
|
|
376
|
+
stage: DisputeStage;
|
|
377
|
+
status: DisputeStatus;
|
|
378
|
+
reason: string;
|
|
379
|
+
evidence: Evidence[];
|
|
380
|
+
amount: number;
|
|
381
|
+
resolution?: { outcome: 'UPHELD' | 'REJECTED' | 'PARTIAL'; adjustedAmount: number };
|
|
382
|
+
openedAt: Date;
|
|
383
|
+
resolvedAt?: Date;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// ── Policy Types ──────────────────────────────────────────────
|
|
387
|
+
|
|
388
|
+
export type PolicyType =
|
|
389
|
+
| 'SPENDING_LIMIT'
|
|
390
|
+
| 'COUNTERPARTY_ALLOWLIST'
|
|
391
|
+
| 'SERVICE_ALLOWLIST'
|
|
392
|
+
| 'RATE_REASONABLENESS'
|
|
393
|
+
| 'OUTCOME_VERIFICATION'
|
|
394
|
+
| 'COMPLIANCE'
|
|
395
|
+
| 'ANOMALY_DETECTION'
|
|
396
|
+
| 'BUDGET_ALLOCATION';
|
|
397
|
+
|
|
398
|
+
export type ViolationAction = 'HARD_BLOCK' | 'SOFT_BLOCK' | 'ALERT' | 'LOG';
|
|
399
|
+
|
|
400
|
+
export interface Policy {
|
|
401
|
+
policyId: string;
|
|
402
|
+
type: PolicyType;
|
|
403
|
+
agentId: string;
|
|
404
|
+
config: Record<string, unknown>;
|
|
405
|
+
violationAction: ViolationAction;
|
|
406
|
+
enabled: boolean;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ── SDK Config ────────────────────────────────────────────────
|
|
410
|
+
|
|
411
|
+
export interface UMPConfig {
|
|
412
|
+
apiKey: string;
|
|
413
|
+
baseUrl?: string;
|
|
414
|
+
timeout?: number;
|
|
415
|
+
retries?: number;
|
|
416
|
+
onAudit?: (record: AuditRecord) => void;
|
|
417
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UMP Error Hierarchy
|
|
3
|
+
* All SDK errors extend UMPError for easy catch-all handling.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export class UMPError extends Error {
|
|
7
|
+
public readonly code: string;
|
|
8
|
+
|
|
9
|
+
constructor(message: string, code: string) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'UMPError';
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class AuthorityExceededError extends UMPError {
|
|
17
|
+
public readonly limit: number;
|
|
18
|
+
public readonly attempted: number;
|
|
19
|
+
|
|
20
|
+
constructor(limitType: string, limit: number, attempted: number) {
|
|
21
|
+
super(
|
|
22
|
+
`Authority exceeded: ${limitType} limit is ${limit}, attempted ${attempted}`,
|
|
23
|
+
'AUTHORITY_EXCEEDED'
|
|
24
|
+
);
|
|
25
|
+
this.name = 'AuthorityExceededError';
|
|
26
|
+
this.limit = limit;
|
|
27
|
+
this.attempted = attempted;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class InsufficientFundsError extends UMPError {
|
|
32
|
+
public readonly available: number;
|
|
33
|
+
public readonly required: number;
|
|
34
|
+
|
|
35
|
+
constructor(available: number, required: number) {
|
|
36
|
+
super(
|
|
37
|
+
`Insufficient funds: available ${available}, required ${required}`,
|
|
38
|
+
'INSUFFICIENT_FUNDS'
|
|
39
|
+
);
|
|
40
|
+
this.name = 'InsufficientFundsError';
|
|
41
|
+
this.available = available;
|
|
42
|
+
this.required = required;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class WalletFrozenError extends UMPError {
|
|
47
|
+
constructor(walletId: string) {
|
|
48
|
+
super(`Wallet ${walletId} is frozen — all transactions blocked`, 'WALLET_FROZEN');
|
|
49
|
+
this.name = 'WalletFrozenError';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class AgentNotFoundError extends UMPError {
|
|
54
|
+
constructor(agentId: string) {
|
|
55
|
+
super(`Agent not found: ${agentId}`, 'AGENT_NOT_FOUND');
|
|
56
|
+
this.name = 'AgentNotFoundError';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class AgentRevokedError extends UMPError {
|
|
61
|
+
constructor(agentId: string) {
|
|
62
|
+
super(`Agent ${agentId} has been revoked`, 'AGENT_REVOKED');
|
|
63
|
+
this.name = 'AgentRevokedError';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class ContractNotFoundError extends UMPError {
|
|
68
|
+
constructor(contractId: string) {
|
|
69
|
+
super(`Contract not found: ${contractId}`, 'CONTRACT_NOT_FOUND');
|
|
70
|
+
this.name = 'ContractNotFoundError';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class PolicyViolationError extends UMPError {
|
|
75
|
+
public readonly policyType: string;
|
|
76
|
+
public readonly action: string;
|
|
77
|
+
|
|
78
|
+
constructor(policyType: string, action: string, details: string) {
|
|
79
|
+
super(`Policy violation [${policyType}]: ${details}`, 'POLICY_VIOLATION');
|
|
80
|
+
this.name = 'PolicyViolationError';
|
|
81
|
+
this.policyType = policyType;
|
|
82
|
+
this.action = action;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class DisputeError extends UMPError {
|
|
87
|
+
constructor(message: string) {
|
|
88
|
+
super(message, 'DISPUTE_ERROR');
|
|
89
|
+
this.name = 'DisputeError';
|
|
90
|
+
}
|
|
91
|
+
}
|
package/src/utils/id.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate a prefixed UUID for UMP entities.
|
|
5
|
+
* Format: {prefix}_{uuid} e.g. "agt_a1b2c3d4-..."
|
|
6
|
+
*/
|
|
7
|
+
export function generateId(prefix: string): string {
|
|
8
|
+
return `${prefix}_${uuidv4()}`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Parse a monetary string like "$50" or "50.00" into a number.
|
|
13
|
+
*/
|
|
14
|
+
export function parseMoney(value: number | string): number {
|
|
15
|
+
if (typeof value === 'number') return value;
|
|
16
|
+
const cleaned = value.replace(/[^0-9.-]/g, '');
|
|
17
|
+
const parsed = parseFloat(cleaned);
|
|
18
|
+
if (isNaN(parsed)) throw new Error(`Invalid monetary value: ${value}`);
|
|
19
|
+
return parsed;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Round to N decimal places (default 2 for currency).
|
|
24
|
+
*/
|
|
25
|
+
export function round(value: number, decimals = 2): number {
|
|
26
|
+
const factor = Math.pow(10, decimals);
|
|
27
|
+
return Math.round(value * factor) / factor;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* High-resolution timestamp for audit records.
|
|
32
|
+
*/
|
|
33
|
+
export function hrTimestamp(): Date {
|
|
34
|
+
return new Date();
|
|
35
|
+
}
|