@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.
@@ -7,75 +7,35 @@ import { isNumber, round } from "./numbers.js";
7
7
 
8
8
  export const defaultCurrencyCode = "USD";
9
9
 
10
- /**
11
- * This enum identifies the unit of currency associated with a CurrencyAmount.
12
- * *
13
- */
14
- export enum CurrencyUnit {
15
- /**
16
- * This is an enum value that represents values that could be added in the
17
- * future. Clients should support unknown values as more of them could be
18
- * added without notice.
19
- */
20
- FUTURE_VALUE = "FUTURE_VALUE",
21
- /**
22
- * Bitcoin is the cryptocurrency native to the Bitcoin network.
23
- * It is used as the native medium for value transfer for the Lightning
24
- * Network. *
25
- */
26
- BITCOIN = "BITCOIN",
27
- /**
28
- * 0.00000001 (10e-8) Bitcoin or one hundred millionth of a Bitcoin.
29
- * This is the unit most commonly used in Lightning transactions.
30
- * *
31
- */
32
- SATOSHI = "SATOSHI",
33
- /**
34
- * 0.001 Satoshi, or 10e-11 Bitcoin. We recommend using the Satoshi unit
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
- /** The original unit of currency for this CurrencyAmount. **/
65
- originalUnit: CurrencyUnit;
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: CurrencyUnit,
167
- toUnit: CurrencyUnit,
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: CurrencyAmountType,
195
- toUnit: CurrencyUnit,
196
- ): CurrencyAmountType => {
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
- * Technically the generated graphql schema has value as `any` but it's
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?: CurrencyUnit;
252
- __typename?: "CurrencyAmount";
207
+ unit?: CurrencyUnitType;
208
+ __typename?: "CurrencyAmount" | undefined;
253
209
  };
254
210
 
255
211
  export type CurrencyAmountArg =
256
212
  | CurrencyAmountObj
257
- | CurrencyAmountType
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 isCurrencyAmount(arg: unknown): arg is CurrencyAmountType {
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 (isCurrencyAmountObj(currencyAmountArg)) {
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: CurrencyUnit) => {
371
+ export const abbrCurrencyUnit = (unit: CurrencyUnitType) => {
414
372
  switch (unit) {
415
373
  case CurrencyUnit.BITCOIN:
416
374
  return "BTC";
@@ -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
+ }