@fleet-sdk/common 0.4.1 → 0.8.0
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 +12 -0
- package/dist/index.d.mts +30 -29
- package/dist/index.d.ts +30 -29
- package/dist/index.js +53 -73
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -73
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# @fleet-sdk/common
|
2
2
|
|
3
|
+
## 0.8.0
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 36adc61: Fix `chunk` when passing an empty input array.
|
8
|
+
|
9
|
+
## 0.6.4
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 504974e: Fix `utxoDiff` miscalculation when tokens are present in subtrahend but not in minuend
|
14
|
+
|
3
15
|
## 0.4.1
|
4
16
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
@@ -6,21 +6,7 @@ type OneOrMore<T> = T | T[];
|
|
6
6
|
type SortingSelector<T> = (item: T) => string | number | bigint;
|
7
7
|
type SortingDirection = "asc" | "desc";
|
8
8
|
type FilterPredicate<T> = (item: T) => boolean;
|
9
|
-
type
|
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];
|
9
|
+
type PlainObjectType = "minimal" | "EIP-12";
|
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
|
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(
|
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:
|
454
|
-
declare function utxoSum(boxes:
|
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
|
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.
|
@@ -555,11 +558,11 @@ type EnsureDefaultsOptions = {
|
|
555
558
|
*/
|
556
559
|
declare function ensureDefaults<T extends object, R extends object>(partial: T | undefined, defaults: R, options?: EnsureDefaultsOptions): R & T;
|
557
560
|
|
558
|
-
type
|
561
|
+
type ErrorMessage = string | Error | (() => string);
|
559
562
|
type Constructable = Function;
|
560
|
-
type
|
561
|
-
declare function assert(condition: boolean, error:
|
562
|
-
declare function assertTypeOf<T>(obj: T, expected:
|
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;
|
563
566
|
declare function assertInstanceOf<T>(obj: T, expected: Constructable): asserts obj;
|
564
567
|
declare function isEmpty<T>(target: T | null | undefined): target is undefined | null;
|
565
568
|
declare function some<T>(target: T | null | undefined): target is T;
|
@@ -642,10 +645,8 @@ declare class FleetError extends Error {
|
|
642
645
|
constructor(message?: string, options?: ErrorOptions);
|
643
646
|
}
|
644
647
|
declare class NotSupportedError extends FleetError {
|
645
|
-
constructor(message?: string);
|
646
648
|
}
|
647
649
|
declare class BlockchainProviderError extends FleetError {
|
648
|
-
constructor(message?: string, options?: ErrorOptions);
|
649
650
|
}
|
650
651
|
|
651
|
-
export { AddressType, Amount,
|
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, 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 PlainObjectType, 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
@@ -6,21 +6,7 @@ type OneOrMore<T> = T | T[];
|
|
6
6
|
type SortingSelector<T> = (item: T) => string | number | bigint;
|
7
7
|
type SortingDirection = "asc" | "desc";
|
8
8
|
type FilterPredicate<T> = (item: T) => boolean;
|
9
|
-
type
|
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];
|
9
|
+
type PlainObjectType = "minimal" | "EIP-12";
|
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
|
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(
|
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:
|
454
|
-
declare function utxoSum(boxes:
|
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
|
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.
|
@@ -555,11 +558,11 @@ type EnsureDefaultsOptions = {
|
|
555
558
|
*/
|
556
559
|
declare function ensureDefaults<T extends object, R extends object>(partial: T | undefined, defaults: R, options?: EnsureDefaultsOptions): R & T;
|
557
560
|
|
558
|
-
type
|
561
|
+
type ErrorMessage = string | Error | (() => string);
|
559
562
|
type Constructable = Function;
|
560
|
-
type
|
561
|
-
declare function assert(condition: boolean, error:
|
562
|
-
declare function assertTypeOf<T>(obj: T, expected:
|
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;
|
563
566
|
declare function assertInstanceOf<T>(obj: T, expected: Constructable): asserts obj;
|
564
567
|
declare function isEmpty<T>(target: T | null | undefined): target is undefined | null;
|
565
568
|
declare function some<T>(target: T | null | undefined): target is T;
|
@@ -642,10 +645,8 @@ declare class FleetError extends Error {
|
|
642
645
|
constructor(message?: string, options?: ErrorOptions);
|
643
646
|
}
|
644
647
|
declare class NotSupportedError extends FleetError {
|
645
|
-
constructor(message?: string);
|
646
648
|
}
|
647
649
|
declare class BlockchainProviderError extends FleetError {
|
648
|
-
constructor(message?: string, options?: ErrorOptions);
|
649
650
|
}
|
650
651
|
|
651
|
-
export { AddressType, Amount,
|
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, 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 PlainObjectType, 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(
|
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
|
}
|
@@ -93,9 +88,8 @@ function hasDuplicatesBy(array, selector) {
|
|
93
88
|
});
|
94
89
|
}
|
95
90
|
function chunk(array, size) {
|
96
|
-
if (array
|
97
|
-
|
98
|
-
}
|
91
|
+
if (isEmpty(array)) return [];
|
92
|
+
if (array.length <= size) return [array];
|
99
93
|
const chunks = [];
|
100
94
|
for (let i = 0; i < array.length; i += size) {
|
101
95
|
chunks.push(array.slice(i, i + size));
|
@@ -104,20 +98,16 @@ function chunk(array, size) {
|
|
104
98
|
}
|
105
99
|
function orderBy(array, iteratee, order = "asc") {
|
106
100
|
return [...array].sort((a, b) => {
|
107
|
-
if (iteratee(a) > iteratee(b))
|
108
|
-
|
109
|
-
|
110
|
-
return order === "asc" ? -1 : 1;
|
111
|
-
} else {
|
112
|
-
return 0;
|
113
|
-
}
|
101
|
+
if (iteratee(a) > iteratee(b)) return order === "asc" ? 1 : -1;
|
102
|
+
if (iteratee(a) < iteratee(b)) return order === "asc" ? -1 : 1;
|
103
|
+
return 0;
|
114
104
|
});
|
115
105
|
}
|
116
106
|
function areEqual(array1, array2) {
|
117
107
|
if (array1 === array2) {
|
118
108
|
return true;
|
119
109
|
}
|
120
|
-
if (array1.length
|
110
|
+
if (array1.length !== array2.length) {
|
121
111
|
return false;
|
122
112
|
}
|
123
113
|
for (let i = 0; i < array1.length; i++) {
|
@@ -131,7 +121,7 @@ function areEqualBy(array1, array2, selector) {
|
|
131
121
|
if (array1 === array2) {
|
132
122
|
return true;
|
133
123
|
}
|
134
|
-
if (array1.length
|
124
|
+
if (array1.length !== array2.length) {
|
135
125
|
return false;
|
136
126
|
}
|
137
127
|
for (let i = 0; i < array1.length; i++) {
|
@@ -205,22 +195,20 @@ var _128n = BigInt(128);
|
|
205
195
|
function ensureBigInt(number) {
|
206
196
|
return typeof number === "bigint" ? number : BigInt(number);
|
207
197
|
}
|
208
|
-
function undecimalize(
|
209
|
-
if (!
|
210
|
-
|
211
|
-
}
|
212
|
-
options = typeof options == "number" ? { decimals: options } : options;
|
198
|
+
function undecimalize(input, options) {
|
199
|
+
if (!input) return _0n;
|
200
|
+
options = typeof options === "number" ? { decimals: options } : options;
|
213
201
|
if (isUndefined(options)) {
|
214
202
|
options = {};
|
215
203
|
}
|
216
204
|
options.decimals = options.decimals || 0;
|
217
205
|
options.decimalMark = options.decimalMark || ".";
|
218
|
-
const fragments =
|
206
|
+
const fragments = input.split(options.decimalMark);
|
219
207
|
if (fragments.length > 2) {
|
220
208
|
throw new Error("Invalid numeric string.");
|
221
209
|
}
|
222
210
|
let [integer, decimal] = fragments;
|
223
|
-
integer =
|
211
|
+
integer = removeLeadingZeros(integer);
|
224
212
|
const negative = integer.startsWith("-") ? "-" : "";
|
225
213
|
if (!decimal) {
|
226
214
|
decimal = "0".repeat(options.decimals);
|
@@ -237,39 +225,36 @@ function decimalize(value, options) {
|
|
237
225
|
if (!options) {
|
238
226
|
return value.toString();
|
239
227
|
}
|
240
|
-
options = typeof options
|
228
|
+
options = typeof options === "number" ? { decimals: options } : options;
|
241
229
|
options.decimals = options.decimals || 0;
|
242
230
|
options.decimalMark = options.decimalMark || ".";
|
243
231
|
const pow = _10n ** BigInt(options.decimals);
|
244
232
|
const integer = value / pow;
|
245
233
|
const decimal = value - integer * pow;
|
246
|
-
return
|
234
|
+
return buildFormattedDecimal(integer.toString(10), decimal.toString(10), options);
|
247
235
|
}
|
248
236
|
function percent(value, percentage, precision = 2n) {
|
249
237
|
return value * percentage / 10n ** precision;
|
250
238
|
}
|
251
|
-
function
|
252
|
-
const
|
253
|
-
const
|
254
|
-
if (
|
255
|
-
|
256
|
-
} else {
|
257
|
-
return integerPart;
|
258
|
-
}
|
239
|
+
function buildFormattedDecimal(integer, decimal, options) {
|
240
|
+
const int = addThousandMarks(integer, options.thousandMark);
|
241
|
+
const dec = stripTrailingZeros(decimal.padStart(options.decimals, "0"));
|
242
|
+
if (dec) return `${int}${options.decimalMark}${dec}`;
|
243
|
+
return int;
|
259
244
|
}
|
260
|
-
function
|
245
|
+
function addThousandMarks(value, mark) {
|
261
246
|
if (!mark) {
|
262
247
|
return value;
|
263
248
|
}
|
264
249
|
return value.replace(/\B(?=(\d{3})+(?!\d))/g, mark);
|
265
250
|
}
|
266
|
-
function
|
251
|
+
function stripTrailingZeros(value) {
|
267
252
|
if (!value.endsWith("0")) {
|
268
253
|
return value;
|
269
254
|
}
|
270
255
|
return value.replace(/\.?0+$/, "");
|
271
256
|
}
|
272
|
-
function
|
257
|
+
function removeLeadingZeros(value) {
|
273
258
|
if (!value.startsWith("0")) {
|
274
259
|
return value;
|
275
260
|
}
|
@@ -331,20 +316,26 @@ function utxoSum(boxes, tokenId) {
|
|
331
316
|
tokens: Object.keys(balances).filter((x) => x !== NANOERGS_TOKEN_ID).map((tokenId2) => ({ tokenId: tokenId2, amount: balances[tokenId2] }))
|
332
317
|
};
|
333
318
|
}
|
334
|
-
function utxoDiff(minuend, subtrahend) {
|
335
|
-
if (Array.isArray(minuend))
|
336
|
-
|
337
|
-
}
|
338
|
-
if (Array.isArray(subtrahend)) {
|
339
|
-
subtrahend = utxoSum(subtrahend);
|
340
|
-
}
|
319
|
+
function utxoDiff(minuend, subtrahend, ignoreSubtrahendLeftoverTokens = false) {
|
320
|
+
if (Array.isArray(minuend)) minuend = utxoSum(minuend);
|
321
|
+
if (Array.isArray(subtrahend)) subtrahend = utxoSum(subtrahend);
|
341
322
|
const tokens = [];
|
342
323
|
const nanoErgs = minuend.nanoErgs - subtrahend.nanoErgs;
|
324
|
+
const subtrahendTokens = new Map(subtrahend.tokens.map((t) => [t.tokenId, t.amount]));
|
343
325
|
for (const token of minuend.tokens) {
|
344
|
-
const
|
326
|
+
const subtrahendAmount = subtrahendTokens.get(token.tokenId) || _0n;
|
327
|
+
const balance = token.amount - (subtrahendAmount || _0n);
|
345
328
|
if (balance !== _0n) {
|
346
329
|
tokens.push({ tokenId: token.tokenId, amount: balance });
|
347
330
|
}
|
331
|
+
if (subtrahendAmount > _0n) {
|
332
|
+
subtrahendTokens.delete(token.tokenId);
|
333
|
+
}
|
334
|
+
}
|
335
|
+
if (!ignoreSubtrahendLeftoverTokens && subtrahendTokens.size > 0) {
|
336
|
+
for (const [tokenId, amount] of subtrahendTokens) {
|
337
|
+
tokens.push({ tokenId, amount: -amount });
|
338
|
+
}
|
348
339
|
}
|
349
340
|
return { nanoErgs, tokens };
|
350
341
|
}
|
@@ -428,10 +419,8 @@ function clearUndefined(value) {
|
|
428
419
|
return result;
|
429
420
|
}
|
430
421
|
function ensureDefaults(partial, defaults, options) {
|
431
|
-
if (isEmpty(partial))
|
432
|
-
|
433
|
-
if (options?.keepUndefinedKeys)
|
434
|
-
return { ...defaults, ...partial };
|
422
|
+
if (isEmpty(partial)) return defaults;
|
423
|
+
if (options?.keepUndefinedKeys) return { ...defaults, ...partial };
|
435
424
|
const merged = { ...defaults, ...partial };
|
436
425
|
for (const key in merged) {
|
437
426
|
merged[key] = partial[key] ?? defaults[key];
|
@@ -451,18 +440,14 @@ function concatBytes(...arrays) {
|
|
451
440
|
return r;
|
452
441
|
}
|
453
442
|
function isHex(value) {
|
454
|
-
if (!value || value.length % 2)
|
455
|
-
|
456
|
-
if (!value.startsWith("0x")) {
|
457
|
-
value = "0x" + value;
|
458
|
-
}
|
459
|
-
return !isNaN(Number(value));
|
443
|
+
if (!value || value.length % 2) return false;
|
444
|
+
return !Number.isNaN(Number(value.startsWith("0x") ? value : `0x${value}`));
|
460
445
|
}
|
461
446
|
function byteSizeOf(hex) {
|
462
447
|
return hex.length / 2;
|
463
448
|
}
|
464
449
|
|
465
|
-
// src/types/
|
450
|
+
// src/types/enums.ts
|
466
451
|
var Network = /* @__PURE__ */ ((Network2) => {
|
467
452
|
Network2[Network2["Mainnet"] = 0] = "Mainnet";
|
468
453
|
Network2[Network2["Testnet"] = 16] = "Testnet";
|
@@ -472,6 +457,7 @@ var AddressType = /* @__PURE__ */ ((AddressType2) => {
|
|
472
457
|
AddressType2[AddressType2["P2PK"] = 1] = "P2PK";
|
473
458
|
AddressType2[AddressType2["P2SH"] = 2] = "P2SH";
|
474
459
|
AddressType2[AddressType2["P2S"] = 3] = "P2S";
|
460
|
+
AddressType2[AddressType2["ADH"] = 4] = "ADH";
|
475
461
|
return AddressType2;
|
476
462
|
})(AddressType || {});
|
477
463
|
var ergoTreeHeaderFlags = {
|
@@ -579,14 +565,8 @@ var FleetError = class extends Error {
|
|
579
565
|
}
|
580
566
|
};
|
581
567
|
var NotSupportedError = class extends FleetError {
|
582
|
-
constructor(message) {
|
583
|
-
super(message);
|
584
|
-
}
|
585
568
|
};
|
586
569
|
var BlockchainProviderError = class extends FleetError {
|
587
|
-
constructor(message, options) {
|
588
|
-
super(message, options);
|
589
|
-
}
|
590
570
|
};
|
591
571
|
|
592
572
|
exports.AddressType = AddressType;
|
@@ -644,5 +624,5 @@ exports.uniqBy = uniqBy;
|
|
644
624
|
exports.utxoDiff = utxoDiff;
|
645
625
|
exports.utxoFilter = utxoFilter;
|
646
626
|
exports.utxoSum = utxoSum;
|
647
|
-
//# sourceMappingURL=
|
627
|
+
//# sourceMappingURL=index.js.map
|
648
628
|
//# sourceMappingURL=index.js.map
|