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
321 lines (287 loc) · 10.3 KB
/
Copy pathcommon.ts
File metadata and controls
321 lines (287 loc) · 10.3 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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';
const DECIMAL_AMOUNT_PATTERN = /^\d+(\.\d+)?$/;
/**
* Options for {@link normalizeMoneyAmount}.
*/
export interface NormalizeMoneyAmountOptions {
/**
* Explicit fractional scale (0-7).
* When omitted, formats whole and single-digit decimals to 2 decimal places,
* while preserving up to 7 decimal places for sub-cent amounts without precision loss.
*/
scale?: number;
}
/**
* Normalizes a decimal string amount.
*
* Leading zeros are stripped. By default, formats whole and single-digit decimals
* to at least 2 decimal places (e.g. `'100'` -> `'100.00'`, `'50.5'` -> `'50.50'`),
* while preserving up to 7 decimal places for Stellar sub-cent amounts (e.g. `'0.0000001'`),
* ensuring non-zero fractions are never silently dropped or zeroed out.
*
* If an explicit `scale` is provided (either as an options object or number),
* the fraction is truncated/padded to that scale.
*
* Throws a `RangeError` when the amount is not a base-10 decimal string
* or when `scale` is outside the allowed [0, 7] range.
*/
export function normalizeMoneyAmount(
input: MoneyAmount,
options?: NormalizeMoneyAmountOptions | number,
): MoneyAmount {
if (
typeof input.amount !== 'string' ||
!DECIMAL_AMOUNT_PATTERN.test(input.amount)
) {
throw new RangeError(
`MoneyAmount.amount must be a base-10 decimal string, got ${JSON.stringify(input.amount)}.`,
);
}
const explicitScale = typeof options === 'number' ? options : options?.scale;
if (
explicitScale !== undefined &&
(!Number.isInteger(explicitScale) || explicitScale < 0 || explicitScale > 7)
) {
throw new RangeError('scale must be an integer between 0 and 7.');
}
const [wholeRaw = '', fractionRaw = ''] = input.amount.split('.');
const whole = wholeRaw.replace(/^0+(?=\d)/, '');
const fraction = fractionRaw.slice(0, 7).padEnd(2, '0');
return { ...input, amount: `${whole}.${fraction}` };
}
/**
* Maximum precision scale supported by the Stellar network (1 stroop = 0.0000001).
*/
export const MAX_STELLAR_SCALE = 7;
/**
* Converts a JavaScript `number` into a safe, base-10 fixed-decimal string
* suitable for `MoneyAmount.amount`, eliminating IEEE-754 floating-point artifacts
* (e.g. `0.1 + 0.2`), expanding scientific/exponential notation (e.g. `1e-7`),
* and enforcing a documented rounding/scale policy.
*
* ### Rounding Policy:
* - **When `scale` is specified:** The value is rounded half-up (`Math.round`) to exactly
* `scale` decimal places and padded with trailing zeros to guarantee fixed width.
* - **When `scale` is omitted (`undefined`):** The value is cleaned of float artifacts,
* rounded to Stellar's maximum 7 decimal places, and trailing zeros/decimal points
* are stripped (e.g. `0.1 + 0.2` becomes `'0.3'`, `1e-7` becomes `'0.0000001'`).
*
* @param value - The numeric amount to convert. Must be a finite number.
* @param scale - Optional non-negative integer (0–18) defining the exact number of decimal places.
* @returns A clean, non-exponential base-10 decimal string.
* @throws {RangeError} If `value` is not a finite number (`NaN`, `Infinity`, `-Infinity`).
* @throws {RangeError} If `scale` is provided but is negative, non-integer, or greater than 18.
*
* @example
* ```ts
* toAmountString(0.1 + 0.2); // '0.3' (no float artifacts!)
* toAmountString(0.1 + 0.2, 2); // '0.30'
* toAmountString(12.3456, 2); // '12.35'
* toAmountString(1e-7); // '0.0000001'
* toAmountString(1000000); // '1000000'
* ```
*/
export function toAmountString(value: number, scale?: number): string {
if (typeof value !== 'number' || !Number.isFinite(value)) {
throw new RangeError(
`Money amount must be a finite number, got ${JSON.stringify(value)}.`,
);
}
if (scale !== undefined) {
if (
typeof scale !== 'number' ||
!Number.isInteger(scale) ||
scale < 0 ||
scale > 18
) {
throw new RangeError(
`Scale must be a non-negative integer between 0 and 18, got ${JSON.stringify(scale)}.`,
);
}
}
if (value === 0) {
if (scale !== undefined && scale > 0) {
return `0.${'0'.repeat(scale)}`;
}
return '0';
}
const isNegative = value < 0;
const absVal = Math.abs(value);
// Preserve exact representation for safe integers
if (Number.isInteger(absVal) && absVal <= Number.MAX_SAFE_INTEGER) {
const intStr = (isNegative ? '-' : '') + absVal.toString();
if (scale !== undefined && scale > 0) {
return `${intStr}.${'0'.repeat(scale)}`;
}
return intStr;
}
// Eliminate IEEE-754 floating-point accumulator artifacts (e.g. 0.30000000000000004 -> 0.3)
const cleaned = parseFloat(absVal.toPrecision(15));
const targetScale = scale !== undefined ? scale : MAX_STELLAR_SCALE;
const factor = Math.pow(10, targetScale);
const rounded = Math.round(cleaned * factor) / factor;
let s = rounded.toFixed(targetScale);
if (scale === undefined) {
// Strip redundant trailing zeros and decimal point when scale is not explicitly requested
s = s.replace(/(\.\d*?[1-9])0+$/, '$1').replace(/\.0+$/, '');
}
return (isNegative ? '-' : '') + s;
}
/**
* Options bag for constructing a {@link MoneyAmount} using {@link toMoneyAmount}.
*/
export interface ToMoneyAmountOptions {
/**
* The monetary quantity as a number or pre-formatted decimal string.
*/
amount: number | string;
/**
* The asset code representing the currency or token (e.g. `'XLM'`, `'USDC'`).
*/
assetCode: string;
/**
* The 56-character Stellar public key (G-address) of the issuing account.
* Omit or leave undefined for the native asset (`XLM`).
*/
assetIssuer?: string;
/**
* Optional scale/precision for numeric amounts.
*/
scale?: number;
}
/**
* Helper to safely construct a {@link MoneyAmount} object from numeric or string amounts.
*
* If `amount` is a number, it will be safely converted via {@link toAmountString}
* avoiding floating-point precision loss. If it is already a string, it will be validated
* to ensure it is a valid base-10 decimal string.
*
* @param amountOrOptions - An options object or the amount (number | string).
* @param assetCode - The asset code (when calling with positional arguments).
* @param assetIssuer - Optional issuer public key (when calling with positional arguments).
* @param scale - Optional scale for numeric rounding (when calling with positional arguments).
* @returns A validated {@link MoneyAmount} object.
*
* @example
* ```ts
* const native = toMoneyAmount(0.1 + 0.2, 'XLM');
* // { assetCode: 'XLM', amount: '0.3' }
*
* const usdc = toMoneyAmount({
* amount: 100.5,
* assetCode: 'USDC',
* assetIssuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN',
* scale: 2,
* });
* // { assetCode: 'USDC', assetIssuer: '...', amount: '100.50' }
* ```
*/
export function toMoneyAmount(
amountOrOptions: number | string | ToMoneyAmountOptions,
assetCode?: string,
assetIssuer?: string,
scale?: number,
): MoneyAmount {
let amountVal: number | string;
let code: string;
let issuer: string | undefined;
let targetScale: number | undefined;
if (typeof amountOrOptions === 'object' && amountOrOptions !== null) {
amountVal = amountOrOptions.amount;
code = amountOrOptions.assetCode;
issuer = amountOrOptions.assetIssuer;
targetScale = amountOrOptions.scale;
} else {
amountVal = amountOrOptions;
code = assetCode ?? '';
issuer = assetIssuer;
targetScale = scale;
}
if (!code || typeof code !== 'string') {
throw new RangeError('assetCode must be a non-empty string.');
}
const finalAmount =
typeof amountVal === 'number'
? toAmountString(amountVal, targetScale)
: amountVal;
if (
typeof finalAmount !== 'string' ||
!DECIMAL_AMOUNT_PATTERN.test(finalAmount)
) {
throw new RangeError(
`MoneyAmount.amount must be a base-10 decimal string, got ${JSON.stringify(finalAmount)}.`,
);
}
const result: MoneyAmount = {
assetCode: code,
amount: finalAmount,
};
if (issuer) {
result.assetIssuer = issuer;
}
return result;
}