@fleet-sdk/common 0.3.4 → 0.6.4

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @fleet-sdk/common
2
2
 
3
+ ## 0.6.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 504974e: Fix `utxoDiff` miscalculation when tokens are present in subtrahend but not in minuend
8
+
9
+ ## 0.4.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 28e3467: Fix `ensureDefaults()` behavior with `undefined` fields
14
+
3
15
  ## 0.3.4
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -7,20 +7,6 @@ type SortingSelector<T> = (item: T) => string | number | bigint;
7
7
  type SortingDirection = "asc" | "desc";
8
8
  type FilterPredicate<T> = (item: T) => boolean;
9
9
  type BuildOutputType = "default" | "EIP-12";
10
- declare enum Network {
11
- Mainnet = 0,
12
- Testnet = 16
13
- }
14
- declare enum AddressType {
15
- P2PK = 1,
16
- P2SH = 2,
17
- P2S = 3
18
- }
19
- declare const ergoTreeHeaderFlags: {
20
- readonly sizeInclusion: 8;
21
- readonly constantSegregation: 16;
22
- };
23
- type ErgoTreeHeaderFlag = (typeof ergoTreeHeaderFlags)[keyof typeof ergoTreeHeaderFlags];
24
10
 
25
11
  type NonMandatoryRegisters<T = HexString> = {
26
12
  R4?: T;
@@ -162,6 +148,23 @@ type Block = {
162
148
  size: number;
163
149
  };
164
150
 
151
+ type EnumConst<T extends object> = T[keyof T];
152
+ declare enum Network {
153
+ Mainnet = 0,
154
+ Testnet = 16
155
+ }
156
+ declare enum AddressType {
157
+ P2PK = 1,
158
+ P2SH = 2,
159
+ P2S = 3,
160
+ ADH = 4
161
+ }
162
+ declare const ergoTreeHeaderFlags: {
163
+ readonly sizeInclusion: 8;
164
+ readonly constantSegregation: 16;
165
+ };
166
+ type ErgoTreeHeaderFlag = EnumConst<typeof ergoTreeHeaderFlags>;
167
+
165
168
  type ObjectSelector<T> = (item: T) => T[keyof T];
166
169
  /**
167
170
  * Returns the first element of an array.
@@ -354,7 +357,7 @@ type ParsingOptions = {
354
357
  };
355
358
  /**
356
359
  * Parse a decimal string into a bigint with options
357
- * @param decimalStr
360
+ * @param input
358
361
  * @param options
359
362
  *
360
363
  * @example
@@ -362,7 +365,7 @@ type ParsingOptions = {
362
365
  * undecimalize("1", { decimals: 2 }) // 100n
363
366
  * undecimalize("1", 2) // 100n
364
367
  */
365
- declare function undecimalize(decimalStr: string, options?: ParsingOptions | number): bigint;
368
+ declare function undecimalize(input: string, options?: ParsingOptions | number): bigint;
366
369
  type FormattingOptions = {
367
370
  /**
368
371
  * Number of decimals.
@@ -450,8 +453,8 @@ declare function max<T extends bigint | number>(...numbers: T[]): T;
450
453
  * // { nanoErgs: 30n, tokens: [{ tokenId: "test", amount: 50n }] }
451
454
  * ```
452
455
  */
453
- declare function utxoSum(boxes: MinimalBoxAmountsArray): BoxSummary;
454
- declare function utxoSum(boxes: MinimalBoxAmountsArray, tokenId: TokenId): bigint;
456
+ declare function utxoSum(boxes: readonly BoxAmounts[]): BoxSummary;
457
+ declare function utxoSum(boxes: readonly BoxAmounts[], tokenId: TokenId): bigint;
455
458
  /**
456
459
  * Calculates the difference between two utxos or utxo sets.
457
460
  * @param minuend
@@ -466,7 +469,7 @@ declare function utxoSum(boxes: MinimalBoxAmountsArray, tokenId: TokenId): bigin
466
469
  * // { nanoErgs: 20n, tokens: [{ tokenId: "test", amount: 30n }] }
467
470
  * ```
468
471
  */
469
- declare function utxoDiff(minuend: BoxSummary | Box<Amount>[], subtrahend: BoxSummary | Box<Amount>[]): BoxSummary;
472
+ declare function utxoDiff(minuend: BoxSummary | Box<Amount>[], subtrahend: BoxSummary | Box<Amount>[], ignoreSubtrahendLeftoverTokens?: boolean): BoxSummary;
470
473
  /**
471
474
  * Checks if the given registers are densely packed.
472
475
  * @param registers
@@ -514,10 +517,10 @@ type BoxSummary = {
514
517
  nanoErgs: bigint;
515
518
  tokens: TokenAmount<bigint>[];
516
519
  };
517
- type MinimalBoxAmountsArray = readonly {
520
+ type BoxAmounts = {
518
521
  value: Amount;
519
522
  assets: TokenAmount<Amount>[];
520
- }[];
523
+ };
521
524
  /**
522
525
  * Ensures that the value and asset amounts of a given box are represented as BigInts.
523
526
  * @returns A new box object with BigInt representation for the value and asset amounts.
@@ -537,9 +540,12 @@ declare function ensureUTxOBigInt(candidate: BoxCandidate<Amount>): BoxCandidate
537
540
  * ```
538
541
  */
539
542
  declare function clearUndefined(value: Record<string, unknown>): Record<string, unknown>;
543
+ type EnsureDefaultsOptions = {
544
+ keepUndefinedKeys: boolean;
545
+ };
540
546
  /**
541
547
  * Ensure that the options object has all the default values
542
- * @param options
548
+ * @param partial
543
549
  * @param defaults
544
550
  *
545
551
  * @example
@@ -550,13 +556,13 @@ declare function clearUndefined(value: Record<string, unknown>): Record<string,
550
556
  * console.log(result); // { a: 1, b: 3 }
551
557
  * ```
552
558
  */
553
- declare function ensureDefaults<T extends object, R extends object>(options: T | undefined, defaults: R): R & T;
559
+ declare function ensureDefaults<T extends object, R extends object>(partial: T | undefined, defaults: R, options?: EnsureDefaultsOptions): R & T;
554
560
 
555
- type AssertErrorMessageInput = string | Error | (() => string);
561
+ type ErrorMessage = string | Error | (() => string);
556
562
  type Constructable = Function;
557
- type JSPrimitiveTypes = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
558
- declare function assert(condition: boolean, error: AssertErrorMessageInput): asserts condition;
559
- declare function assertTypeOf<T>(obj: T, expected: JSPrimitiveTypes): asserts obj;
563
+ type JSPrimitive = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
564
+ declare function assert(condition: boolean, error: ErrorMessage): asserts condition;
565
+ declare function assertTypeOf<T>(obj: T, expected: JSPrimitive): asserts obj;
560
566
  declare function assertInstanceOf<T>(obj: T, expected: Constructable): asserts obj;
561
567
  declare function isEmpty<T>(target: T | null | undefined): target is undefined | null;
562
568
  declare function some<T>(target: T | null | undefined): target is T;
@@ -639,10 +645,8 @@ declare class FleetError extends Error {
639
645
  constructor(message?: string, options?: ErrorOptions);
640
646
  }
641
647
  declare class NotSupportedError extends FleetError {
642
- constructor(message?: string);
643
648
  }
644
649
  declare class BlockchainProviderError extends FleetError {
645
- constructor(message?: string, options?: ErrorOptions);
646
650
  }
647
651
 
648
- export { AddressType, Amount, AssertErrorMessageInput, Base58String, Block, BlockHeader, BlockHeaderId, BlockTransactions, BlockchainProviderError, Box, BoxCandidate, BoxId, BoxSummary, BuildOutputType, Collection, CollectionAddOptions, ContextExtension, DataInput, EIP12UnsignedDataInput, EIP12UnsignedInput, EIP12UnsignedTransaction, ErgoTreeHeaderFlag, ErgoTreeHex, FilterPredicate, FleetError, HexString, MinimalBoxAmountsArray, Network, NewToken, NonMandatoryRegisters, NotSupportedError, OneOrMore, PoWSolution, ProverResult, SignedInput, SignedTransaction, SortingDirection, SortingSelector, TokenAmount, TokenId, TokenTargetAmount, TransactionId, UTxOFilterParams, UnsignedInput, UnsignedTransaction, _0n, _10n, _127n, _128n, _1n, _63n, _7n, areEqual, areEqualBy, areRegistersDenselyPacked, assert, assertInstanceOf, assertTypeOf, at, byteSizeOf, chunk, clearUndefined, concatBytes, decimalize, depthOf, endsWith, ensureBigInt, ensureDefaults, ensureUTxOBigInt, ergoTreeHeaderFlags, first, hasDuplicates, hasDuplicatesBy, hasKey, isDefined, isEmpty, isFalsy, isHex, isTruthy, isUndefined, last, max, min, orderBy, percent, some, startsWith, sumBy, undecimalize, uniq, uniqBy, utxoDiff, utxoFilter, utxoSum };
652
+ export { AddressType, type Amount, type Base58String, type Block, type BlockHeader, type BlockHeaderId, type BlockTransactions, BlockchainProviderError, type Box, type BoxAmounts, type BoxCandidate, type BoxId, type BoxSummary, type BuildOutputType, Collection, type CollectionAddOptions, type ContextExtension, type DataInput, type EIP12UnsignedDataInput, type EIP12UnsignedInput, type EIP12UnsignedTransaction, type EnsureDefaultsOptions, type EnumConst, type ErgoTreeHeaderFlag, type ErgoTreeHex, type ErrorMessage, type FilterPredicate, FleetError, type HexString, Network, type NewToken, type NonMandatoryRegisters, NotSupportedError, type OneOrMore, type PoWSolution, type ProverResult, type SignedInput, type SignedTransaction, type SortingDirection, type SortingSelector, type TokenAmount, type TokenId, type TokenTargetAmount, type TransactionId, type UTxOFilterParams, type UnsignedInput, type UnsignedTransaction, _0n, _10n, _127n, _128n, _1n, _63n, _7n, areEqual, areEqualBy, areRegistersDenselyPacked, assert, assertInstanceOf, assertTypeOf, at, byteSizeOf, chunk, clearUndefined, concatBytes, decimalize, depthOf, endsWith, ensureBigInt, ensureDefaults, ensureUTxOBigInt, ergoTreeHeaderFlags, first, hasDuplicates, hasDuplicatesBy, hasKey, isDefined, isEmpty, isFalsy, isHex, isTruthy, isUndefined, last, max, min, orderBy, percent, some, startsWith, sumBy, undecimalize, uniq, uniqBy, utxoDiff, utxoFilter, utxoSum };
package/dist/index.d.ts CHANGED
@@ -7,20 +7,6 @@ type SortingSelector<T> = (item: T) => string | number | bigint;
7
7
  type SortingDirection = "asc" | "desc";
8
8
  type FilterPredicate<T> = (item: T) => boolean;
9
9
  type BuildOutputType = "default" | "EIP-12";
10
- declare enum Network {
11
- Mainnet = 0,
12
- Testnet = 16
13
- }
14
- declare enum AddressType {
15
- P2PK = 1,
16
- P2SH = 2,
17
- P2S = 3
18
- }
19
- declare const ergoTreeHeaderFlags: {
20
- readonly sizeInclusion: 8;
21
- readonly constantSegregation: 16;
22
- };
23
- type ErgoTreeHeaderFlag = (typeof ergoTreeHeaderFlags)[keyof typeof ergoTreeHeaderFlags];
24
10
 
25
11
  type NonMandatoryRegisters<T = HexString> = {
26
12
  R4?: T;
@@ -162,6 +148,23 @@ type Block = {
162
148
  size: number;
163
149
  };
164
150
 
151
+ type EnumConst<T extends object> = T[keyof T];
152
+ declare enum Network {
153
+ Mainnet = 0,
154
+ Testnet = 16
155
+ }
156
+ declare enum AddressType {
157
+ P2PK = 1,
158
+ P2SH = 2,
159
+ P2S = 3,
160
+ ADH = 4
161
+ }
162
+ declare const ergoTreeHeaderFlags: {
163
+ readonly sizeInclusion: 8;
164
+ readonly constantSegregation: 16;
165
+ };
166
+ type ErgoTreeHeaderFlag = EnumConst<typeof ergoTreeHeaderFlags>;
167
+
165
168
  type ObjectSelector<T> = (item: T) => T[keyof T];
166
169
  /**
167
170
  * Returns the first element of an array.
@@ -354,7 +357,7 @@ type ParsingOptions = {
354
357
  };
355
358
  /**
356
359
  * Parse a decimal string into a bigint with options
357
- * @param decimalStr
360
+ * @param input
358
361
  * @param options
359
362
  *
360
363
  * @example
@@ -362,7 +365,7 @@ type ParsingOptions = {
362
365
  * undecimalize("1", { decimals: 2 }) // 100n
363
366
  * undecimalize("1", 2) // 100n
364
367
  */
365
- declare function undecimalize(decimalStr: string, options?: ParsingOptions | number): bigint;
368
+ declare function undecimalize(input: string, options?: ParsingOptions | number): bigint;
366
369
  type FormattingOptions = {
367
370
  /**
368
371
  * Number of decimals.
@@ -450,8 +453,8 @@ declare function max<T extends bigint | number>(...numbers: T[]): T;
450
453
  * // { nanoErgs: 30n, tokens: [{ tokenId: "test", amount: 50n }] }
451
454
  * ```
452
455
  */
453
- declare function utxoSum(boxes: MinimalBoxAmountsArray): BoxSummary;
454
- declare function utxoSum(boxes: MinimalBoxAmountsArray, tokenId: TokenId): bigint;
456
+ declare function utxoSum(boxes: readonly BoxAmounts[]): BoxSummary;
457
+ declare function utxoSum(boxes: readonly BoxAmounts[], tokenId: TokenId): bigint;
455
458
  /**
456
459
  * Calculates the difference between two utxos or utxo sets.
457
460
  * @param minuend
@@ -466,7 +469,7 @@ declare function utxoSum(boxes: MinimalBoxAmountsArray, tokenId: TokenId): bigin
466
469
  * // { nanoErgs: 20n, tokens: [{ tokenId: "test", amount: 30n }] }
467
470
  * ```
468
471
  */
469
- declare function utxoDiff(minuend: BoxSummary | Box<Amount>[], subtrahend: BoxSummary | Box<Amount>[]): BoxSummary;
472
+ declare function utxoDiff(minuend: BoxSummary | Box<Amount>[], subtrahend: BoxSummary | Box<Amount>[], ignoreSubtrahendLeftoverTokens?: boolean): BoxSummary;
470
473
  /**
471
474
  * Checks if the given registers are densely packed.
472
475
  * @param registers
@@ -514,10 +517,10 @@ type BoxSummary = {
514
517
  nanoErgs: bigint;
515
518
  tokens: TokenAmount<bigint>[];
516
519
  };
517
- type MinimalBoxAmountsArray = readonly {
520
+ type BoxAmounts = {
518
521
  value: Amount;
519
522
  assets: TokenAmount<Amount>[];
520
- }[];
523
+ };
521
524
  /**
522
525
  * Ensures that the value and asset amounts of a given box are represented as BigInts.
523
526
  * @returns A new box object with BigInt representation for the value and asset amounts.
@@ -537,9 +540,12 @@ declare function ensureUTxOBigInt(candidate: BoxCandidate<Amount>): BoxCandidate
537
540
  * ```
538
541
  */
539
542
  declare function clearUndefined(value: Record<string, unknown>): Record<string, unknown>;
543
+ type EnsureDefaultsOptions = {
544
+ keepUndefinedKeys: boolean;
545
+ };
540
546
  /**
541
547
  * Ensure that the options object has all the default values
542
- * @param options
548
+ * @param partial
543
549
  * @param defaults
544
550
  *
545
551
  * @example
@@ -550,13 +556,13 @@ declare function clearUndefined(value: Record<string, unknown>): Record<string,
550
556
  * console.log(result); // { a: 1, b: 3 }
551
557
  * ```
552
558
  */
553
- declare function ensureDefaults<T extends object, R extends object>(options: T | undefined, defaults: R): R & T;
559
+ declare function ensureDefaults<T extends object, R extends object>(partial: T | undefined, defaults: R, options?: EnsureDefaultsOptions): R & T;
554
560
 
555
- type AssertErrorMessageInput = string | Error | (() => string);
561
+ type ErrorMessage = string | Error | (() => string);
556
562
  type Constructable = Function;
557
- type JSPrimitiveTypes = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
558
- declare function assert(condition: boolean, error: AssertErrorMessageInput): asserts condition;
559
- declare function assertTypeOf<T>(obj: T, expected: JSPrimitiveTypes): asserts obj;
563
+ type JSPrimitive = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
564
+ declare function assert(condition: boolean, error: ErrorMessage): asserts condition;
565
+ declare function assertTypeOf<T>(obj: T, expected: JSPrimitive): asserts obj;
560
566
  declare function assertInstanceOf<T>(obj: T, expected: Constructable): asserts obj;
561
567
  declare function isEmpty<T>(target: T | null | undefined): target is undefined | null;
562
568
  declare function some<T>(target: T | null | undefined): target is T;
@@ -639,10 +645,8 @@ declare class FleetError extends Error {
639
645
  constructor(message?: string, options?: ErrorOptions);
640
646
  }
641
647
  declare class NotSupportedError extends FleetError {
642
- constructor(message?: string);
643
648
  }
644
649
  declare class BlockchainProviderError extends FleetError {
645
- constructor(message?: string, options?: ErrorOptions);
646
650
  }
647
651
 
648
- export { AddressType, Amount, AssertErrorMessageInput, Base58String, Block, BlockHeader, BlockHeaderId, BlockTransactions, BlockchainProviderError, Box, BoxCandidate, BoxId, BoxSummary, BuildOutputType, Collection, CollectionAddOptions, ContextExtension, DataInput, EIP12UnsignedDataInput, EIP12UnsignedInput, EIP12UnsignedTransaction, ErgoTreeHeaderFlag, ErgoTreeHex, FilterPredicate, FleetError, HexString, MinimalBoxAmountsArray, Network, NewToken, NonMandatoryRegisters, NotSupportedError, OneOrMore, PoWSolution, ProverResult, SignedInput, SignedTransaction, SortingDirection, SortingSelector, TokenAmount, TokenId, TokenTargetAmount, TransactionId, UTxOFilterParams, UnsignedInput, UnsignedTransaction, _0n, _10n, _127n, _128n, _1n, _63n, _7n, areEqual, areEqualBy, areRegistersDenselyPacked, assert, assertInstanceOf, assertTypeOf, at, byteSizeOf, chunk, clearUndefined, concatBytes, decimalize, depthOf, endsWith, ensureBigInt, ensureDefaults, ensureUTxOBigInt, ergoTreeHeaderFlags, first, hasDuplicates, hasDuplicatesBy, hasKey, isDefined, isEmpty, isFalsy, isHex, isTruthy, isUndefined, last, max, min, orderBy, percent, some, startsWith, sumBy, undecimalize, uniq, uniqBy, utxoDiff, utxoFilter, utxoSum };
652
+ export { AddressType, type Amount, type Base58String, type Block, type BlockHeader, type BlockHeaderId, type BlockTransactions, BlockchainProviderError, type Box, type BoxAmounts, type BoxCandidate, type BoxId, type BoxSummary, type BuildOutputType, Collection, type CollectionAddOptions, type ContextExtension, type DataInput, type EIP12UnsignedDataInput, type EIP12UnsignedInput, type EIP12UnsignedTransaction, type EnsureDefaultsOptions, type EnumConst, type ErgoTreeHeaderFlag, type ErgoTreeHex, type ErrorMessage, type FilterPredicate, FleetError, type HexString, Network, type NewToken, type NonMandatoryRegisters, NotSupportedError, type OneOrMore, type PoWSolution, type ProverResult, type SignedInput, type SignedTransaction, type SortingDirection, type SortingSelector, type TokenAmount, type TokenId, type TokenTargetAmount, type TransactionId, type UTxOFilterParams, type UnsignedInput, type UnsignedTransaction, _0n, _10n, _127n, _128n, _1n, _63n, _7n, areEqual, areEqualBy, areRegistersDenselyPacked, assert, assertInstanceOf, assertTypeOf, at, byteSizeOf, chunk, clearUndefined, concatBytes, decimalize, depthOf, endsWith, ensureBigInt, ensureDefaults, ensureUTxOBigInt, ergoTreeHeaderFlags, first, hasDuplicates, hasDuplicatesBy, hasKey, isDefined, isEmpty, isFalsy, isHex, isTruthy, isUndefined, last, max, min, orderBy, percent, some, startsWith, sumBy, undecimalize, uniq, uniqBy, utxoDiff, utxoFilter, utxoSum };
package/dist/index.js CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  // src/utils/assertions.ts
4
4
  function assert(condition, error) {
5
- if (condition)
6
- return;
5
+ if (condition) return;
7
6
  let err = void 0;
8
7
  switch (typeof error) {
9
8
  case "string":
@@ -23,22 +22,21 @@ function assertTypeOf(obj, expected) {
23
22
  throw new Error(`Expected an object of type '${expected}', got '${type}'.`);
24
23
  }
25
24
  }
26
- var toString = (value) => Object.prototype.toString.call(value);
27
25
  function getTypeName(value) {
28
- if (value === null)
29
- return "null";
26
+ if (value === null) return "null";
30
27
  const type = typeof value;
31
- return type === "object" || type === "function" ? toString(value).slice(8, -1) : type;
28
+ return type === "object" || type === "function" ? Object.prototype.toString.call(value).slice(8, -1) : type;
32
29
  }
33
30
  function assertInstanceOf(obj, expected) {
34
31
  const condition = obj instanceof expected;
35
32
  if (!condition) {
36
- throw new Error(`Expected an instance of '${expected.name}', got '${getTypeName(obj)}'.`);
33
+ throw new Error(
34
+ `Expected an instance of '${expected.name}', got '${getTypeName(obj)}'.`
35
+ );
37
36
  }
38
37
  }
39
38
  function isEmpty(target) {
40
- if (!target)
41
- return true;
39
+ if (!target) return true;
42
40
  return Array.isArray(target) ? target.length === 0 : Object.keys(target).length === 0;
43
41
  }
44
42
  function some(target) {
@@ -62,21 +60,18 @@ function hasKey(o, key) {
62
60
 
63
61
  // src/utils/array.ts
64
62
  function first(array) {
65
- if (!array)
66
- return void 0;
63
+ if (!array) return void 0;
67
64
  assert(array.length > 0, "Empty array.");
68
65
  return array[0];
69
66
  }
70
67
  function last(array) {
71
- if (!array)
72
- return void 0;
68
+ if (!array) return void 0;
73
69
  assert(array.length > 0, "Empty array.");
74
70
  return at(array, -1);
75
71
  }
76
72
  function at(array, index) {
77
73
  const len = array?.length;
78
- if (!len)
79
- return void 0;
74
+ if (!len) return void 0;
80
75
  if (index < 0) {
81
76
  index += len;
82
77
  }
@@ -104,20 +99,16 @@ function chunk(array, size) {
104
99
  }
105
100
  function orderBy(array, iteratee, order = "asc") {
106
101
  return [...array].sort((a, b) => {
107
- if (iteratee(a) > iteratee(b)) {
108
- return order === "asc" ? 1 : -1;
109
- } else if (iteratee(a) < iteratee(b)) {
110
- return order === "asc" ? -1 : 1;
111
- } else {
112
- return 0;
113
- }
102
+ if (iteratee(a) > iteratee(b)) return order === "asc" ? 1 : -1;
103
+ if (iteratee(a) < iteratee(b)) return order === "asc" ? -1 : 1;
104
+ return 0;
114
105
  });
115
106
  }
116
107
  function areEqual(array1, array2) {
117
108
  if (array1 === array2) {
118
109
  return true;
119
110
  }
120
- if (array1.length != array2.length) {
111
+ if (array1.length !== array2.length) {
121
112
  return false;
122
113
  }
123
114
  for (let i = 0; i < array1.length; i++) {
@@ -131,7 +122,7 @@ function areEqualBy(array1, array2, selector) {
131
122
  if (array1 === array2) {
132
123
  return true;
133
124
  }
134
- if (array1.length != array2.length) {
125
+ if (array1.length !== array2.length) {
135
126
  return false;
136
127
  }
137
128
  for (let i = 0; i < array1.length; i++) {
@@ -205,22 +196,20 @@ var _128n = BigInt(128);
205
196
  function ensureBigInt(number) {
206
197
  return typeof number === "bigint" ? number : BigInt(number);
207
198
  }
208
- function undecimalize(decimalStr, options) {
209
- if (!decimalStr) {
210
- return _0n;
211
- }
212
- options = typeof options == "number" ? { decimals: options } : options;
199
+ function undecimalize(input, options) {
200
+ if (!input) return _0n;
201
+ options = typeof options === "number" ? { decimals: options } : options;
213
202
  if (isUndefined(options)) {
214
203
  options = {};
215
204
  }
216
205
  options.decimals = options.decimals || 0;
217
206
  options.decimalMark = options.decimalMark || ".";
218
- const fragments = decimalStr.split(options.decimalMark);
207
+ const fragments = input.split(options.decimalMark);
219
208
  if (fragments.length > 2) {
220
209
  throw new Error("Invalid numeric string.");
221
210
  }
222
211
  let [integer, decimal] = fragments;
223
- integer = _removeLeadingZeros(integer);
212
+ integer = removeLeadingZeros(integer);
224
213
  const negative = integer.startsWith("-") ? "-" : "";
225
214
  if (!decimal) {
226
215
  decimal = "0".repeat(options.decimals);
@@ -237,39 +226,36 @@ function decimalize(value, options) {
237
226
  if (!options) {
238
227
  return value.toString();
239
228
  }
240
- options = typeof options == "number" ? { decimals: options } : options;
229
+ options = typeof options === "number" ? { decimals: options } : options;
241
230
  options.decimals = options.decimals || 0;
242
231
  options.decimalMark = options.decimalMark || ".";
243
232
  const pow = _10n ** BigInt(options.decimals);
244
233
  const integer = value / pow;
245
234
  const decimal = value - integer * pow;
246
- return _buildFormattedDecimal(integer.toString(10), decimal.toString(10), options);
235
+ return buildFormattedDecimal(integer.toString(10), decimal.toString(10), options);
247
236
  }
248
237
  function percent(value, percentage, precision = 2n) {
249
238
  return value * percentage / 10n ** precision;
250
239
  }
251
- function _buildFormattedDecimal(integer, decimal, options) {
252
- const integerPart = _addThousandMarks(integer, options.thousandMark);
253
- const decimalPart = _stripTrailingZeros(decimal.padStart(options.decimals, "0"));
254
- if (decimalPart) {
255
- return `${integerPart}${options.decimalMark}${decimalPart}`;
256
- } else {
257
- return integerPart;
258
- }
240
+ function buildFormattedDecimal(integer, decimal, options) {
241
+ const int = addThousandMarks(integer, options.thousandMark);
242
+ const dec = stripTrailingZeros(decimal.padStart(options.decimals, "0"));
243
+ if (dec) return `${int}${options.decimalMark}${dec}`;
244
+ return int;
259
245
  }
260
- function _addThousandMarks(value, mark) {
246
+ function addThousandMarks(value, mark) {
261
247
  if (!mark) {
262
248
  return value;
263
249
  }
264
250
  return value.replace(/\B(?=(\d{3})+(?!\d))/g, mark);
265
251
  }
266
- function _stripTrailingZeros(value) {
252
+ function stripTrailingZeros(value) {
267
253
  if (!value.endsWith("0")) {
268
254
  return value;
269
255
  }
270
256
  return value.replace(/\.?0+$/, "");
271
257
  }
272
- function _removeLeadingZeros(value) {
258
+ function removeLeadingZeros(value) {
273
259
  if (!value.startsWith("0")) {
274
260
  return value;
275
261
  }
@@ -331,20 +317,26 @@ function utxoSum(boxes, tokenId) {
331
317
  tokens: Object.keys(balances).filter((x) => x !== NANOERGS_TOKEN_ID).map((tokenId2) => ({ tokenId: tokenId2, amount: balances[tokenId2] }))
332
318
  };
333
319
  }
334
- function utxoDiff(minuend, subtrahend) {
335
- if (Array.isArray(minuend)) {
336
- minuend = utxoSum(minuend);
337
- }
338
- if (Array.isArray(subtrahend)) {
339
- subtrahend = utxoSum(subtrahend);
340
- }
320
+ function utxoDiff(minuend, subtrahend, ignoreSubtrahendLeftoverTokens = false) {
321
+ if (Array.isArray(minuend)) minuend = utxoSum(minuend);
322
+ if (Array.isArray(subtrahend)) subtrahend = utxoSum(subtrahend);
341
323
  const tokens = [];
342
324
  const nanoErgs = minuend.nanoErgs - subtrahend.nanoErgs;
325
+ const subtrahendTokens = new Map(subtrahend.tokens.map((t) => [t.tokenId, t.amount]));
343
326
  for (const token of minuend.tokens) {
344
- const balance = token.amount - (subtrahend.tokens.find((t) => t.tokenId === token.tokenId)?.amount || _0n);
327
+ const subtrahendAmount = subtrahendTokens.get(token.tokenId) || _0n;
328
+ const balance = token.amount - (subtrahendAmount || _0n);
345
329
  if (balance !== _0n) {
346
330
  tokens.push({ tokenId: token.tokenId, amount: balance });
347
331
  }
332
+ if (subtrahendAmount > _0n) {
333
+ subtrahendTokens.delete(token.tokenId);
334
+ }
335
+ }
336
+ if (!ignoreSubtrahendLeftoverTokens && subtrahendTokens.size > 0) {
337
+ for (const [tokenId, amount] of subtrahendTokens) {
338
+ tokens.push({ tokenId, amount: -amount });
339
+ }
348
340
  }
349
341
  return { nanoErgs, tokens };
350
342
  }
@@ -427,8 +419,14 @@ function clearUndefined(value) {
427
419
  }
428
420
  return result;
429
421
  }
430
- function ensureDefaults(options, defaults) {
431
- return isEmpty(options) ? defaults : { ...defaults, ...options };
422
+ function ensureDefaults(partial, defaults, options) {
423
+ if (isEmpty(partial)) return defaults;
424
+ if (options?.keepUndefinedKeys) return { ...defaults, ...partial };
425
+ const merged = { ...defaults, ...partial };
426
+ for (const key in merged) {
427
+ merged[key] = partial[key] ?? defaults[key];
428
+ }
429
+ return merged;
432
430
  }
433
431
 
434
432
  // src/utils/bytes.ts
@@ -443,18 +441,14 @@ function concatBytes(...arrays) {
443
441
  return r;
444
442
  }
445
443
  function isHex(value) {
446
- if (!value || value.length % 2)
447
- return false;
448
- if (!value.startsWith("0x")) {
449
- value = "0x" + value;
450
- }
451
- return !isNaN(Number(value));
444
+ if (!value || value.length % 2) return false;
445
+ return !Number.isNaN(Number(value.startsWith("0x") ? value : `0x${value}`));
452
446
  }
453
447
  function byteSizeOf(hex) {
454
448
  return hex.length / 2;
455
449
  }
456
450
 
457
- // src/types/common.ts
451
+ // src/types/enums.ts
458
452
  var Network = /* @__PURE__ */ ((Network2) => {
459
453
  Network2[Network2["Mainnet"] = 0] = "Mainnet";
460
454
  Network2[Network2["Testnet"] = 16] = "Testnet";
@@ -464,6 +458,7 @@ var AddressType = /* @__PURE__ */ ((AddressType2) => {
464
458
  AddressType2[AddressType2["P2PK"] = 1] = "P2PK";
465
459
  AddressType2[AddressType2["P2SH"] = 2] = "P2SH";
466
460
  AddressType2[AddressType2["P2S"] = 3] = "P2S";
461
+ AddressType2[AddressType2["ADH"] = 4] = "ADH";
467
462
  return AddressType2;
468
463
  })(AddressType || {});
469
464
  var ergoTreeHeaderFlags = {
@@ -571,14 +566,8 @@ var FleetError = class extends Error {
571
566
  }
572
567
  };
573
568
  var NotSupportedError = class extends FleetError {
574
- constructor(message) {
575
- super(message);
576
- }
577
569
  };
578
570
  var BlockchainProviderError = class extends FleetError {
579
- constructor(message, options) {
580
- super(message, options);
581
- }
582
571
  };
583
572
 
584
573
  exports.AddressType = AddressType;
@@ -636,5 +625,5 @@ exports.uniqBy = uniqBy;
636
625
  exports.utxoDiff = utxoDiff;
637
626
  exports.utxoFilter = utxoFilter;
638
627
  exports.utxoSum = utxoSum;
639
- //# sourceMappingURL=out.js.map
628
+ //# sourceMappingURL=index.js.map
640
629
  //# sourceMappingURL=index.js.map