@reyaxyz/common 0.214.0 → 0.214.2
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/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -2,6 +2,7 @@ import { ethers } from 'ethers';
|
|
|
2
2
|
import { Address } from '../../types';
|
|
3
3
|
export declare const descale: (tokenDecimals: number) => (value: bigint) => number;
|
|
4
4
|
export declare const scale: (tokenDecimals: number) => (value: number) => bigint;
|
|
5
|
+
export declare const scaleV2: (tokenDecimals: number) => (value: number) => bigint;
|
|
5
6
|
export declare const getERC20TokenContract: (tokenAddress: string, subject: ethers.Signer | ethers.Provider) => ethers.Contract;
|
|
6
7
|
export declare const convertToAddress: (str: string) => Address;
|
|
7
8
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"/","sources":["utils/token/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,eAAO,MAAM,OAAO,kBAAmB,MAAM,aACzB,MAAM,WAKzB,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"/","sources":["utils/token/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,eAAO,MAAM,OAAO,kBAAmB,MAAM,aACzB,MAAM,WAKzB,CAAC;AAGF,eAAO,MAAM,KAAK,kBAAmB,MAAM,aAAY,MAAM,KAAK,MAiBjE,CAAC;AAEF,eAAO,MAAM,OAAO,kBAAmB,MAAM,aAAY,MAAM,KAAK,MA8CnE,CAAC;AAEF,eAAO,MAAM,qBAAqB,iBAClB,MAAM,WACX,OAAO,MAAM,GAAG,OAAO,QAAQ,KACvC,OAAO,QAWT,CAAC;AAEF,eAAO,MAAM,gBAAgB,QAAS,MAAM,KAAG,OAE9C,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToAddress = exports.getERC20TokenContract = exports.scale = exports.descale = void 0;
|
|
3
|
+
exports.convertToAddress = exports.getERC20TokenContract = exports.scaleV2 = exports.scale = exports.descale = void 0;
|
|
4
4
|
var ethers_1 = require("ethers");
|
|
5
5
|
var descale = function (tokenDecimals) {
|
|
6
6
|
var f = function (value) {
|
|
@@ -9,10 +9,11 @@ var descale = function (tokenDecimals) {
|
|
|
9
9
|
return f;
|
|
10
10
|
};
|
|
11
11
|
exports.descale = descale;
|
|
12
|
+
// Note: this function is in process of deprecation, please use scaleV2 instead
|
|
12
13
|
var scale = function (tokenDecimals) {
|
|
13
14
|
return function (value) {
|
|
14
15
|
// Convert the number to a string
|
|
15
|
-
var valueStr = value.toString();
|
|
16
|
+
var valueStr = value.toString(10);
|
|
16
17
|
// Split the string into whole and fractional parts
|
|
17
18
|
// eslint-disable-next-line prefer-const
|
|
18
19
|
var _a = valueStr.split('.'), whole = _a[0], _b = _a[1], fractional = _b === void 0 ? '' : _b;
|
|
@@ -28,6 +29,44 @@ var scale = function (tokenDecimals) {
|
|
|
28
29
|
};
|
|
29
30
|
};
|
|
30
31
|
exports.scale = scale;
|
|
32
|
+
var scaleV2 = function (tokenDecimals) {
|
|
33
|
+
if (tokenDecimals < 0) {
|
|
34
|
+
throw new Error('Invalid token decimals');
|
|
35
|
+
}
|
|
36
|
+
return function (value) {
|
|
37
|
+
if (value < 0) {
|
|
38
|
+
return -(0, exports.scaleV2)(tokenDecimals)(-value);
|
|
39
|
+
}
|
|
40
|
+
if (value < 1) {
|
|
41
|
+
var requiredDecimals = 0;
|
|
42
|
+
while (requiredDecimals < tokenDecimals &&
|
|
43
|
+
value * Math.pow(10, requiredDecimals) < 1) {
|
|
44
|
+
requiredDecimals++;
|
|
45
|
+
}
|
|
46
|
+
if (requiredDecimals > 0) {
|
|
47
|
+
return (0, exports.scaleV2)(tokenDecimals - requiredDecimals)(value * Math.pow(10, requiredDecimals));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (value < 1) {
|
|
51
|
+
return BigInt(0);
|
|
52
|
+
}
|
|
53
|
+
// Convert the number to a string
|
|
54
|
+
var valueStr = value.toString(10);
|
|
55
|
+
// Split the string into whole and fractional parts
|
|
56
|
+
// eslint-disable-next-line prefer-const
|
|
57
|
+
var _a = valueStr.split('.'), whole = _a[0], _b = _a[1], fractional = _b === void 0 ? '' : _b;
|
|
58
|
+
// Ensure the fractional part is the correct length, padding with zeros if necessary
|
|
59
|
+
fractional = fractional
|
|
60
|
+
.padEnd(tokenDecimals, '0')
|
|
61
|
+
.substring(0, tokenDecimals);
|
|
62
|
+
// Concatenate the whole part with the correctly length-limited fractional part
|
|
63
|
+
var scaledValueStr = whole + fractional;
|
|
64
|
+
// Convert the concatenated string to a BigInt, assuming it represents a value in wei (or equivalent smallest unit)
|
|
65
|
+
// This is safe as we're not losing precision - just scaling the number to its smallest unit representation
|
|
66
|
+
return BigInt(scaledValueStr);
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
exports.scaleV2 = scaleV2;
|
|
31
70
|
var getERC20TokenContract = function (tokenAddress, subject) {
|
|
32
71
|
var abi = [
|
|
33
72
|
"function approve(address, uint256) external returns (bool)",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"/","sources":["utils/token/utils.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAGzB,IAAM,OAAO,GAAG,UAAC,aAAqB;IAC3C,IAAM,CAA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nH,2GAA2G;QAC3G,OAAO,MAAM,CAAC,cAAc,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC,CAAC;AA9CW,QAAA,OAAO,WA8ClB;AAEK,IAAM,qBAAqB,GAAG,UACnC,YAAoB,EACpB,OAAwC;IAExC,IAAM,GAAG,GAAa;QACpB,4DAA4D;QAC5D,6DAA6D;QAC7D,qEAAqE;QACrE,2DAA2D;KAC5D,CAAC;IAEF,IAAM,QAAQ,GAAG,IAAI,eAAM,CAAC,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IAEjE,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAdW,QAAA,qBAAqB,yBAchC;AAEK,IAAM,gBAAgB,GAAG,UAAC,GAAW;IAC1C,OAAO,GAAG,CAAC,WAAW,EAAa,CAAC;AACtC,CAAC,CAAC;AAFW,QAAA,gBAAgB,oBAE3B","sourcesContent":["import { ethers } from 'ethers';\nimport { Address } from '../../types';\n\nexport const descale = (tokenDecimals: number) => {\n const f = (value: bigint) => {\n return Number(ethers.formatUnits(value.toString(), tokenDecimals));\n };\n\n return f;\n};\n\n// Note: this function is in process of deprecation, please use scaleV2 instead\nexport const scale = (tokenDecimals: number): ((value: number) => bigint) => {\n return (value: number): bigint => {\n // Convert the number to a string\n const valueStr = value.toString(10);\n // Split the string into whole and fractional parts\n // eslint-disable-next-line prefer-const\n let [whole, fractional = ''] = valueStr.split('.');\n // Ensure the fractional part is the correct length, padding with zeros if necessary\n fractional = fractional\n .padEnd(tokenDecimals, '0')\n .substring(0, tokenDecimals);\n // Concatenate the whole part with the correctly length-limited fractional part\n const scaledValueStr = whole + fractional;\n // Convert the concatenated string to a BigInt, assuming it represents a value in wei (or equivalent smallest unit)\n // This is safe as we're not losing precision - just scaling the number to its smallest unit representation\n return BigInt(scaledValueStr);\n };\n};\n\nexport const scaleV2 = (tokenDecimals: number): ((value: number) => bigint) => {\n if (tokenDecimals < 0) {\n throw new Error('Invalid token decimals');\n }\n\n return (value: number): bigint => {\n if (value < 0) {\n return -scaleV2(tokenDecimals)(-value);\n }\n\n if (value < 1) {\n let requiredDecimals = 0;\n\n while (\n requiredDecimals < tokenDecimals &&\n value * 10 ** requiredDecimals < 1\n ) {\n requiredDecimals++;\n }\n\n if (requiredDecimals > 0) {\n return scaleV2(tokenDecimals - requiredDecimals)(\n value * 10 ** requiredDecimals,\n );\n }\n }\n\n if (value < 1) {\n return BigInt(0);\n }\n\n // Convert the number to a string\n const valueStr = value.toString(10);\n // Split the string into whole and fractional parts\n // eslint-disable-next-line prefer-const\n let [whole, fractional = ''] = valueStr.split('.');\n // Ensure the fractional part is the correct length, padding with zeros if necessary\n fractional = fractional\n .padEnd(tokenDecimals, '0')\n .substring(0, tokenDecimals);\n // Concatenate the whole part with the correctly length-limited fractional part\n const scaledValueStr = whole + fractional;\n // Convert the concatenated string to a BigInt, assuming it represents a value in wei (or equivalent smallest unit)\n // This is safe as we're not losing precision - just scaling the number to its smallest unit representation\n return BigInt(scaledValueStr);\n };\n};\n\nexport const getERC20TokenContract = (\n tokenAddress: string,\n subject: ethers.Signer | ethers.Provider,\n): ethers.Contract => {\n const abi: string[] = [\n `function approve(address, uint256) external returns (bool)`,\n `function balanceOf(address) external view returns (uint256)`,\n `function allowance(address,address) external view returns (uint256)`,\n `function approve(address,uint256) external returns (bool)`,\n ];\n\n const contract = new ethers.Contract(tokenAddress, abi, subject);\n\n return contract;\n};\n\nexport const convertToAddress = (str: string): Address => {\n return str.toLowerCase() as Address;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/common",
|
|
3
|
-
"version": "0.214.
|
|
3
|
+
"version": "0.214.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
44
44
|
},
|
|
45
45
|
"packageManager": "pnpm@8.3.1",
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "16d781e38a7a8bc0c47f7d254300db6451027141"
|
|
47
47
|
}
|
package/src/utils/token/utils.ts
CHANGED
|
@@ -9,10 +9,59 @@ export const descale = (tokenDecimals: number) => {
|
|
|
9
9
|
return f;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
// Note: this function is in process of deprecation, please use scaleV2 instead
|
|
12
13
|
export const scale = (tokenDecimals: number): ((value: number) => bigint) => {
|
|
13
14
|
return (value: number): bigint => {
|
|
14
15
|
// Convert the number to a string
|
|
15
|
-
const valueStr = value.toString();
|
|
16
|
+
const valueStr = value.toString(10);
|
|
17
|
+
// Split the string into whole and fractional parts
|
|
18
|
+
// eslint-disable-next-line prefer-const
|
|
19
|
+
let [whole, fractional = ''] = valueStr.split('.');
|
|
20
|
+
// Ensure the fractional part is the correct length, padding with zeros if necessary
|
|
21
|
+
fractional = fractional
|
|
22
|
+
.padEnd(tokenDecimals, '0')
|
|
23
|
+
.substring(0, tokenDecimals);
|
|
24
|
+
// Concatenate the whole part with the correctly length-limited fractional part
|
|
25
|
+
const scaledValueStr = whole + fractional;
|
|
26
|
+
// Convert the concatenated string to a BigInt, assuming it represents a value in wei (or equivalent smallest unit)
|
|
27
|
+
// This is safe as we're not losing precision - just scaling the number to its smallest unit representation
|
|
28
|
+
return BigInt(scaledValueStr);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const scaleV2 = (tokenDecimals: number): ((value: number) => bigint) => {
|
|
33
|
+
if (tokenDecimals < 0) {
|
|
34
|
+
throw new Error('Invalid token decimals');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return (value: number): bigint => {
|
|
38
|
+
if (value < 0) {
|
|
39
|
+
return -scaleV2(tokenDecimals)(-value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (value < 1) {
|
|
43
|
+
let requiredDecimals = 0;
|
|
44
|
+
|
|
45
|
+
while (
|
|
46
|
+
requiredDecimals < tokenDecimals &&
|
|
47
|
+
value * 10 ** requiredDecimals < 1
|
|
48
|
+
) {
|
|
49
|
+
requiredDecimals++;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (requiredDecimals > 0) {
|
|
53
|
+
return scaleV2(tokenDecimals - requiredDecimals)(
|
|
54
|
+
value * 10 ** requiredDecimals,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (value < 1) {
|
|
60
|
+
return BigInt(0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Convert the number to a string
|
|
64
|
+
const valueStr = value.toString(10);
|
|
16
65
|
// Split the string into whole and fractional parts
|
|
17
66
|
// eslint-disable-next-line prefer-const
|
|
18
67
|
let [whole, fractional = ''] = valueStr.split('.');
|