@tari-project/tari-extension-query-builder 0.0.4
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/LICENSE +29 -0
- package/README.md +1 -0
- package/components.json +21 -0
- package/dist/App.d.ts +3 -0
- package/dist/App.d.ts.map +1 -0
- package/dist/codegen/BuilderCodegen.d.ts +20 -0
- package/dist/codegen/BuilderCodegen.d.ts.map +1 -0
- package/dist/codegen/sample.d.ts +3 -0
- package/dist/codegen/sample.d.ts.map +1 -0
- package/dist/components/query-builder/edges/button-edge.d.ts +3 -0
- package/dist/components/query-builder/edges/button-edge.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input-checkbox.d.ts +11 -0
- package/dist/components/query-builder/input/call-input-checkbox.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input-select.d.ts +14 -0
- package/dist/components/query-builder/input/call-input-select.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input-text.d.ts +18 -0
- package/dist/components/query-builder/input/call-input-text.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input.d.ts +13 -0
- package/dist/components/query-builder/input/call-input.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/call-node.types.d.ts +4 -0
- package/dist/components/query-builder/nodes/call-node.types.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/constants.d.ts +5 -0
- package/dist/components/query-builder/nodes/constants.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/enter-connection.d.ts +3 -0
- package/dist/components/query-builder/nodes/enter-connection.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/exit-connection.d.ts +3 -0
- package/dist/components/query-builder/nodes/exit-connection.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/generic/generic-node.d.ts +5 -0
- package/dist/components/query-builder/nodes/generic/generic-node.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/generic/node-icon.d.ts +8 -0
- package/dist/components/query-builder/nodes/generic/node-icon.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/generic-node.types.d.ts +6 -0
- package/dist/components/query-builder/nodes/generic-node.types.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/input/constants.d.ts +2 -0
- package/dist/components/query-builder/nodes/input/constants.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/input/editable-label.d.ts +9 -0
- package/dist/components/query-builder/nodes/input/editable-label.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/input/input-params-node.d.ts +5 -0
- package/dist/components/query-builder/nodes/input/input-params-node.d.ts.map +1 -0
- package/dist/components/query-builder/query-builder.d.ts +13 -0
- package/dist/components/query-builder/query-builder.d.ts.map +1 -0
- package/dist/components/ui/alert-dialog.d.ts +15 -0
- package/dist/components/ui/alert-dialog.d.ts.map +1 -0
- package/dist/components/ui/badge.d.ts +10 -0
- package/dist/components/ui/badge.d.ts.map +1 -0
- package/dist/components/ui/button.d.ts +11 -0
- package/dist/components/ui/button.d.ts.map +1 -0
- package/dist/components/ui/checkbox.d.ts +5 -0
- package/dist/components/ui/checkbox.d.ts.map +1 -0
- package/dist/components/ui/dropdown-menu.d.ts +26 -0
- package/dist/components/ui/dropdown-menu.d.ts.map +1 -0
- package/dist/components/ui/input.d.ts +4 -0
- package/dist/components/ui/input.d.ts.map +1 -0
- package/dist/components/ui/label.d.ts +5 -0
- package/dist/components/ui/label.d.ts.map +1 -0
- package/dist/components/ui/loading-spinner.d.ts +7 -0
- package/dist/components/ui/loading-spinner.d.ts.map +1 -0
- package/dist/components/ui/popover.d.ts +8 -0
- package/dist/components/ui/popover.d.ts.map +1 -0
- package/dist/components/ui/select.d.ts +16 -0
- package/dist/components/ui/select.d.ts.map +1 -0
- package/dist/components/ui/separator.d.ts +5 -0
- package/dist/components/ui/separator.d.ts.map +1 -0
- package/dist/components/ui/sonner.d.ts +4 -0
- package/dist/components/ui/sonner.d.ts.map +1 -0
- package/dist/components/ui/textarea.d.ts +4 -0
- package/dist/components/ui/textarea.d.ts.map +1 -0
- package/dist/components/ui/tooltip.d.ts +8 -0
- package/dist/components/ui/tooltip.d.ts.map +1 -0
- package/dist/execute/AmbiguousOrderError.d.ts +6 -0
- package/dist/execute/AmbiguousOrderError.d.ts.map +1 -0
- package/dist/execute/CycleDetectedError.d.ts +4 -0
- package/dist/execute/CycleDetectedError.d.ts.map +1 -0
- package/dist/execute/ExecutionPlanner.d.ts +30 -0
- package/dist/execute/ExecutionPlanner.d.ts.map +1 -0
- package/dist/execute/ExecutionPlanner.spec.d.ts +2 -0
- package/dist/execute/ExecutionPlanner.spec.d.ts.map +1 -0
- package/dist/execute/MissingDataError.d.ts +5 -0
- package/dist/execute/MissingDataError.d.ts.map +1 -0
- package/dist/execute/types.d.ts +65 -0
- package/dist/execute/types.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/get-next-available.d.ts +2 -0
- package/dist/lib/get-next-available.d.ts.map +1 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/query-builder/tari-type.d.ts +29 -0
- package/dist/query-builder/tari-type.d.ts.map +1 -0
- package/dist/query-builder/template-reader.d.ts +13 -0
- package/dist/query-builder/template-reader.d.ts.map +1 -0
- package/dist/store/persistence/handlers.d.ts +12 -0
- package/dist/store/persistence/handlers.d.ts.map +1 -0
- package/dist/store/persistence/types.d.ts +18 -0
- package/dist/store/persistence/types.d.ts.map +1 -0
- package/dist/store/persistence/v1_0.d.ts +5 -0
- package/dist/store/persistence/v1_0.d.ts.map +1 -0
- package/dist/store/store.d.ts +4 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/types.d.ts +97 -0
- package/dist/store/types.d.ts.map +1 -0
- package/dist/tari-extension-query-builder.css +1 -0
- package/dist/tari-extension-query-builder.es.js +183368 -0
- package/dist/tari-extension-query-builder.umd.js +620 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -0
- package/eslint.config.js +34 -0
- package/index.html +12 -0
- package/moon.yml +43 -0
- package/package.json +84 -0
- package/src/App.tsx +114 -0
- package/src/assets/react.svg +1 -0
- package/src/codegen/BuilderCodegen.ts +516 -0
- package/src/codegen/sample.ts +58 -0
- package/src/components/query-builder/edges/button-edge.tsx +50 -0
- package/src/components/query-builder/input/call-input-checkbox.tsx +54 -0
- package/src/components/query-builder/input/call-input-select.tsx +87 -0
- package/src/components/query-builder/input/call-input-text.tsx +98 -0
- package/src/components/query-builder/input/call-input.tsx +51 -0
- package/src/components/query-builder/nodes/call-node.types.ts +3 -0
- package/src/components/query-builder/nodes/constants.ts +4 -0
- package/src/components/query-builder/nodes/enter-connection.tsx +16 -0
- package/src/components/query-builder/nodes/exit-connection.tsx +16 -0
- package/src/components/query-builder/nodes/generic/generic-node.tsx +252 -0
- package/src/components/query-builder/nodes/generic/node-icon.tsx +37 -0
- package/src/components/query-builder/nodes/generic-node.types.ts +5 -0
- package/src/components/query-builder/nodes/input/constants.ts +1 -0
- package/src/components/query-builder/nodes/input/editable-label.tsx +142 -0
- package/src/components/query-builder/nodes/input/input-params-node.tsx +190 -0
- package/src/components/query-builder/query-builder.tsx +577 -0
- package/src/components/ui/alert-dialog.tsx +111 -0
- package/src/components/ui/badge.tsx +37 -0
- package/src/components/ui/button.tsx +50 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/dropdown-menu.tsx +217 -0
- package/src/components/ui/input.tsx +21 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/loading-spinner.tsx +46 -0
- package/src/components/ui/popover.tsx +40 -0
- package/src/components/ui/select.tsx +158 -0
- package/src/components/ui/separator.tsx +26 -0
- package/src/components/ui/sonner.tsx +23 -0
- package/src/components/ui/textarea.tsx +18 -0
- package/src/components/ui/tooltip.tsx +46 -0
- package/src/execute/AmbiguousOrderError.ts +13 -0
- package/src/execute/CycleDetectedError.ts +7 -0
- package/src/execute/ExecutionPlanner.spec.ts +174 -0
- package/src/execute/ExecutionPlanner.ts +445 -0
- package/src/execute/MissingDataError.ts +10 -0
- package/src/execute/types.ts +87 -0
- package/src/index.css +124 -0
- package/src/index.ts +5 -0
- package/src/lib/get-next-available.ts +12 -0
- package/src/lib/utils.ts +6 -0
- package/src/main.tsx +13 -0
- package/src/query-builder/tari-type.ts +185 -0
- package/src/query-builder/template-reader.ts +69 -0
- package/src/root.css +4 -0
- package/src/store/persistence/handlers.ts +16 -0
- package/src/store/persistence/types.ts +14 -0
- package/src/store/persistence/v1_0.ts +35 -0
- package/src/store/store.ts +396 -0
- package/src/store/types.ts +115 -0
- package/src/stories/data/tari-swap-pool.json +317 -0
- package/src/stories/data/wallet-functions.json +580 -0
- package/src/types.ts +7 -0
- package/src/vite-env.d.ts +1 -0
- package/src/xy-theme.css +144 -0
- package/tsconfig.app.json +31 -0
- package/tsconfig.json +15 -0
- package/tsconfig.lib.json +13 -0
- package/tsconfig.node.json +24 -0
- package/vite.config.ts +35 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddInstructionDescription,
|
|
3
|
+
AllocateComponentAddress,
|
|
4
|
+
AllocateResourceAddress,
|
|
5
|
+
ArgValue,
|
|
6
|
+
CallFunctionDescription,
|
|
7
|
+
CallMethodDescription,
|
|
8
|
+
FeeTransactionPayFromComponentDescription,
|
|
9
|
+
InputParameter,
|
|
10
|
+
SaveVarDescription,
|
|
11
|
+
TransactionContext,
|
|
12
|
+
TransactionDetails,
|
|
13
|
+
} from "@/execute/types";
|
|
14
|
+
import { Type } from "@tari-project/typescript-bindings";
|
|
15
|
+
import * as ts from "typescript";
|
|
16
|
+
|
|
17
|
+
const factory = ts.factory;
|
|
18
|
+
const syntaxKindValues = Object.values(ts.SyntaxKind).filter((value) => typeof value === "number") as number[];
|
|
19
|
+
|
|
20
|
+
export class BuilderCodegen {
|
|
21
|
+
constructor(private readonly details: TransactionDetails) {}
|
|
22
|
+
|
|
23
|
+
public generateTypescriptCode(): string {
|
|
24
|
+
return this.generateCode(ts.ScriptKind.TS);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public generateJavascriptCode(): string {
|
|
28
|
+
const code = this.generateCode(ts.ScriptKind.TS);
|
|
29
|
+
|
|
30
|
+
const javascript = ts.transpileModule(code, {
|
|
31
|
+
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
|
|
32
|
+
});
|
|
33
|
+
return javascript.outputText;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private generateCode(kind: ts.ScriptKind): string {
|
|
37
|
+
const filename = kind === ts.ScriptKind.TS ? "transaction.ts" : "transaction.js";
|
|
38
|
+
const file = ts.createSourceFile(filename, "", ts.ScriptTarget.ES2022, false, kind);
|
|
39
|
+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
|
|
40
|
+
const result = printer.printList(ts.ListFormat.MultiLine, this.buildAst(), file);
|
|
41
|
+
return stripEmptyComments(result);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private buildAst(): ts.NodeArray<ts.Statement> {
|
|
45
|
+
return factory.createNodeArray([
|
|
46
|
+
factory.createImportDeclaration(
|
|
47
|
+
undefined,
|
|
48
|
+
factory.createImportClause(
|
|
49
|
+
false,
|
|
50
|
+
undefined,
|
|
51
|
+
factory.createNamedImports([
|
|
52
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("buildTransactionRequest")),
|
|
53
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("fromWorkspace")),
|
|
54
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("Network")),
|
|
55
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("ReqSubstate")),
|
|
56
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("submitAndWaitForTransaction")),
|
|
57
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("SubmitTxResult")),
|
|
58
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("TariSigner")),
|
|
59
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("Transaction")),
|
|
60
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier("TransactionBuilder")),
|
|
61
|
+
]),
|
|
62
|
+
),
|
|
63
|
+
factory.createStringLiteral("@tari-project/tarijs-all"),
|
|
64
|
+
undefined,
|
|
65
|
+
),
|
|
66
|
+
...addEmptyCommentToFirstNode(buildInterfaces(this.details.context)),
|
|
67
|
+
...addEmptyCommentToFirstNode(buildInputVariables(this.details.context)),
|
|
68
|
+
addEmptyComment(
|
|
69
|
+
factory.createFunctionDeclaration(
|
|
70
|
+
undefined,
|
|
71
|
+
undefined,
|
|
72
|
+
factory.createIdentifier("buildTransaction"),
|
|
73
|
+
undefined,
|
|
74
|
+
buildInputArgs(this.details.context),
|
|
75
|
+
factory.createTypeReferenceNode(factory.createIdentifier("Transaction"), undefined),
|
|
76
|
+
factory.createBlock(
|
|
77
|
+
[
|
|
78
|
+
factory.createVariableStatement(
|
|
79
|
+
undefined,
|
|
80
|
+
factory.createVariableDeclarationList(
|
|
81
|
+
[
|
|
82
|
+
factory.createVariableDeclaration(
|
|
83
|
+
factory.createIdentifier("builder"),
|
|
84
|
+
undefined,
|
|
85
|
+
undefined,
|
|
86
|
+
factory.createNewExpression(factory.createIdentifier("TransactionBuilder"), undefined, []),
|
|
87
|
+
),
|
|
88
|
+
],
|
|
89
|
+
ts.NodeFlags.Const,
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
...this.createStatements(),
|
|
93
|
+
factory.createReturnStatement(
|
|
94
|
+
factory.createCallExpression(
|
|
95
|
+
factory.createPropertyAccessExpression(
|
|
96
|
+
factory.createIdentifier("builder"),
|
|
97
|
+
factory.createIdentifier("build"),
|
|
98
|
+
),
|
|
99
|
+
undefined,
|
|
100
|
+
[],
|
|
101
|
+
),
|
|
102
|
+
),
|
|
103
|
+
],
|
|
104
|
+
true,
|
|
105
|
+
),
|
|
106
|
+
),
|
|
107
|
+
),
|
|
108
|
+
addEmptyComment(
|
|
109
|
+
factory.createFunctionDeclaration(
|
|
110
|
+
[factory.createToken(ts.SyntaxKind.ExportKeyword), factory.createToken(ts.SyntaxKind.AsyncKeyword)],
|
|
111
|
+
undefined,
|
|
112
|
+
factory.createIdentifier("executeTransaction"),
|
|
113
|
+
undefined,
|
|
114
|
+
[
|
|
115
|
+
factory.createParameterDeclaration(
|
|
116
|
+
undefined,
|
|
117
|
+
undefined,
|
|
118
|
+
factory.createIdentifier("signer"),
|
|
119
|
+
undefined,
|
|
120
|
+
factory.createTypeReferenceNode(factory.createIdentifier("TariSigner"), undefined),
|
|
121
|
+
undefined,
|
|
122
|
+
),
|
|
123
|
+
factory.createParameterDeclaration(
|
|
124
|
+
undefined,
|
|
125
|
+
undefined,
|
|
126
|
+
factory.createIdentifier("network"),
|
|
127
|
+
undefined,
|
|
128
|
+
factory.createTypeReferenceNode(factory.createIdentifier("Network"), undefined),
|
|
129
|
+
undefined,
|
|
130
|
+
),
|
|
131
|
+
factory.createParameterDeclaration(
|
|
132
|
+
undefined,
|
|
133
|
+
undefined,
|
|
134
|
+
factory.createIdentifier("accountId"),
|
|
135
|
+
undefined,
|
|
136
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
|
|
137
|
+
undefined,
|
|
138
|
+
),
|
|
139
|
+
factory.createParameterDeclaration(
|
|
140
|
+
undefined,
|
|
141
|
+
undefined,
|
|
142
|
+
factory.createIdentifier("requiredSubstates"),
|
|
143
|
+
undefined,
|
|
144
|
+
factory.createArrayTypeNode(
|
|
145
|
+
factory.createTypeReferenceNode(factory.createIdentifier("ReqSubstate"), undefined),
|
|
146
|
+
),
|
|
147
|
+
factory.createArrayLiteralExpression([], false),
|
|
148
|
+
),
|
|
149
|
+
factory.createParameterDeclaration(
|
|
150
|
+
undefined,
|
|
151
|
+
undefined,
|
|
152
|
+
factory.createIdentifier("isDryRun"),
|
|
153
|
+
undefined,
|
|
154
|
+
undefined,
|
|
155
|
+
factory.createFalse(),
|
|
156
|
+
),
|
|
157
|
+
],
|
|
158
|
+
factory.createTypeReferenceNode(factory.createIdentifier("Promise"), [
|
|
159
|
+
factory.createTypeReferenceNode(factory.createIdentifier("SubmitTxResult"), undefined),
|
|
160
|
+
]),
|
|
161
|
+
factory.createBlock(
|
|
162
|
+
[
|
|
163
|
+
factory.createVariableStatement(
|
|
164
|
+
undefined,
|
|
165
|
+
factory.createVariableDeclarationList(
|
|
166
|
+
[
|
|
167
|
+
factory.createVariableDeclaration(
|
|
168
|
+
factory.createIdentifier("submitTransactionRequest"),
|
|
169
|
+
undefined,
|
|
170
|
+
undefined,
|
|
171
|
+
factory.createCallExpression(factory.createIdentifier("buildTransactionRequest"), undefined, [
|
|
172
|
+
factory.createCallExpression(
|
|
173
|
+
factory.createIdentifier("buildTransaction"),
|
|
174
|
+
undefined,
|
|
175
|
+
Object.keys(this.details.context.inputParams).map((key) => factory.createIdentifier(key)),
|
|
176
|
+
),
|
|
177
|
+
factory.createIdentifier("accountId"),
|
|
178
|
+
factory.createIdentifier("requiredSubstates"),
|
|
179
|
+
factory.createIdentifier("undefined"),
|
|
180
|
+
factory.createIdentifier("isDryRun"),
|
|
181
|
+
factory.createIdentifier("network"),
|
|
182
|
+
]),
|
|
183
|
+
),
|
|
184
|
+
],
|
|
185
|
+
ts.NodeFlags.Const,
|
|
186
|
+
),
|
|
187
|
+
),
|
|
188
|
+
factory.createVariableStatement(
|
|
189
|
+
undefined,
|
|
190
|
+
factory.createVariableDeclarationList(
|
|
191
|
+
[
|
|
192
|
+
factory.createVariableDeclaration(
|
|
193
|
+
factory.createIdentifier("txResult"),
|
|
194
|
+
undefined,
|
|
195
|
+
undefined,
|
|
196
|
+
factory.createAwaitExpression(
|
|
197
|
+
factory.createCallExpression(
|
|
198
|
+
factory.createIdentifier("submitAndWaitForTransaction"),
|
|
199
|
+
undefined,
|
|
200
|
+
[factory.createIdentifier("signer"), factory.createIdentifier("submitTransactionRequest")],
|
|
201
|
+
),
|
|
202
|
+
),
|
|
203
|
+
),
|
|
204
|
+
],
|
|
205
|
+
ts.NodeFlags.Const,
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
factory.createReturnStatement(factory.createIdentifier("txResult")),
|
|
209
|
+
],
|
|
210
|
+
true,
|
|
211
|
+
),
|
|
212
|
+
),
|
|
213
|
+
),
|
|
214
|
+
]);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private createStatements(): ts.NodeArray<ts.Statement> {
|
|
218
|
+
const factory = ts.factory;
|
|
219
|
+
return factory.createNodeArray(
|
|
220
|
+
this.details.descriptions.map((description) => {
|
|
221
|
+
switch (description.type) {
|
|
222
|
+
case "feeTransactionPayFromComponent":
|
|
223
|
+
return this.createFeeTransactionPayFromComponent(description);
|
|
224
|
+
case "callMethod":
|
|
225
|
+
return this.createCallMethod(description);
|
|
226
|
+
case "callFunction":
|
|
227
|
+
return this.createCallFunction(description);
|
|
228
|
+
case "addInstruction":
|
|
229
|
+
return this.createAddInstruction(description);
|
|
230
|
+
case "saveVar":
|
|
231
|
+
return this.createSaveVar(description);
|
|
232
|
+
case "allocateComponentAddress":
|
|
233
|
+
return this.createAllocateComponentAddress(description);
|
|
234
|
+
case "allocateResourceAddress":
|
|
235
|
+
return this.createAllocateResourceAddress(description);
|
|
236
|
+
}
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private createFeeTransactionPayFromComponent(description: FeeTransactionPayFromComponentDescription): ts.Statement {
|
|
242
|
+
return factory.createExpressionStatement(
|
|
243
|
+
factory.createCallExpression(
|
|
244
|
+
factory.createPropertyAccessExpression(
|
|
245
|
+
factory.createIdentifier("builder"),
|
|
246
|
+
factory.createIdentifier("feeTransactionPayFromComponent"),
|
|
247
|
+
),
|
|
248
|
+
undefined,
|
|
249
|
+
[factory.createStringLiteral(description.args[0]), factory.createStringLiteral(description.args[1])],
|
|
250
|
+
),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private createArgValueAst(arg: ArgValue): ts.Expression {
|
|
255
|
+
switch (arg.type) {
|
|
256
|
+
case "workspace":
|
|
257
|
+
return factory.createCallExpression(factory.createIdentifier("fromWorkspace"), undefined, [
|
|
258
|
+
factory.createStringLiteral(arg.value),
|
|
259
|
+
]);
|
|
260
|
+
case "input":
|
|
261
|
+
return factory.createPropertyAccessExpression(
|
|
262
|
+
factory.createIdentifier(arg.reference.name),
|
|
263
|
+
factory.createIdentifier(arg.reference.inputParam.name),
|
|
264
|
+
);
|
|
265
|
+
default:
|
|
266
|
+
return transformObjectToAstArray(arg.value)[0];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private createArgValuesAst(args: ArgValue[]): ts.Expression {
|
|
271
|
+
const expressions = args.map((arg) => this.createArgValueAst(arg));
|
|
272
|
+
return factory.createArrayLiteralExpression(expressions, false);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private createCallMethod(description: CallMethodDescription): ts.Statement {
|
|
276
|
+
const objExpression = factory.createObjectLiteralExpression(
|
|
277
|
+
[
|
|
278
|
+
factory.createPropertyAssignment(
|
|
279
|
+
factory.createIdentifier("componentAddress"),
|
|
280
|
+
this.createArgValueAst(description.componentAddress),
|
|
281
|
+
),
|
|
282
|
+
factory.createPropertyAssignment(
|
|
283
|
+
factory.createIdentifier("methodName"),
|
|
284
|
+
transformObjectToAstArray(description.methodName)[0],
|
|
285
|
+
),
|
|
286
|
+
],
|
|
287
|
+
true,
|
|
288
|
+
);
|
|
289
|
+
return factory.createExpressionStatement(
|
|
290
|
+
factory.createCallExpression(
|
|
291
|
+
factory.createPropertyAccessExpression(
|
|
292
|
+
factory.createIdentifier("builder"),
|
|
293
|
+
factory.createIdentifier("callMethod"),
|
|
294
|
+
),
|
|
295
|
+
undefined,
|
|
296
|
+
[objExpression, this.createArgValuesAst(description.args)],
|
|
297
|
+
),
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private createCallFunction(description: CallFunctionDescription): ts.Statement {
|
|
302
|
+
return factory.createExpressionStatement(
|
|
303
|
+
factory.createCallExpression(
|
|
304
|
+
factory.createPropertyAccessExpression(
|
|
305
|
+
factory.createIdentifier("builder"),
|
|
306
|
+
factory.createIdentifier("callFunction"),
|
|
307
|
+
),
|
|
308
|
+
undefined,
|
|
309
|
+
[transformObjectToAstArray(description.function)[0], this.createArgValuesAst(description.args)],
|
|
310
|
+
),
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private createAddInstruction(description: AddInstructionDescription): ts.Statement {
|
|
315
|
+
if (description.name !== "EmitLog") {
|
|
316
|
+
throw new Error(`Unknown instruction: ${description.name}`);
|
|
317
|
+
}
|
|
318
|
+
const getArg = (idx: number) => {
|
|
319
|
+
const arg = description.args[idx];
|
|
320
|
+
return arg.type === "input"
|
|
321
|
+
? factory.createPropertyAccessExpression(
|
|
322
|
+
factory.createIdentifier(arg.reference.name),
|
|
323
|
+
factory.createIdentifier(arg.reference.inputParam.name),
|
|
324
|
+
)
|
|
325
|
+
: description.args[idx].value;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const call = {
|
|
329
|
+
[description.name]: {
|
|
330
|
+
level: getArg(0),
|
|
331
|
+
message: getArg(1),
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
return factory.createExpressionStatement(
|
|
336
|
+
factory.createCallExpression(
|
|
337
|
+
factory.createPropertyAccessExpression(
|
|
338
|
+
factory.createIdentifier("builder"),
|
|
339
|
+
factory.createIdentifier("addInstruction"),
|
|
340
|
+
),
|
|
341
|
+
undefined,
|
|
342
|
+
transformObjectToAstArray(call),
|
|
343
|
+
),
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private createSaveVar(description: SaveVarDescription): ts.Statement {
|
|
348
|
+
return factory.createExpressionStatement(
|
|
349
|
+
factory.createCallExpression(
|
|
350
|
+
factory.createPropertyAccessExpression(
|
|
351
|
+
factory.createIdentifier("builder"),
|
|
352
|
+
factory.createIdentifier("saveVar"),
|
|
353
|
+
),
|
|
354
|
+
undefined,
|
|
355
|
+
[factory.createStringLiteral(description.key)],
|
|
356
|
+
),
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private createAllocateComponentAddress(description: AllocateComponentAddress): ts.Statement {
|
|
361
|
+
return factory.createExpressionStatement(
|
|
362
|
+
factory.createCallExpression(
|
|
363
|
+
factory.createPropertyAccessExpression(
|
|
364
|
+
factory.createIdentifier("builder"),
|
|
365
|
+
factory.createIdentifier("allocateAddress"),
|
|
366
|
+
),
|
|
367
|
+
undefined,
|
|
368
|
+
[factory.createStringLiteral("Component"), factory.createStringLiteral(description.workspaceId)],
|
|
369
|
+
),
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private createAllocateResourceAddress(description: AllocateResourceAddress): ts.Statement {
|
|
374
|
+
return factory.createExpressionStatement(
|
|
375
|
+
factory.createCallExpression(
|
|
376
|
+
factory.createPropertyAccessExpression(
|
|
377
|
+
factory.createIdentifier("builder"),
|
|
378
|
+
factory.createIdentifier("allocateAddress"),
|
|
379
|
+
),
|
|
380
|
+
undefined,
|
|
381
|
+
[factory.createStringLiteral("Resource"), factory.createStringLiteral(description.workspaceId)],
|
|
382
|
+
),
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function transformObjectToAst(obj: unknown): ts.Expression | ts.Expression[] {
|
|
388
|
+
if (Array.isArray(obj)) {
|
|
389
|
+
return obj.map((item) => transformObjectToAst(item) as ts.Expression);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (typeof obj === "object" && obj !== null) {
|
|
393
|
+
if (isTypescriptASTNode(obj)) {
|
|
394
|
+
return obj;
|
|
395
|
+
}
|
|
396
|
+
const properties = Object.entries(obj).map(([key, value]) =>
|
|
397
|
+
factory.createPropertyAssignment(factory.createIdentifier(key), transformObjectToAst(value) as ts.Expression),
|
|
398
|
+
);
|
|
399
|
+
return factory.createObjectLiteralExpression(properties, true);
|
|
400
|
+
} else if (typeof obj === "string") {
|
|
401
|
+
return factory.createStringLiteral(obj);
|
|
402
|
+
} else if (typeof obj === "number") {
|
|
403
|
+
return factory.createNumericLiteral(obj);
|
|
404
|
+
} else if (typeof obj === "boolean") {
|
|
405
|
+
return obj ? factory.createTrue() : factory.createFalse();
|
|
406
|
+
} else if (obj === null) {
|
|
407
|
+
return factory.createNull();
|
|
408
|
+
} else if (obj === undefined) {
|
|
409
|
+
return factory.createIdentifier("undefined");
|
|
410
|
+
} else {
|
|
411
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
412
|
+
return factory.createIdentifier(String(obj));
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function transformObjectToAstArray(obj: unknown): ts.Expression[] {
|
|
417
|
+
const expressions = transformObjectToAst(obj);
|
|
418
|
+
return Array.isArray(expressions) ? expressions : [expressions];
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function addEmptyComment<T extends ts.Node>(node: T): T {
|
|
422
|
+
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.SingleLineCommentTrivia, "", true);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function addEmptyCommentToFirstNode<T extends ts.Node>(nodes: T[]) {
|
|
426
|
+
const [firstNode, ...otherNodes] = nodes;
|
|
427
|
+
return [addEmptyComment(firstNode), ...otherNodes];
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function stripEmptyComments(code: string): string {
|
|
431
|
+
return code
|
|
432
|
+
.split("\n")
|
|
433
|
+
.map((line) => (/^\s*\/\/\s*$/.test(line) ? "" : line))
|
|
434
|
+
.join("\n");
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function getInterfaceName(name: string) {
|
|
438
|
+
if (!name.length) {
|
|
439
|
+
throw new Error("Empty interface name");
|
|
440
|
+
}
|
|
441
|
+
const capitalized = name[0].toUpperCase() + name.substring(1);
|
|
442
|
+
return `${capitalized}Props`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function buildInterface(name: string, params: InputParameter[]): ts.InterfaceDeclaration {
|
|
446
|
+
return factory.createInterfaceDeclaration(
|
|
447
|
+
[factory.createToken(ts.SyntaxKind.ExportKeyword)],
|
|
448
|
+
factory.createIdentifier(getInterfaceName(name)),
|
|
449
|
+
undefined,
|
|
450
|
+
undefined,
|
|
451
|
+
params.map((param) =>
|
|
452
|
+
factory.createPropertySignature(
|
|
453
|
+
undefined,
|
|
454
|
+
factory.createIdentifier(param.type.name),
|
|
455
|
+
undefined,
|
|
456
|
+
factory.createKeywordTypeNode(getTsType(param.type.type)),
|
|
457
|
+
),
|
|
458
|
+
),
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function getTsType(type: Type): ts.KeywordTypeSyntaxKind {
|
|
463
|
+
if (type === "Bool") {
|
|
464
|
+
return ts.SyntaxKind.BooleanKeyword;
|
|
465
|
+
} else if (typeof type === "string" && /^[IU].*/.test(type)) {
|
|
466
|
+
return ts.SyntaxKind.NumberKeyword;
|
|
467
|
+
} else {
|
|
468
|
+
return ts.SyntaxKind.StringKeyword;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function buildInterfaces(context: TransactionContext): ts.InterfaceDeclaration[] {
|
|
473
|
+
return Object.entries(context.inputParams).map(([name, params]) => buildInterface(name, params));
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function buildInputArgs(context: TransactionContext): ts.ParameterDeclaration[] {
|
|
477
|
+
return Object.entries(context.inputParams).map(([name]) =>
|
|
478
|
+
factory.createParameterDeclaration(
|
|
479
|
+
undefined,
|
|
480
|
+
undefined,
|
|
481
|
+
factory.createIdentifier(name),
|
|
482
|
+
undefined,
|
|
483
|
+
factory.createTypeReferenceNode(factory.createIdentifier(getInterfaceName(name)), undefined),
|
|
484
|
+
undefined,
|
|
485
|
+
),
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function buildInputVariable(name: string, params: InputParameter[]) {
|
|
490
|
+
const obj = Object.fromEntries(params.map((param) => [param.type.name, param.value]));
|
|
491
|
+
return factory.createVariableStatement(
|
|
492
|
+
undefined,
|
|
493
|
+
factory.createVariableDeclarationList(
|
|
494
|
+
[
|
|
495
|
+
factory.createVariableDeclaration(
|
|
496
|
+
factory.createIdentifier(name),
|
|
497
|
+
undefined,
|
|
498
|
+
factory.createTypeReferenceNode(factory.createIdentifier(getInterfaceName(name)), undefined),
|
|
499
|
+
transformObjectToAstArray(obj)[0],
|
|
500
|
+
),
|
|
501
|
+
],
|
|
502
|
+
ts.NodeFlags.Const,
|
|
503
|
+
),
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function buildInputVariables(context: TransactionContext): ts.VariableStatement[] {
|
|
508
|
+
return Object.entries(context.inputParams).map(([name, params]) => buildInputVariable(name, params));
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function isTypescriptASTNode(obj: unknown): obj is ts.Expression {
|
|
512
|
+
if (typeof obj !== "object" || obj === null || !("kind" in obj) || typeof obj.kind !== "number") {
|
|
513
|
+
return false;
|
|
514
|
+
}
|
|
515
|
+
return syntaxKindValues.includes(obj.kind);
|
|
516
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildTransactionRequest,
|
|
3
|
+
fromWorkspace,
|
|
4
|
+
Network,
|
|
5
|
+
ReqSubstate,
|
|
6
|
+
submitAndWaitForTransaction,
|
|
7
|
+
SubmitTxResult,
|
|
8
|
+
TariSigner,
|
|
9
|
+
Transaction,
|
|
10
|
+
TransactionBuilder,
|
|
11
|
+
} from "@tari-project/tarijs-all";
|
|
12
|
+
|
|
13
|
+
function buildTransaction(): Transaction {
|
|
14
|
+
const builder = new TransactionBuilder();
|
|
15
|
+
builder.feeTransactionPayFromComponent("ACCOUNT_ADDRESS", "12345");
|
|
16
|
+
builder.callMethod(
|
|
17
|
+
{
|
|
18
|
+
componentAddress: "COMPONENT_ADDRESS",
|
|
19
|
+
methodName: "method_name",
|
|
20
|
+
},
|
|
21
|
+
["a1", 2, fromWorkspace("a2")],
|
|
22
|
+
);
|
|
23
|
+
builder.saveVar("b3");
|
|
24
|
+
builder.callFunction(
|
|
25
|
+
{
|
|
26
|
+
templateAddress: "TEMPLATE_ADDRESS",
|
|
27
|
+
functionName: "function_name",
|
|
28
|
+
},
|
|
29
|
+
["b2", 3, fromWorkspace("b3")],
|
|
30
|
+
);
|
|
31
|
+
builder.addInstruction({
|
|
32
|
+
EmitLog: {
|
|
33
|
+
level: "Info",
|
|
34
|
+
message: "Hello, world!",
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
return builder.build();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function executeTransaction(
|
|
41
|
+
signer: TariSigner,
|
|
42
|
+
network: Network,
|
|
43
|
+
accountId: number,
|
|
44
|
+
requiredSubstates: ReqSubstate[] = [],
|
|
45
|
+
isDryRun = false,
|
|
46
|
+
): Promise<SubmitTxResult> {
|
|
47
|
+
const submitTransactionRequest = buildTransactionRequest(
|
|
48
|
+
buildTransaction(),
|
|
49
|
+
accountId,
|
|
50
|
+
requiredSubstates,
|
|
51
|
+
undefined,
|
|
52
|
+
isDryRun,
|
|
53
|
+
network,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const txResult = await submitAndWaitForTransaction(signer, submitTransactionRequest);
|
|
57
|
+
return txResult;
|
|
58
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Button } from "@/components/ui/button";
|
|
2
|
+
import { TrashIcon } from "@radix-ui/react-icons";
|
|
3
|
+
import { BaseEdge, EdgeLabelRenderer, getBezierPath, useReactFlow, type EdgeProps } from "@xyflow/react";
|
|
4
|
+
|
|
5
|
+
export default function ButtonEdge({
|
|
6
|
+
id,
|
|
7
|
+
sourceX,
|
|
8
|
+
sourceY,
|
|
9
|
+
targetX,
|
|
10
|
+
targetY,
|
|
11
|
+
sourcePosition,
|
|
12
|
+
targetPosition,
|
|
13
|
+
style = {},
|
|
14
|
+
markerEnd,
|
|
15
|
+
}: EdgeProps) {
|
|
16
|
+
const { setEdges } = useReactFlow();
|
|
17
|
+
const [edgePath, labelX, labelY] = getBezierPath({
|
|
18
|
+
sourceX,
|
|
19
|
+
sourceY,
|
|
20
|
+
sourcePosition,
|
|
21
|
+
targetX,
|
|
22
|
+
targetY,
|
|
23
|
+
targetPosition,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const onEdgeClick = () => {
|
|
27
|
+
setEdges((edges) => edges.filter((edge) => edge.id !== id));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<>
|
|
32
|
+
<BaseEdge path={edgePath} markerEnd={markerEnd} style={style} />
|
|
33
|
+
<EdgeLabelRenderer>
|
|
34
|
+
<div
|
|
35
|
+
className="nodrag nopan"
|
|
36
|
+
style={{
|
|
37
|
+
position: "absolute",
|
|
38
|
+
pointerEvents: "all",
|
|
39
|
+
transformOrigin: "center",
|
|
40
|
+
transform: `translate(-50%, -50%) translate(${labelX.toString()}px,${labelY.toString()}px)`,
|
|
41
|
+
}}
|
|
42
|
+
>
|
|
43
|
+
<Button variant="ghost" size="icon" onClick={onEdgeClick}>
|
|
44
|
+
<TrashIcon className="h-4 w-4" />
|
|
45
|
+
</Button>
|
|
46
|
+
</div>
|
|
47
|
+
</EdgeLabelRenderer>
|
|
48
|
+
</>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Checkbox } from "@/components/ui/checkbox";
|
|
2
|
+
import CallInput, { CallInputProps } from "./call-input";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { SafeParseReturnType } from "zod";
|
|
5
|
+
import { CheckedState } from "@radix-ui/react-checkbox";
|
|
6
|
+
import { EditableLabelProps } from "../nodes/input/editable-label";
|
|
7
|
+
|
|
8
|
+
type CallInputCheckboxProps = {
|
|
9
|
+
readOnly?: boolean;
|
|
10
|
+
value?: SafeParseReturnType<unknown, unknown>;
|
|
11
|
+
onChange?: (value: SafeParseReturnType<unknown, unknown>) => void;
|
|
12
|
+
} & Omit<CallInputProps, "children"> &
|
|
13
|
+
Omit<EditableLabelProps, "initialLabel">;
|
|
14
|
+
|
|
15
|
+
function CallInputCheckbox({
|
|
16
|
+
readOnly = false,
|
|
17
|
+
name,
|
|
18
|
+
labelWidth,
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
isValidLabel,
|
|
22
|
+
onLabelChange,
|
|
23
|
+
onRemove,
|
|
24
|
+
}: CallInputCheckboxProps) {
|
|
25
|
+
const [checked, setChecked] = useState(value?.success ? (value.data as boolean) : false);
|
|
26
|
+
|
|
27
|
+
const handleChange = (checkedState: CheckedState) => {
|
|
28
|
+
const checked = typeof checkedState === "boolean" && checkedState;
|
|
29
|
+
setChecked(checked);
|
|
30
|
+
if (onChange) {
|
|
31
|
+
onChange({ success: true, data: checked });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<CallInput
|
|
37
|
+
name={name}
|
|
38
|
+
labelWidth={labelWidth}
|
|
39
|
+
isValidLabel={isValidLabel}
|
|
40
|
+
onLabelChange={onLabelChange}
|
|
41
|
+
onRemove={onRemove}
|
|
42
|
+
>
|
|
43
|
+
<Checkbox
|
|
44
|
+
disabled={readOnly}
|
|
45
|
+
name={name}
|
|
46
|
+
className="nodrag flex justify-start border border-gray-400 dark:border-gray-700"
|
|
47
|
+
checked={checked}
|
|
48
|
+
onCheckedChange={handleChange}
|
|
49
|
+
/>
|
|
50
|
+
</CallInput>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default CallInputCheckbox;
|