@helios-lang/contract-utils 0.1.20 → 0.1.24
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/examples/multi-validator/README.md +13 -0
- package/examples/multi-validator/contract.js +28 -0
- package/examples/{spending → multi-validator}/index.d.ts +3 -3
- package/examples/multi-validator/index.js +27 -0
- package/examples/multi-validator/minting.hl +5 -0
- package/examples/{spending → multi-validator}/package-lock.json +6 -15
- package/examples/{spending → multi-validator}/package.json +0 -1
- package/examples/multi-validator/spending.hl +18 -0
- package/examples/multi-validator/utils.hl +9 -0
- package/package.json +7 -6
- package/src/{runtime → cast}/Cast.js +31 -8
- package/src/cast/index.js +12 -0
- package/src/codegen/LoadedModule.js +9 -0
- package/src/codegen/{TypescriptWriter.js → LoadedScriptsWriter.js} +149 -108
- package/src/codegen/LoadedValidator.js +23 -0
- package/src/codegen/{Module.js → TypeCheckedModule.js} +3 -3
- package/src/codegen/{Validator.js → TypeCheckedValidator.js} +5 -8
- package/src/codegen/TypeSchema.js +6 -2
- package/src/codegen/index.js +5 -4
- package/src/compiler/CompilerLib.js +51 -0
- package/src/{lib/Libg_v0_16.js → compiler/CompilerLib_v0_16.js} +244 -255
- package/src/compiler/index.js +6 -0
- package/src/compiler/ops.js +137 -0
- package/src/context/ContractContext.js +78 -0
- package/src/context/ContractContextBuilder.js +69 -0
- package/src/context/DagCompiler.js +229 -0
- package/src/context/index.js +1 -0
- package/src/hl2ts.js +116 -104
- package/src/index.js +12 -1
- package/types/cast/Cast.d.ts +32 -0
- package/types/cast/Cast.d.ts.map +1 -0
- package/types/cast/PermissiveType.d.ts +3 -0
- package/types/cast/PermissiveType.d.ts.map +1 -0
- package/types/cast/StrictType.d.ts +3 -0
- package/types/cast/StrictType.d.ts.map +1 -0
- package/types/cast/index.d.ts +5 -0
- package/types/cast/index.d.ts.map +1 -0
- package/types/codegen/LoadedModule.d.ts +6 -0
- package/types/codegen/LoadedModule.d.ts.map +1 -0
- package/types/codegen/LoadedScriptsWriter.d.ts +57 -0
- package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -0
- package/types/codegen/LoadedValidator.d.ts +16 -0
- package/types/codegen/LoadedValidator.d.ts.map +1 -0
- package/types/codegen/TypeCheckedModule.d.ts +11 -0
- package/types/codegen/TypeCheckedModule.d.ts.map +1 -0
- package/types/codegen/TypeCheckedValidator.d.ts +8 -0
- package/types/codegen/TypeCheckedValidator.d.ts.map +1 -0
- package/types/codegen/TypeSchema.d.ts +10 -0
- package/types/codegen/TypeSchema.d.ts.map +1 -1
- package/types/codegen/TypescriptWriter.d.ts +4 -4
- package/types/codegen/Validator.d.ts +1 -1
- package/types/codegen/index.d.ts +5 -4
- package/types/codegen/index.d.ts.map +1 -1
- package/types/compiler/CompilerLib.d.ts +45 -0
- package/types/compiler/CompilerLib.d.ts.map +1 -0
- package/types/compiler/CompilerLib_v0_16.d.ts +271 -0
- package/types/compiler/CompilerLib_v0_16.d.ts.map +1 -0
- package/types/compiler/index.d.ts +4 -0
- package/types/compiler/index.d.ts.map +1 -0
- package/types/compiler/ops.d.ts +24 -0
- package/types/compiler/ops.d.ts.map +1 -0
- package/types/context/ContractContext.d.ts +23 -0
- package/types/context/ContractContext.d.ts.map +1 -0
- package/types/context/ContractContextBuilder.d.ts +53 -0
- package/types/context/ContractContextBuilder.d.ts.map +1 -0
- package/types/context/DagCompiler.d.ts +64 -0
- package/types/context/DagCompiler.d.ts.map +1 -0
- package/types/context/index.d.ts +2 -0
- package/types/context/index.d.ts.map +1 -0
- package/types/hl2ts.d.ts +9 -4
- package/types/hl2ts.d.ts.map +1 -1
- package/types/index.d.ts +4 -1
- package/types/index.d.ts.map +1 -1
- package/types/lib/Lib.d.ts +13 -71
- package/types/lib/Lib.d.ts.map +1 -1
- package/types/lib/Libg_v0_16.d.ts +53 -52
- package/types/lib/Libg_v0_16.d.ts.map +1 -1
- package/types/lib/index.d.ts +0 -1
- package/types/lib/index.d.ts.map +1 -1
- package/types/lib/load.d.ts +1 -7
- package/types/lib/load.d.ts.map +1 -1
- package/types/runtime/ContractContext.d.ts +22 -43
- package/types/runtime/ContractContext.d.ts.map +1 -1
- package/types/runtime/ContractContextBuilder.d.ts +15 -123
- package/types/runtime/ContractContextBuilder.d.ts.map +1 -1
- package/types/runtime/DagCompiler.d.ts +64 -0
- package/types/runtime/DagCompiler.d.ts.map +1 -0
- package/types/runtime/PermissiveType.d.ts +1 -1
- package/types/runtime/StrictType.d.ts +1 -1
- package/types/runtime/index.d.ts +0 -2
- package/examples/spending/contract.js +0 -16
- package/examples/spending/index.js +0 -27
- package/examples/spending/minting.hl +0 -5
- package/examples/spending/spending.hl +0 -17
- package/examples/spending/utils.hl +0 -5
- package/src/codegen/Writer.js +0 -37
- package/src/lib/Lib.js +0 -56
- package/src/lib/index.js +0 -6
- package/src/lib/load.js +0 -26
- package/src/runtime/ContractContext.js +0 -35
- package/src/runtime/ContractContextBuilder.js +0 -340
- package/src/runtime/index.js +0 -3
- /package/src/{runtime → cast}/PermissiveType.js +0 -0
- /package/src/{runtime → cast}/StrictType.js +0 -0
|
@@ -2,14 +2,14 @@ import { bytesToHex } from "@helios-lang/compiler"
|
|
|
2
2
|
import { readHeader } from "@helios-lang/compiler-utils"
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @typedef {import("../codegen/index.js").
|
|
6
|
-
* @typedef {import("../codegen/index.js").
|
|
5
|
+
* @typedef {import("../codegen/index.js").TypeCheckedModule} TypeCheckedModule
|
|
6
|
+
* @typedef {import("../codegen/index.js").TypeCheckedValidator} TypeCheckedValidator
|
|
7
7
|
* @typedef {import("../codegen/index.js").TypeSchema} InternalTypeDetails
|
|
8
|
-
* @typedef {import("./
|
|
9
|
-
* @typedef {import("./
|
|
10
|
-
* @typedef {import("./
|
|
11
|
-
* @typedef {import("./
|
|
12
|
-
* @typedef {import("./
|
|
8
|
+
* @typedef {import("./CompilerLib.js").CompileOptions} CompileOptions
|
|
9
|
+
* @typedef {import("./CompilerLib.js").CompileOutput} CompileOutput
|
|
10
|
+
* @typedef {import("./CompilerLib.js").CompilerLib} CompilerLib
|
|
11
|
+
* @typedef {import("./CompilerLib.js").SourceDetails} SourceDetails
|
|
12
|
+
* @typedef {import("./CompilerLib.js").TypeCheckOutput} TypeCheckOutput
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -20,7 +20,7 @@ import { readHeader } from "@helios-lang/compiler-utils"
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* @typedef {{
|
|
23
|
-
* }}
|
|
23
|
+
* }} ScriptHashType
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -47,7 +47,22 @@ import { readHeader } from "@helios-lang/compiler-utils"
|
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* @typedef {{
|
|
50
|
-
*
|
|
50
|
+
* src: {
|
|
51
|
+
* raw: string
|
|
52
|
+
* }
|
|
53
|
+
* }} Site
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @typedef {{
|
|
58
|
+
* value: string
|
|
59
|
+
* site: Site
|
|
60
|
+
* }} WordToken
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @typedef {{
|
|
65
|
+
* name: WordToken
|
|
51
66
|
* filterDependencies: (all: Module[]) => Module[]
|
|
52
67
|
* statements: Statement[]
|
|
53
68
|
* }} Module
|
|
@@ -55,6 +70,7 @@ import { readHeader } from "@helios-lang/compiler-utils"
|
|
|
55
70
|
/**
|
|
56
71
|
* @typedef {{
|
|
57
72
|
* name: string
|
|
73
|
+
* purpose: "testing" | "minting" | "spending" | "staking" | "endpoint" | "module" | "unknown"
|
|
58
74
|
* toIR: (ctx: any, extra: Map<string, IR>) => IR
|
|
59
75
|
* types: UserTypes
|
|
60
76
|
* mainImportedModules: Module[]
|
|
@@ -111,30 +127,14 @@ import { readHeader } from "@helios-lang/compiler-utils"
|
|
|
111
127
|
*/
|
|
112
128
|
|
|
113
129
|
/**
|
|
114
|
-
* @implements {
|
|
130
|
+
* @implements {CompilerLib}
|
|
115
131
|
*/
|
|
116
|
-
export class
|
|
117
|
-
/**
|
|
118
|
-
* @private
|
|
119
|
-
* @readonly
|
|
120
|
-
* @type {LibOptions}
|
|
121
|
-
*/
|
|
122
|
-
options
|
|
123
|
-
|
|
132
|
+
export class CompilerLib_v0_16 {
|
|
124
133
|
/**
|
|
125
134
|
* @param {any} lib
|
|
126
|
-
* @param {LibOptions} options
|
|
127
135
|
*/
|
|
128
|
-
constructor(lib
|
|
136
|
+
constructor(lib) {
|
|
129
137
|
this.lib = lib
|
|
130
|
-
|
|
131
|
-
this.lib.config.set({
|
|
132
|
-
IS_TESTNET: !(options?.isMainnet ?? false),
|
|
133
|
-
CHECK_CASTS: options?.checkCasts ?? false,
|
|
134
|
-
IGNORE_UNEVALUATED_CONSTANTS: true
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
this.options = options
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
/**
|
|
@@ -146,9 +146,9 @@ export class Lib_v0_16 {
|
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
148
|
* @param {string} purpose
|
|
149
|
-
* @returns {
|
|
149
|
+
* @returns {ScriptHashType}
|
|
150
150
|
*/
|
|
151
|
-
|
|
151
|
+
getScriptHashType(purpose) {
|
|
152
152
|
switch (purpose) {
|
|
153
153
|
case "spending":
|
|
154
154
|
return this.lib.ValidatorHashType
|
|
@@ -161,33 +161,227 @@ export class Lib_v0_16 {
|
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
+
/**
|
|
165
|
+
* @param {string[]} validators
|
|
166
|
+
* @param {string[]} modules
|
|
167
|
+
* @returns {TypeCheckOutput}
|
|
168
|
+
*/
|
|
169
|
+
typeCheck(validators, modules) {
|
|
170
|
+
const validatorTypes = Object.fromEntries(
|
|
171
|
+
validators.map((v) => {
|
|
172
|
+
const [purpose, name] = readHeader(v)
|
|
173
|
+
return [name, this.getScriptHashType(purpose)]
|
|
174
|
+
})
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
// create validator programs
|
|
178
|
+
let validatorPrograms = this.createPrograms(
|
|
179
|
+
validators,
|
|
180
|
+
modules,
|
|
181
|
+
validatorTypes
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
// build dag
|
|
185
|
+
const dag = this.buildDagDependencies(validatorPrograms, validatorTypes)
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @type {{[name: string]: TypeCheckedValidator}}
|
|
189
|
+
*/
|
|
190
|
+
const typeCheckedValidators = {}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @type {{[name: string]: TypeCheckedModule}}
|
|
194
|
+
*/
|
|
195
|
+
const typeCheckedModules = {}
|
|
196
|
+
|
|
197
|
+
// collect the validators and the modules from the typechecked programs
|
|
198
|
+
for (let v of validatorPrograms) {
|
|
199
|
+
const name = v.name
|
|
200
|
+
const purpose = v.purpose
|
|
201
|
+
const sourceCode = v.mainModule.name.site.src.raw
|
|
202
|
+
const hashDependencies = dag[name]
|
|
203
|
+
|
|
204
|
+
const allModules = v.mainImportedModules
|
|
205
|
+
const moduleDepedencies = v.mainModule
|
|
206
|
+
.filterDependencies(allModules)
|
|
207
|
+
.map((m) => m.name.value)
|
|
208
|
+
|
|
209
|
+
typeCheckedValidators[name] = {
|
|
210
|
+
name: name,
|
|
211
|
+
purpose: purpose,
|
|
212
|
+
sourceCode: sourceCode,
|
|
213
|
+
hashDependencies: hashDependencies,
|
|
214
|
+
moduleDepedencies: moduleDepedencies,
|
|
215
|
+
types: {},
|
|
216
|
+
Redeemer: this.getInternalTypeDetails(
|
|
217
|
+
v.mainArgTypes[purpose == "spending" ? 1 : 0]
|
|
218
|
+
),
|
|
219
|
+
Datum:
|
|
220
|
+
purpose == "spending"
|
|
221
|
+
? this.getInternalTypeDetails(v.mainArgTypes[0])
|
|
222
|
+
: undefined
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// add any module dependencies that haven't been added before
|
|
226
|
+
for (let m of v.mainImportedModules) {
|
|
227
|
+
const name = m.name.value
|
|
228
|
+
const sourceCode = m.name.site.src.raw
|
|
229
|
+
|
|
230
|
+
if (name in typeCheckedModules) {
|
|
231
|
+
continue
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
typeCheckedModules[name] = {
|
|
235
|
+
name: name,
|
|
236
|
+
purpose: "module",
|
|
237
|
+
sourceCode: sourceCode,
|
|
238
|
+
moduleDepedencies: m
|
|
239
|
+
.filterDependencies(allModules)
|
|
240
|
+
.map((m) => m.name.value),
|
|
241
|
+
types: {}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
modules: typeCheckedModules,
|
|
248
|
+
validators: typeCheckedValidators
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Generate additional IR definitions
|
|
254
|
+
* * dependency on own precalculated hash (eg. unoptimized program should use hash of optimized program)
|
|
255
|
+
* * dependency on own hash through methods defined on the ScriptContext
|
|
256
|
+
* * dependency on hashes of other validators
|
|
257
|
+
* @private
|
|
258
|
+
* @param {string} name - name of the validator being compiled
|
|
259
|
+
* @param {string} purpose - purpose the validator being compiled
|
|
260
|
+
* @param {number} nPosParams
|
|
261
|
+
* @param {CompileOptions} options
|
|
262
|
+
* @returns {Map<string, IR>}
|
|
263
|
+
*/
|
|
264
|
+
generateExtraIRDefinitions(name, purpose, nPosParams, options) {
|
|
265
|
+
/**
|
|
266
|
+
* @type {Map<string, IR>}
|
|
267
|
+
*/
|
|
268
|
+
const extra = new Map()
|
|
269
|
+
|
|
270
|
+
const IR = this.lib.IR
|
|
271
|
+
|
|
272
|
+
// inject own hash (either precalculated, or via ScriptContext)
|
|
273
|
+
if (options.ownHash) {
|
|
274
|
+
extra.set(
|
|
275
|
+
`__helios__scripts__${name}`,
|
|
276
|
+
new IR(`#${options.ownHash}`)
|
|
277
|
+
)
|
|
278
|
+
} else if (options.dependsOnOwnHash) {
|
|
279
|
+
const key = `__helios__scripts__${name}`
|
|
280
|
+
let ir = new IR(`__PARAM_${nPosParams - 1}`)
|
|
281
|
+
|
|
282
|
+
switch (purpose) {
|
|
283
|
+
case "spending":
|
|
284
|
+
ir = new IR([
|
|
285
|
+
new IR(
|
|
286
|
+
`__helios__scriptcontext__get_current_validator_hash(`
|
|
287
|
+
),
|
|
288
|
+
ir,
|
|
289
|
+
new IR(`)()`)
|
|
290
|
+
])
|
|
291
|
+
break
|
|
292
|
+
case "minting":
|
|
293
|
+
ir = new IR([
|
|
294
|
+
new IR(
|
|
295
|
+
`__helios__scriptcontext__get_current_minting_policy_hash(`
|
|
296
|
+
),
|
|
297
|
+
ir,
|
|
298
|
+
new IR(`)()`)
|
|
299
|
+
])
|
|
300
|
+
break
|
|
301
|
+
default:
|
|
302
|
+
throw new Error("unhandled purpose")
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
extra.set(key, ir)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// inject hashes of other validators
|
|
309
|
+
Object.entries(options.hashDependencies ?? {}).forEach(
|
|
310
|
+
([depName, dep]) => {
|
|
311
|
+
extra.set(`__helios__scripts__${depName}`, new IR(`#${dep}`))
|
|
312
|
+
}
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
return extra
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @param {string} main
|
|
320
|
+
* @param {string[]} modules
|
|
321
|
+
* @param {CompileOptions} options
|
|
322
|
+
* @returns {CompileOutput}
|
|
323
|
+
*/
|
|
324
|
+
compile(main, modules, options) {
|
|
325
|
+
const [purpose, name] = readHeader(main)
|
|
326
|
+
|
|
327
|
+
// use `Program.newInternal()` instead of `Program.new()` so we can inject custom IR before finally compiling to a UplcProgram
|
|
328
|
+
const program = this.lib.Program.newInternal(
|
|
329
|
+
main,
|
|
330
|
+
modules,
|
|
331
|
+
options.allValidatorHashTypes,
|
|
332
|
+
{
|
|
333
|
+
allowPosParams: false,
|
|
334
|
+
invertEntryPoint: true
|
|
335
|
+
}
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
const extra = this.generateExtraIRDefinitions(
|
|
339
|
+
name,
|
|
340
|
+
purpose,
|
|
341
|
+
program.nPosParams,
|
|
342
|
+
options
|
|
343
|
+
)
|
|
344
|
+
const optimize = options.optimize
|
|
345
|
+
const ir = program.toIR(new this.lib.ToIRContext(optimize), extra)
|
|
346
|
+
|
|
347
|
+
const irProgram =
|
|
348
|
+
program.nPosParams > 0
|
|
349
|
+
? this.lib.IRParametricProgram.new(
|
|
350
|
+
ir,
|
|
351
|
+
purpose,
|
|
352
|
+
program.nPosParams,
|
|
353
|
+
optimize
|
|
354
|
+
)
|
|
355
|
+
: this.lib.IRProgram.new(ir, purpose, optimize)
|
|
356
|
+
|
|
357
|
+
const cborHex = bytesToHex(irProgram.toUplc().toCbor())
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
prettyIR: irProgram.program.annotate(),
|
|
361
|
+
cborHex
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
164
365
|
/**
|
|
165
366
|
* @private
|
|
166
|
-
* @param {
|
|
167
|
-
* @param {
|
|
168
|
-
* @param {{[name: string]:
|
|
367
|
+
* @param {string[]} validators
|
|
368
|
+
* @param {string[]} modules
|
|
369
|
+
* @param {{[name: string]: ScriptHashType}} validatorTypes
|
|
169
370
|
* @returns {Program[]}
|
|
170
371
|
*/
|
|
171
372
|
createPrograms(validators, modules, validatorTypes) {
|
|
172
|
-
const moduleSrcs = modules.map((m) => m.sourceCode)
|
|
173
|
-
|
|
174
373
|
return validators.map((v) =>
|
|
175
|
-
this.lib.Program.newInternal(
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
{
|
|
180
|
-
allowPosParams: false,
|
|
181
|
-
invertEntryPoint: true
|
|
182
|
-
}
|
|
183
|
-
)
|
|
374
|
+
this.lib.Program.newInternal(v, modules, validatorTypes, {
|
|
375
|
+
allowPosParams: false,
|
|
376
|
+
invertEntryPoint: true
|
|
377
|
+
})
|
|
184
378
|
)
|
|
185
379
|
}
|
|
186
380
|
|
|
187
381
|
/**
|
|
188
382
|
* @private
|
|
189
383
|
* @param {Program} program
|
|
190
|
-
* @param {{[name: string]:
|
|
384
|
+
* @param {{[name: string]: ScriptHashType}} validatorTypes
|
|
191
385
|
* @returns {IR}
|
|
192
386
|
*/
|
|
193
387
|
toTestIR(program, validatorTypes) {
|
|
@@ -206,7 +400,7 @@ export class Lib_v0_16 {
|
|
|
206
400
|
/**
|
|
207
401
|
* @private
|
|
208
402
|
* @param {Program[]} validators
|
|
209
|
-
* @param {{[name: string]:
|
|
403
|
+
* @param {{[name: string]: ScriptHashType}} validatorTypes
|
|
210
404
|
* @returns {DagDependencies}
|
|
211
405
|
*/
|
|
212
406
|
buildDagDependencies(validators, validatorTypes) {
|
|
@@ -227,84 +421,7 @@ export class Lib_v0_16 {
|
|
|
227
421
|
}
|
|
228
422
|
|
|
229
423
|
/**
|
|
230
|
-
* @
|
|
231
|
-
* @param {{[name: string]: SourceDetails}} modules
|
|
232
|
-
* @returns {{modules: {[name: string]: ModuleDetails}, validators: {[name: string]: ValidatorDetails}}}
|
|
233
|
-
*/
|
|
234
|
-
typeCheck(validators, modules) {
|
|
235
|
-
const validatorTypes = Object.fromEntries(
|
|
236
|
-
Object.values(validators).map((v) => [
|
|
237
|
-
v.name,
|
|
238
|
-
this.getValidatorType(v.purpose)
|
|
239
|
-
])
|
|
240
|
-
)
|
|
241
|
-
|
|
242
|
-
// create validator programs
|
|
243
|
-
let validatorPrograms = this.createPrograms(
|
|
244
|
-
Object.values(validators),
|
|
245
|
-
Object.values(modules),
|
|
246
|
-
validatorTypes
|
|
247
|
-
)
|
|
248
|
-
|
|
249
|
-
// build dag
|
|
250
|
-
const dag = this.buildDagDependencies(validatorPrograms, validatorTypes)
|
|
251
|
-
|
|
252
|
-
// sort the validators according to the dag, a valid order should exist
|
|
253
|
-
// validatorPrograms = this.sortValidators(validatorPrograms, dag)
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* @type {{[name: string]: ValidatorDetails}}
|
|
257
|
-
*/
|
|
258
|
-
const validatorDetails = {}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @type {{[name: string]: ModuleDetails}}
|
|
262
|
-
*/
|
|
263
|
-
const moduleDetails = {}
|
|
264
|
-
|
|
265
|
-
for (let v of validatorPrograms) {
|
|
266
|
-
const hashDependencies = dag[v.name]
|
|
267
|
-
const allModules = v.mainImportedModules
|
|
268
|
-
const moduleDepedencies = v.mainModule
|
|
269
|
-
.filterDependencies(allModules)
|
|
270
|
-
.map((m) => m.name.value)
|
|
271
|
-
const purpose = validators[v.name].purpose
|
|
272
|
-
|
|
273
|
-
validatorDetails[v.name] = {
|
|
274
|
-
name: v.name,
|
|
275
|
-
purpose: validators[v.name].purpose,
|
|
276
|
-
sourceCode: validators[v.name].sourceCode,
|
|
277
|
-
hashDependencies: hashDependencies,
|
|
278
|
-
moduleDepedencies: moduleDepedencies,
|
|
279
|
-
types: {},
|
|
280
|
-
Redeemer: this.getInternalTypeDetails(
|
|
281
|
-
v.mainArgTypes[purpose == "spending" ? 1 : 0]
|
|
282
|
-
),
|
|
283
|
-
Datum:
|
|
284
|
-
purpose == "spending"
|
|
285
|
-
? this.getInternalTypeDetails(v.mainArgTypes[0])
|
|
286
|
-
: undefined
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
for (let m of v.mainImportedModules) {
|
|
290
|
-
if (!(m.name.value in moduleDetails)) {
|
|
291
|
-
moduleDetails[m.name.value] = {
|
|
292
|
-
name: m.name.value,
|
|
293
|
-
purpose: "module",
|
|
294
|
-
sourceCode: modules[m.name.value].sourceCode,
|
|
295
|
-
moduleDepedencies: m
|
|
296
|
-
.filterDependencies(allModules)
|
|
297
|
-
.map((m) => m.name.value),
|
|
298
|
-
types: {} // not yet exported
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return { modules: moduleDetails, validators: validatorDetails }
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
424
|
+
* @private
|
|
308
425
|
* @param {TypeSchema} it
|
|
309
426
|
* @returns {InternalTypeDetails}
|
|
310
427
|
*/
|
|
@@ -341,6 +458,9 @@ export class Lib_v0_16 {
|
|
|
341
458
|
}
|
|
342
459
|
|
|
343
460
|
/**
|
|
461
|
+
* Tries to convert a compiler type into TypeSchema
|
|
462
|
+
* TODO: make sure it works for all structs and enums
|
|
463
|
+
* @private
|
|
344
464
|
* @param {DataType} dt
|
|
345
465
|
* @returns {InternalTypeDetails}
|
|
346
466
|
*/
|
|
@@ -357,135 +477,4 @@ export class Lib_v0_16 {
|
|
|
357
477
|
}
|
|
358
478
|
}
|
|
359
479
|
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* @param {string[]} modules
|
|
363
|
-
* @returns {string[]}
|
|
364
|
-
*/
|
|
365
|
-
sortModules(modules) {
|
|
366
|
-
if (!this.options?.moduleOrder) {
|
|
367
|
-
return modules
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const n = modules.length
|
|
371
|
-
const tmp = modules.slice()
|
|
372
|
-
|
|
373
|
-
tmp.sort((a, b) => {
|
|
374
|
-
const nameA = readHeader(a)[1]
|
|
375
|
-
const nameB = readHeader(b)[1]
|
|
376
|
-
|
|
377
|
-
let iA = this.options.moduleOrder?.indexOf(nameA)
|
|
378
|
-
let iB = this.options.moduleOrder?.indexOf(nameB)
|
|
379
|
-
|
|
380
|
-
if (iA === undefined || iA == -1) {
|
|
381
|
-
iA = n
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
if (iB === undefined || iB == -1) {
|
|
385
|
-
iB = n
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
return iA - iB
|
|
389
|
-
})
|
|
390
|
-
|
|
391
|
-
return tmp
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* @param {string} main
|
|
396
|
-
* @param {string[]} modules
|
|
397
|
-
* @param {CompileOptions} options
|
|
398
|
-
* @returns {CompileOutput}
|
|
399
|
-
*/
|
|
400
|
-
compile(main, modules, options) {
|
|
401
|
-
const [purpose, name] = readHeader(main)
|
|
402
|
-
const otherValidators = options.otherValidators ?? {}
|
|
403
|
-
const otherValidatorTypes = options.allValidatorTypes
|
|
404
|
-
|
|
405
|
-
modules = this.sortModules(modules)
|
|
406
|
-
|
|
407
|
-
const program = this.lib.Program.newInternal(
|
|
408
|
-
main,
|
|
409
|
-
modules,
|
|
410
|
-
otherValidatorTypes,
|
|
411
|
-
{
|
|
412
|
-
allowPosParams: false,
|
|
413
|
-
invertEntryPoint: true
|
|
414
|
-
}
|
|
415
|
-
)
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* @type {Map<any, any>}
|
|
419
|
-
*/
|
|
420
|
-
const extra = new Map()
|
|
421
|
-
const IR = this.lib.IR
|
|
422
|
-
|
|
423
|
-
if (options.ownHash) {
|
|
424
|
-
const key = `__helios__scripts__${name}`
|
|
425
|
-
|
|
426
|
-
extra.set(key, new IR(`#${options.ownHash}`))
|
|
427
|
-
} else if (options.dependsOnOwnHash) {
|
|
428
|
-
const key = `__helios__scripts__${name}`
|
|
429
|
-
let ir = new IR(`__PARAM_${program.nPosParams - 1}`)
|
|
430
|
-
|
|
431
|
-
switch (purpose) {
|
|
432
|
-
case "spending":
|
|
433
|
-
ir = new IR([
|
|
434
|
-
new IR(
|
|
435
|
-
`__helios__scriptcontext__get_current_validator_hash(`
|
|
436
|
-
),
|
|
437
|
-
ir,
|
|
438
|
-
new IR(`)()`)
|
|
439
|
-
])
|
|
440
|
-
break
|
|
441
|
-
case "minting":
|
|
442
|
-
ir = new IR([
|
|
443
|
-
new IR(
|
|
444
|
-
`__helios__scriptcontext__get_current_minting_policy_hash(`
|
|
445
|
-
),
|
|
446
|
-
ir,
|
|
447
|
-
new IR(`)()`)
|
|
448
|
-
])
|
|
449
|
-
break
|
|
450
|
-
default:
|
|
451
|
-
throw new Error("unhandled purpose")
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
extra.set(key, ir)
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
for (let other in otherValidators) {
|
|
458
|
-
const key = `__helios__scripts__${other}`
|
|
459
|
-
|
|
460
|
-
extra.set(key, new IR(`#${otherValidators[other].hash}`))
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const optimize = options.optimize
|
|
464
|
-
const ir = program.toIR(new this.lib.ToIRContext(optimize), extra)
|
|
465
|
-
|
|
466
|
-
if (program.nPosParams > 0) {
|
|
467
|
-
const irProgram = this.lib.IRParametricProgram.new(
|
|
468
|
-
ir,
|
|
469
|
-
purpose,
|
|
470
|
-
program.nPosParams,
|
|
471
|
-
optimize
|
|
472
|
-
)
|
|
473
|
-
|
|
474
|
-
const cborHex = bytesToHex(irProgram.toUplc().toCbor())
|
|
475
|
-
|
|
476
|
-
return {
|
|
477
|
-
prettyIR: irProgram.program.annotate(),
|
|
478
|
-
cborHex
|
|
479
|
-
}
|
|
480
|
-
} else {
|
|
481
|
-
const irProgram = this.lib.IRProgram.new(ir, purpose, optimize)
|
|
482
|
-
|
|
483
|
-
const cborHex = bytesToHex(irProgram.toUplc().toCbor())
|
|
484
|
-
|
|
485
|
-
return {
|
|
486
|
-
prettyIR: irProgram.program.annotate(),
|
|
487
|
-
cborHex
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
480
|
}
|