@seljs/env 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 (2026-03-13)
4
+
5
+
6
+ ### Features
7
+
8
+ * first implementation of seljs ([7548fe0](https://github.com/abinnovision/seljs/commit/7548fe06cbb22ec6b74b20e38ef07d026b3f8def))
package/LICENSE.md ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 abi group GmbH
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # sel-env
2
+
3
+ Schema builder. Turns ABIs, context definitions, and Zod schemas into a SEL schema. Handles ABI parsing, struct field resolution, context extraction, and provides CEL built-in definitions.
@@ -0,0 +1,36 @@
1
+ import type { SELSchema } from "@seljs/schema";
2
+ import type { ContextDefinition } from "@seljs/types";
3
+ import type { AbiParameter } from "abitype";
4
+ import type { Abi, Address } from "viem";
5
+ export interface StructTypeDefinition {
6
+ typeName: string;
7
+ fields: Record<string, string>;
8
+ }
9
+ export interface BuildStructFieldsResult {
10
+ fields: Record<string, string>;
11
+ nestedTypes: StructTypeDefinition[];
12
+ }
13
+ /**
14
+ * Recursively builds struct field definitions.
15
+ *
16
+ * @param components Array of ABI parameters representing the components of a tuple type.
17
+ * @param parentTypeName Name to use for the struct type representing the current level of nesting.
18
+ */
19
+ export declare const buildStructFields: (components: readonly AbiParameter[], parentTypeName: string) => BuildStructFieldsResult;
20
+ export interface ContractInput {
21
+ address: Address;
22
+ abi: Abi;
23
+ description?: string;
24
+ }
25
+ export interface SchemaBuilderConfig {
26
+ contracts?: Record<string, ContractInput>;
27
+ context?: ContextDefinition;
28
+ }
29
+ /**
30
+ * Builds a SELSchema based on the provided configuration, which includes contract definitions and an optional context for variables.
31
+ *
32
+ * @param config Configuration object containing contract definitions and an optional context for variables.
33
+ * @returns A SELSchema object representing the schema for the SEL environment.
34
+ */
35
+ export declare const buildSchema: (config: SchemaBuilderConfig) => SELSchema;
36
+ //# sourceMappingURL=builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAGX,SAAS,EAGT,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAEX,iBAAiB,EAEjB,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,GAAG,EAAe,OAAO,EAAE,MAAM,MAAM,CAAC;AAEtD,MAAM,WAAW,oBAAoB;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC/B;AAED,MAAM,WAAW,uBAAuB;IACvC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,WAAW,EAAE,oBAAoB,EAAE,CAAC;CACpC;AAiDD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAC7B,YAAY,SAAS,YAAY,EAAE,EACnC,gBAAgB,MAAM,KACpB,uBAiCF,CAAC;AAoMF,MAAM,WAAW,aAAa;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE,GAAG,CAAC;IACT,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC1C,OAAO,CAAC,EAAE,iBAAiB,CAAC;CAC5B;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,QAAQ,mBAAmB,KAAG,SAezD,CAAC"}
@@ -0,0 +1,223 @@
1
+ import { structTypeName } from "@seljs/common";
2
+ import { mapSolidityTypeToCEL } from "@seljs/types";
3
+ import { CEL_BUILTIN_FUNCTIONS, CEL_BUILTIN_MACROS, SOLIDITY_PRIMITIVE_TYPES, } from "./builtins.js";
4
+ /**
5
+ * Checks if the given ABI parameter is a tuple type with components, indicating that it represents a struct in Solidity.
6
+ * @param param
7
+ */
8
+ const isTupleParam = (param) => param.type === "tuple" && "components" in param;
9
+ /**
10
+ * Checks if the given ABI parameter is an array of tuples with components, indicating that it represents an array of structs in Solidity.
11
+ * @param param
12
+ */
13
+ const isTupleArrayParam = (param) => param.type === "tuple[]" && "components" in param;
14
+ /**
15
+ * Checks if the given ABI parameter is either a tuple or an array of tuples, both of which indicate the presence of components and thus represent struct types in Solidity.
16
+ *
17
+ * @param param
18
+ */
19
+ const isAnyTupleParam = (param) => isTupleParam(param) || isTupleArrayParam(param);
20
+ /**
21
+ * Generates a unique type name for a nested struct based on the parent type name and the field name, used for naming struct types in the SEL schema.
22
+ *
23
+ * @param parentTypeName Name of the parent type (e.g., contract name or parent struct name) to which the field belongs, used as a prefix for the nested struct type name.
24
+ * @param fieldName Name of the field for which to generate the nested struct type name, used as a suffix in the nested struct type name.
25
+ */
26
+ const nestedTypeName = (parentTypeName, fieldName) => `${parentTypeName}__${fieldName}`;
27
+ /**
28
+ * Recursively builds struct field definitions.
29
+ *
30
+ * @param components Array of ABI parameters representing the components of a tuple type.
31
+ * @param parentTypeName Name to use for the struct type representing the current level of nesting.
32
+ */
33
+ export const buildStructFields = (components, parentTypeName) => {
34
+ const fields = {};
35
+ const nestedTypes = [];
36
+ for (const component of components) {
37
+ const fieldName = component.name ?? "arg0";
38
+ if (isTupleParam(component) && component.components.length > 0) {
39
+ const childTypeName = nestedTypeName(parentTypeName, fieldName);
40
+ const child = buildStructFields(component.components, childTypeName);
41
+ nestedTypes.push(...child.nestedTypes);
42
+ nestedTypes.push({ typeName: childTypeName, fields: child.fields });
43
+ fields[fieldName] = childTypeName;
44
+ continue;
45
+ }
46
+ if (isTupleArrayParam(component) && component.components.length > 0) {
47
+ const childTypeName = nestedTypeName(parentTypeName, fieldName);
48
+ const child = buildStructFields(component.components, childTypeName);
49
+ nestedTypes.push(...child.nestedTypes);
50
+ nestedTypes.push({ typeName: childTypeName, fields: child.fields });
51
+ fields[fieldName] = `list<${childTypeName}>`;
52
+ continue;
53
+ }
54
+ // For primitive types, directly map to CEL type or use "dyn" if mapping is not possible.
55
+ fields[fieldName] = mapSolidityTypeToCEL(component.type) ?? "dyn";
56
+ }
57
+ return { fields, nestedTypes };
58
+ };
59
+ /**
60
+ * Builds method parameters for a given array of ABI parameters.
61
+ *
62
+ * @param inputs Array of ABI parameters representing the inputs of a function.
63
+ */
64
+ const buildMethodParams = (inputs) => {
65
+ return inputs.map((param, index) => ({
66
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- empty string name means unnamed param
67
+ name: param.name || `arg${String(index)}`,
68
+ type: mapSolidityTypeToCEL(param.type) ?? "dyn",
69
+ }));
70
+ };
71
+ /**
72
+ * Builds the return type resolution for a method based on its ABI components, handling nested structs as needed.
73
+ *
74
+ * @param components Array of ABI parameters representing the components of a tuple return type.
75
+ * @param typeName Name to use for the struct type representing the return value, used for naming nested struct types as well.
76
+ */
77
+ const buildStructReturn = (components, typeName) => {
78
+ const result = buildStructFields(components, typeName);
79
+ const structTypes = [];
80
+ for (const nested of result.nestedTypes) {
81
+ structTypes.push({
82
+ name: nested.typeName,
83
+ kind: "struct",
84
+ fields: Object.entries(nested.fields).map(([name, type]) => ({
85
+ name,
86
+ type,
87
+ })),
88
+ });
89
+ }
90
+ structTypes.push({
91
+ name: typeName,
92
+ kind: "struct",
93
+ fields: Object.entries(result.fields).map(([name, type]) => ({
94
+ name,
95
+ type,
96
+ })),
97
+ });
98
+ return { returns: typeName, structTypes };
99
+ };
100
+ /**
101
+ * Resolves the return type of method based on its ABI function definition.
102
+ *
103
+ * @param fn ABI function for which to resolve the return type.
104
+ * @param contractName Name of the contract to which the function belongs, used for naming nested struct types.
105
+ */
106
+ const resolveMethodReturnType = (fn, contractName) => {
107
+ const firstOutput = fn.outputs[0];
108
+ if (!firstOutput) {
109
+ return { returns: "dyn", structTypes: [] };
110
+ }
111
+ /*
112
+ * This is the most basic case:
113
+ * Single non-tuple return value can be directly mapped to a CEL type without needing to define a struct.
114
+ */
115
+ if (fn.outputs.length === 1 && !isAnyTupleParam(firstOutput)) {
116
+ const celType = mapSolidityTypeToCEL(firstOutput.type) ?? "dyn";
117
+ return { returns: celType, structTypes: [] };
118
+ }
119
+ const typeName = structTypeName(contractName, fn.name);
120
+ // For more complex cases, we need to define struct types.
121
+ const isSingleOutput = fn.outputs.length === 1;
122
+ const isSingleTupleArray = isSingleOutput && isTupleArrayParam(firstOutput);
123
+ const isSingleTuple = isSingleOutput && isTupleParam(firstOutput);
124
+ let components;
125
+ if (isSingleTuple || isSingleTupleArray) {
126
+ components = firstOutput.components;
127
+ }
128
+ else {
129
+ components = fn.outputs.map((o, i) => ({
130
+ ...o,
131
+ name: o.name ?? `arg${String(i)}`,
132
+ }));
133
+ }
134
+ // Build the struct return type and collect any nested struct types needed for the return value.
135
+ const resolution = buildStructReturn(components, typeName);
136
+ if (isSingleTupleArray) {
137
+ return {
138
+ returns: `list<${typeName}>`,
139
+ structTypes: resolution.structTypes,
140
+ };
141
+ }
142
+ else {
143
+ return resolution;
144
+ }
145
+ };
146
+ /**
147
+ * Builds a MethodSchema for a given ABI function.
148
+ *
149
+ * @param fn ABI function for which to build the MethodSchema.
150
+ * @param contractName Name of the contract to which the function belongs, used for naming nested struct types.
151
+ * @param acc Accumulator to keep track of seen struct types and collect unique TypeSchemas for nested structs.
152
+ */
153
+ const buildMethodSchema = (fn, contractName, acc) => {
154
+ const params = buildMethodParams(fn.inputs);
155
+ // Handle the return type resolution.
156
+ const resolution = resolveMethodReturnType(fn, contractName);
157
+ // Add any struct types discovered during return type resolution to the accumulator, ensuring uniqueness.
158
+ for (const st of resolution.structTypes) {
159
+ if (!acc.seen.has(st.name)) {
160
+ acc.types.push(st);
161
+ acc.seen.add(st.name);
162
+ }
163
+ }
164
+ return { name: fn.name, params, returns: resolution.returns, abi: fn };
165
+ };
166
+ /**
167
+ * Builds a ContractSchema for a given contract by processing its ABI and extracting view/pure functions as methods.
168
+ *
169
+ * @param contractName Name of the contract for which to build the schema.
170
+ * @param input ContractInput containing the ABI and other relevant information for the contract.
171
+ * @param acc Accumulator to keep track of seen struct types and collect unique TypeSchemas for nested structs.
172
+ */
173
+ const buildContractSchema = (contractName, input, acc) => {
174
+ // Filter ABI to include only view and pure functions, which are relevant for read-only interactions in the SEL environment.
175
+ const viewFunctions = input.abi.filter((item) => item.type === "function" &&
176
+ (item.stateMutability === "view" || item.stateMutability === "pure"));
177
+ const methods = viewFunctions.map((fn) => buildMethodSchema(fn, contractName, acc));
178
+ return {
179
+ name: contractName,
180
+ address: input.address,
181
+ description: input.description,
182
+ methods,
183
+ };
184
+ };
185
+ /**
186
+ * Resolves a ContextFieldDefinition to its CEL type string.
187
+ */
188
+ const resolveFieldType = (field) => typeof field === "string" ? field : field.type;
189
+ /**
190
+ * Resolves the optional description from a ContextFieldDefinition.
191
+ */
192
+ const resolveFieldDescription = (field) => typeof field === "string" ? undefined : field.description;
193
+ /**
194
+ * Extracts variables from the provided context definition and builds an array of VariableSchema objects.
195
+ */
196
+ const buildVariablesFromContext = (config) => {
197
+ if (!config.context) {
198
+ return [];
199
+ }
200
+ return Object.entries(config.context).map(([name, field]) => ({
201
+ name,
202
+ type: resolveFieldType(field),
203
+ description: resolveFieldDescription(field),
204
+ }));
205
+ };
206
+ /**
207
+ * Builds a SELSchema based on the provided configuration, which includes contract definitions and an optional context for variables.
208
+ *
209
+ * @param config Configuration object containing contract definitions and an optional context for variables.
210
+ * @returns A SELSchema object representing the schema for the SEL environment.
211
+ */
212
+ export const buildSchema = (config) => {
213
+ const acc = { seen: new Set(), types: [] };
214
+ const contracts = Object.entries(config.contracts ?? {}).map(([name, input]) => buildContractSchema(name, input, acc));
215
+ return {
216
+ version: "1.0.0",
217
+ contracts,
218
+ variables: buildVariablesFromContext(config),
219
+ types: [...SOLIDITY_PRIMITIVE_TYPES, ...acc.types],
220
+ functions: CEL_BUILTIN_FUNCTIONS,
221
+ macros: CEL_BUILTIN_MACROS,
222
+ };
223
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=builder.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder.spec.d.ts","sourceRoot":"","sources":["../src/builder.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,428 @@
1
+ import { parseAbi } from "viem";
2
+ import { describe, expect, it } from "vitest";
3
+ import { buildSchema } from "./builder.js";
4
+ const SIMPLE_ABI = [
5
+ {
6
+ type: "function",
7
+ name: "balanceOf",
8
+ stateMutability: "view",
9
+ inputs: [{ name: "account", type: "address" }],
10
+ outputs: [{ name: "", type: "uint256" }],
11
+ },
12
+ {
13
+ type: "function",
14
+ name: "totalSupply",
15
+ stateMutability: "view",
16
+ inputs: [],
17
+ outputs: [{ name: "", type: "uint256" }],
18
+ },
19
+ {
20
+ type: "function",
21
+ name: "name",
22
+ stateMutability: "view",
23
+ inputs: [],
24
+ outputs: [{ name: "", type: "string" }],
25
+ },
26
+ ];
27
+ /**
28
+ * Comprehensive ABI covering all struct return scenarios:
29
+ * - tuple return (getPool)
30
+ * - multi-return (getReserves)
31
+ * - nested tuple (getInfo)
32
+ * - tuple[] return (getPositions)
33
+ * - tuple with tuple[] field (getAccount)
34
+ */
35
+ const STRUCT_ABI = [
36
+ {
37
+ type: "function",
38
+ name: "getPool",
39
+ stateMutability: "view",
40
+ inputs: [{ name: "id", type: "uint256" }],
41
+ outputs: [
42
+ {
43
+ name: "",
44
+ type: "tuple",
45
+ components: [
46
+ { name: "token", type: "address" },
47
+ { name: "balance", type: "uint256" },
48
+ ],
49
+ },
50
+ ],
51
+ },
52
+ {
53
+ type: "function",
54
+ name: "getReserves",
55
+ stateMutability: "view",
56
+ inputs: [],
57
+ outputs: [
58
+ { name: "reserve0", type: "uint112" },
59
+ { name: "reserve1", type: "uint112" },
60
+ { name: "blockTimestampLast", type: "uint32" },
61
+ ],
62
+ },
63
+ {
64
+ type: "function",
65
+ name: "getInfo",
66
+ stateMutability: "view",
67
+ inputs: [],
68
+ outputs: [
69
+ {
70
+ name: "",
71
+ type: "tuple",
72
+ components: [
73
+ { name: "id", type: "uint256" },
74
+ {
75
+ name: "details",
76
+ type: "tuple",
77
+ components: [
78
+ { name: "token", type: "address" },
79
+ { name: "amount", type: "uint256" },
80
+ ],
81
+ },
82
+ ],
83
+ },
84
+ ],
85
+ },
86
+ {
87
+ type: "function",
88
+ name: "getPositions",
89
+ stateMutability: "view",
90
+ inputs: [{ name: "user", type: "address" }],
91
+ outputs: [
92
+ {
93
+ name: "",
94
+ type: "tuple[]",
95
+ components: [
96
+ { name: "token", type: "address" },
97
+ { name: "amount", type: "uint256" },
98
+ ],
99
+ },
100
+ ],
101
+ },
102
+ {
103
+ type: "function",
104
+ name: "getAccount",
105
+ stateMutability: "view",
106
+ inputs: [],
107
+ outputs: [
108
+ {
109
+ name: "",
110
+ type: "tuple",
111
+ components: [
112
+ { name: "owner", type: "address" },
113
+ {
114
+ name: "positions",
115
+ type: "tuple[]",
116
+ components: [
117
+ { name: "token", type: "address" },
118
+ { name: "amount", type: "uint256" },
119
+ ],
120
+ },
121
+ ],
122
+ },
123
+ ],
124
+ },
125
+ ];
126
+ describe("src/builder.ts", () => {
127
+ describe("contracts", () => {
128
+ it("builds a schema with the correct contract name and method count", () => {
129
+ const schema = buildSchema({
130
+ contracts: {
131
+ erc20: {
132
+ address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
133
+ abi: SIMPLE_ABI,
134
+ },
135
+ },
136
+ });
137
+ expect(schema.version).toBe("1.0.0");
138
+ expect(schema.contracts).toHaveLength(1);
139
+ expect(schema.contracts[0]?.name).toBe("erc20");
140
+ expect(schema.contracts[0]?.methods).toHaveLength(3);
141
+ });
142
+ it("includes contract description", () => {
143
+ const schema = buildSchema({
144
+ contracts: {
145
+ erc20: {
146
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
147
+ abi: SIMPLE_ABI,
148
+ description: "ERC-20 Token",
149
+ },
150
+ },
151
+ });
152
+ expect(schema.contracts[0]?.description).toBe("ERC-20 Token");
153
+ });
154
+ it("handles empty config", () => {
155
+ const schema = buildSchema({});
156
+ expect(schema.contracts).toEqual([]);
157
+ expect(schema.variables).toEqual([]);
158
+ expect(schema.version).toBe("1.0.0");
159
+ });
160
+ it("includes builtin types, functions, and macros", () => {
161
+ const schema = buildSchema({});
162
+ expect(schema.types.length).toBeGreaterThan(0);
163
+ expect(schema.functions.length).toBeGreaterThan(0);
164
+ expect(schema.macros.length).toBeGreaterThan(0);
165
+ });
166
+ it("only includes view and pure functions (excludes nonpayable, payable, events)", () => {
167
+ const schema = buildSchema({
168
+ contracts: {
169
+ c: {
170
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
171
+ abi: [
172
+ {
173
+ type: "function",
174
+ name: "viewFn",
175
+ stateMutability: "view",
176
+ inputs: [],
177
+ outputs: [{ name: "", type: "uint256" }],
178
+ },
179
+ {
180
+ type: "function",
181
+ name: "pureFn",
182
+ stateMutability: "pure",
183
+ inputs: [],
184
+ outputs: [{ name: "", type: "bool" }],
185
+ },
186
+ {
187
+ type: "function",
188
+ name: "nonpayableFn",
189
+ stateMutability: "nonpayable",
190
+ inputs: [],
191
+ outputs: [],
192
+ },
193
+ {
194
+ type: "function",
195
+ name: "payableFn",
196
+ stateMutability: "payable",
197
+ inputs: [],
198
+ outputs: [],
199
+ },
200
+ {
201
+ type: "event",
202
+ name: "Transfer",
203
+ inputs: [],
204
+ },
205
+ ],
206
+ },
207
+ },
208
+ });
209
+ const methodNames = schema.contracts[0]?.methods.map((m) => m.name);
210
+ expect(methodNames).toContain("viewFn");
211
+ expect(methodNames).toContain("pureFn");
212
+ expect(methodNames).not.toContain("nonpayableFn");
213
+ expect(methodNames).not.toContain("payableFn");
214
+ expect(methodNames).not.toContain("Transfer");
215
+ });
216
+ it("maps unnamed params to arg{index}", () => {
217
+ const schema = buildSchema({
218
+ contracts: {
219
+ c: {
220
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
221
+ abi: [
222
+ {
223
+ type: "function",
224
+ name: "foo",
225
+ stateMutability: "view",
226
+ inputs: [
227
+ { name: "", type: "address" },
228
+ { name: "", type: "uint256" },
229
+ ],
230
+ outputs: [{ name: "", type: "bool" }],
231
+ },
232
+ ],
233
+ },
234
+ },
235
+ });
236
+ const params = schema.contracts[0]?.methods[0]?.params;
237
+ expect(params?.[0]?.name).toBe("arg0");
238
+ expect(params?.[1]?.name).toBe("arg1");
239
+ });
240
+ });
241
+ describe("method param and return types (CEL types)", () => {
242
+ const schema = buildSchema({
243
+ contracts: {
244
+ token: {
245
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
246
+ abi: SIMPLE_ABI,
247
+ },
248
+ },
249
+ });
250
+ it("maps address param to sol_address", () => {
251
+ const method = schema.contracts[0]?.methods.find((m) => m.name === "balanceOf");
252
+ expect(method?.params[0]?.name).toBe("account");
253
+ expect(method?.params[0]?.type).toBe("sol_address");
254
+ });
255
+ it.each([
256
+ ["balanceOf", "sol_int"],
257
+ ["totalSupply", "sol_int"],
258
+ ["name", "string"],
259
+ ])("%s returns %s", (methodName, expectedReturn) => {
260
+ const method = schema.contracts[0]?.methods.find((m) => m.name === methodName);
261
+ expect(method?.returns).toBe(expectedReturn);
262
+ });
263
+ });
264
+ describe("context variables", () => {
265
+ it("builds a schema with context variables", () => {
266
+ const schema = buildSchema({
267
+ context: {
268
+ user: "sol_address",
269
+ amount: "sol_int",
270
+ },
271
+ });
272
+ expect(schema.variables).toHaveLength(2);
273
+ expect(schema.variables).toEqual(expect.arrayContaining([
274
+ expect.objectContaining({ name: "user", type: "sol_address" }),
275
+ expect.objectContaining({ name: "amount", type: "sol_int" }),
276
+ ]));
277
+ });
278
+ it("builds a schema with both contracts and context", () => {
279
+ const schema = buildSchema({
280
+ contracts: {
281
+ erc20: {
282
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
283
+ abi: SIMPLE_ABI,
284
+ },
285
+ },
286
+ context: {
287
+ user: "sol_address",
288
+ },
289
+ });
290
+ expect(schema.contracts).toHaveLength(1);
291
+ expect(schema.variables).toHaveLength(1);
292
+ });
293
+ });
294
+ describe("struct returns", () => {
295
+ const schema = buildSchema({
296
+ contracts: {
297
+ vault: {
298
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
299
+ abi: STRUCT_ABI,
300
+ },
301
+ },
302
+ });
303
+ it("generates TypeSchema for struct-returning method", () => {
304
+ const structType = schema.types.find((t) => t.name === "SEL_Struct_vault_getPool");
305
+ expect(structType).toBeDefined();
306
+ expect(structType?.kind).toBe("struct");
307
+ });
308
+ it("sets method returns to struct type name", () => {
309
+ const method = schema.contracts[0]?.methods.find((m) => m.name === "getPool");
310
+ expect(method?.returns).toBe("SEL_Struct_vault_getPool");
311
+ });
312
+ it("struct fields use CEL types (sol_address, sol_int)", () => {
313
+ const structType = schema.types.find((t) => t.name === "SEL_Struct_vault_getPool");
314
+ expect(structType?.fields).toEqual(expect.arrayContaining([
315
+ { name: "token", type: "sol_address" },
316
+ { name: "balance", type: "sol_int" },
317
+ ]));
318
+ });
319
+ it("generates TypeSchema for multi-return method", () => {
320
+ const structType = schema.types.find((t) => t.name === "SEL_Struct_vault_getReserves");
321
+ expect(structType).toBeDefined();
322
+ expect(structType?.kind).toBe("struct");
323
+ expect(structType?.fields).toHaveLength(3);
324
+ expect(structType?.fields).toEqual(expect.arrayContaining([
325
+ { name: "reserve0", type: "sol_int" },
326
+ { name: "reserve1", type: "sol_int" },
327
+ { name: "blockTimestampLast", type: "sol_int" },
328
+ ]));
329
+ });
330
+ it("does not generate struct types for simple returns", () => {
331
+ const simpleSchema = buildSchema({
332
+ contracts: {
333
+ erc20: {
334
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
335
+ abi: SIMPLE_ABI,
336
+ },
337
+ },
338
+ });
339
+ const structTypes = simpleSchema.types.filter((t) => t.kind === "struct");
340
+ expect(structTypes).toHaveLength(0);
341
+ });
342
+ it("emits struct type name for struct method and CEL type for simple method", () => {
343
+ const mixedSchema = buildSchema({
344
+ contracts: {
345
+ token: {
346
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
347
+ abi: SIMPLE_ABI,
348
+ },
349
+ pool: {
350
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
351
+ abi: STRUCT_ABI,
352
+ },
353
+ },
354
+ });
355
+ const balanceOf = mixedSchema.contracts
356
+ .find((c) => c.name === "token")
357
+ ?.methods.find((m) => m.name === "balanceOf");
358
+ expect(balanceOf?.returns).toBe("sol_int");
359
+ const getPool = mixedSchema.contracts
360
+ .find((c) => c.name === "pool")
361
+ ?.methods.find((m) => m.name === "getPool");
362
+ expect(getPool?.returns).toBe("SEL_Struct_pool_getPool");
363
+ });
364
+ it("generates nested struct TypeSchema entries", () => {
365
+ const parentType = schema.types.find((t) => t.name === "SEL_Struct_vault_getInfo");
366
+ expect(parentType).toBeDefined();
367
+ expect(parentType?.kind).toBe("struct");
368
+ const nestedType = schema.types.find((t) => t.name === "SEL_Struct_vault_getInfo__details");
369
+ expect(nestedType).toBeDefined();
370
+ expect(nestedType?.kind).toBe("struct");
371
+ expect(nestedType?.fields).toEqual(expect.arrayContaining([
372
+ { name: "token", type: "sol_address" },
373
+ { name: "amount", type: "sol_int" },
374
+ ]));
375
+ const detailsField = parentType?.fields?.find((f) => f.name === "details");
376
+ expect(detailsField?.type).toBe("SEL_Struct_vault_getInfo__details");
377
+ });
378
+ it("generates list<struct> return for tuple[] method", () => {
379
+ const method = schema.contracts[0]?.methods.find((m) => m.name === "getPositions");
380
+ expect(method?.returns).toBe("list<SEL_Struct_vault_getPositions>");
381
+ });
382
+ it("registers struct type for tuple[] return", () => {
383
+ const structType = schema.types.find((t) => t.name === "SEL_Struct_vault_getPositions");
384
+ expect(structType).toBeDefined();
385
+ expect(structType?.kind).toBe("struct");
386
+ expect(structType?.fields).toEqual(expect.arrayContaining([
387
+ { name: "token", type: "sol_address" },
388
+ { name: "amount", type: "sol_int" },
389
+ ]));
390
+ });
391
+ it("resolves tuple[] field inside struct to list<nested struct>", () => {
392
+ const parentType = schema.types.find((t) => t.name === "SEL_Struct_vault_getAccount");
393
+ const positionsField = parentType?.fields?.find((f) => f.name === "positions");
394
+ expect(positionsField?.type).toBe("list<SEL_Struct_vault_getAccount__positions>");
395
+ });
396
+ it("registers nested struct type for tuple[] field", () => {
397
+ const nestedType = schema.types.find((t) => t.name === "SEL_Struct_vault_getAccount__positions");
398
+ expect(nestedType).toBeDefined();
399
+ expect(nestedType?.kind).toBe("struct");
400
+ expect(nestedType?.fields).toEqual(expect.arrayContaining([
401
+ { name: "token", type: "sol_address" },
402
+ { name: "amount", type: "sol_int" },
403
+ ]));
404
+ });
405
+ it("nested struct types appear before parent in types array", () => {
406
+ const typeNames = schema.types.map((t) => t.name);
407
+ const nestedIdx = typeNames.indexOf("SEL_Struct_vault_getInfo__details");
408
+ const parentIdx = typeNames.indexOf("SEL_Struct_vault_getInfo");
409
+ expect(nestedIdx).toBeGreaterThan(-1);
410
+ expect(parentIdx).toBeGreaterThan(-1);
411
+ expect(nestedIdx).toBeLessThan(parentIdx);
412
+ });
413
+ it("includes AbiFunction on method schema", () => {
414
+ const abi = parseAbi([
415
+ "function balanceOf(address owner) view returns (uint256)",
416
+ ]);
417
+ const schema = buildSchema({
418
+ contracts: {
419
+ token: { address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", abi },
420
+ },
421
+ });
422
+ const method = schema.contracts[0].methods[0];
423
+ expect(method.abi).toBeDefined();
424
+ expect(method.abi.name).toBe("balanceOf");
425
+ expect(method.abi.type).toBe("function");
426
+ });
427
+ });
428
+ });
@@ -0,0 +1,17 @@
1
+ import type { FunctionSchema, MacroSchema, TypeSchema } from "@seljs/schema";
2
+ /**
3
+ * Built-in CEL functions available in SEL expressions.
4
+ * These functions are part of the CEL standard library.
5
+ */
6
+ export declare const CEL_BUILTIN_FUNCTIONS: FunctionSchema[];
7
+ /**
8
+ * Built-in CEL macros available in SEL expressions.
9
+ * Macros are syntactic sugar that expand to function calls.
10
+ */
11
+ export declare const CEL_BUILTIN_MACROS: MacroSchema[];
12
+ /**
13
+ * Primitive types from Solidity available in SEL.
14
+ * These are the basic type building blocks for the schema.
15
+ */
16
+ export declare const SOLIDITY_PRIMITIVE_TYPES: TypeSchema[];
17
+ //# sourceMappingURL=builtins.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builtins.d.ts","sourceRoot":"","sources":["../src/builtins.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE7E;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,cAAc,EAsNjD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,EAAE,WAAW,EAiD3C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,EAAE,UAAU,EAkChD,CAAC"}
@@ -0,0 +1,307 @@
1
+ import { SOLIDITY_TYPES } from "@seljs/types";
2
+ /**
3
+ * Built-in CEL functions available in SEL expressions.
4
+ * These functions are part of the CEL standard library.
5
+ */
6
+ export const CEL_BUILTIN_FUNCTIONS = [
7
+ {
8
+ name: "size",
9
+ signature: "size(list|map|string|bytes): int",
10
+ description: "Returns the size of a collection or string",
11
+ params: [{ name: "value", type: "list|map|string|bytes" }],
12
+ returns: "int",
13
+ },
14
+ {
15
+ name: "size",
16
+ signature: "string.size(): int",
17
+ description: "Returns the length of the string",
18
+ params: [],
19
+ returns: "int",
20
+ receiverType: "string",
21
+ },
22
+ {
23
+ name: "size",
24
+ signature: "list.size(): int",
25
+ description: "Returns the number of elements in the list",
26
+ params: [],
27
+ returns: "int",
28
+ receiverType: "list",
29
+ },
30
+ {
31
+ name: "size",
32
+ signature: "map.size(): int",
33
+ description: "Returns the number of entries in the map",
34
+ params: [],
35
+ returns: "int",
36
+ receiverType: "map",
37
+ },
38
+ {
39
+ name: "size",
40
+ signature: "bytes.size(): int",
41
+ description: "Returns the number of bytes",
42
+ params: [],
43
+ returns: "int",
44
+ receiverType: "bytes",
45
+ },
46
+ {
47
+ name: "type",
48
+ signature: "type(value): type",
49
+ description: "Returns the type of a value",
50
+ params: [{ name: "value", type: "any" }],
51
+ returns: "type",
52
+ },
53
+ {
54
+ name: "int",
55
+ signature: "int(value): int",
56
+ description: "Converts a value to an integer",
57
+ params: [{ name: "value", type: "any" }],
58
+ returns: "int",
59
+ },
60
+ {
61
+ name: "uint",
62
+ signature: "uint(value): uint",
63
+ description: "Converts a value to an unsigned integer",
64
+ params: [{ name: "value", type: "any" }],
65
+ returns: "uint",
66
+ },
67
+ {
68
+ name: "double",
69
+ signature: "double(value): double",
70
+ description: "Converts a value to a double",
71
+ params: [{ name: "value", type: "any" }],
72
+ returns: "double",
73
+ },
74
+ {
75
+ name: "string",
76
+ signature: "string(value): string",
77
+ description: "Converts a value to a string",
78
+ params: [{ name: "value", type: "any" }],
79
+ returns: "string",
80
+ },
81
+ {
82
+ name: "bytes",
83
+ signature: "bytes(value): bytes",
84
+ description: "Converts a value to bytes",
85
+ params: [{ name: "value", type: "any" }],
86
+ returns: "bytes",
87
+ },
88
+ {
89
+ name: "dyn",
90
+ signature: "dyn(value): dyn",
91
+ description: "Converts a value to a dynamic type",
92
+ params: [{ name: "value", type: "any" }],
93
+ returns: "dyn",
94
+ },
95
+ {
96
+ name: "bool",
97
+ signature: "bool(value): bool",
98
+ description: "Converts a value to a boolean",
99
+ params: [{ name: "value", type: "string" }],
100
+ returns: "bool",
101
+ },
102
+ {
103
+ name: "solInt",
104
+ signature: "solInt(string): sol_int",
105
+ description: "Converts a string or int to a sol_int (arbitrary-precision integer)",
106
+ params: [{ name: "value", type: "string|int" }],
107
+ returns: "sol_int",
108
+ },
109
+ {
110
+ name: "solAddress",
111
+ signature: "solAddress(string): sol_address",
112
+ description: "Converts a hex string to a sol_address type",
113
+ params: [{ name: "value", type: "string" }],
114
+ returns: "sol_address",
115
+ },
116
+ {
117
+ name: "parseUnits",
118
+ signature: "parseUnits(string|int|double|sol_int, int): sol_int",
119
+ description: "Scales a human-readable value by 10^decimals, producing a sol_int. Mirrors viem/ethers parseUnits.",
120
+ params: [
121
+ { name: "value", type: "string|int|double|sol_int" },
122
+ { name: "decimals", type: "int" },
123
+ ],
124
+ returns: "sol_int",
125
+ },
126
+ {
127
+ name: "formatUnits",
128
+ signature: "formatUnits(sol_int|int, int): double",
129
+ description: "Scales a sol_int down by 10^decimals, producing a double for readable comparisons.",
130
+ params: [
131
+ { name: "value", type: "sol_int|int" },
132
+ { name: "decimals", type: "int" },
133
+ ],
134
+ returns: "double",
135
+ },
136
+ {
137
+ name: "min",
138
+ signature: "min(sol_int|int, sol_int|int): sol_int",
139
+ description: "Returns the smaller of two integer values",
140
+ params: [
141
+ { name: "a", type: "sol_int|int" },
142
+ { name: "b", type: "sol_int|int" },
143
+ ],
144
+ returns: "sol_int",
145
+ },
146
+ {
147
+ name: "max",
148
+ signature: "max(sol_int|int, sol_int|int): sol_int",
149
+ description: "Returns the larger of two integer values",
150
+ params: [
151
+ { name: "a", type: "sol_int|int" },
152
+ { name: "b", type: "sol_int|int" },
153
+ ],
154
+ returns: "sol_int",
155
+ },
156
+ {
157
+ name: "abs",
158
+ signature: "abs(sol_int|int): sol_int",
159
+ description: "Returns the absolute value of an integer",
160
+ params: [{ name: "value", type: "sol_int|int" }],
161
+ returns: "sol_int",
162
+ },
163
+ {
164
+ name: "isZeroAddress",
165
+ signature: "isZeroAddress(sol_address|string): bool",
166
+ description: "Returns true if the address is the zero address (0x0000...0000)",
167
+ params: [{ name: "address", type: "sol_address|string" }],
168
+ returns: "bool",
169
+ },
170
+ {
171
+ name: "timestamp",
172
+ signature: "timestamp(string): timestamp",
173
+ description: "Parses a timestamp string",
174
+ params: [{ name: "value", type: "string" }],
175
+ returns: "timestamp",
176
+ },
177
+ {
178
+ name: "duration",
179
+ signature: "duration(string): duration",
180
+ description: "Parses a duration string",
181
+ params: [{ name: "value", type: "string" }],
182
+ returns: "duration",
183
+ },
184
+ {
185
+ name: "matches",
186
+ signature: "string.matches(pattern): bool",
187
+ description: "Tests if a string matches a regex pattern",
188
+ params: [{ name: "pattern", type: "string" }],
189
+ returns: "bool",
190
+ receiverType: "string",
191
+ },
192
+ {
193
+ name: "contains",
194
+ signature: "string.contains(substring): bool",
195
+ description: "Tests if a string contains a substring",
196
+ params: [{ name: "substring", type: "string" }],
197
+ returns: "bool",
198
+ receiverType: "string",
199
+ },
200
+ {
201
+ name: "startsWith",
202
+ signature: "string.startsWith(prefix): bool",
203
+ description: "Tests if a string starts with a prefix",
204
+ params: [{ name: "prefix", type: "string" }],
205
+ returns: "bool",
206
+ receiverType: "string",
207
+ },
208
+ {
209
+ name: "endsWith",
210
+ signature: "string.endsWith(suffix): bool",
211
+ description: "Tests if a string ends with a suffix",
212
+ params: [{ name: "suffix", type: "string" }],
213
+ returns: "bool",
214
+ receiverType: "string",
215
+ },
216
+ ];
217
+ /**
218
+ * Built-in CEL macros available in SEL expressions.
219
+ * Macros are syntactic sugar that expand to function calls.
220
+ */
221
+ export const CEL_BUILTIN_MACROS = [
222
+ {
223
+ name: "all",
224
+ pattern: "list.all(identifier, predicate)",
225
+ description: "Returns true if all elements satisfy the predicate",
226
+ example: "tokens.all(t, t.balance > 0)",
227
+ },
228
+ {
229
+ name: "exists",
230
+ pattern: "list.exists(identifier, predicate)",
231
+ description: "Returns true if any element satisfies the predicate",
232
+ example: "users.exists(u, u.isAdmin)",
233
+ },
234
+ {
235
+ name: "exists_one",
236
+ pattern: "list.exists_one(identifier, predicate)",
237
+ description: "Returns true if exactly one element satisfies the predicate",
238
+ example: "tokens.exists_one(t, t.id == 123)",
239
+ },
240
+ {
241
+ name: "map",
242
+ pattern: "list.map(identifier, transform)",
243
+ description: "Transforms each element in the list",
244
+ example: "tokens.map(t, t.balance)",
245
+ },
246
+ {
247
+ name: "map",
248
+ pattern: "list.map(identifier, predicate, transform)",
249
+ description: "Transforms elements that satisfy the predicate",
250
+ example: "tokens.map(t, t.balance > 0, t.balance)",
251
+ },
252
+ {
253
+ name: "filter",
254
+ pattern: "list.filter(identifier, predicate)",
255
+ description: "Filters elements by predicate",
256
+ example: "users.filter(u, u.age >= 18)",
257
+ },
258
+ {
259
+ name: "has",
260
+ pattern: "has(e.f)",
261
+ description: "Tests if a field exists on an object",
262
+ example: "has(user.email)",
263
+ },
264
+ {
265
+ name: "cel.bind",
266
+ pattern: "cel.bind(var, value, expr)",
267
+ description: "Binds a variable for use in an expression",
268
+ example: "cel.bind(x, user.balance, x > 1000)",
269
+ },
270
+ ];
271
+ /**
272
+ * Primitive types from Solidity available in SEL.
273
+ * These are the basic type building blocks for the schema.
274
+ */
275
+ export const SOLIDITY_PRIMITIVE_TYPES = [
276
+ // Solidity types from the registry (address, bytes)
277
+ ...SOLIDITY_TYPES.map((it) => {
278
+ /*
279
+ * The registry contains both built-in and custom types.
280
+ * We only want to include the custom types here, since the built-in types are already included as CEL primitives below.
281
+ */
282
+ if (it.type !== "custom") {
283
+ return null;
284
+ }
285
+ return {
286
+ name: it.name,
287
+ kind: "primitive",
288
+ description: it.schema.description,
289
+ };
290
+ }).filter((it) => it !== null),
291
+ // CEL builtins presented as Solidity primitives for editor tooling
292
+ {
293
+ name: "bool",
294
+ kind: "primitive",
295
+ description: "Boolean value (true or false)",
296
+ },
297
+ {
298
+ name: "string",
299
+ kind: "primitive",
300
+ description: "UTF-8 string",
301
+ },
302
+ {
303
+ name: "bytes32",
304
+ kind: "primitive",
305
+ description: "32-byte fixed array",
306
+ },
307
+ ];
@@ -0,0 +1,3 @@
1
+ export * from "./builder.js";
2
+ export * from "./builtins.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./builder.js";
2
+ export * from "./builtins.js";
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@seljs/env",
3
+ "version": "1.0.0",
4
+ "license": "Apache-2.0",
5
+ "author": {
6
+ "name": "abi group GmbH",
7
+ "email": "info@abigroup.io",
8
+ "url": "https://abigroup.io/"
9
+ },
10
+ "repository": {
11
+ "url": "https://github.com/abinnovision/seljs"
12
+ },
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "types": "./dist/index.d.ts"
18
+ }
19
+ },
20
+ "main": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "files": [
23
+ "dist",
24
+ "LICENSE.md"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsc -p tsconfig.build.json",
28
+ "format:check": "prettier --check '{{src,test}/**/*,*}.{{t,j}s{,x},json{,5},md,y{,a}ml}'",
29
+ "format:fix": "prettier --write '{{src,test}/**/*,*}.{{t,j}s{,x},json{,5},md,y{,a}ml}'",
30
+ "lint:check": "eslint '{{src,test}/**/*,*}.{t,j}s{,x}'",
31
+ "lint:fix": "eslint '{{src,test}/**/*,*}.{t,j}s{,x}' --fix",
32
+ "test-unit": "vitest run",
33
+ "test-unit:watch": "vitest watch",
34
+ "typecheck": "tsc --noEmit"
35
+ },
36
+ "lint-staged": {
37
+ "{{src,test}/**/*,*}.{{t,j}s{,x},json{,5},md,y{,a}ml}": [
38
+ "prettier --write"
39
+ ],
40
+ "{{src,test}/**/*,*}.{t,j}s{,x}": [
41
+ "eslint --fix"
42
+ ]
43
+ },
44
+ "dependencies": {
45
+ "@seljs/common": "1.0.0",
46
+ "@seljs/schema": "1.0.0",
47
+ "@seljs/types": "1.0.0",
48
+ "abitype": "^1.2.3",
49
+ "viem": "^2.47.2"
50
+ },
51
+ "devDependencies": {
52
+ "@abinnovision/eslint-config-base": "^3.2.0",
53
+ "@abinnovision/prettier-config": "^2.1.5",
54
+ "@seljs-internal/tsconfig": "^0.0.0",
55
+ "eslint": "^9.39.4",
56
+ "prettier": "^3.8.1",
57
+ "typescript": "^5.9.3",
58
+ "vitest": "^4.0.18"
59
+ },
60
+ "publishConfig": {
61
+ "npm": true,
62
+ "npmAccess": "public"
63
+ }
64
+ }