@helios-lang/contract-utils 0.1.20 → 0.1.23

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 (99) hide show
  1. package/examples/multi-validator/README.md +5 -0
  2. package/examples/{spending → multi-validator}/contract.js +7 -2
  3. package/examples/{spending → multi-validator}/index.d.ts +1 -1
  4. package/examples/{spending → multi-validator}/index.js +3 -3
  5. package/examples/{spending → multi-validator}/spending.hl +3 -2
  6. package/package.json +5 -5
  7. package/src/{runtime → cast}/Cast.js +31 -8
  8. package/src/cast/index.js +12 -0
  9. package/src/codegen/LoadedModule.js +9 -0
  10. package/src/codegen/{TypescriptWriter.js → LoadedScriptsWriter.js} +117 -107
  11. package/src/codegen/LoadedValidator.js +23 -0
  12. package/src/codegen/{Module.js → TypeCheckedModule.js} +3 -3
  13. package/src/codegen/{Validator.js → TypeCheckedValidator.js} +5 -8
  14. package/src/codegen/TypeSchema.js +4 -0
  15. package/src/codegen/index.js +5 -4
  16. package/src/compiler/CompilerLib.js +51 -0
  17. package/src/{lib/Libg_v0_16.js → compiler/CompilerLib_v0_16.js} +243 -255
  18. package/src/compiler/index.js +6 -0
  19. package/src/compiler/ops.js +137 -0
  20. package/src/context/ContractContext.js +78 -0
  21. package/src/context/ContractContextBuilder.js +69 -0
  22. package/src/context/DagCompiler.js +229 -0
  23. package/src/context/index.js +1 -0
  24. package/src/hl2ts.js +31 -59
  25. package/src/index.js +12 -1
  26. package/types/cast/Cast.d.ts +32 -0
  27. package/types/cast/Cast.d.ts.map +1 -0
  28. package/types/cast/PermissiveType.d.ts +3 -0
  29. package/types/cast/PermissiveType.d.ts.map +1 -0
  30. package/types/cast/StrictType.d.ts +3 -0
  31. package/types/cast/StrictType.d.ts.map +1 -0
  32. package/types/cast/index.d.ts +5 -0
  33. package/types/cast/index.d.ts.map +1 -0
  34. package/types/codegen/LoadedModule.d.ts +6 -0
  35. package/types/codegen/LoadedModule.d.ts.map +1 -0
  36. package/types/codegen/LoadedScriptsWriter.d.ts +56 -0
  37. package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -0
  38. package/types/codegen/LoadedValidator.d.ts +16 -0
  39. package/types/codegen/LoadedValidator.d.ts.map +1 -0
  40. package/types/codegen/TypeCheckedModule.d.ts +11 -0
  41. package/types/codegen/TypeCheckedModule.d.ts.map +1 -0
  42. package/types/codegen/TypeCheckedValidator.d.ts +8 -0
  43. package/types/codegen/TypeCheckedValidator.d.ts.map +1 -0
  44. package/types/codegen/TypeSchema.d.ts +10 -0
  45. package/types/codegen/TypeSchema.d.ts.map +1 -1
  46. package/types/codegen/TypescriptWriter.d.ts +4 -4
  47. package/types/codegen/Validator.d.ts +1 -1
  48. package/types/codegen/index.d.ts +5 -4
  49. package/types/codegen/index.d.ts.map +1 -1
  50. package/types/compiler/CompilerLib.d.ts +45 -0
  51. package/types/compiler/CompilerLib.d.ts.map +1 -0
  52. package/types/compiler/CompilerLib_v0_16.d.ts +270 -0
  53. package/types/compiler/CompilerLib_v0_16.d.ts.map +1 -0
  54. package/types/compiler/index.d.ts +4 -0
  55. package/types/compiler/index.d.ts.map +1 -0
  56. package/types/compiler/ops.d.ts +24 -0
  57. package/types/compiler/ops.d.ts.map +1 -0
  58. package/types/context/ContractContext.d.ts +23 -0
  59. package/types/context/ContractContext.d.ts.map +1 -0
  60. package/types/context/ContractContextBuilder.d.ts +53 -0
  61. package/types/context/ContractContextBuilder.d.ts.map +1 -0
  62. package/types/context/DagCompiler.d.ts +64 -0
  63. package/types/context/DagCompiler.d.ts.map +1 -0
  64. package/types/context/index.d.ts +2 -0
  65. package/types/context/index.d.ts.map +1 -0
  66. package/types/hl2ts.d.ts +8 -4
  67. package/types/hl2ts.d.ts.map +1 -1
  68. package/types/index.d.ts +4 -1
  69. package/types/index.d.ts.map +1 -1
  70. package/types/lib/Lib.d.ts +13 -71
  71. package/types/lib/Lib.d.ts.map +1 -1
  72. package/types/lib/Libg_v0_16.d.ts +53 -52
  73. package/types/lib/Libg_v0_16.d.ts.map +1 -1
  74. package/types/lib/index.d.ts +0 -1
  75. package/types/lib/index.d.ts.map +1 -1
  76. package/types/lib/load.d.ts +1 -7
  77. package/types/lib/load.d.ts.map +1 -1
  78. package/types/runtime/ContractContext.d.ts +22 -43
  79. package/types/runtime/ContractContext.d.ts.map +1 -1
  80. package/types/runtime/ContractContextBuilder.d.ts +15 -123
  81. package/types/runtime/ContractContextBuilder.d.ts.map +1 -1
  82. package/types/runtime/DagCompiler.d.ts +64 -0
  83. package/types/runtime/DagCompiler.d.ts.map +1 -0
  84. package/types/runtime/PermissiveType.d.ts +1 -1
  85. package/types/runtime/StrictType.d.ts +1 -1
  86. package/types/runtime/index.d.ts +0 -2
  87. package/src/codegen/Writer.js +0 -37
  88. package/src/lib/Lib.js +0 -56
  89. package/src/lib/index.js +0 -6
  90. package/src/lib/load.js +0 -26
  91. package/src/runtime/ContractContext.js +0 -35
  92. package/src/runtime/ContractContextBuilder.js +0 -340
  93. package/src/runtime/index.js +0 -3
  94. /package/examples/{spending → multi-validator}/minting.hl +0 -0
  95. /package/examples/{spending → multi-validator}/package-lock.json +0 -0
  96. /package/examples/{spending → multi-validator}/package.json +0 -0
  97. /package/examples/{spending → multi-validator}/utils.hl +0 -0
  98. /package/src/{runtime → cast}/PermissiveType.js +0 -0
  99. /package/src/{runtime → cast}/StrictType.js +0 -0
@@ -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
File without changes