@lightsparkdev/core 1.0.20 → 1.0.22
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/CHANGELOG.md +13 -0
- package/dist/{chunk-ZPGGNCAE.js → chunk-RC2KOZKZ.js} +134 -123
- package/dist/{index-324f8ba4.d.ts → index-9ecb1570.d.ts} +30 -74
- package/dist/index.cjs +139 -125
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -5
- package/dist/utils/index.cjs +135 -123
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +5 -3
- package/package.json +1 -1
- package/src/crypto/crypto.ts +6 -2
- package/src/crypto/tests/crypto.test.ts +5 -0
- package/src/utils/currency.ts +46 -88
- package/src/utils/types.ts +6 -0
package/src/utils/currency.ts
CHANGED
|
@@ -7,75 +7,35 @@ import { isNumber, round } from "./numbers.js";
|
|
|
7
7
|
|
|
8
8
|
export const defaultCurrencyCode = "USD";
|
|
9
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
|
-
* instead when possible. *
|
|
36
|
-
*/
|
|
37
|
-
MILLISATOSHI = "MILLISATOSHI",
|
|
38
|
-
/** United States Dollar. **/
|
|
39
|
-
USD = "USD",
|
|
40
|
-
/**
|
|
41
|
-
* 0.000000001 (10e-9) Bitcoin or a billionth of a Bitcoin.
|
|
42
|
-
* We recommend using the Satoshi unit instead when possible.
|
|
43
|
-
* *
|
|
44
|
-
*/
|
|
45
|
-
NANOBITCOIN = "NANOBITCOIN",
|
|
46
|
-
/**
|
|
47
|
-
* 0.000001 (10e-6) Bitcoin or a millionth of a Bitcoin.
|
|
48
|
-
* We recommend using the Satoshi unit instead when possible.
|
|
49
|
-
* *
|
|
50
|
-
*/
|
|
51
|
-
MICROBITCOIN = "MICROBITCOIN",
|
|
52
|
-
/**
|
|
53
|
-
* 0.001 (10e-3) Bitcoin or a thousandth of a Bitcoin.
|
|
54
|
-
* We recommend using the Satoshi unit instead when possible.
|
|
55
|
-
* *
|
|
56
|
-
*/
|
|
57
|
-
MILLIBITCOIN = "MILLIBITCOIN",
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** This object represents the value and unit for an amount of currency. **/
|
|
61
|
-
export type CurrencyAmountType = {
|
|
62
|
-
/** The original numeric value for this CurrencyAmount. **/
|
|
10
|
+
/* This const identifies the unit of currency associated with a CurrencyAmount as created by the SDK
|
|
11
|
+
* writer and used in JS SDKs. The schema version uses camel case for the keys so we convert
|
|
12
|
+
* arguments of that type to this format for use in the functions below. */
|
|
13
|
+
export const CurrencyUnit = {
|
|
14
|
+
FUTURE_VALUE: "FUTURE_VALUE",
|
|
15
|
+
BITCOIN: "BITCOIN",
|
|
16
|
+
SATOSHI: "SATOSHI",
|
|
17
|
+
MILLISATOSHI: "MILLISATOSHI",
|
|
18
|
+
USD: "USD",
|
|
19
|
+
NANOBITCOIN: "NANOBITCOIN",
|
|
20
|
+
MICROBITCOIN: "MICROBITCOIN",
|
|
21
|
+
MILLIBITCOIN: "MILLIBITCOIN",
|
|
22
|
+
|
|
23
|
+
Bitcoin: "BITCOIN",
|
|
24
|
+
Microbitcoin: "MICROBITCOIN",
|
|
25
|
+
Millibitcoin: "MILLIBITCOIN",
|
|
26
|
+
Millisatoshi: "MILLISATOSHI",
|
|
27
|
+
Nanobitcoin: "NANOBITCOIN",
|
|
28
|
+
Satoshi: "SATOSHI",
|
|
29
|
+
Usd: "USD",
|
|
30
|
+
} as const;
|
|
31
|
+
|
|
32
|
+
export type CurrencyUnitType = (typeof CurrencyUnit)[keyof typeof CurrencyUnit];
|
|
33
|
+
|
|
34
|
+
export type SDKCurrencyAmountType = {
|
|
63
35
|
originalValue: number;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
/** The unit of user's preferred currency. **/
|
|
67
|
-
preferredCurrencyUnit: CurrencyUnit;
|
|
68
|
-
/**
|
|
69
|
-
* The rounded numeric value for this CurrencyAmount in the very base level
|
|
70
|
-
* of user's preferred currency. For example, for USD, the value will be in
|
|
71
|
-
* cents.
|
|
72
|
-
**/
|
|
36
|
+
originalUnit: CurrencyUnitType;
|
|
37
|
+
preferredCurrencyUnit: CurrencyUnitType;
|
|
73
38
|
preferredCurrencyValueRounded: number;
|
|
74
|
-
/**
|
|
75
|
-
* The approximate float value for this CurrencyAmount in the very base level
|
|
76
|
-
* of user's preferred currency. For example, for USD, the value will be in
|
|
77
|
-
* cents.
|
|
78
|
-
**/
|
|
79
39
|
preferredCurrencyValueApprox: number;
|
|
80
40
|
};
|
|
81
41
|
|
|
@@ -163,8 +123,8 @@ const CONVERSION_MAP = {
|
|
|
163
123
|
};
|
|
164
124
|
|
|
165
125
|
export function convertCurrencyAmountValue(
|
|
166
|
-
fromUnit:
|
|
167
|
-
toUnit:
|
|
126
|
+
fromUnit: CurrencyUnitType,
|
|
127
|
+
toUnit: CurrencyUnitType,
|
|
168
128
|
amount: number,
|
|
169
129
|
centsPerBtc = 1,
|
|
170
130
|
): number {
|
|
@@ -191,9 +151,9 @@ export function convertCurrencyAmountValue(
|
|
|
191
151
|
}
|
|
192
152
|
|
|
193
153
|
export const convertCurrencyAmount = (
|
|
194
|
-
from:
|
|
195
|
-
toUnit:
|
|
196
|
-
):
|
|
154
|
+
from: SDKCurrencyAmountType,
|
|
155
|
+
toUnit: CurrencyUnitType,
|
|
156
|
+
): SDKCurrencyAmountType => {
|
|
197
157
|
const value = convertCurrencyAmountValue(
|
|
198
158
|
from.originalUnit,
|
|
199
159
|
toUnit,
|
|
@@ -240,21 +200,17 @@ export type CurrencyMap = {
|
|
|
240
200
|
};
|
|
241
201
|
|
|
242
202
|
export type CurrencyAmountObj = {
|
|
243
|
-
/*
|
|
244
|
-
*
|
|
245
|
-
* always a number.
|
|
246
|
-
* We are intentionally widening the type here to allow for more forgiving
|
|
247
|
-
* input:
|
|
248
|
-
*/
|
|
203
|
+
/* Technically the generated graphql schema has value as `any` but it's always a number.
|
|
204
|
+
* We are intentionally widening the type here to allow for more forgiving input: */
|
|
249
205
|
value?: number | string | null;
|
|
250
206
|
/* assume satoshi if not provided */
|
|
251
|
-
unit?:
|
|
252
|
-
__typename?: "CurrencyAmount";
|
|
207
|
+
unit?: CurrencyUnitType;
|
|
208
|
+
__typename?: "CurrencyAmount" | undefined;
|
|
253
209
|
};
|
|
254
210
|
|
|
255
211
|
export type CurrencyAmountArg =
|
|
256
212
|
| CurrencyAmountObj
|
|
257
|
-
|
|
|
213
|
+
| SDKCurrencyAmountType
|
|
258
214
|
| undefined
|
|
259
215
|
| null;
|
|
260
216
|
|
|
@@ -264,10 +220,13 @@ export function isCurrencyAmountObj(arg: unknown): arg is CurrencyAmountObj {
|
|
|
264
220
|
);
|
|
265
221
|
}
|
|
266
222
|
|
|
267
|
-
export function
|
|
223
|
+
export function isSDKCurrencyAmount(
|
|
224
|
+
arg: unknown,
|
|
225
|
+
): arg is SDKCurrencyAmountType {
|
|
268
226
|
return (
|
|
269
227
|
typeof arg === "object" &&
|
|
270
228
|
arg !== null &&
|
|
229
|
+
/* We can expect all SDK CurrencyAmount types to always have these exact properties: */
|
|
271
230
|
"originalValue" in arg &&
|
|
272
231
|
"originalUnit" in arg &&
|
|
273
232
|
"preferredCurrencyUnit" in arg &&
|
|
@@ -275,7 +234,6 @@ export function isCurrencyAmount(arg: unknown): arg is CurrencyAmountType {
|
|
|
275
234
|
"preferredCurrencyValueApprox" in arg
|
|
276
235
|
);
|
|
277
236
|
}
|
|
278
|
-
|
|
279
237
|
function asNumber(value: string | number | null | undefined) {
|
|
280
238
|
if (typeof value === "string") {
|
|
281
239
|
return Number(value);
|
|
@@ -287,12 +245,12 @@ function getCurrencyAmount(currencyAmountArg: CurrencyAmountArg) {
|
|
|
287
245
|
let value = 0;
|
|
288
246
|
let unit = undefined;
|
|
289
247
|
|
|
290
|
-
if (
|
|
291
|
-
value = asNumber(currencyAmountArg.value);
|
|
292
|
-
unit = currencyAmountArg.unit;
|
|
293
|
-
} else if (isCurrencyAmount(currencyAmountArg)) {
|
|
248
|
+
if (isSDKCurrencyAmount(currencyAmountArg)) {
|
|
294
249
|
value = currencyAmountArg.originalValue;
|
|
295
250
|
unit = currencyAmountArg.originalUnit;
|
|
251
|
+
} else if (isCurrencyAmountObj(currencyAmountArg)) {
|
|
252
|
+
value = asNumber(currencyAmountArg.value);
|
|
253
|
+
unit = currencyAmountArg.unit;
|
|
296
254
|
}
|
|
297
255
|
|
|
298
256
|
return {
|
|
@@ -410,7 +368,7 @@ export const isCurrencyMap = (
|
|
|
410
368
|
typeof currencyMap.type === "string" &&
|
|
411
369
|
currencyMap.type === "CurrencyMap";
|
|
412
370
|
|
|
413
|
-
export const abbrCurrencyUnit = (unit:
|
|
371
|
+
export const abbrCurrencyUnit = (unit: CurrencyUnitType) => {
|
|
414
372
|
switch (unit) {
|
|
415
373
|
case CurrencyUnit.BITCOIN:
|
|
416
374
|
return "BTC";
|
package/src/utils/types.ts
CHANGED
|
@@ -33,3 +33,9 @@ export type JSONType = JSONLiteral | JSONType[] | { [key: string]: JSONType };
|
|
|
33
33
|
export type JSONObject = { [key: string]: JSONType };
|
|
34
34
|
|
|
35
35
|
export type NN<T> = NonNullable<T>;
|
|
36
|
+
|
|
37
|
+
export function notNullUndefined<TValue>(
|
|
38
|
+
value: TValue | null | undefined,
|
|
39
|
+
): value is TValue {
|
|
40
|
+
return value !== null && value !== undefined;
|
|
41
|
+
}
|