@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.
Files changed (104) hide show
  1. package/examples/multi-validator/README.md +13 -0
  2. package/examples/multi-validator/contract.js +28 -0
  3. package/examples/{spending → multi-validator}/index.d.ts +3 -3
  4. package/examples/multi-validator/index.js +27 -0
  5. package/examples/multi-validator/minting.hl +5 -0
  6. package/examples/{spending → multi-validator}/package-lock.json +6 -15
  7. package/examples/{spending → multi-validator}/package.json +0 -1
  8. package/examples/multi-validator/spending.hl +18 -0
  9. package/examples/multi-validator/utils.hl +9 -0
  10. package/package.json +7 -6
  11. package/src/{runtime → cast}/Cast.js +31 -8
  12. package/src/cast/index.js +12 -0
  13. package/src/codegen/LoadedModule.js +9 -0
  14. package/src/codegen/{TypescriptWriter.js → LoadedScriptsWriter.js} +149 -108
  15. package/src/codegen/LoadedValidator.js +23 -0
  16. package/src/codegen/{Module.js → TypeCheckedModule.js} +3 -3
  17. package/src/codegen/{Validator.js → TypeCheckedValidator.js} +5 -8
  18. package/src/codegen/TypeSchema.js +6 -2
  19. package/src/codegen/index.js +5 -4
  20. package/src/compiler/CompilerLib.js +51 -0
  21. package/src/{lib/Libg_v0_16.js → compiler/CompilerLib_v0_16.js} +244 -255
  22. package/src/compiler/index.js +6 -0
  23. package/src/compiler/ops.js +137 -0
  24. package/src/context/ContractContext.js +78 -0
  25. package/src/context/ContractContextBuilder.js +69 -0
  26. package/src/context/DagCompiler.js +229 -0
  27. package/src/context/index.js +1 -0
  28. package/src/hl2ts.js +116 -104
  29. package/src/index.js +12 -1
  30. package/types/cast/Cast.d.ts +32 -0
  31. package/types/cast/Cast.d.ts.map +1 -0
  32. package/types/cast/PermissiveType.d.ts +3 -0
  33. package/types/cast/PermissiveType.d.ts.map +1 -0
  34. package/types/cast/StrictType.d.ts +3 -0
  35. package/types/cast/StrictType.d.ts.map +1 -0
  36. package/types/cast/index.d.ts +5 -0
  37. package/types/cast/index.d.ts.map +1 -0
  38. package/types/codegen/LoadedModule.d.ts +6 -0
  39. package/types/codegen/LoadedModule.d.ts.map +1 -0
  40. package/types/codegen/LoadedScriptsWriter.d.ts +57 -0
  41. package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -0
  42. package/types/codegen/LoadedValidator.d.ts +16 -0
  43. package/types/codegen/LoadedValidator.d.ts.map +1 -0
  44. package/types/codegen/TypeCheckedModule.d.ts +11 -0
  45. package/types/codegen/TypeCheckedModule.d.ts.map +1 -0
  46. package/types/codegen/TypeCheckedValidator.d.ts +8 -0
  47. package/types/codegen/TypeCheckedValidator.d.ts.map +1 -0
  48. package/types/codegen/TypeSchema.d.ts +10 -0
  49. package/types/codegen/TypeSchema.d.ts.map +1 -1
  50. package/types/codegen/TypescriptWriter.d.ts +4 -4
  51. package/types/codegen/Validator.d.ts +1 -1
  52. package/types/codegen/index.d.ts +5 -4
  53. package/types/codegen/index.d.ts.map +1 -1
  54. package/types/compiler/CompilerLib.d.ts +45 -0
  55. package/types/compiler/CompilerLib.d.ts.map +1 -0
  56. package/types/compiler/CompilerLib_v0_16.d.ts +271 -0
  57. package/types/compiler/CompilerLib_v0_16.d.ts.map +1 -0
  58. package/types/compiler/index.d.ts +4 -0
  59. package/types/compiler/index.d.ts.map +1 -0
  60. package/types/compiler/ops.d.ts +24 -0
  61. package/types/compiler/ops.d.ts.map +1 -0
  62. package/types/context/ContractContext.d.ts +23 -0
  63. package/types/context/ContractContext.d.ts.map +1 -0
  64. package/types/context/ContractContextBuilder.d.ts +53 -0
  65. package/types/context/ContractContextBuilder.d.ts.map +1 -0
  66. package/types/context/DagCompiler.d.ts +64 -0
  67. package/types/context/DagCompiler.d.ts.map +1 -0
  68. package/types/context/index.d.ts +2 -0
  69. package/types/context/index.d.ts.map +1 -0
  70. package/types/hl2ts.d.ts +9 -4
  71. package/types/hl2ts.d.ts.map +1 -1
  72. package/types/index.d.ts +4 -1
  73. package/types/index.d.ts.map +1 -1
  74. package/types/lib/Lib.d.ts +13 -71
  75. package/types/lib/Lib.d.ts.map +1 -1
  76. package/types/lib/Libg_v0_16.d.ts +53 -52
  77. package/types/lib/Libg_v0_16.d.ts.map +1 -1
  78. package/types/lib/index.d.ts +0 -1
  79. package/types/lib/index.d.ts.map +1 -1
  80. package/types/lib/load.d.ts +1 -7
  81. package/types/lib/load.d.ts.map +1 -1
  82. package/types/runtime/ContractContext.d.ts +22 -43
  83. package/types/runtime/ContractContext.d.ts.map +1 -1
  84. package/types/runtime/ContractContextBuilder.d.ts +15 -123
  85. package/types/runtime/ContractContextBuilder.d.ts.map +1 -1
  86. package/types/runtime/DagCompiler.d.ts +64 -0
  87. package/types/runtime/DagCompiler.d.ts.map +1 -0
  88. package/types/runtime/PermissiveType.d.ts +1 -1
  89. package/types/runtime/StrictType.d.ts +1 -1
  90. package/types/runtime/index.d.ts +0 -2
  91. package/examples/spending/contract.js +0 -16
  92. package/examples/spending/index.js +0 -27
  93. package/examples/spending/minting.hl +0 -5
  94. package/examples/spending/spending.hl +0 -17
  95. package/examples/spending/utils.hl +0 -5
  96. package/src/codegen/Writer.js +0 -37
  97. package/src/lib/Lib.js +0 -56
  98. package/src/lib/index.js +0 -6
  99. package/src/lib/load.js +0 -26
  100. package/src/runtime/ContractContext.js +0 -35
  101. package/src/runtime/ContractContextBuilder.js +0 -340
  102. package/src/runtime/index.js +0 -3
  103. /package/src/{runtime → cast}/PermissiveType.js +0 -0
  104. /package/src/{runtime → cast}/StrictType.js +0 -0
@@ -1,37 +0,0 @@
1
- export class Writer {
2
- /**
3
- * @private
4
- * @readonly
5
- * @type {string[]}
6
- */
7
- parts
8
-
9
- constructor() {
10
- this.parts = []
11
- }
12
-
13
- /**
14
- * @param {string} part
15
- * @returns {Writer}
16
- */
17
- write(part) {
18
- this.parts.push(part)
19
- return this
20
- }
21
-
22
- /**
23
- * @param {string} line
24
- * @returns {Writer}
25
- */
26
- writeLine(line) {
27
- this.parts.push(`${line}\n`)
28
- return this
29
- }
30
-
31
- /**
32
- * @returns {string}
33
- */
34
- finalize() {
35
- return this.parts.join("")
36
- }
37
- }
package/src/lib/Lib.js DELETED
@@ -1,56 +0,0 @@
1
- /**
2
- * @typedef {{
3
- * allValidatorTypes: {[name: string]: any},
4
- * otherValidators: {[name: string]: {purpose: string, hash: string}},
5
- * dependsOnOwnHash?: boolean,
6
- * ownHash?: string,
7
- * optimize: boolean
8
- * }} CompileOptions
9
- */
10
-
11
- /**
12
- * @typedef {{
13
- * cborHex: string
14
- * prettyIR: string
15
- * }} CompileOutput
16
- */
17
-
18
- /**
19
- * @typedef {{
20
- * isMainnet?: boolean
21
- * checkCasts?: boolean
22
- * moduleOrder?: string[]
23
- * }} LibOptions
24
- */
25
-
26
- /**
27
- * @typedef {{
28
- * version: string
29
- * getValidatorType: (purpose: string) => any
30
- * typeCheck: (validators: {[name: string]: SourceDetails}, modules: {[name: string]: SourceDetails}) => ({
31
- * modules: {[name: string]: ModuleDetails}, validators: {[name: string]: ValidatorDetails}})
32
- * compile: (main: string, modules: string[], options: CompileOptions) => CompileOutput
33
- * }} Lib
34
- */
35
-
36
- /**
37
- * @typedef {import("../codegen/index.js").TypeSchema} InternalTypeDetails
38
- * @typedef {import("../codegen/index.js").Module} ModuleDetails
39
- * @typedef {import("../codegen/index.js").Validator} ValidatorDetails
40
- */
41
-
42
- /**
43
- * @typedef {{
44
- * name: string
45
- * purpose: string
46
- * sourceCode: string
47
- * }} SourceDetails
48
- */
49
-
50
- /**
51
- * @param {{VERSION: string}} lib
52
- * @returns {number[]}
53
- */
54
- export function getVersion(lib) {
55
- return lib.VERSION.split(".").map((v) => Number(v))
56
- }
package/src/lib/index.js DELETED
@@ -1,6 +0,0 @@
1
- export { loadLibrary } from "./load.js"
2
-
3
- /**
4
- * @typedef {import("./Lib.js").Lib} Lib
5
- * @typedef {import("./Lib.js").LibOptions} LibOptions
6
- */
package/src/lib/load.js DELETED
@@ -1,26 +0,0 @@
1
- import * as lib from "@helios-lang/compiler"
2
- import { getVersion } from "./Lib.js"
3
- import { Lib_v0_16 } from "./Libg_v0_16.js"
4
-
5
- /**
6
- * @typedef {import("./Lib.js").Lib} Lib
7
- * @typedef {import("./Lib.js").LibOptions} LibOptions
8
- */
9
-
10
- /**
11
- * @param {LibOptions} options
12
- * @returns {Lib}
13
- */
14
- export function loadLibrary(options = {}) {
15
- const [major, minor] = getVersion(lib)
16
-
17
- switch (major) {
18
- case 0:
19
- switch (minor) {
20
- case 16:
21
- return new Lib_v0_16(lib, options)
22
- }
23
- default:
24
- throw new Error(`compiler version ${lib.VERSION} not supported`)
25
- }
26
- }
@@ -1,35 +0,0 @@
1
- import { ScriptHash } from "@helios-lang/ledger"
2
- import { UplcProgramV2 } from "@helios-lang/uplc"
3
- import { Cast } from "./Cast.js"
4
-
5
- /**
6
- * @typedef {{
7
- * $name: string
8
- * $purpose: string
9
- * $program: {
10
- * optimized: UplcProgramV2
11
- * unoptimized: UplcProgramV2
12
- * }
13
- * $hash: ScriptHash
14
- * $Redeemer: Cast<any, any>
15
- * $Datum?: Cast<any, any>
16
- * }} Validator
17
- */
18
-
19
- /**
20
- * @template {{[name: string]: Validator}} T
21
- */
22
- export class ContractContext {
23
- /**
24
- * @readonly
25
- * @type {T}
26
- */
27
- validators
28
-
29
- /**
30
- * @param {T} validators
31
- */
32
- constructor(validators) {
33
- this.validators = validators
34
- }
35
- }
@@ -1,340 +0,0 @@
1
- import { bytesToHex } from "@helios-lang/codec-utils"
2
- import {
3
- ScriptHash,
4
- StakingValidatorHash,
5
- ValidatorHash,
6
- MintingPolicyHash
7
- } from "@helios-lang/ledger"
8
- import { None, expectSome } from "@helios-lang/type-utils"
9
- import { UplcProgramV2 } from "@helios-lang/uplc"
10
- import { loadLibrary } from "../lib/index.js"
11
- import { Cast } from "./Cast.js"
12
- import { ContractContext } from "./ContractContext.js"
13
-
14
- /**
15
- * @typedef {import("../lib/index.js").Lib} Lib
16
- * @typedef {import("../lib/index.js").LibOptions} LibOptions
17
- * @typedef {import("./ContractContext.js").Validator} CompiledValidator
18
- */
19
-
20
- /**
21
- * @template TRedeemerStrict
22
- * @template TRedeemerPermissive
23
- * @typedef {import("@helios-lang/ledger").MintingContext<TRedeemerStrict, TRedeemerPermissive>} MintingContext
24
- */
25
-
26
- /**
27
- * @template TDatumStrict
28
- * @template TDatumPermissive
29
- * @template TRedeemerStrict
30
- * @template TRedeemerPermissive
31
- * @typedef {import("@helios-lang/ledger").SpendingContext<TDatumStrict, TDatumPermissive, TRedeemerStrict, TRedeemerPermissive>} SpendingContext
32
- */
33
-
34
- /**
35
- * @template TRedeemerStrict
36
- * @template TRedeemerPermissive
37
- * @typedef {import("@helios-lang/ledger").StakingContext<TRedeemerStrict, TRedeemerPermissive>} StakingContext
38
- */
39
-
40
- /**
41
- * @template {Cast} C
42
- * @typedef {import("./PermissiveType.js").PermissiveType<C>} PermissiveType
43
- */
44
-
45
- /**
46
- * @template {Cast} C
47
- * @typedef {import("./StrictType.js").StrictType<C>} StrictType
48
- */
49
-
50
- /**
51
- * @typedef {{
52
- * $name: string
53
- * $sourceCode: string
54
- * $dependencies: Module[]
55
- * }} Module
56
- */
57
-
58
- /**
59
- * @typedef {{
60
- * $name: string
61
- * $purpose: string
62
- * $sourceCode: string
63
- * $dependencies: Module[]
64
- * $hashDependencies: Validator[]
65
- * $dependsOnOwnHash: boolean
66
- * $Redeemer: Cast<any, any>
67
- * $Datum?: Cast<any, any>
68
- * }} Validator
69
- */
70
-
71
- /**
72
- * @template {{[name: string]: Validator}} T
73
- */
74
- export class ContractContextBuilder {
75
- /**
76
- * @private
77
- * @readonly
78
- * @type {T}
79
- */
80
- validators
81
-
82
- /**
83
- * @private
84
- * @readonly
85
- * @type {Option<Module[]>}
86
- */
87
- forcedModules
88
-
89
- /**
90
- * @private
91
- * @param {T} validators
92
- * @param {Option<Module[]>} forcedModules
93
- */
94
- constructor(validators, forcedModules = None) {
95
- this.validators = validators
96
- this.forcedModules = forcedModules
97
- }
98
-
99
- /**
100
- * @returns {ContractContextBuilder<{}>}
101
- */
102
- static new() {
103
- return new ContractContextBuilder({})
104
- }
105
-
106
- /**
107
- * @param {Module[]} modules
108
- * @returns {ContractContextBuilder<T>}
109
- */
110
- setModules(modules) {
111
- return new ContractContextBuilder(this.validators, modules)
112
- }
113
-
114
- /**
115
- * @template {Validator} V
116
- * @param {V} validator
117
- * @returns {ContractContextBuilder<T & {
118
- * [K in V["$name"]]: V
119
- * }>}
120
- */
121
- with(validator) {
122
- return new ContractContextBuilder(
123
- {
124
- ...this.validators,
125
- [validator.$name]: validator
126
- },
127
- this.forcedModules
128
- )
129
- }
130
-
131
- /**
132
- * @private
133
- * @param {Validator} validator
134
- * @returns {Map<string, string>}
135
- */
136
- getModules(validator) {
137
- if (this.forcedModules) {
138
- return new Map(
139
- this.forcedModules.map((m) => [m.$name, m.$sourceCode])
140
- )
141
- }
142
-
143
- /**
144
- * @type {Map<string, string>}
145
- */
146
- const result = new Map()
147
- let stack = validator.$dependencies.slice()
148
- let m = stack.pop()
149
-
150
- while (m) {
151
- if (!result.has(m.$name)) {
152
- result.set(m.$name, m.$sourceCode)
153
-
154
- stack = stack.concat(m.$dependencies)
155
- }
156
-
157
- m = stack.pop()
158
- }
159
-
160
- return result
161
- }
162
-
163
- /**
164
- * @private
165
- * @param {Lib} lib
166
- * @returns {{[name: string]: any}}
167
- */
168
- getValidatorTypes(lib) {
169
- return Object.fromEntries(
170
- Object.entries(this.validators).map(([name, v]) => {
171
- return [name, lib.getValidatorType(v.$purpose)]
172
- })
173
- )
174
- }
175
-
176
- /**
177
- * @param {Option<{[name: string]: string}>} expectedHashes
178
- * @param {LibOptions} options
179
- * @returns {{
180
- * [K in keyof T]:
181
- * T[K]["$purpose"] extends "spending" ? ValidatorHash<
182
- * SpendingContext<
183
- * StrictType<T[K]["$Datum"]>,
184
- * PermissiveType<T[K]["$Datum"]>,
185
- * StrictType<T[K]["$Redeemer"]>,
186
- * PermissiveType<T[K]["$Redeemer"]>
187
- * >
188
- * > :
189
- * T[K]["$purpose"] extends "minting" ? MintingPolicyHash<
190
- * MintingContext<
191
- * StrictType<T[K]["$Redeemer"]>,
192
- * PermissiveType<T[K]["$Redeemer"]>
193
- * >
194
- * > :
195
- * T[K]["$purpose"] extends ("certifying" | "rewarding") ? StakingValidatorHash<
196
- * StakingContext<
197
- * StrictType<T[K]["$Redeemer"]>,
198
- * PermissiveType<T[K]["$Redeemer"]>
199
- * >
200
- * > : never
201
- * }}
202
- */
203
- build(expectedHashes = None, options = {}) {
204
- const lib = loadLibrary(options)
205
-
206
- const allValidatorTypes = this.getValidatorTypes(lib)
207
-
208
- /**
209
- * @type {{[name: string]: (
210
- * ValidatorHash<SpendingContext<any, any, any, any>> |
211
- * MintingPolicyHash<MintingContext<any, any>> |
212
- * StakingValidatorHash<StakingContext<any, any>>
213
- * )}}
214
- */
215
- const validators = {}
216
-
217
- /**
218
- * @returns {{[name: string]: {purpose: string, hash: string}}}
219
- */
220
- function getOtherValidators() {
221
- /**
222
- * @type {{[name: string]: {purpose: string, hash: string}}}
223
- */
224
- const otherValidators = {}
225
-
226
- for (let k in validators) {
227
- const v = validators[k]
228
-
229
- if (v instanceof ValidatorHash) {
230
- otherValidators[k] = {
231
- purpose: "spending",
232
- hash: v.toHex()
233
- }
234
- } else if (v instanceof MintingPolicyHash) {
235
- otherValidators[k] = {
236
- purpose: "minting",
237
- hash: v.toHex()
238
- }
239
- } else if (v instanceof StakingValidatorHash) {
240
- otherValidators[k] = {
241
- purpose: "staking",
242
- hash: v.toHex()
243
- }
244
- } else {
245
- throw new Error("unexpected ScriptHash")
246
- }
247
- }
248
-
249
- return otherValidators
250
- }
251
-
252
- /**
253
- * @param {Validator} validator
254
- */
255
- const buildValidator = (validator) => {
256
- const name = validator.$name
257
-
258
- if (name in validators) {
259
- return
260
- }
261
-
262
- for (let v of validator.$hashDependencies) {
263
- buildValidator(v)
264
- }
265
-
266
- const modules = this.getModules(validator)
267
-
268
- const { cborHex: optimizedCborHex, prettyIR } = lib.compile(
269
- validator.$sourceCode,
270
- Array.from(modules.values()),
271
- {
272
- optimize: true,
273
- otherValidators: getOtherValidators(),
274
- allValidatorTypes: allValidatorTypes,
275
- dependsOnOwnHash: validator.$dependsOnOwnHash
276
- }
277
- )
278
-
279
- const optimizedProgram = UplcProgramV2.fromCbor(optimizedCborHex)
280
-
281
- const ownHash = bytesToHex(optimizedProgram.hash())
282
-
283
- if (expectedHashes && name in expectedHashes) {
284
- if (expectedHashes[name] != ownHash) {
285
- console.log(prettyIR)
286
-
287
- throw new Error(
288
- `expected hash ${expectedHashes[name]} for validator ${name}, got ${ownHash}`
289
- )
290
- }
291
- }
292
-
293
- const purpose = validator.$purpose
294
-
295
- const { cborHex: unoptimizedCborHex } = lib.compile(
296
- validator.$sourceCode,
297
- Array.from(modules.values()),
298
- {
299
- optimize: false,
300
- otherValidators: getOtherValidators(),
301
- allValidatorTypes: allValidatorTypes,
302
- ownHash: validator.$dependsOnOwnHash ? ownHash : undefined
303
- }
304
- )
305
-
306
- const unoptimizedProgram =
307
- UplcProgramV2.fromCbor(unoptimizedCborHex)
308
-
309
- if (purpose == "spending") {
310
- validators[name] = new ValidatorHash(ownHash, {
311
- program: optimizedProgram.withAlt(unoptimizedProgram),
312
- datum: expectSome(validator.$Datum),
313
- redeemer: validator.$Redeemer
314
- })
315
- } else if (purpose == "minting") {
316
- validators[name] = new MintingPolicyHash(ownHash, {
317
- program: optimizedProgram.withAlt(unoptimizedProgram),
318
- redeemer: validator.$Redeemer
319
- })
320
- } else if (
321
- purpose == "staking" ||
322
- purpose == "certifying" ||
323
- purpose == "rewarding"
324
- ) {
325
- validators[name] = new StakingValidatorHash(ownHash, {
326
- program: optimizedProgram.withAlt(unoptimizedProgram),
327
- redeemer: validator.$Redeemer
328
- })
329
- } else {
330
- throw new Error("unhandled purpose")
331
- }
332
- }
333
-
334
- for (let k in this.validators) {
335
- buildValidator(this.validators[k])
336
- }
337
-
338
- return /** @type {any} */ (validators)
339
- }
340
- }
@@ -1,3 +0,0 @@
1
- export { Cast } from "./Cast.js"
2
- export { ContractContext } from "./ContractContext.js"
3
- export { ContractContextBuilder } from "./ContractContextBuilder.js"
File without changes
File without changes