@shapeshiftoss/utils 1.0.4 → 1.0.6

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 (41) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/assetData/baseAssets.d.ts +27 -1
  3. package/dist/cjs/assetData/baseAssets.js +427 -17
  4. package/dist/cjs/assetData/decodeAssetData.js +2 -2
  5. package/dist/cjs/assetData/getBaseAsset.js +54 -2
  6. package/dist/cjs/bigAmount/bigAmount.d.ts +82 -0
  7. package/dist/cjs/bigAmount/bigAmount.js +276 -0
  8. package/dist/cjs/bigAmount/bigAmount.test.d.ts +1 -0
  9. package/dist/cjs/bigAmount/bigAmount.test.js +1194 -0
  10. package/dist/cjs/chainIdToFeeAssetId.js +54 -2
  11. package/dist/cjs/encoding.d.ts +2 -0
  12. package/dist/cjs/encoding.js +13 -0
  13. package/dist/cjs/getAssetNamespaceFromChainId.js +32 -3
  14. package/dist/cjs/getChainShortName.d.ts +1 -1
  15. package/dist/cjs/getChainShortName.js +54 -2
  16. package/dist/cjs/getNativeFeeAssetReference.js +69 -2
  17. package/dist/cjs/index.d.ts +16 -13
  18. package/dist/cjs/index.js +22 -16
  19. package/dist/cjs/treasury.d.ts +6 -2
  20. package/dist/cjs/treasury.js +9 -1
  21. package/dist/esm/.tsbuildinfo +1 -1
  22. package/dist/esm/assetData/baseAssets.d.ts +27 -1
  23. package/dist/esm/assetData/baseAssets.js +426 -16
  24. package/dist/esm/assetData/decodeAssetData.js +1 -1
  25. package/dist/esm/assetData/getBaseAsset.js +55 -3
  26. package/dist/esm/bigAmount/bigAmount.d.ts +82 -0
  27. package/dist/esm/bigAmount/bigAmount.js +272 -0
  28. package/dist/esm/bigAmount/bigAmount.test.d.ts +1 -0
  29. package/dist/esm/bigAmount/bigAmount.test.js +1192 -0
  30. package/dist/esm/chainIdToFeeAssetId.js +55 -3
  31. package/dist/esm/encoding.d.ts +2 -0
  32. package/dist/esm/encoding.js +8 -0
  33. package/dist/esm/getAssetNamespaceFromChainId.js +32 -3
  34. package/dist/esm/getChainShortName.d.ts +1 -1
  35. package/dist/esm/getChainShortName.js +54 -2
  36. package/dist/esm/getNativeFeeAssetReference.js +69 -2
  37. package/dist/esm/index.d.ts +16 -13
  38. package/dist/esm/index.js +19 -14
  39. package/dist/esm/treasury.d.ts +6 -2
  40. package/dist/esm/treasury.js +8 -0
  41. package/package.json +7 -7
@@ -0,0 +1,82 @@
1
+ import type BigNumber from 'bignumber.js';
2
+ type NullableScalar = BigNumber.Value | null | undefined;
3
+ type WithPrecision = {
4
+ value: BigNumber.Value | null | undefined;
5
+ precision: number;
6
+ assetId?: never;
7
+ };
8
+ type WithAssetId = {
9
+ value: BigNumber.Value | null | undefined;
10
+ assetId: string;
11
+ precision?: never;
12
+ };
13
+ type FactoryArgs = WithPrecision | WithAssetId;
14
+ export type BigAmountConfig = {
15
+ resolvePrecision: (assetId: string) => number;
16
+ resolvePrice: (assetId: string) => string;
17
+ resolvePriceUsd: (assetId: string) => string;
18
+ };
19
+ export declare class BigAmount {
20
+ private readonly value;
21
+ readonly precision: number;
22
+ readonly assetId?: string;
23
+ private static config?;
24
+ private constructor();
25
+ static configure(config: BigAmountConfig): void;
26
+ static getConfig(): BigAmountConfig | undefined;
27
+ static resetConfig(): void;
28
+ static fromBaseUnit(args: FactoryArgs): BigAmount;
29
+ static fromPrecision(args: FactoryArgs): BigAmount;
30
+ static zero({ precision, assetId }: {
31
+ precision: number;
32
+ assetId?: string;
33
+ }): BigAmount;
34
+ static fromBN({ value, precision, assetId, }: {
35
+ value: BigNumber;
36
+ precision: number;
37
+ assetId?: string;
38
+ }): BigAmount;
39
+ static fromJSON({ value, precision, assetId, }: {
40
+ value: string;
41
+ precision: number;
42
+ assetId?: string;
43
+ }): BigAmount;
44
+ static min(...amounts: BigAmount[]): BigAmount;
45
+ static max(...amounts: BigAmount[]): BigAmount;
46
+ static isBigAmount(value: unknown): value is BigAmount;
47
+ plus(other: BigAmount | NullableScalar): BigAmount;
48
+ minus(other: BigAmount | NullableScalar): BigAmount;
49
+ times(scalar: NullableScalar): BigAmount;
50
+ div(scalar: NullableScalar): BigAmount;
51
+ abs(): BigAmount;
52
+ negated(): BigAmount;
53
+ positiveOrZero(): BigAmount;
54
+ decimalPlaces(n: number, rm?: BigNumber.RoundingMode): BigAmount;
55
+ gt(other: BigAmount | NullableScalar): boolean;
56
+ gte(other: BigAmount | NullableScalar): boolean;
57
+ lt(other: BigAmount | NullableScalar): boolean;
58
+ lte(other: BigAmount | NullableScalar): boolean;
59
+ eq(other: BigAmount | NullableScalar): boolean;
60
+ isZero(): boolean;
61
+ isPositive(): boolean;
62
+ isNegative(): boolean;
63
+ isFinite(): boolean;
64
+ toBaseUnit(): string;
65
+ toPrecision(): string;
66
+ toBN(): BigNumber;
67
+ toFixed(decimals?: number): string;
68
+ toString(): string;
69
+ toNumber(): number;
70
+ toSignificant(digits: number): string;
71
+ toUserCurrency(decimals?: number): string;
72
+ toUSD(decimals?: number): string;
73
+ static fromThorBaseUnit(value: BigNumber.Value | null | undefined): BigAmount;
74
+ toThorBaseUnit(): string;
75
+ toJSON(): {
76
+ value: string;
77
+ precision: number;
78
+ assetId?: string;
79
+ };
80
+ private static resolveConfigPrecision;
81
+ }
82
+ export {};
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BigAmount = void 0;
4
+ const bignumber_1 = require("../bignumber/bignumber");
5
+ const ROUND_DOWN = 1;
6
+ const ROUND_HALF_UP = 4;
7
+ const TEN = (0, bignumber_1.bn)(10);
8
+ const THOR_PRECISION = 8;
9
+ class BigAmount {
10
+ // Internal value is ALWAYS in base units (raw blockchain integer scale).
11
+ value;
12
+ precision;
13
+ assetId;
14
+ static config;
15
+ constructor(value, precision, assetId) {
16
+ this.value = value;
17
+ this.precision = precision;
18
+ this.assetId = assetId;
19
+ }
20
+ // ── Configuration ───────────────────────────────
21
+ static configure(config) {
22
+ BigAmount.config = config;
23
+ }
24
+ static getConfig() {
25
+ return BigAmount.config;
26
+ }
27
+ static resetConfig() {
28
+ BigAmount.config = undefined;
29
+ }
30
+ // ── Construction (all nullish-safe) ───────────────
31
+ static fromBaseUnit(args) {
32
+ if ('assetId' in args && args.assetId !== undefined) {
33
+ const precision = BigAmount.resolveConfigPrecision(args.assetId);
34
+ return new BigAmount((0, bignumber_1.bnOrZero)(args.value), precision, args.assetId);
35
+ }
36
+ return new BigAmount((0, bignumber_1.bnOrZero)(args.value), args.precision);
37
+ }
38
+ static fromPrecision(args) {
39
+ if ('assetId' in args && args.assetId !== undefined) {
40
+ const precision = BigAmount.resolveConfigPrecision(args.assetId);
41
+ return new BigAmount((0, bignumber_1.bnOrZero)(args.value).times(TEN.pow(precision)), precision, args.assetId);
42
+ }
43
+ return new BigAmount((0, bignumber_1.bnOrZero)(args.value).times(TEN.pow(args.precision)), args.precision);
44
+ }
45
+ static zero({ precision, assetId }) {
46
+ return new BigAmount((0, bignumber_1.bn)(0), precision, assetId);
47
+ }
48
+ static fromBN({ value, precision, assetId, }) {
49
+ const safeValue = value.isFinite() ? value : (0, bignumber_1.bn)(0);
50
+ return new BigAmount(safeValue.times(TEN.pow(precision)), precision, assetId);
51
+ }
52
+ static fromJSON({ value, precision, assetId, }) {
53
+ return new BigAmount((0, bignumber_1.bn)(value), precision, assetId);
54
+ }
55
+ static min(...amounts) {
56
+ if (amounts.length === 0)
57
+ throw new Error('BigAmount.min requires at least one argument');
58
+ return amounts.reduce((acc, cur) => {
59
+ assertSamePrecision(acc, cur);
60
+ return acc.value.lte(cur.value) ? acc : cur;
61
+ });
62
+ }
63
+ static max(...amounts) {
64
+ if (amounts.length === 0)
65
+ throw new Error('BigAmount.max requires at least one argument');
66
+ return amounts.reduce((acc, cur) => {
67
+ assertSamePrecision(acc, cur);
68
+ return acc.value.gte(cur.value) ? acc : cur;
69
+ });
70
+ }
71
+ static isBigAmount(value) {
72
+ return value instanceof BigAmount;
73
+ }
74
+ // ── Arithmetic (chainable → BigAmount) ────────────
75
+ plus(other) {
76
+ if (other instanceof BigAmount) {
77
+ assertSamePrecision(this, other);
78
+ return new BigAmount(this.value.plus(other.value), this.precision, this.assetId);
79
+ }
80
+ return new BigAmount(this.value.plus((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision))), this.precision, this.assetId);
81
+ }
82
+ minus(other) {
83
+ if (other instanceof BigAmount) {
84
+ assertSamePrecision(this, other);
85
+ return new BigAmount(this.value.minus(other.value), this.precision, this.assetId);
86
+ }
87
+ return new BigAmount(this.value.minus((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision))), this.precision, this.assetId);
88
+ }
89
+ times(scalar) {
90
+ assertNotBigAmount(scalar, 'times');
91
+ return new BigAmount(this.value.times((0, bignumber_1.bnOrZero)(scalar)), this.precision, this.assetId);
92
+ }
93
+ div(scalar) {
94
+ assertNotBigAmount(scalar, 'div');
95
+ const divisor = (0, bignumber_1.bnOrZero)(scalar);
96
+ if (divisor.isZero()) {
97
+ return BigAmount.zero({ precision: this.precision, assetId: this.assetId });
98
+ }
99
+ return new BigAmount(this.value.div(divisor), this.precision, this.assetId);
100
+ }
101
+ abs() {
102
+ return new BigAmount(this.value.abs(), this.precision, this.assetId);
103
+ }
104
+ negated() {
105
+ return new BigAmount(this.value.negated(), this.precision, this.assetId);
106
+ }
107
+ positiveOrZero() {
108
+ return this.value.isPositive()
109
+ ? this
110
+ : BigAmount.zero({ precision: this.precision, assetId: this.assetId });
111
+ }
112
+ decimalPlaces(n, rm) {
113
+ const precisionValue = this.value.div(TEN.pow(this.precision));
114
+ const truncated = precisionValue.decimalPlaces(n, rm);
115
+ return new BigAmount(truncated.times(TEN.pow(this.precision)), this.precision, this.assetId);
116
+ }
117
+ // ── Comparison (terminal → boolean) ───────────────
118
+ gt(other) {
119
+ if (other instanceof BigAmount) {
120
+ assertSamePrecision(this, other);
121
+ return this.value.gt(other.value);
122
+ }
123
+ return this.value.gt((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision)));
124
+ }
125
+ gte(other) {
126
+ if (other instanceof BigAmount) {
127
+ assertSamePrecision(this, other);
128
+ return this.value.gte(other.value);
129
+ }
130
+ return this.value.gte((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision)));
131
+ }
132
+ lt(other) {
133
+ if (other instanceof BigAmount) {
134
+ assertSamePrecision(this, other);
135
+ return this.value.lt(other.value);
136
+ }
137
+ return this.value.lt((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision)));
138
+ }
139
+ lte(other) {
140
+ if (other instanceof BigAmount) {
141
+ assertSamePrecision(this, other);
142
+ return this.value.lte(other.value);
143
+ }
144
+ return this.value.lte((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision)));
145
+ }
146
+ eq(other) {
147
+ if (other instanceof BigAmount) {
148
+ assertSamePrecision(this, other);
149
+ return this.value.eq(other.value);
150
+ }
151
+ return this.value.eq((0, bignumber_1.bnOrZero)(other).times(TEN.pow(this.precision)));
152
+ }
153
+ isZero() {
154
+ return this.value.isZero();
155
+ }
156
+ isPositive() {
157
+ return this.value.isPositive();
158
+ }
159
+ isNegative() {
160
+ return this.value.isNegative();
161
+ }
162
+ isFinite() {
163
+ return this.value.isFinite();
164
+ }
165
+ // ── Output ────────────────────────────────────────
166
+ toBaseUnit() {
167
+ return this.value.toFixed(0, ROUND_HALF_UP);
168
+ }
169
+ toPrecision() {
170
+ return this.value.div(TEN.pow(this.precision)).toFixed();
171
+ }
172
+ toBN() {
173
+ return this.value.div(TEN.pow(this.precision));
174
+ }
175
+ toFixed(decimals) {
176
+ const precisionValue = this.value.div(TEN.pow(this.precision));
177
+ if (typeof decimals === 'number') {
178
+ return precisionValue.toFixed(decimals, ROUND_DOWN);
179
+ }
180
+ return precisionValue.toFixed();
181
+ }
182
+ toString() {
183
+ return this.toPrecision();
184
+ }
185
+ toNumber() {
186
+ return this.value.div(TEN.pow(this.precision)).toNumber();
187
+ }
188
+ toSignificant(digits) {
189
+ const precisionValue = this.value.div(TEN.pow(this.precision));
190
+ if (precisionValue.isZero())
191
+ return '0';
192
+ const fixed = precisionValue.toFixed();
193
+ const isNeg = fixed.startsWith('-');
194
+ const abs = isNeg ? fixed.slice(1) : fixed;
195
+ const dotIndex = abs.indexOf('.');
196
+ if (dotIndex === -1)
197
+ return fixed;
198
+ const intPart = abs.slice(0, dotIndex);
199
+ const decPart = abs.slice(dotIndex + 1);
200
+ if (intPart !== '0') {
201
+ const intDigits = intPart.length;
202
+ if (intDigits >= digits)
203
+ return (isNeg ? '-' : '') + intPart;
204
+ const trimmed = decPart.slice(0, digits - intDigits);
205
+ const result = `${intPart}.${trimmed}`.replace(/\.?0+$/, '');
206
+ return (isNeg ? '-' : '') + result;
207
+ }
208
+ let firstNonZero = 0;
209
+ while (firstNonZero < decPart.length && decPart[firstNonZero] === '0') {
210
+ firstNonZero++;
211
+ }
212
+ const significantPart = decPart.slice(firstNonZero, firstNonZero + digits);
213
+ const leadingZeros = decPart.slice(0, firstNonZero);
214
+ const result = `0.${leadingZeros}${significantPart}`.replace(/0+$/, '');
215
+ return (isNeg ? '-' : '') + result;
216
+ }
217
+ // ── Fiat conversion ────────────────────────────────
218
+ toUserCurrency(decimals = 2) {
219
+ if (!this.assetId)
220
+ throw new Error('BigAmount: toUserCurrency() requires assetId');
221
+ if (!BigAmount.config?.resolvePrice)
222
+ throw new Error('BigAmount: not configured');
223
+ const price = BigAmount.config.resolvePrice(this.assetId);
224
+ return this.value
225
+ .div(TEN.pow(this.precision))
226
+ .times((0, bignumber_1.bnOrZero)(price))
227
+ .toFixed(decimals, ROUND_HALF_UP);
228
+ }
229
+ toUSD(decimals = 2) {
230
+ if (!this.assetId)
231
+ throw new Error('BigAmount: toUSD() requires assetId');
232
+ if (!BigAmount.config?.resolvePriceUsd)
233
+ throw new Error('BigAmount: not configured');
234
+ const priceUsd = BigAmount.config.resolvePriceUsd(this.assetId);
235
+ return this.value
236
+ .div(TEN.pow(this.precision))
237
+ .times((0, bignumber_1.bnOrZero)(priceUsd))
238
+ .toFixed(decimals, ROUND_HALF_UP);
239
+ }
240
+ // ── THORChain precision ──────────────────────────
241
+ static fromThorBaseUnit(value) {
242
+ return BigAmount.fromBaseUnit({ value, precision: THOR_PRECISION });
243
+ }
244
+ toThorBaseUnit() {
245
+ return this.value
246
+ .times(TEN.pow(THOR_PRECISION))
247
+ .div(TEN.pow(this.precision))
248
+ .toFixed(0, ROUND_HALF_UP);
249
+ }
250
+ // ── Interop ───────────────────────────────────────
251
+ toJSON() {
252
+ return {
253
+ value: this.value.toFixed(0, ROUND_HALF_UP),
254
+ precision: this.precision,
255
+ assetId: this.assetId,
256
+ };
257
+ }
258
+ // ── Private helpers ────────────────────────────────
259
+ static resolveConfigPrecision(assetId) {
260
+ if (!BigAmount.config?.resolvePrecision) {
261
+ throw new Error('BigAmount: not configured — call BigAmount.configure() first');
262
+ }
263
+ return BigAmount.config.resolvePrecision(assetId);
264
+ }
265
+ }
266
+ exports.BigAmount = BigAmount;
267
+ function assertSamePrecision(a, b) {
268
+ if (a.precision !== b.precision) {
269
+ throw new Error(`Cannot operate on amounts with different precisions: ${a.precision} vs ${b.precision}`);
270
+ }
271
+ }
272
+ function assertNotBigAmount(value, method) {
273
+ if (value instanceof BigAmount) {
274
+ throw new TypeError(`BigAmount.${method} does not accept BigAmount as argument (dimensionally invalid). Use .toPrecision() or .toBaseUnit() to extract the scalar first.`);
275
+ }
276
+ }
@@ -0,0 +1 @@
1
+ export {};