@seljs/types 1.0.0 → 1.0.1-beta.9
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/dist/abstracts/{type-wrapper.d.ts → type-wrapper.d.cts} +6 -4
- package/dist/abstracts/type-wrapper.d.mts +15 -0
- package/dist/{context.d.ts → context.d.cts} +17 -18
- package/dist/context.d.mts +57 -0
- package/dist/conversion.cjs +41 -0
- package/dist/{conversion.d.ts → conversion.d.cts} +5 -3
- package/dist/conversion.d.mts +15 -0
- package/dist/conversion.mjs +40 -0
- package/dist/custom-types/solidity-address-type-wrapper.cjs +38 -0
- package/dist/custom-types/solidity-address-type-wrapper.d.cts +25 -0
- package/dist/custom-types/solidity-address-type-wrapper.d.mts +25 -0
- package/dist/custom-types/solidity-address-type-wrapper.mjs +37 -0
- package/dist/custom-types/solidity-int-type-wrapper.cjs +31 -0
- package/dist/custom-types/{solidity-int-type-wrapper.d.ts → solidity-int-type-wrapper.d.cts} +10 -7
- package/dist/custom-types/solidity-int-type-wrapper.d.mts +22 -0
- package/dist/custom-types/solidity-int-type-wrapper.mjs +30 -0
- package/dist/index.cjs +15 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +6 -0
- package/dist/registry.cjs +65 -0
- package/dist/registry.d.cts +28 -0
- package/dist/registry.d.mts +28 -0
- package/dist/registry.mjs +65 -0
- package/dist/units.cjs +44 -0
- package/dist/{units.d.ts → units.d.cts} +7 -4
- package/dist/units.d.mts +18 -0
- package/dist/units.mjs +43 -0
- package/package.json +17 -10
- package/dist/abstracts/index.d.ts +0 -2
- package/dist/abstracts/index.d.ts.map +0 -1
- package/dist/abstracts/index.js +0 -1
- package/dist/abstracts/type-wrapper.d.ts.map +0 -1
- package/dist/abstracts/type-wrapper.js +0 -1
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -1
- package/dist/context.spec.d.ts +0 -2
- package/dist/context.spec.d.ts.map +0 -1
- package/dist/context.spec.js +0 -38
- package/dist/conversion.d.ts.map +0 -1
- package/dist/conversion.js +0 -48
- package/dist/conversion.spec.d.ts +0 -2
- package/dist/conversion.spec.d.ts.map +0 -1
- package/dist/conversion.spec.js +0 -53
- package/dist/custom-types/index.d.ts +0 -3
- package/dist/custom-types/index.d.ts.map +0 -1
- package/dist/custom-types/index.js +0 -2
- package/dist/custom-types/solidity-address-type-wrapper.d.ts +0 -22
- package/dist/custom-types/solidity-address-type-wrapper.d.ts.map +0 -1
- package/dist/custom-types/solidity-address-type-wrapper.js +0 -40
- package/dist/custom-types/solidity-int-type-wrapper.d.ts.map +0 -1
- package/dist/custom-types/solidity-int-type-wrapper.js +0 -35
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -6
- package/dist/registry.d.ts +0 -26
- package/dist/registry.d.ts.map +0 -1
- package/dist/registry.js +0 -58
- package/dist/registry.spec.d.ts +0 -2
- package/dist/registry.spec.d.ts.map +0 -1
- package/dist/registry.spec.js +0 -57
- package/dist/units.d.ts.map +0 -1
- package/dist/units.js +0 -55
- package/dist/units.spec.d.ts +0 -2
- package/dist/units.spec.d.ts.map +0 -1
- package/dist/units.spec.js +0 -77
package/dist/registry.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { SolidityAddressTypeWrapper, SolidityIntTypeWrapper, } from "./custom-types/index.js";
|
|
2
|
-
/**
|
|
3
|
-
* Generates the list of Solidity bytes type aliases (bytes, bytes1, ..., bytes32).
|
|
4
|
-
*/
|
|
5
|
-
const generateBytesAliases = () => {
|
|
6
|
-
const aliases = ["bytes"];
|
|
7
|
-
// Generate bytes1 to bytes32 aliases.
|
|
8
|
-
for (let size = 1; size <= 32; size++) {
|
|
9
|
-
aliases.push(`bytes${String(size)}`);
|
|
10
|
-
}
|
|
11
|
-
return aliases;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Generates the list of Solidity int/uint type aliases (int, uint, int8, uint8, ..., int256, uint256).
|
|
15
|
-
*/
|
|
16
|
-
const generateIntAliases = () => {
|
|
17
|
-
const aliases = ["uint", "int"];
|
|
18
|
-
// Generate uint8 to uint256 aliases (including "uint" as alias for "uint256").
|
|
19
|
-
for (let size = 8; size <= 256; size += 8) {
|
|
20
|
-
aliases.push(`uint${String(size)}`);
|
|
21
|
-
}
|
|
22
|
-
// Generate int8 to int256 aliases (including "int" as alias for "int256").
|
|
23
|
-
for (let size = 8; size <= 256; size += 8) {
|
|
24
|
-
aliases.push(`int${String(size)}`);
|
|
25
|
-
}
|
|
26
|
-
return aliases;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* The single source of truth for Solidity type identity in the CEL environment.
|
|
30
|
-
* Keyed by CEL type name.
|
|
31
|
-
*/
|
|
32
|
-
export const SOLIDITY_TYPES = [
|
|
33
|
-
/*
|
|
34
|
-
* - Byte types: bytes, bytes1-bytes32 → "bytes"
|
|
35
|
-
* This maps all byte array types to a single "bytes" CEL type.
|
|
36
|
-
*/
|
|
37
|
-
{ type: "builtin", name: "bytes", solidityAliases: generateBytesAliases() },
|
|
38
|
-
{ type: "builtin", name: "string", solidityAliases: ["string"] },
|
|
39
|
-
{ type: "builtin", name: "bool", solidityAliases: ["bool"] },
|
|
40
|
-
// - Address type: address → "sol_address"
|
|
41
|
-
{
|
|
42
|
-
type: "custom",
|
|
43
|
-
name: "sol_address",
|
|
44
|
-
solidityAliases: ["address"],
|
|
45
|
-
wrapperClass: SolidityAddressTypeWrapper,
|
|
46
|
-
schema: { kind: "primitive", description: "20-byte Ethereum address" },
|
|
47
|
-
castSignatures: ["solAddress(string): sol_address"],
|
|
48
|
-
},
|
|
49
|
-
// - Integer types: sol_int
|
|
50
|
-
{
|
|
51
|
-
type: "custom",
|
|
52
|
-
name: "sol_int",
|
|
53
|
-
solidityAliases: generateIntAliases(),
|
|
54
|
-
wrapperClass: SolidityIntTypeWrapper,
|
|
55
|
-
schema: { kind: "primitive", description: "Arbitrary-size integer" },
|
|
56
|
-
castSignatures: ["solInt(string): sol_int", "solInt(int): sol_int"],
|
|
57
|
-
},
|
|
58
|
-
];
|
package/dist/registry.spec.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"registry.spec.d.ts","sourceRoot":"","sources":["../src/registry.spec.ts"],"names":[],"mappings":""}
|
package/dist/registry.spec.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { SolidityAddressTypeWrapper, SolidityIntTypeWrapper, } from "./custom-types/index.js";
|
|
3
|
-
import { SOLIDITY_TYPES } from "./registry.js";
|
|
4
|
-
describe("src/registry.ts", () => {
|
|
5
|
-
const findByName = (name) => SOLIDITY_TYPES.find((t) => t.name === name);
|
|
6
|
-
describe("registered types", () => {
|
|
7
|
-
it.each(["bytes", "string", "bool", "sol_address", "sol_int"])("has %j registered", (name) => {
|
|
8
|
-
expect(findByName(name)).toBeDefined();
|
|
9
|
-
});
|
|
10
|
-
});
|
|
11
|
-
describe("builtins", () => {
|
|
12
|
-
it.each([
|
|
13
|
-
["bytes", "builtin"],
|
|
14
|
-
["string", "builtin"],
|
|
15
|
-
["bool", "builtin"],
|
|
16
|
-
])("%j is type %j", (name, expectedType) => {
|
|
17
|
-
expect(findByName(name)?.type).toBe(expectedType);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
describe("custom types", () => {
|
|
21
|
-
it.each([
|
|
22
|
-
["sol_address", SolidityAddressTypeWrapper],
|
|
23
|
-
["sol_int", SolidityIntTypeWrapper],
|
|
24
|
-
])("%j has wrapperClass", (name, expectedClass) => {
|
|
25
|
-
const entry = findByName(name);
|
|
26
|
-
expect(entry?.type).toBe("custom");
|
|
27
|
-
expect(entry.wrapperClass).toBe(expectedClass);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe("bytes aliases", () => {
|
|
31
|
-
const bytesEntry = findByName("bytes");
|
|
32
|
-
it("includes dynamic bytes", () => {
|
|
33
|
-
expect(bytesEntry?.solidityAliases).toContain("bytes");
|
|
34
|
-
});
|
|
35
|
-
it.each(["bytes1", "bytes16", "bytes32"])("includes %j", (alias) => {
|
|
36
|
-
expect(bytesEntry?.solidityAliases).toContain(alias);
|
|
37
|
-
});
|
|
38
|
-
it("has 33 aliases (bytes + bytes1..bytes32)", () => {
|
|
39
|
-
expect(bytesEntry?.solidityAliases).toHaveLength(33);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
describe("int aliases", () => {
|
|
43
|
-
const intEntry = findByName("sol_int");
|
|
44
|
-
it.each(["uint", "int", "uint8", "int8", "uint256", "int256"])("includes %j", (alias) => {
|
|
45
|
-
expect(intEntry?.solidityAliases).toContain(alias);
|
|
46
|
-
});
|
|
47
|
-
it("has 66 aliases (uint + int + uint8..uint256 + int8..int256)", () => {
|
|
48
|
-
// 2 bare aliases + 32 uint sizes + 32 int sizes = 66
|
|
49
|
-
expect(intEntry?.solidityAliases).toHaveLength(66);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
describe("address aliases", () => {
|
|
53
|
-
it("has only 'address'", () => {
|
|
54
|
-
expect(findByName("sol_address")?.solidityAliases).toEqual(["address"]);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
});
|
package/dist/units.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../src/units.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAY,MAAM,yBAAyB,CAAC;AAkC3E;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAC3B,OAAO,OAAO,EACd,UAAU,MAAM,KACd,sBAwBF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,OAAO,EAAE,UAAU,MAAM,KAAG,MAOnE,CAAC"}
|
package/dist/units.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { SolidityIntTypeWrapper, toBigInt } from "./custom-types/index.js";
|
|
2
|
-
const parseDecimalString = (value, decimals, scale) => {
|
|
3
|
-
const negative = value.startsWith("-");
|
|
4
|
-
const abs = negative ? value.slice(1) : value;
|
|
5
|
-
const parts = abs.split(".");
|
|
6
|
-
if (parts.length > 2) {
|
|
7
|
-
throw new TypeError(`parseUnits: invalid decimal string: "${value}"`);
|
|
8
|
-
}
|
|
9
|
-
const wholePart = parts[0] ?? "0";
|
|
10
|
-
const fracPart = parts[1] ?? "";
|
|
11
|
-
if (fracPart.length > decimals) {
|
|
12
|
-
throw new TypeError(`parseUnits: fractional part "${fracPart}" exceeds ${String(decimals)} decimals`);
|
|
13
|
-
}
|
|
14
|
-
const whole = BigInt(wholePart) * scale;
|
|
15
|
-
const frac = fracPart.length > 0 ? BigInt(fracPart.padEnd(decimals, "0")) : 0n;
|
|
16
|
-
const result = whole + frac;
|
|
17
|
-
return new SolidityIntTypeWrapper(negative ? -result : result);
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Parse a human-readable value into a scaled sol_int.
|
|
21
|
-
* Mirrors viem/ethers parseUnits behavior.
|
|
22
|
-
*
|
|
23
|
-
* Accepts: bigint, number (int or float), string (decimal notation), SolidityIntTypeWrapper.
|
|
24
|
-
* Floats are converted to string first to avoid floating-point math.
|
|
25
|
-
*/
|
|
26
|
-
export const parseUnitsValue = (value, decimals) => {
|
|
27
|
-
const scale = 10n ** BigInt(decimals);
|
|
28
|
-
if (value instanceof SolidityIntTypeWrapper) {
|
|
29
|
-
return new SolidityIntTypeWrapper(value.value * scale);
|
|
30
|
-
}
|
|
31
|
-
if (typeof value === "bigint") {
|
|
32
|
-
return new SolidityIntTypeWrapper(value * scale);
|
|
33
|
-
}
|
|
34
|
-
if (typeof value === "number") {
|
|
35
|
-
if (Number.isInteger(value)) {
|
|
36
|
-
return new SolidityIntTypeWrapper(BigInt(value) * scale);
|
|
37
|
-
}
|
|
38
|
-
return parseUnitsValue(String(value), decimals);
|
|
39
|
-
}
|
|
40
|
-
if (typeof value === "string") {
|
|
41
|
-
return parseDecimalString(value, decimals, scale);
|
|
42
|
-
}
|
|
43
|
-
throw new TypeError(`parseUnits: unsupported value type: ${typeof value}`);
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Format a sol_int value into a human-readable double.
|
|
47
|
-
* Mirrors viem/ethers formatUnits behavior.
|
|
48
|
-
*/
|
|
49
|
-
export const formatUnitsValue = (value, decimals) => {
|
|
50
|
-
const raw = toBigInt(value);
|
|
51
|
-
const scale = 10n ** BigInt(decimals);
|
|
52
|
-
const whole = raw / scale;
|
|
53
|
-
const remainder = raw % scale;
|
|
54
|
-
return Number(whole) + Number(remainder) / Number(scale);
|
|
55
|
-
};
|
package/dist/units.spec.d.ts
DELETED
package/dist/units.spec.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"units.spec.d.ts","sourceRoot":"","sources":["../src/units.spec.ts"],"names":[],"mappings":""}
|
package/dist/units.spec.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { SolidityIntTypeWrapper } from "./custom-types/index.js";
|
|
3
|
-
import { parseUnitsValue, formatUnitsValue } from "./units.js";
|
|
4
|
-
describe("parseUnitsValue", () => {
|
|
5
|
-
it("scales a bigint by decimals", () => {
|
|
6
|
-
expect(parseUnitsValue(1000n, 6)).toEqual(new SolidityIntTypeWrapper(1000000000n));
|
|
7
|
-
});
|
|
8
|
-
it("scales an integer number by decimals", () => {
|
|
9
|
-
expect(parseUnitsValue(1000, 6)).toEqual(new SolidityIntTypeWrapper(1000000000n));
|
|
10
|
-
});
|
|
11
|
-
it("parses a decimal string", () => {
|
|
12
|
-
expect(parseUnitsValue("1.5", 18)).toEqual(new SolidityIntTypeWrapper(1500000000000000000n));
|
|
13
|
-
});
|
|
14
|
-
it("parses a whole-number string", () => {
|
|
15
|
-
expect(parseUnitsValue("1000", 6)).toEqual(new SolidityIntTypeWrapper(1000000000n));
|
|
16
|
-
});
|
|
17
|
-
it("parses a string with trailing zeros in fraction", () => {
|
|
18
|
-
expect(parseUnitsValue("1.50", 18)).toEqual(new SolidityIntTypeWrapper(1500000000000000000n));
|
|
19
|
-
});
|
|
20
|
-
it("handles zero values", () => {
|
|
21
|
-
expect(parseUnitsValue(0, 18)).toEqual(new SolidityIntTypeWrapper(0n));
|
|
22
|
-
expect(parseUnitsValue("0", 18)).toEqual(new SolidityIntTypeWrapper(0n));
|
|
23
|
-
expect(parseUnitsValue("0.0", 18)).toEqual(new SolidityIntTypeWrapper(0n));
|
|
24
|
-
});
|
|
25
|
-
it("handles double input by converting to string first", () => {
|
|
26
|
-
expect(parseUnitsValue(1.5, 18)).toEqual(new SolidityIntTypeWrapper(1500000000000000000n));
|
|
27
|
-
});
|
|
28
|
-
it("handles SolidityIntTypeWrapper input", () => {
|
|
29
|
-
const input = new SolidityIntTypeWrapper(1000n);
|
|
30
|
-
expect(parseUnitsValue(input, 6)).toEqual(new SolidityIntTypeWrapper(1000000000n));
|
|
31
|
-
});
|
|
32
|
-
it("throws when fractional part exceeds decimals", () => {
|
|
33
|
-
expect(() => parseUnitsValue("1.1234567", 6)).toThrow("exceeds");
|
|
34
|
-
});
|
|
35
|
-
it("handles zero decimals with integer", () => {
|
|
36
|
-
expect(parseUnitsValue(1000, 0)).toEqual(new SolidityIntTypeWrapper(1000n));
|
|
37
|
-
});
|
|
38
|
-
it("throws for zero decimals with fractional string", () => {
|
|
39
|
-
expect(() => parseUnitsValue("1.5", 0)).toThrow("exceeds");
|
|
40
|
-
});
|
|
41
|
-
it("throws for unsupported types", () => {
|
|
42
|
-
expect(() => parseUnitsValue(true, 6)).toThrow("unsupported");
|
|
43
|
-
});
|
|
44
|
-
it("throws for invalid decimal string", () => {
|
|
45
|
-
expect(() => parseUnitsValue("1.2.3", 6)).toThrow("invalid");
|
|
46
|
-
});
|
|
47
|
-
it("parses a negative decimal string", () => {
|
|
48
|
-
expect(parseUnitsValue("-1.5", 18)).toEqual(new SolidityIntTypeWrapper(-1500000000000000000n));
|
|
49
|
-
});
|
|
50
|
-
it("parses a negative whole-number string", () => {
|
|
51
|
-
expect(parseUnitsValue("-1000", 6)).toEqual(new SolidityIntTypeWrapper(-1000000000n));
|
|
52
|
-
});
|
|
53
|
-
it("parses a negative double input", () => {
|
|
54
|
-
expect(parseUnitsValue(-1.5, 18)).toEqual(new SolidityIntTypeWrapper(-1500000000000000000n));
|
|
55
|
-
});
|
|
56
|
-
it("handles negative bigint input", () => {
|
|
57
|
-
expect(parseUnitsValue(-1000n, 6)).toEqual(new SolidityIntTypeWrapper(-1000000000n));
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
describe("formatUnitsValue", () => {
|
|
61
|
-
it("formats a bigint with decimals", () => {
|
|
62
|
-
expect(formatUnitsValue(1000000000n, 6)).toBe(1000);
|
|
63
|
-
});
|
|
64
|
-
it("formats a fractional result", () => {
|
|
65
|
-
expect(formatUnitsValue(1500000000000000000n, 18)).toBe(1.5);
|
|
66
|
-
});
|
|
67
|
-
it("formats zero", () => {
|
|
68
|
-
expect(formatUnitsValue(0n, 18)).toBe(0);
|
|
69
|
-
});
|
|
70
|
-
it("formats a SolidityIntTypeWrapper", () => {
|
|
71
|
-
const input = new SolidityIntTypeWrapper(1000000n);
|
|
72
|
-
expect(formatUnitsValue(input, 6)).toBe(1);
|
|
73
|
-
});
|
|
74
|
-
it("handles zero decimals", () => {
|
|
75
|
-
expect(formatUnitsValue(1000n, 0)).toBe(1000);
|
|
76
|
-
});
|
|
77
|
-
});
|