forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
83 lines (77 loc) · 2.91 KB
/
Copy pathcommon.ts
File metadata and controls
83 lines (77 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
export interface AuditMetadata {
createdAt: string;
updatedAt: string;
}
export interface PaginationQuery {
limit?: number;
cursor?: string;
}
/**
* Represents a monetary amount and currency/asset identifier within Lily Protocol
* and the underlying Stellar network.
*
* All amounts must be formatted as base-10 decimal strings (e.g. `'10.50'`) rather than
* JavaScript numbers to prevent floating-point precision loss and truncation errors.
*
* ### Stellar Asset Semantics:
* - **Native Asset (`XLM`):** Omit `assetIssuer` (or set to `undefined`). Native Stellar Lumens
* have no issuing account.
* - **Issued Assets (e.g. `USDC`, `EURC`):** Specify `assetCode` (1-12 alphanumeric chars) and
* `assetIssuer` (56-character Stellar public key / G-address).
* - **Precision:** Stellar supports up to 7 decimal places (1 stroop = `0.0000001`).
*
* @example
* ```ts
* // Native Stellar Lumens
* const nativeAmount: MoneyAmount = {
* assetCode: 'XLM',
* amount: '25.5000000',
* };
*
* // Issued asset with issuer public key
* const usdcAmount: MoneyAmount = {
* assetCode: 'USDC',
* assetIssuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN',
* amount: '100.00',
* };
* ```
*/
export interface MoneyAmount {
/**
* The asset code representing the currency or token.
*
* For the native network asset, use `'XLM'`. For credit/issued assets, use a 1-to-12 character
* alphanumeric code (Alpha4: 1-4 chars like `'USDC'`; Alpha12: 5-12 chars).
*/
assetCode: string;
/**
* The 56-character Stellar public key (G-address) of the issuing account.
*
* - **Native asset (`XLM`):** Must be omitted or `undefined`.
* - **Issued assets:** Required to uniquely identify the asset on Stellar (alongside `assetCode`),
* linking to the issuer's account and home domain SEP-1 metadata.
*/
assetIssuer?: string;
/**
* The monetary quantity formatted strictly as a base-10 decimal string (e.g., `'10.50'`, `'0.0000001'`).
*
* Floating-point numbers (`number`) and exponential notation (e.g., `'1e-5'`) are prohibited
* to avoid floating-point rounding/truncation bugs. Precision should match the asset's scale,
* up to Stellar's maximum 7 decimal places (1 stroop = 0.0000001).
*/
amount: string;
}
export type ResourceStatus = 'pending' | 'active' | 'inactive' | 'failed' | 'paused';
/**
* Normalizes a decimal string amount to exactly two decimal places.
*
* Leading zeros are stripped and the fractional part is truncated (not
* rounded) to two digits and padded with trailing zeros, e.g.
* `'0075.5'` becomes `'75.50'`. The input object is not mutated.
*/
export function normalizeMoneyAmount(input: MoneyAmount): MoneyAmount {
const [wholeRaw = '', fractionRaw = ''] = input.amount.split('.');
const whole = wholeRaw.replace(/^0+(?=\d)/, '');
const fraction = fractionRaw.slice(0, 2).padEnd(2, '0');
return { ...input, amount: `${whole}.${fraction}` };
}