@fleet-sdk/common 0.2.2 → 0.3.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/src/utils/utxo.ts DELETED
@@ -1,183 +0,0 @@
1
- import {
2
- Amount,
3
- AmountType,
4
- Box,
5
- BoxCandidate,
6
- NonMandatoryRegisters,
7
- TokenAmount,
8
- TokenId
9
- } from "../types";
10
- import { isDefined, isEmpty, isUndefined } from "./assertions";
11
- import { ensureBigInt } from "./bigInt";
12
- import { _0n } from "./bigInt";
13
-
14
- const NANOERGS_TOKEN_ID = "nanoErgs";
15
-
16
- export function utxoSum(boxes: MinimalBoxAmounts): BoxSummary;
17
- export function utxoSum(boxes: MinimalBoxAmounts, tokenId: TokenId): bigint;
18
- export function utxoSum(boxes: MinimalBoxAmounts, tokenId?: TokenId): BoxSummary | bigint {
19
- const balances: { [tokenId: string]: bigint } = {};
20
-
21
- for (const box of boxes) {
22
- if (isUndefined(tokenId) || tokenId === NANOERGS_TOKEN_ID) {
23
- balances[NANOERGS_TOKEN_ID] = (balances[NANOERGS_TOKEN_ID] || _0n) + ensureBigInt(box.value);
24
- }
25
-
26
- if (tokenId !== NANOERGS_TOKEN_ID) {
27
- for (const token of box.assets) {
28
- if (isDefined(tokenId) && tokenId !== token.tokenId) {
29
- continue;
30
- }
31
-
32
- balances[token.tokenId] = (balances[token.tokenId] || _0n) + ensureBigInt(token.amount);
33
- }
34
- }
35
- }
36
-
37
- if (isDefined(tokenId)) {
38
- return balances[tokenId] || _0n;
39
- }
40
-
41
- return {
42
- nanoErgs: balances[NANOERGS_TOKEN_ID] || _0n,
43
- tokens: Object.keys(balances)
44
- .filter((x) => x !== NANOERGS_TOKEN_ID)
45
- .map((tokenId) => ({ tokenId, amount: balances[tokenId] }))
46
- };
47
- }
48
-
49
- export function utxoDiff(
50
- minuend: BoxSummary | Box<Amount>[],
51
- subtrahend: BoxSummary | Box<Amount>[]
52
- ): BoxSummary {
53
- if (Array.isArray(minuend)) {
54
- minuend = utxoSum(minuend);
55
- }
56
-
57
- if (Array.isArray(subtrahend)) {
58
- subtrahend = utxoSum(subtrahend);
59
- }
60
-
61
- const tokens: TokenAmount<bigint>[] = [];
62
- const nanoErgs = minuend.nanoErgs - subtrahend.nanoErgs;
63
-
64
- for (const token of minuend.tokens) {
65
- const balance =
66
- token.amount - (subtrahend.tokens.find((t) => t.tokenId === token.tokenId)?.amount || _0n);
67
-
68
- if (balance !== _0n) {
69
- tokens.push({ tokenId: token.tokenId, amount: balance });
70
- }
71
- }
72
-
73
- return { nanoErgs, tokens };
74
- }
75
-
76
- const MIN_NON_MANDATORY_REGISTER_INDEX = 4;
77
- const MAX_NON_MANDATORY_REGISTER_INDEX = 9;
78
-
79
- export function areRegistersDenselyPacked(registers: NonMandatoryRegisters): boolean {
80
- let lastIndex = 0;
81
- for (let i = MIN_NON_MANDATORY_REGISTER_INDEX; i <= MAX_NON_MANDATORY_REGISTER_INDEX; i++) {
82
- const key = `R${i}` as keyof NonMandatoryRegisters;
83
- if (registers[key]) {
84
- if (i === MIN_NON_MANDATORY_REGISTER_INDEX) {
85
- lastIndex = i;
86
- continue;
87
- }
88
-
89
- if (i - lastIndex > 1) {
90
- return false;
91
- }
92
-
93
- lastIndex = i;
94
- }
95
- }
96
-
97
- return true;
98
- }
99
-
100
- export function utxoFilter<T extends AmountType>(
101
- utxos: Box<T>[],
102
- filterParams: UTxOFilterParams<T>
103
- ) {
104
- if (isEmpty(filterParams) || isEmpty(utxos)) {
105
- return utxos;
106
- }
107
-
108
- const { by, max } = filterParams;
109
- let filtered = utxos;
110
-
111
- if (by) {
112
- filtered = utxos.filter(by);
113
- if (isEmpty(filtered)) {
114
- return filtered;
115
- }
116
- }
117
-
118
- if (!max) {
119
- return filtered;
120
- }
121
-
122
- if (isDefined(max.aggregatedDistinctTokens)) {
123
- const tokenIds = _getDistinctTokenIds(filtered, max.aggregatedDistinctTokens);
124
- filtered = filtered.filter(
125
- (utxo) => isEmpty(utxo.assets) || utxo.assets.every((token) => tokenIds.has(token.tokenId))
126
- );
127
- }
128
-
129
- if (isDefined(max.count) && filtered.length > max.count) {
130
- filtered = filtered.slice(0, max.count);
131
- }
132
-
133
- return filtered;
134
- }
135
-
136
- function _getDistinctTokenIds(utxos: Box<AmountType>[], max: number): Set<string> {
137
- const tokenIds = new Set<string>();
138
-
139
- for (let i = 0; i < utxos.length && tokenIds.size < max; i++) {
140
- if (isEmpty(utxos[i].assets) || utxos[i].assets.length > max) {
141
- continue;
142
- }
143
-
144
- for (const token of utxos[i].assets) {
145
- tokenIds.add(token.tokenId);
146
- }
147
- }
148
-
149
- return tokenIds;
150
- }
151
-
152
- export type UTxOFilterParams<T extends AmountType> = {
153
- by?: (utxo: Box<T>) => boolean;
154
- max?: {
155
- count?: number;
156
- aggregatedDistinctTokens?: number;
157
- };
158
- };
159
-
160
- export type BoxSummary = {
161
- nanoErgs: bigint;
162
- tokens: TokenAmount<bigint>[];
163
- };
164
-
165
- export type MinimalBoxAmounts = readonly {
166
- value: Amount;
167
- assets: TokenAmount<Amount>[];
168
- }[];
169
-
170
- export function ensureUTxOBigInt(box: Box<Amount>): Box<bigint>;
171
- export function ensureUTxOBigInt(candidate: BoxCandidate<Amount>): BoxCandidate<bigint>;
172
- export function ensureUTxOBigInt(
173
- box: Box<Amount> | BoxCandidate<Amount>
174
- ): BoxCandidate<bigint> | Box<bigint> {
175
- return {
176
- ...box,
177
- value: ensureBigInt(box.value),
178
- assets: box.assets.map((token) => ({
179
- tokenId: token.tokenId,
180
- amount: ensureBigInt(token.amount)
181
- }))
182
- };
183
- }