@pioneer-platform/helpers 4.0.5 → 4.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.
- package/__tests__/test-module.js +7 -6
- package/lib/modules/bigIntArithmetics.d.ts +2 -6
- package/lib/modules/bigIntArithmetics.js +110 -84
- package/lib/modules/swapKitNumber.js +2 -1
- package/package.json +1 -1
- package/src/modules/bigIntArithmetics.ts +109 -101
- package/src/modules/swapKitNumber.ts +3 -3
package/__tests__/test-module.js
CHANGED
@@ -25,19 +25,20 @@ let run_test = async function(){
|
|
25
25
|
// console.log("assetValue: ",assetValue.getValue('string'))
|
26
26
|
|
27
27
|
//get save value
|
28
|
-
let value = helpers.formatBigIntToSafeValue({
|
29
|
-
|
30
|
-
|
31
|
-
})
|
32
|
-
console.log("value: ",value)
|
28
|
+
// let value = helpers.formatBigIntToSafeValue({
|
29
|
+
// value: 62128117093292225n,
|
30
|
+
// decimal: 18,
|
31
|
+
// })
|
32
|
+
// console.log("value: ",value)
|
33
33
|
// console.log("value: ",typeof(value))
|
34
34
|
|
35
35
|
let assetValue = helpers.AssetValue.fromChainOrSignature(
|
36
36
|
'BASE',
|
37
|
-
|
37
|
+
'0.062128117093292224',
|
38
38
|
);
|
39
39
|
console.log("assetValue: ",assetValue)
|
40
40
|
console.log("assetValue: ",assetValue.getValue('string'))
|
41
|
+
|
41
42
|
|
42
43
|
//formatBigIntToSafeValue
|
43
44
|
// let value = helpers.formatBigIntToSafeValue({
|
@@ -11,9 +11,10 @@ type SKBigIntParams = InitialisationValueType | {
|
|
11
11
|
value: number | string;
|
12
12
|
};
|
13
13
|
type AllowedNumberTypes = 'bigint' | 'number' | 'string';
|
14
|
+
export declare const toMultiplier: (decimal: number) => bigint;
|
14
15
|
export declare function formatBigIntToSafeValue({ value, bigIntDecimal, decimal, }: {
|
15
16
|
value: bigint;
|
16
|
-
bigIntDecimal?:
|
17
|
+
bigIntDecimal?: bigint;
|
17
18
|
decimal?: number;
|
18
19
|
}): string;
|
19
20
|
export declare class BigIntArithmetics {
|
@@ -21,11 +22,6 @@ export declare class BigIntArithmetics {
|
|
21
22
|
private bigIntValue;
|
22
23
|
private decimal?;
|
23
24
|
static fromBigInt(value: bigint, decimal?: number): BigIntArithmetics;
|
24
|
-
static shiftDecimals({ value, from, to, }: {
|
25
|
-
value: InstanceType<typeof SwapKitNumber>;
|
26
|
-
from: number;
|
27
|
-
to: number;
|
28
|
-
}): BigIntArithmetics;
|
29
25
|
constructor(params: SKBigIntParams);
|
30
26
|
set(value: SKBigIntParams): this;
|
31
27
|
add(...args: InitialisationValueType[]): this;
|
@@ -9,7 +9,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
9
9
|
return to.concat(ar || Array.prototype.slice.call(from));
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.BigIntArithmetics = exports.formatBigIntToSafeValue = void 0;
|
12
|
+
exports.BigIntArithmetics = exports.formatBigIntToSafeValue = exports.toMultiplier = void 0;
|
13
13
|
var types_1 = require("@coinmasters/types");
|
14
14
|
var TAG = " | BigIntArithmetics | ";
|
15
15
|
var DEFAULT_DECIMAL = 8;
|
@@ -35,7 +35,13 @@ var toMultiplier = function (decimal) {
|
|
35
35
|
var tag = TAG + " | toMultiplier | ";
|
36
36
|
console.log(tag + 'input decimal:', decimal);
|
37
37
|
try {
|
38
|
-
|
38
|
+
if (decimal < 0) {
|
39
|
+
throw new Error("Decimal must be non-negative");
|
40
|
+
}
|
41
|
+
var result = BigInt(1);
|
42
|
+
for (var i = 0; i < decimal; i++) {
|
43
|
+
result *= BigInt(10);
|
44
|
+
}
|
39
45
|
console.log(tag + 'result:', result);
|
40
46
|
return result;
|
41
47
|
}
|
@@ -44,6 +50,7 @@ var toMultiplier = function (decimal) {
|
|
44
50
|
return BigInt(10);
|
45
51
|
}
|
46
52
|
};
|
53
|
+
exports.toMultiplier = toMultiplier;
|
47
54
|
var decimalFromMultiplier = function (multiplier) {
|
48
55
|
var tag = TAG + " | decimalFromMultiplier | ";
|
49
56
|
try {
|
@@ -55,80 +62,79 @@ var decimalFromMultiplier = function (multiplier) {
|
|
55
62
|
}
|
56
63
|
};
|
57
64
|
function formatBigIntToSafeValue(_a) {
|
58
|
-
var value = _a.value,
|
59
|
-
var tag = TAG + " |
|
65
|
+
var value = _a.value, bigIntDecimal = _a.bigIntDecimal, decimal = _a.decimal;
|
66
|
+
var tag = TAG + " | BigIntArithmetics | ";
|
60
67
|
try {
|
61
68
|
console.log(tag, 'value:', value, 'bigIntDecimal:', bigIntDecimal, 'decimal:', decimal);
|
69
|
+
if (!decimal)
|
70
|
+
decimal = DEFAULT_DECIMAL;
|
71
|
+
if (!bigIntDecimal)
|
72
|
+
bigIntDecimal = (0, exports.toMultiplier)(decimal);
|
62
73
|
// Check if the value is negative and throw an error if true
|
63
74
|
if (value < BigInt(0)) {
|
64
|
-
throw new Error(
|
75
|
+
throw new Error(TAG + 'Negative value is not allowed');
|
65
76
|
}
|
66
|
-
// Convert bigint to string and
|
77
|
+
// Convert bigint to string and then to number
|
67
78
|
var valueString = value.toString();
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
var
|
78
|
-
//
|
79
|
-
|
80
|
-
|
81
|
-
}
|
82
|
-
// Construct the final string with the integer and decimal parts
|
83
|
-
var formattedValue = "".concat(valueString.slice(0, decimalIndex), ".").concat(decimalString).replace(/\.?0*$/, '');
|
84
|
-
// Log the formatted value for debugging
|
85
|
-
console.log(tag + 'Formatted Value:', formattedValue);
|
79
|
+
console.log(tag, 'valueString:', valueString);
|
80
|
+
console.log(tag, 'valueString:', valueString.length);
|
81
|
+
console.log(tag, 'decimal:', decimal);
|
82
|
+
// Ensure the valueString has enough length for the decimal places
|
83
|
+
if (valueString.length <= decimal) {
|
84
|
+
valueString = '0'.repeat(decimal - valueString.length + 1) + valueString;
|
85
|
+
}
|
86
|
+
// Determine the integer and decimal parts
|
87
|
+
var integerPart = valueString.slice(0, valueString.length - decimal) || '0';
|
88
|
+
var decimalPart = valueString.slice(valueString.length - decimal).padEnd(decimal, '0');
|
89
|
+
// Construct the final formatted value
|
90
|
+
var formattedValue = "".concat(integerPart, ".").concat(decimalPart);
|
91
|
+
console.log(tag, "formattedValue:", formattedValue);
|
86
92
|
return formattedValue;
|
87
93
|
}
|
88
94
|
catch (error) {
|
89
|
-
|
90
|
-
console.error(tag + 'Error in formatBigIntToSafeValue:', error);
|
95
|
+
console.error(TAG + 'Error in formatBigIntToSafeValue:', error);
|
91
96
|
return '';
|
92
97
|
}
|
93
98
|
}
|
94
99
|
exports.formatBigIntToSafeValue = formatBigIntToSafeValue;
|
95
|
-
// export function formatBigIntToSafeValue({
|
96
|
-
// value,
|
97
|
-
// bigIntDecimal = DEFAULT_DECIMAL,
|
98
|
-
// decimal = DEFAULT_DECIMAL,
|
99
|
-
// }: {
|
100
|
-
// value: bigint;
|
101
|
-
// bigIntDecimal?: number;
|
102
|
-
// decimal?: number;
|
103
|
-
// }): string {
|
104
|
-
// let tag = TAG + " | formatBigIntToSafeValue | ";
|
105
|
-
// try {
|
106
|
-
// const isNegative = value < BigInt(0);
|
107
|
-
// let valueString = value.toString().substring(isNegative ? 1 : 0);
|
108
|
-
//
|
109
|
-
// const padLength = decimal - (valueString.length - 1);
|
110
|
-
//
|
111
|
-
// if (padLength > 0) {
|
112
|
-
// valueString = '0'.repeat(padLength) + valueString;
|
113
|
-
// }
|
114
|
-
//
|
115
|
-
// const decimalIndex = valueString.length - decimal;
|
116
|
-
// let decimalString = valueString.slice(-decimal);
|
117
|
-
//
|
118
|
-
// if (decimalString.length > bigIntDecimal) {
|
119
|
-
// decimalString = decimalString.substring(0, bigIntDecimal);
|
120
|
-
// }
|
121
|
-
//
|
122
|
-
// return `${isNegative ? '-' : ''}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
123
|
-
// /\.?0*$/,
|
124
|
-
// '',
|
125
|
-
// );
|
126
|
-
// } catch (error) {
|
127
|
-
// console.error(tag + 'Error in formatBigIntToSafeValue:', error);
|
128
|
-
// return '';
|
129
|
-
// }
|
130
|
-
// }
|
131
100
|
var BigIntArithmetics = /** @class */ (function () {
|
101
|
+
// static shiftDecimals({
|
102
|
+
// value,
|
103
|
+
// from,
|
104
|
+
// to,
|
105
|
+
// }: {
|
106
|
+
// value: InstanceType<typeof SwapKitNumber>;
|
107
|
+
// from: number;
|
108
|
+
// to: number;
|
109
|
+
// }): BigIntArithmetics {
|
110
|
+
// let tag = TAG + " | shiftDecimals | ";
|
111
|
+
// try {
|
112
|
+
// return this.fromBigInt(
|
113
|
+
// (value.getBaseValue('bigint') * toMultiplier(to)) / toMultiplier(from),
|
114
|
+
// to,
|
115
|
+
// );
|
116
|
+
// } catch (error) {
|
117
|
+
// console.error(tag + 'Error in shiftDecimals:', error);
|
118
|
+
// return new BigIntArithmetics(0);
|
119
|
+
// }
|
120
|
+
// }
|
121
|
+
/*
|
122
|
+
AssetValue
|
123
|
+
{
|
124
|
+
decimalMultiplier: 1000000000000000000n,
|
125
|
+
bigIntValue: 62128120000000000n,
|
126
|
+
decimal: 18,
|
127
|
+
isGasAsset: true,
|
128
|
+
isSynthetic: false,
|
129
|
+
type: 'Native',
|
130
|
+
chain: 'BASE',
|
131
|
+
ticker: 'ETH',
|
132
|
+
symbol: 'ETH',
|
133
|
+
address: undefined,
|
134
|
+
tax: undefined,
|
135
|
+
caip: 'eip155:8453/slip44:60'
|
136
|
+
}
|
137
|
+
*/
|
132
138
|
function BigIntArithmetics(params) {
|
133
139
|
var tag = TAG + " | constructor | ";
|
134
140
|
try {
|
@@ -140,11 +146,11 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
140
146
|
console.log(tag, 'Decimal:', this.decimal);
|
141
147
|
console.log(tag, 'isComplex:', isComplex);
|
142
148
|
if (isComplex) {
|
143
|
-
this.decimalMultiplier = this.decimal;
|
149
|
+
this.decimalMultiplier = (0, exports.toMultiplier)(this.decimal || DEFAULT_DECIMAL);
|
144
150
|
}
|
145
151
|
else {
|
146
152
|
var maxDecimals = Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0);
|
147
|
-
this.decimalMultiplier = toMultiplier(maxDecimals);
|
153
|
+
this.decimalMultiplier = (0, exports.toMultiplier)(maxDecimals);
|
148
154
|
}
|
149
155
|
console.log(tag + 'Decimal Multiplier:', this.decimalMultiplier);
|
150
156
|
this.setValue(value);
|
@@ -160,9 +166,10 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
160
166
|
BigIntArithmetics.fromBigInt = function (value, decimal) {
|
161
167
|
var tag = TAG + " | fromBigInt | ";
|
162
168
|
try {
|
169
|
+
console.log(tag, "Decimal: ", decimal);
|
163
170
|
return new BigIntArithmetics({
|
164
171
|
decimal: decimal,
|
165
|
-
value: formatBigIntToSafeValue({ value: value, bigIntDecimal: decimal, decimal: decimal }),
|
172
|
+
value: formatBigIntToSafeValue({ value: value, bigIntDecimal: (0, exports.toMultiplier)(decimal || DEFAULT_DECIMAL), decimal: decimal }),
|
166
173
|
});
|
167
174
|
}
|
168
175
|
catch (error) {
|
@@ -170,17 +177,33 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
170
177
|
return new BigIntArithmetics(0);
|
171
178
|
}
|
172
179
|
};
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
180
|
+
// constructor(params: SKBigIntParams) {
|
181
|
+
// let tag = TAG + " | constructor | ";
|
182
|
+
// try {
|
183
|
+
// console.log(tag + 'Constructor Params:', params);
|
184
|
+
// const value = getStringValue(params);
|
185
|
+
// console.log(tag + 'String Value:', value);
|
186
|
+
// const isComplex = typeof params === 'object' && params !== null;
|
187
|
+
// this.decimal = isComplex ? (params as { decimal?: number }).decimal : undefined;
|
188
|
+
// console.log(tag , 'Decimal:', this.decimal);
|
189
|
+
// console.log(tag , 'isComplex:', isComplex);
|
190
|
+
// if (isComplex) {
|
191
|
+
// this.decimalMultiplier = this.decimal;
|
192
|
+
// } else {
|
193
|
+
// const maxDecimals = Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0);
|
194
|
+
// this.decimalMultiplier = toMultiplier(maxDecimals);
|
195
|
+
// }
|
196
|
+
// console.log(tag + 'Decimal Multiplier:', this.decimalMultiplier);
|
197
|
+
//
|
198
|
+
// this.setValue(value);
|
199
|
+
// //@ts-ignore
|
200
|
+
// console.log(tag + 'BigInt Value:', this.bigIntValue);
|
201
|
+
// } catch (error) {
|
202
|
+
// console.error(tag + 'Error in constructor:', error);
|
203
|
+
// this.decimalMultiplier = BigInt(1);
|
204
|
+
// this.bigIntValue = BigInt(0);
|
205
|
+
// }
|
206
|
+
// }
|
184
207
|
BigIntArithmetics.prototype.set = function (value) {
|
185
208
|
var tag = TAG + " | set | ";
|
186
209
|
try {
|
@@ -306,8 +329,8 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
306
329
|
console.log(tag, 'decimalMultiplier: ', this.decimalMultiplier);
|
307
330
|
var value = formatBigIntToSafeValue({
|
308
331
|
value: this.bigIntValue,
|
309
|
-
bigIntDecimal: this.decimal ||
|
310
|
-
decimal: this.
|
332
|
+
bigIntDecimal: (0, exports.toMultiplier)(this.decimal || DEFAULT_DECIMAL),
|
333
|
+
decimal: this.decimal || DEFAULT_DECIMAL,
|
311
334
|
});
|
312
335
|
console.log(tag, 'value:', value);
|
313
336
|
switch (type) {
|
@@ -329,7 +352,7 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
329
352
|
BigIntArithmetics.prototype.getBaseValue = function (type) {
|
330
353
|
var tag = TAG + " | getBaseValue | ";
|
331
354
|
try {
|
332
|
-
var divisor = this.decimalMultiplier / toMultiplier(this.decimal || types_1.BaseDecimal.THOR);
|
355
|
+
var divisor = this.decimalMultiplier / (0, exports.toMultiplier)(this.decimal || types_1.BaseDecimal.THOR);
|
333
356
|
var baseValue = this.bigIntValue / divisor;
|
334
357
|
switch (type) {
|
335
358
|
case 'number':
|
@@ -456,7 +479,7 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
456
479
|
try {
|
457
480
|
var precisionDecimal = this.retrievePrecisionDecimal.apply(this, __spreadArray([this], args, false));
|
458
481
|
var decimal_1 = Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier));
|
459
|
-
var precisionDecimalMultiplier_1 = toMultiplier(decimal_1);
|
482
|
+
var precisionDecimalMultiplier_1 = (0, exports.toMultiplier)(decimal_1);
|
460
483
|
var result = args.reduce(function (acc, arg) {
|
461
484
|
var value = _this.getBigIntValue(arg, decimal_1);
|
462
485
|
switch (method) {
|
@@ -476,12 +499,12 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
476
499
|
}
|
477
500
|
}, (this.bigIntValue * precisionDecimalMultiplier_1) / this.decimalMultiplier);
|
478
501
|
var formattedValue = formatBigIntToSafeValue({
|
479
|
-
bigIntDecimal: decimal_1,
|
502
|
+
bigIntDecimal: (0, exports.toMultiplier)(decimal_1),
|
480
503
|
decimal: decimal_1,
|
481
504
|
value: result,
|
482
505
|
});
|
483
506
|
return new BigIntArithmetics({
|
484
|
-
decimalMultiplier: toMultiplier(decimal_1),
|
507
|
+
decimalMultiplier: (0, exports.toMultiplier)(decimal_1),
|
485
508
|
decimal: this.decimal,
|
486
509
|
value: formattedValue,
|
487
510
|
});
|
@@ -522,7 +545,10 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
522
545
|
BigIntArithmetics.prototype.setValue = function (value) {
|
523
546
|
var tag = TAG + " | setValue | ";
|
524
547
|
try {
|
525
|
-
|
548
|
+
console.log(tag, 'value:', value);
|
549
|
+
console.log(tag, ' this.decimal:', this.decimal);
|
550
|
+
var safeValue = formatBigIntToSafeValue({ value: value, decimal: this.decimal });
|
551
|
+
console.log(tag, 'safeValue:', safeValue);
|
526
552
|
this.bigIntValue = this.toBigInt(safeValue);
|
527
553
|
}
|
528
554
|
catch (error) {
|
@@ -555,7 +581,7 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
555
581
|
BigIntArithmetics.prototype.toBigInt = function (value, decimal) {
|
556
582
|
var tag = TAG + " | toBigInt | ";
|
557
583
|
try {
|
558
|
-
var multiplier = decimal ? toMultiplier(decimal) : this.decimalMultiplier;
|
584
|
+
var multiplier = decimal ? (0, exports.toMultiplier)(decimal) : this.decimalMultiplier;
|
559
585
|
var padDecimal = decimalFromMultiplier(multiplier);
|
560
586
|
var _a = value.split('.'), _b = _a[0], integerPart = _b === void 0 ? '' : _b, _c = _a[1], decimalPart = _c === void 0 ? '' : _c;
|
561
587
|
return BigInt("".concat(integerPart).concat(decimalPart.padEnd(padDecimal, '0')));
|
@@ -17,6 +17,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
18
18
|
exports.SwapKitNumber = void 0;
|
19
19
|
var bigIntArithmetics_1 = require("./bigIntArithmetics");
|
20
|
+
var DEFAULT_DECIMAL = 8;
|
20
21
|
var SwapKitNumber = /** @class */ (function (_super) {
|
21
22
|
__extends(SwapKitNumber, _super);
|
22
23
|
function SwapKitNumber() {
|
@@ -28,7 +29,7 @@ var SwapKitNumber = /** @class */ (function (_super) {
|
|
28
29
|
SwapKitNumber.fromBigInt = function (value, decimal) {
|
29
30
|
return new SwapKitNumber({
|
30
31
|
decimal: decimal,
|
31
|
-
value: (0, bigIntArithmetics_1.formatBigIntToSafeValue)({ value: value, bigIntDecimal: decimal, decimal: decimal }),
|
32
|
+
value: (0, bigIntArithmetics_1.formatBigIntToSafeValue)({ value: value, bigIntDecimal: (0, bigIntArithmetics_1.toMultiplier)(decimal || DEFAULT_DECIMAL), decimal: decimal }),
|
32
33
|
});
|
33
34
|
};
|
34
35
|
return SwapKitNumber;
|
package/package.json
CHANGED
@@ -34,11 +34,17 @@ const bigintPow = (base: bigint, exponent: number): bigint => {
|
|
34
34
|
}
|
35
35
|
};
|
36
36
|
|
37
|
-
const toMultiplier = (decimal: number): bigint => {
|
37
|
+
export const toMultiplier = (decimal: number): bigint => {
|
38
38
|
let tag = TAG + " | toMultiplier | ";
|
39
39
|
console.log(tag + 'input decimal:', decimal);
|
40
40
|
try {
|
41
|
-
|
41
|
+
if (decimal < 0) {
|
42
|
+
throw new Error("Decimal must be non-negative");
|
43
|
+
}
|
44
|
+
let result = BigInt(1);
|
45
|
+
for (let i = 0; i < decimal; i++) {
|
46
|
+
result *= BigInt(10);
|
47
|
+
}
|
42
48
|
console.log(tag + 'result:', result);
|
43
49
|
return result;
|
44
50
|
} catch (error) {
|
@@ -59,97 +65,50 @@ const decimalFromMultiplier = (multiplier: bigint): number => {
|
|
59
65
|
|
60
66
|
export function formatBigIntToSafeValue({
|
61
67
|
value,
|
62
|
-
bigIntDecimal
|
63
|
-
decimal
|
68
|
+
bigIntDecimal,
|
69
|
+
decimal,
|
64
70
|
}: {
|
65
71
|
value: bigint;
|
66
|
-
bigIntDecimal?:
|
72
|
+
bigIntDecimal?: bigint;
|
67
73
|
decimal?: number;
|
68
74
|
}): string {
|
69
|
-
|
75
|
+
const tag = TAG+" | BigIntArithmetics | ";
|
70
76
|
try {
|
71
|
-
console.log(tag,
|
77
|
+
console.log(tag,'value:',value,'bigIntDecimal:',bigIntDecimal,'decimal:',decimal);
|
78
|
+
if(!decimal) decimal = DEFAULT_DECIMAL;
|
79
|
+
if(!bigIntDecimal) bigIntDecimal = toMultiplier(decimal);
|
80
|
+
|
72
81
|
// Check if the value is negative and throw an error if true
|
73
82
|
if (value < BigInt(0)) {
|
74
|
-
throw new Error(
|
83
|
+
throw new Error(TAG + 'Negative value is not allowed');
|
75
84
|
}
|
76
85
|
|
77
|
-
// Convert bigint to string and
|
86
|
+
// Convert bigint to string and then to number
|
78
87
|
let valueString = value.toString();
|
88
|
+
console.log(tag,'valueString:',valueString);
|
89
|
+
console.log(tag,'valueString:',valueString.length);
|
79
90
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
if (padLength > 0) {
|
85
|
-
valueString = '0'.repeat(padLength) + valueString;
|
91
|
+
console.log(tag,'decimal:',decimal);
|
92
|
+
// Ensure the valueString has enough length for the decimal places
|
93
|
+
if (valueString.length <= decimal) {
|
94
|
+
valueString = '0'.repeat(decimal - valueString.length + 1) + valueString;
|
86
95
|
}
|
87
96
|
|
88
|
-
// Determine the
|
89
|
-
const
|
97
|
+
// Determine the integer and decimal parts
|
98
|
+
const integerPart = valueString.slice(0, valueString.length - decimal) || '0';
|
99
|
+
const decimalPart = valueString.slice(valueString.length - decimal).padEnd(decimal, '0');
|
90
100
|
|
91
|
-
//
|
92
|
-
|
93
|
-
|
94
|
-
// Trim the decimal string to match the specified bigIntDecimal length
|
95
|
-
if (decimalString.length > bigIntDecimal) {
|
96
|
-
decimalString = decimalString.substring(0, bigIntDecimal);
|
97
|
-
}
|
98
|
-
|
99
|
-
// Construct the final string with the integer and decimal parts
|
100
|
-
const formattedValue = `${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
101
|
-
/\.?0*$/,
|
102
|
-
'',
|
103
|
-
);
|
104
|
-
|
105
|
-
// Log the formatted value for debugging
|
106
|
-
console.log(tag + 'Formatted Value:', formattedValue);
|
101
|
+
// Construct the final formatted value
|
102
|
+
const formattedValue = `${integerPart}.${decimalPart}`;
|
107
103
|
|
104
|
+
console.log(tag,"formattedValue:", formattedValue);
|
108
105
|
return formattedValue;
|
109
106
|
} catch (error) {
|
110
|
-
|
111
|
-
console.error(tag + 'Error in formatBigIntToSafeValue:', error);
|
107
|
+
console.error(TAG + 'Error in formatBigIntToSafeValue:', error);
|
112
108
|
return '';
|
113
109
|
}
|
114
110
|
}
|
115
111
|
|
116
|
-
// export function formatBigIntToSafeValue({
|
117
|
-
// value,
|
118
|
-
// bigIntDecimal = DEFAULT_DECIMAL,
|
119
|
-
// decimal = DEFAULT_DECIMAL,
|
120
|
-
// }: {
|
121
|
-
// value: bigint;
|
122
|
-
// bigIntDecimal?: number;
|
123
|
-
// decimal?: number;
|
124
|
-
// }): string {
|
125
|
-
// let tag = TAG + " | formatBigIntToSafeValue | ";
|
126
|
-
// try {
|
127
|
-
// const isNegative = value < BigInt(0);
|
128
|
-
// let valueString = value.toString().substring(isNegative ? 1 : 0);
|
129
|
-
//
|
130
|
-
// const padLength = decimal - (valueString.length - 1);
|
131
|
-
//
|
132
|
-
// if (padLength > 0) {
|
133
|
-
// valueString = '0'.repeat(padLength) + valueString;
|
134
|
-
// }
|
135
|
-
//
|
136
|
-
// const decimalIndex = valueString.length - decimal;
|
137
|
-
// let decimalString = valueString.slice(-decimal);
|
138
|
-
//
|
139
|
-
// if (decimalString.length > bigIntDecimal) {
|
140
|
-
// decimalString = decimalString.substring(0, bigIntDecimal);
|
141
|
-
// }
|
142
|
-
//
|
143
|
-
// return `${isNegative ? '-' : ''}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
144
|
-
// /\.?0*$/,
|
145
|
-
// '',
|
146
|
-
// );
|
147
|
-
// } catch (error) {
|
148
|
-
// console.error(tag + 'Error in formatBigIntToSafeValue:', error);
|
149
|
-
// return '';
|
150
|
-
// }
|
151
|
-
// }
|
152
|
-
|
153
112
|
export class BigIntArithmetics {
|
154
113
|
private decimalMultiplier: any;
|
155
114
|
//@ts-ignore
|
@@ -159,9 +118,11 @@ export class BigIntArithmetics {
|
|
159
118
|
static fromBigInt(value: bigint, decimal?: number): BigIntArithmetics {
|
160
119
|
let tag = TAG + " | fromBigInt | ";
|
161
120
|
try {
|
121
|
+
console.log(tag,"Decimal: ", decimal);
|
122
|
+
|
162
123
|
return new BigIntArithmetics({
|
163
124
|
decimal,
|
164
|
-
value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
|
125
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: toMultiplier(decimal || DEFAULT_DECIMAL), decimal }),
|
165
126
|
});
|
166
127
|
} catch (error) {
|
167
128
|
console.error(tag + 'Error in fromBigInt:', error);
|
@@ -169,28 +130,44 @@ export class BigIntArithmetics {
|
|
169
130
|
}
|
170
131
|
}
|
171
132
|
|
172
|
-
static shiftDecimals({
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
}): BigIntArithmetics {
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
|
133
|
+
// static shiftDecimals({
|
134
|
+
// value,
|
135
|
+
// from,
|
136
|
+
// to,
|
137
|
+
// }: {
|
138
|
+
// value: InstanceType<typeof SwapKitNumber>;
|
139
|
+
// from: number;
|
140
|
+
// to: number;
|
141
|
+
// }): BigIntArithmetics {
|
142
|
+
// let tag = TAG + " | shiftDecimals | ";
|
143
|
+
// try {
|
144
|
+
// return this.fromBigInt(
|
145
|
+
// (value.getBaseValue('bigint') * toMultiplier(to)) / toMultiplier(from),
|
146
|
+
// to,
|
147
|
+
// );
|
148
|
+
// } catch (error) {
|
149
|
+
// console.error(tag + 'Error in shiftDecimals:', error);
|
150
|
+
// return new BigIntArithmetics(0);
|
151
|
+
// }
|
152
|
+
// }
|
153
|
+
|
154
|
+
/*
|
155
|
+
AssetValue
|
156
|
+
{
|
157
|
+
decimalMultiplier: 1000000000000000000n,
|
158
|
+
bigIntValue: 62128120000000000n,
|
159
|
+
decimal: 18,
|
160
|
+
isGasAsset: true,
|
161
|
+
isSynthetic: false,
|
162
|
+
type: 'Native',
|
163
|
+
chain: 'BASE',
|
164
|
+
ticker: 'ETH',
|
165
|
+
symbol: 'ETH',
|
166
|
+
address: undefined,
|
167
|
+
tax: undefined,
|
168
|
+
caip: 'eip155:8453/slip44:60'
|
169
|
+
}
|
170
|
+
*/
|
194
171
|
|
195
172
|
constructor(params: SKBigIntParams) {
|
196
173
|
let tag = TAG + " | constructor | ";
|
@@ -203,7 +180,7 @@ export class BigIntArithmetics {
|
|
203
180
|
console.log(tag , 'Decimal:', this.decimal);
|
204
181
|
console.log(tag , 'isComplex:', isComplex);
|
205
182
|
if (isComplex) {
|
206
|
-
this.decimalMultiplier = this.decimal;
|
183
|
+
this.decimalMultiplier = toMultiplier(this.decimal || DEFAULT_DECIMAL);
|
207
184
|
} else {
|
208
185
|
const maxDecimals = Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0);
|
209
186
|
this.decimalMultiplier = toMultiplier(maxDecimals);
|
@@ -219,6 +196,34 @@ export class BigIntArithmetics {
|
|
219
196
|
this.bigIntValue = BigInt(0);
|
220
197
|
}
|
221
198
|
}
|
199
|
+
|
200
|
+
// constructor(params: SKBigIntParams) {
|
201
|
+
// let tag = TAG + " | constructor | ";
|
202
|
+
// try {
|
203
|
+
// console.log(tag + 'Constructor Params:', params);
|
204
|
+
// const value = getStringValue(params);
|
205
|
+
// console.log(tag + 'String Value:', value);
|
206
|
+
// const isComplex = typeof params === 'object' && params !== null;
|
207
|
+
// this.decimal = isComplex ? (params as { decimal?: number }).decimal : undefined;
|
208
|
+
// console.log(tag , 'Decimal:', this.decimal);
|
209
|
+
// console.log(tag , 'isComplex:', isComplex);
|
210
|
+
// if (isComplex) {
|
211
|
+
// this.decimalMultiplier = this.decimal;
|
212
|
+
// } else {
|
213
|
+
// const maxDecimals = Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0);
|
214
|
+
// this.decimalMultiplier = toMultiplier(maxDecimals);
|
215
|
+
// }
|
216
|
+
// console.log(tag + 'Decimal Multiplier:', this.decimalMultiplier);
|
217
|
+
//
|
218
|
+
// this.setValue(value);
|
219
|
+
// //@ts-ignore
|
220
|
+
// console.log(tag + 'BigInt Value:', this.bigIntValue);
|
221
|
+
// } catch (error) {
|
222
|
+
// console.error(tag + 'Error in constructor:', error);
|
223
|
+
// this.decimalMultiplier = BigInt(1);
|
224
|
+
// this.bigIntValue = BigInt(0);
|
225
|
+
// }
|
226
|
+
// }
|
222
227
|
|
223
228
|
set(value: SKBigIntParams): this {
|
224
229
|
let tag = TAG + " | set | ";
|
@@ -328,8 +333,8 @@ export class BigIntArithmetics {
|
|
328
333
|
console.log(tag,'decimalMultiplier: ', this.decimalMultiplier);
|
329
334
|
const value = formatBigIntToSafeValue({
|
330
335
|
value: this.bigIntValue,
|
331
|
-
bigIntDecimal: this.decimal ||
|
332
|
-
decimal: this.
|
336
|
+
bigIntDecimal: toMultiplier(this.decimal || DEFAULT_DECIMAL),
|
337
|
+
decimal: this.decimal || DEFAULT_DECIMAL,
|
333
338
|
});
|
334
339
|
console.log(tag,'value:', value);
|
335
340
|
switch (type) {
|
@@ -526,7 +531,7 @@ export class BigIntArithmetics {
|
|
526
531
|
);
|
527
532
|
|
528
533
|
const formattedValue = formatBigIntToSafeValue({
|
529
|
-
bigIntDecimal: decimal,
|
534
|
+
bigIntDecimal: toMultiplier(decimal),
|
530
535
|
decimal,
|
531
536
|
value: result,
|
532
537
|
});
|
@@ -567,10 +572,13 @@ export class BigIntArithmetics {
|
|
567
572
|
}
|
568
573
|
}
|
569
574
|
|
570
|
-
private setValue(value:
|
575
|
+
private setValue(value: any): void {
|
571
576
|
let tag = TAG + " | setValue | ";
|
572
577
|
try {
|
573
|
-
|
578
|
+
console.log(tag, 'value:', value);
|
579
|
+
console.log(tag, ' this.decimal:', this.decimal);
|
580
|
+
const safeValue = formatBigIntToSafeValue({value:value, decimal: this.decimal})
|
581
|
+
console.log(tag, 'safeValue:', safeValue);
|
574
582
|
this.bigIntValue = this.toBigInt(safeValue);
|
575
583
|
} catch (error) {
|
576
584
|
console.error(tag + 'Error in setValue:', error);
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics';
|
1
|
+
import { BigIntArithmetics, formatBigIntToSafeValue, toMultiplier } from './bigIntArithmetics';
|
2
2
|
|
3
3
|
export type SwapKitValueType = BigIntArithmetics | string | number;
|
4
|
-
|
4
|
+
const DEFAULT_DECIMAL = 8;
|
5
5
|
export class SwapKitNumber extends BigIntArithmetics {
|
6
6
|
eq(value: SwapKitValueType) {
|
7
7
|
return this.eqValue(value);
|
@@ -10,7 +10,7 @@ export class SwapKitNumber extends BigIntArithmetics {
|
|
10
10
|
static fromBigInt(value: bigint, decimal?: number) {
|
11
11
|
return new SwapKitNumber({
|
12
12
|
decimal,
|
13
|
-
value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
|
13
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: toMultiplier(decimal || DEFAULT_DECIMAL), decimal }),
|
14
14
|
});
|
15
15
|
}
|
16
16
|
}
|