@fleet-sdk/common 0.1.2 → 0.1.3

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.
Files changed (80) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/index.d.ts +1 -0
  3. package/dist/cjs/index.js +1 -0
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/models/collection.d.ts +20 -0
  6. package/dist/cjs/models/collection.js +76 -0
  7. package/dist/cjs/models/collection.js.map +1 -0
  8. package/dist/cjs/models/index.d.ts +1 -0
  9. package/dist/cjs/models/index.js +18 -0
  10. package/dist/cjs/models/index.js.map +1 -0
  11. package/dist/cjs/types/boxes.d.ts +2 -2
  12. package/dist/cjs/types/common.d.ts +6 -1
  13. package/dist/cjs/types/common.js +5 -1
  14. package/dist/cjs/types/common.js.map +1 -1
  15. package/dist/cjs/types/inputs.d.ts +3 -3
  16. package/dist/cjs/utils/arrayUtils.d.ts +4 -5
  17. package/dist/cjs/utils/arrayUtils.js +24 -20
  18. package/dist/cjs/utils/arrayUtils.js.map +1 -1
  19. package/dist/cjs/utils/assertions.d.ts +8 -0
  20. package/dist/cjs/utils/assertions.js +40 -0
  21. package/dist/cjs/utils/assertions.js.map +1 -0
  22. package/dist/cjs/utils/bigIntUtils.d.ts +3 -0
  23. package/dist/cjs/utils/bigIntUtils.js +27 -2
  24. package/dist/cjs/utils/bigIntUtils.js.map +1 -1
  25. package/dist/cjs/utils/boxUtils.d.ts +3 -1
  26. package/dist/cjs/utils/boxUtils.js +17 -6
  27. package/dist/cjs/utils/boxUtils.js.map +1 -1
  28. package/dist/cjs/utils/index.d.ts +1 -0
  29. package/dist/cjs/utils/index.js +1 -0
  30. package/dist/cjs/utils/index.js.map +1 -1
  31. package/dist/cjs/utils/objectUtils.d.ts +1 -0
  32. package/dist/cjs/utils/objectUtils.js +5 -1
  33. package/dist/cjs/utils/objectUtils.js.map +1 -1
  34. package/dist/esm/index.d.ts +1 -0
  35. package/dist/esm/index.js +1 -0
  36. package/dist/esm/index.js.map +1 -1
  37. package/dist/esm/models/collection.d.ts +20 -0
  38. package/dist/esm/models/collection.js +72 -0
  39. package/dist/esm/models/collection.js.map +1 -0
  40. package/dist/esm/models/index.d.ts +1 -0
  41. package/dist/esm/models/index.js +2 -0
  42. package/dist/esm/models/index.js.map +1 -0
  43. package/dist/esm/types/boxes.d.ts +2 -2
  44. package/dist/esm/types/common.d.ts +6 -1
  45. package/dist/esm/types/common.js +4 -0
  46. package/dist/esm/types/common.js.map +1 -1
  47. package/dist/esm/types/inputs.d.ts +3 -3
  48. package/dist/esm/utils/arrayUtils.d.ts +4 -5
  49. package/dist/esm/utils/arrayUtils.js +19 -15
  50. package/dist/esm/utils/arrayUtils.js.map +1 -1
  51. package/dist/esm/utils/assertions.d.ts +8 -0
  52. package/dist/esm/utils/assertions.js +32 -0
  53. package/dist/esm/utils/assertions.js.map +1 -0
  54. package/dist/esm/utils/bigIntUtils.d.ts +3 -0
  55. package/dist/esm/utils/bigIntUtils.js +23 -1
  56. package/dist/esm/utils/bigIntUtils.js.map +1 -1
  57. package/dist/esm/utils/boxUtils.d.ts +3 -1
  58. package/dist/esm/utils/boxUtils.js +11 -1
  59. package/dist/esm/utils/boxUtils.js.map +1 -1
  60. package/dist/esm/utils/index.d.ts +1 -0
  61. package/dist/esm/utils/index.js +1 -0
  62. package/dist/esm/utils/index.js.map +1 -1
  63. package/dist/esm/utils/objectUtils.d.ts +1 -0
  64. package/dist/esm/utils/objectUtils.js +3 -0
  65. package/dist/esm/utils/objectUtils.js.map +1 -1
  66. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  67. package/dist/tsconfig.tsbuildinfo +1 -1
  68. package/package.json +1 -1
  69. package/src/index.ts +1 -0
  70. package/src/models/collection.ts +107 -0
  71. package/src/models/index.ts +1 -0
  72. package/src/types/boxes.ts +2 -2
  73. package/src/types/common.ts +10 -3
  74. package/src/types/inputs.ts +3 -3
  75. package/src/utils/arrayUtils.ts +22 -21
  76. package/src/utils/assertions.ts +44 -0
  77. package/src/utils/bigIntUtils.ts +30 -1
  78. package/src/utils/boxUtils.ts +25 -2
  79. package/src/utils/index.ts +1 -0
  80. package/src/utils/objectUtils.ts +4 -0
@@ -0,0 +1,107 @@
1
+ import { OneOrMore } from "../types";
2
+ import { isDefined } from "../utils";
3
+
4
+ export type CollectionAddOptions = { index?: number };
5
+
6
+ export abstract class Collection<InternalType, ExternalType> implements Iterable<InternalType> {
7
+ protected readonly _items: InternalType[];
8
+
9
+ constructor() {
10
+ this._items = [];
11
+ }
12
+
13
+ protected _isIndexOutOfBounds(index: number): boolean {
14
+ return index < 0 || index >= this._items.length;
15
+ }
16
+
17
+ [Symbol.iterator](): Iterator<InternalType> {
18
+ let counter = 0;
19
+
20
+ return {
21
+ next: () => {
22
+ return {
23
+ done: counter >= this.length,
24
+ value: this._items[counter++]
25
+ };
26
+ }
27
+ };
28
+ }
29
+
30
+ public get length(): number {
31
+ return this._items.length;
32
+ }
33
+
34
+ public get isEmpty(): boolean {
35
+ return this.length === 0;
36
+ }
37
+
38
+ public at(index: number): InternalType {
39
+ if (this._isIndexOutOfBounds(index)) {
40
+ throw new RangeError(`Index '${index}' is out of range.`);
41
+ }
42
+
43
+ return this._items[index];
44
+ }
45
+
46
+ public add(items: OneOrMore<ExternalType>, options?: CollectionAddOptions): number {
47
+ return this._addOneOrMore(items, options);
48
+ }
49
+
50
+ abstract remove(item: unknown): number;
51
+
52
+ protected abstract _map(item: ExternalType | InternalType): InternalType;
53
+
54
+ protected _addOne(item: InternalType | ExternalType, options?: CollectionAddOptions): number {
55
+ if (isDefined(options) && isDefined(options.index)) {
56
+ if (options.index === this.length) {
57
+ this._items.push(this._map(item));
58
+
59
+ return this.length;
60
+ }
61
+
62
+ if (this._isIndexOutOfBounds(options.index)) {
63
+ throw new RangeError(`Index '${options.index}' is out of range.`);
64
+ }
65
+
66
+ this._items.splice(options.index, 0, this._map(item));
67
+
68
+ return this.length;
69
+ }
70
+
71
+ this._items.push(this._map(item));
72
+
73
+ return this._items.length;
74
+ }
75
+
76
+ protected _addOneOrMore(items: OneOrMore<ExternalType>, options?: CollectionAddOptions): number {
77
+ if (Array.isArray(items)) {
78
+ if (isDefined(options) && isDefined(options.index)) {
79
+ items = items.reverse();
80
+ }
81
+
82
+ for (const item of items) {
83
+ this._addOne(item, options);
84
+ }
85
+ } else {
86
+ this._addOne(items, options);
87
+ }
88
+
89
+ return this.length;
90
+ }
91
+
92
+ public toArray(): InternalType[] {
93
+ return [...this._items];
94
+ }
95
+
96
+ public reduce<U>(
97
+ callbackFn: (
98
+ accumulator: U,
99
+ currentValue: InternalType,
100
+ currentIndex: number,
101
+ array: InternalType[]
102
+ ) => U,
103
+ initialValue: U
104
+ ): U {
105
+ return this._items.reduce(callbackFn, initialValue);
106
+ }
107
+ }
@@ -0,0 +1 @@
1
+ export * from "./collection";
@@ -1,4 +1,4 @@
1
- import { ErgoTree } from "./common";
1
+ import { ErgoTreeHex } from "./common";
2
2
  import { NonMandatoryRegisters } from "./registers";
3
3
  import { TokenAmount } from "./token";
4
4
  import { TransactionId } from "./transactions";
@@ -7,7 +7,7 @@ export type BoxId = string;
7
7
  export type AmountType = string | bigint;
8
8
 
9
9
  type BoxBaseType<T extends AmountType, R extends NonMandatoryRegisters> = {
10
- ergoTree: ErgoTree;
10
+ ergoTree: ErgoTreeHex;
11
11
  creationHeight: number;
12
12
  value: T;
13
13
  assets: TokenAmount<T>[];
@@ -1,5 +1,5 @@
1
1
  export type HexString = string;
2
- export type ErgoTree = string;
2
+ export type ErgoTreeHex = string;
3
3
  export type Base58String = string;
4
4
  export type Amount = string | bigint;
5
5
 
@@ -13,8 +13,8 @@ export type FilterPredicate<T> = (item: T) => boolean;
13
13
  export type BuildOutputType = "default" | "EIP-12";
14
14
 
15
15
  export enum Network {
16
- Mainnet = 0 << 4,
17
- Testnet = 1 << 4
16
+ Mainnet = 0x00,
17
+ Testnet = 0x10
18
18
  }
19
19
 
20
20
  export enum AddressType {
@@ -22,3 +22,10 @@ export enum AddressType {
22
22
  P2SH = 2,
23
23
  P2S = 3
24
24
  }
25
+
26
+ export const ergoTreeHeaderFlags = {
27
+ sizeInclusion: 0x08,
28
+ constantSegregation: 0x10
29
+ } as const;
30
+
31
+ export type ErgoTreeHeaderFlag = (typeof ergoTreeHeaderFlags)[keyof typeof ergoTreeHeaderFlags];
@@ -1,5 +1,5 @@
1
1
  import { BoxId } from "./boxes";
2
- import { ErgoTree } from "./common";
2
+ import { ErgoTreeHex } from "./common";
3
3
  import { ContextExtension } from "./contextExtension";
4
4
  import { ProverResult } from "./proverResult";
5
5
  import { NonMandatoryRegisters } from "./registers";
@@ -19,7 +19,7 @@ export type UnsignedInput = {
19
19
  export type EIP12UnsignedInput = UnsignedInput & {
20
20
  transactionId: TransactionId;
21
21
  index: number;
22
- ergoTree: ErgoTree;
22
+ ergoTree: ErgoTreeHex;
23
23
  creationHeight: number;
24
24
  value: string;
25
25
  assets: TokenAmount<string>[];
@@ -30,7 +30,7 @@ export type EIP12UnsignedDataInput = {
30
30
  boxId: BoxId;
31
31
  transactionId: TransactionId;
32
32
  index: number;
33
- ergoTree: ErgoTree;
33
+ ergoTree: ErgoTreeHex;
34
34
  creationHeight: number;
35
35
  value: string;
36
36
  assets: TokenAmount<string>[];
@@ -1,36 +1,37 @@
1
1
  import { SortingDirection, SortingSelector } from "../types";
2
+ import { assert, isEmpty } from "./assertions";
2
3
 
3
4
  type ObjectSelector<T> = (item: T) => T[keyof T];
4
5
 
5
- export function isEmpty<T extends object>(obj?: T): obj is undefined;
6
- export function isEmpty<T>(array?: T[]): array is undefined;
7
- export function isEmpty<T>(obj?: T[] | object): obj is undefined {
8
- if (!obj) {
9
- return true;
10
- }
6
+ export function first(array: undefined): undefined;
7
+ export function first<T>(array: ArrayLike<T>): T;
8
+ export function first<T>(array: ArrayLike<T> | undefined): T | number | undefined {
9
+ if (!array) return undefined;
10
+ assert(array.length > 0, "Empty array.");
11
11
 
12
- return Array.isArray(obj) ? obj.length === 0 : Object.keys(obj).length === 0;
12
+ return array[0];
13
13
  }
14
14
 
15
- export function some<T extends object>(obj?: T): obj is T;
16
- export function some<T>(array?: T[]): array is T[];
17
- export function some<T>(obj?: T[] | object): boolean {
18
- return !isEmpty(obj);
15
+ export function last(array: undefined): undefined;
16
+ export function last<T>(array: ArrayLike<T>): T;
17
+ export function last<T>(array: ArrayLike<T> | undefined): T | undefined {
18
+ if (!array) return undefined;
19
+ assert(array.length > 0, "Empty array.");
20
+
21
+ return at(array, -1);
19
22
  }
20
23
 
21
- export function first(array: undefined): undefined;
22
- export function first(array: Uint8Array): number;
23
- export function first<T>(array: ArrayLike<T>): T;
24
- export function first<T>(array?: ArrayLike<T> | Uint8Array): T | number | undefined {
25
- if (!array) {
26
- return;
27
- }
24
+ export function at(array: undefined, index: number): undefined;
25
+ export function at<T>(array: ArrayLike<T>, index: number): T;
26
+ export function at<T>(array: ArrayLike<T> | undefined, index: number): T | undefined {
27
+ const len = array?.length;
28
+ if (!len) return undefined;
28
29
 
29
- if (!array[0]) {
30
- throw Error("Empty array.");
30
+ if (index < 0) {
31
+ index += len;
31
32
  }
32
33
 
33
- return array[0];
34
+ return array[index];
34
35
  }
35
36
 
36
37
  /**
@@ -0,0 +1,44 @@
1
+ export type AssertErrorMessageInput = string | Error | (() => string);
2
+
3
+ export function assert(condition: boolean, errorMsg: AssertErrorMessageInput): asserts condition {
4
+ if (!condition) {
5
+ let error: Error | undefined = undefined;
6
+
7
+ switch (typeof errorMsg) {
8
+ case "string":
9
+ error = new Error(errorMsg);
10
+ break;
11
+ case "function":
12
+ error = new Error(errorMsg());
13
+ break;
14
+ default:
15
+ error = errorMsg;
16
+ }
17
+
18
+ throw error;
19
+ }
20
+ }
21
+
22
+ export function isEmpty<T extends object>(obj?: T): obj is undefined;
23
+ export function isEmpty<T>(array?: T[]): array is undefined;
24
+ export function isEmpty<T>(obj?: T[] | object): obj is undefined {
25
+ if (!obj) {
26
+ return true;
27
+ }
28
+
29
+ return Array.isArray(obj) ? obj.length === 0 : Object.keys(obj).length === 0;
30
+ }
31
+
32
+ export function some<T extends object>(obj?: T): obj is T;
33
+ export function some<T>(array?: T[]): array is T[];
34
+ export function some<T>(obj?: T[] | object): boolean {
35
+ return !isEmpty(obj);
36
+ }
37
+
38
+ export function isTruthy<T>(v?: T): v is NonNullable<T> {
39
+ return Boolean(v);
40
+ }
41
+
42
+ export function isFalsy<T>(v?: T): v is undefined {
43
+ return !isTruthy(v);
44
+ }
@@ -1,5 +1,6 @@
1
1
  import { Amount } from "../types";
2
- import { first, isEmpty } from "./arrayUtils";
2
+ import { first } from "./arrayUtils";
3
+ import { isEmpty } from "./assertions";
3
4
  import { _0n, _10n } from "./bigIntLiterals";
4
5
  import { isUndefined } from "./objectUtils";
5
6
 
@@ -92,6 +93,10 @@ export function decimalize(value: Amount, options?: FormattingOptions | number):
92
93
  return _buildFormattedDecimal(integer.toString(10), decimal.toString(10), options);
93
94
  }
94
95
 
96
+ export function percent(value: bigint, percentage: bigint, precision = 2n) {
97
+ return (value * percentage) / 10n ** precision;
98
+ }
99
+
95
100
  function _buildFormattedDecimal(
96
101
  integer: string,
97
102
  decimal: string,
@@ -221,3 +226,27 @@ export function _bitNegate(value: bigint): bigint {
221
226
 
222
227
  return (~value & mask) + 1n; // invert bits, mask it, and add one
223
228
  }
229
+
230
+ export function min<T extends bigint | number>(...numbers: T[]): T {
231
+ let min = first(numbers);
232
+
233
+ for (const num of numbers) {
234
+ if (num < min) {
235
+ min = num;
236
+ }
237
+ }
238
+
239
+ return min;
240
+ }
241
+
242
+ export function max<T extends bigint | number>(...numbers: T[]): T {
243
+ let max = first(numbers);
244
+
245
+ for (const num of numbers) {
246
+ if (num > max) {
247
+ max = num;
248
+ }
249
+ }
250
+
251
+ return max;
252
+ }
@@ -1,5 +1,13 @@
1
- import { Amount, AmountType, Box, NonMandatoryRegisters, TokenAmount, TokenId } from "../types";
2
- import { isEmpty } from "./arrayUtils";
1
+ import {
2
+ Amount,
3
+ AmountType,
4
+ Box,
5
+ BoxCandidate,
6
+ NonMandatoryRegisters,
7
+ TokenAmount,
8
+ TokenId
9
+ } from "../types";
10
+ import { isEmpty } from "./assertions";
3
11
  import { _0n } from "./bigIntLiterals";
4
12
  import { ensureBigInt } from "./bigIntUtils";
5
13
  import { isDefined, isUndefined } from "./objectUtils";
@@ -159,3 +167,18 @@ export type MinimalBoxAmounts = readonly {
159
167
  value: Amount;
160
168
  assets: TokenAmount<Amount>[];
161
169
  }[];
170
+
171
+ export function ensureUTxOBigInt(box: Box<Amount>): Box<bigint>;
172
+ export function ensureUTxOBigInt(candidate: BoxCandidate<Amount>): BoxCandidate<bigint>;
173
+ export function ensureUTxOBigInt(
174
+ box: Box<Amount> | BoxCandidate<Amount>
175
+ ): BoxCandidate<bigint> | Box<bigint> {
176
+ return {
177
+ ...box,
178
+ value: ensureBigInt(box.value),
179
+ assets: box.assets.map((token) => ({
180
+ tokenId: token.tokenId,
181
+ amount: ensureBigInt(token.amount)
182
+ }))
183
+ };
184
+ }
@@ -4,3 +4,4 @@ export * from "./bigIntUtils";
4
4
  export * from "./boxUtils";
5
5
  export * from "./objectUtils";
6
6
  export * from "./stringUtils";
7
+ export * from "./assertions";
@@ -17,3 +17,7 @@ export function isUndefined(value: unknown): value is undefined {
17
17
  export function isDefined<T>(value: T | undefined): value is T {
18
18
  return !isUndefined(value);
19
19
  }
20
+
21
+ export function hasKey(o: unknown, key: PropertyKey): boolean {
22
+ return Object.prototype.hasOwnProperty.call(o, key);
23
+ }