@helios-lang/contract-utils 0.1.41 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/examples/multi-validator/contract.js +11 -7
  2. package/examples/multi-validator/index.d.ts +19 -9
  3. package/examples/multi-validator/index.js +38 -14
  4. package/examples/multi-validator/package-lock.json +79 -5
  5. package/examples/multi-validator/package.json +1 -0
  6. package/package.json +2 -2
  7. package/src/cast/Cast.js +57 -13
  8. package/src/cast/PermissiveType.js +15 -3
  9. package/src/cast/StrictType.js +15 -3
  10. package/src/cast/index.js +19 -2
  11. package/src/codegen/LoadedModule.js +9 -1
  12. package/src/codegen/LoadedScriptsWriter.js +90 -31
  13. package/src/codegen/LoadedValidator.js +28 -4
  14. package/src/context/ContractContext.js +54 -23
  15. package/src/context/ContractContextBuilder.js +109 -21
  16. package/src/context/DagCompiler.js +18 -7
  17. package/src/index.js +1 -11
  18. package/types/cast/Cast.d.ts +42 -4
  19. package/types/cast/Cast.d.ts.map +1 -1
  20. package/types/cast/PermissiveType.d.ts +3 -2
  21. package/types/cast/PermissiveType.d.ts.map +1 -1
  22. package/types/cast/StrictType.d.ts +3 -2
  23. package/types/cast/StrictType.d.ts.map +1 -1
  24. package/types/cast/index.d.ts +6 -2
  25. package/types/cast/index.d.ts.map +1 -1
  26. package/types/codegen/LoadedModule.d.ts +6 -1
  27. package/types/codegen/LoadedModule.d.ts.map +1 -1
  28. package/types/codegen/LoadedScriptsWriter.d.ts +13 -0
  29. package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -1
  30. package/types/codegen/LoadedValidator.d.ts +19 -5
  31. package/types/codegen/LoadedValidator.d.ts.map +1 -1
  32. package/types/context/ContractContext.d.ts +28 -11
  33. package/types/context/ContractContext.d.ts.map +1 -1
  34. package/types/context/ContractContextBuilder.d.ts +58 -19
  35. package/types/context/ContractContextBuilder.d.ts.map +1 -1
  36. package/types/context/DagCompiler.d.ts +17 -7
  37. package/types/context/DagCompiler.d.ts.map +1 -1
  38. package/types/index.d.ts +1 -3
  39. package/types/index.d.ts.map +1 -1
@@ -127,25 +127,76 @@ export class LoadedScriptsWriter {
127
127
  return this
128
128
  }
129
129
 
130
+ /**
131
+ * @param {string} path
132
+ * @param {string} typeName
133
+ * @param {string[]} typeParams
134
+ * @returns {LoadedScriptsWriter}
135
+ */
136
+ writeImportType(path, typeName, typeParams = []) {
137
+ this.definition.writeLine("/**")
138
+
139
+ for (let tp of typeParams) {
140
+ this.definition.writeLine(` * @template ${tp}`)
141
+ }
142
+
143
+ this.definition
144
+ .writeLine(
145
+ ` * @typedef {import("${path}").${typeName}${typeParams.length > 0 ? `<${typeParams.join(", ")}>` : ""}} ${typeName}`
146
+ )
147
+ .writeLine(" */")
148
+ ;[this.declaration, this.combined].forEach((w) => {
149
+ w.writeLine(`import type { ${typeName} } from "${path}";`)
150
+ })
151
+
152
+ return this
153
+ }
154
+
155
+ /**
156
+ * @param {string} path
157
+ * @param {string[]} objectNames
158
+ * @returns {LoadedScriptsWriter}
159
+ */
160
+ writeImport(path, ...objectNames) {
161
+ ;[this.definition, this.declaration, this.combined].forEach((w) => {
162
+ w.writeLine(`import { ${objectNames.join(", ")} } from "${path}";`)
163
+ })
164
+
165
+ return this
166
+ }
167
+
130
168
  /**
131
169
  * @private
132
170
  */
133
171
  writeHeaders() {
134
- this.definition.writeLine(
135
- 'import { Cast } from "@helios-lang/contract-utils";'
172
+ this.writeImport("@helios-lang/contract-utils", "Cast")
173
+ this.writeImport(
174
+ "@helios-lang/ledger",
175
+ "Address",
176
+ "AssetClass",
177
+ "DatumHash",
178
+ "MintingPolicyHash",
179
+ "PubKey",
180
+ "PubKeyHash",
181
+ "SpendingCredential",
182
+ "StakingCredential",
183
+ "StakingHash",
184
+ "StakingValidatorHash",
185
+ "TimeRange",
186
+ "TxId",
187
+ "TxOutputDatum",
188
+ "ValidatorHash",
189
+ "Value"
136
190
  )
137
- ;[this.declaration, this.combined].forEach((w) => {
138
- w.writeLine(
139
- 'import type { IntLike } from "@helios-lang/codec-utils";'
140
- )
141
- w.writeLine('import { Cast } from "@helios-lang/contract-utils";')
142
- w.writeLine(
143
- 'import type { TimeLike, UplcData } from "@helios-lang/ledger";'
144
- )
145
- w.writeLine(
146
- 'import { Address, AssetClass, DatumHash, MintingPolicyHash, PubKey, PubKeyHash, SpendingCredential, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value } from "@helios-lang/ledger";'
147
- )
148
- })
191
+ this.writeImportType("@helios-lang/codec-utils", "IntLike")
192
+ this.writeImportType("@helios-lang/contract-utils", "CastConfig")
193
+ this.writeImportType(
194
+ "@helios-lang/contract-utils",
195
+ "ConfigurableCast",
196
+ ["TStrict", "TPermissive"]
197
+ )
198
+ this.writeImportType("@helios-lang/ledger", "TimeLike")
199
+ this.writeImportType("@helios-lang/ledger", "UplcData")
149
200
  }
150
201
 
151
202
  /**
@@ -153,20 +204,28 @@ export class LoadedScriptsWriter {
153
204
  * @param {Record<string, TypeSchema>} types
154
205
  */
155
206
  writeTypes(types) {
207
+ ;[this.definition, this.declaration, this.combined].forEach((w) =>
208
+ w.write(" $types: {\n")
209
+ )
210
+
156
211
  for (let key in types) {
157
212
  const t = types[key]
158
213
  const tsTypes = genTypes(t)
159
214
 
160
215
  this.definition.write(
161
- ` ${key}: new Cast(${JSON.stringify(t)}),\n`
216
+ ` ${key}: (config) => /** @type {Cast<${tsTypes[0]}, ${tsTypes[1]}>} */ (new Cast(${JSON.stringify(t)}, config)),\n`
162
217
  )
163
218
  this.declaration.write(
164
- ` ${key}: Cast<${tsTypes[0]}, ${tsTypes[1]}>,\n`
219
+ ` ${key}: ConfigurableCast<${tsTypes[0]}, ${tsTypes[1]}>,\n`
165
220
  )
166
221
  this.combined.write(
167
- ` ${key}: new Cast<${tsTypes[0]}, ${tsTypes[1]}>(${JSON.stringify(t)}),\n`
222
+ ` ${key}: (config: CastConfig) => new Cast<${tsTypes[0]}, ${tsTypes[1]}>(${JSON.stringify(t)}, config),\n`
168
223
  )
169
224
  }
225
+
226
+ ;[this.definition, this.declaration, this.combined].forEach((w) =>
227
+ w.write(" },\n")
228
+ )
170
229
  }
171
230
 
172
231
  /**
@@ -175,10 +234,10 @@ export class LoadedScriptsWriter {
175
234
  */
176
235
  writeModule(m) {
177
236
  this.definition.write(`export const ${m.name} = {
178
- $name: "${m.name}",
179
- $purpose: "${m.purpose}",
237
+ $name: /** @type {const} */ ("${m.name}"),
238
+ $purpose: /** @type {const} */ ("${m.purpose}"),
180
239
  $sourceCode: ${JSON.stringify(m.sourceCode)},
181
- $dependencies: [${m.moduleDepedencies.join(", ")}],\n`)
240
+ $dependencies: /** @type {const} */ ([${m.moduleDepedencies.join(", ")}]),\n`)
182
241
 
183
242
  this.declaration.write(
184
243
  `export const ${m.name}: {
@@ -193,7 +252,7 @@ export class LoadedScriptsWriter {
193
252
  $name: "${m.name}" as const,
194
253
  $purpose: "${m.purpose}" as const,
195
254
  $sourceCode: ${JSON.stringify(m.sourceCode)} as string,
196
- $dependencies: [${m.moduleDepedencies.join(", ")}],\n`
255
+ $dependencies: [${m.moduleDepedencies.join(", ")}] as const,\n`
197
256
  )
198
257
 
199
258
  this.writeTypes(m.types)
@@ -213,14 +272,14 @@ export class LoadedScriptsWriter {
213
272
 
214
273
  this.definition.write(
215
274
  `export const ${v.name} = {
216
- $name: "${v.name}",
217
- $purpose: "${v.purpose}",
275
+ $name: /** @type {const} */ ("${v.name}"),
276
+ $purpose: /** @type {const} */ ("${v.purpose}"),
218
277
  $sourceCode: ${JSON.stringify(v.sourceCode)},
219
- $dependencies: [${v.moduleDepedencies.join(", ")}],
278
+ $dependencies: /** @type {const} */ ([${v.moduleDepedencies.join(", ")}]),
220
279
  $hashDependencies: [${v.hashDependencies.filter((d) => d != v.name).join(", ")}],
221
280
  $dependsOnOwnHash: ${v.hashDependencies.some((d) => d == v.name)},
222
- $Redeemer: new Cast(${JSON.stringify(v.Redeemer)}),
223
- ${datumTypes ? ` $Datum: new Cast(${JSON.stringify(v.Datum)}),\n` : ""}`
281
+ $Redeemer: (config) => /** @type {Cast<${redeemerTypes[0]}, ${redeemerTypes[1]}>} */ (new Cast(${JSON.stringify(v.Redeemer)}, config)),
282
+ ${datumTypes ? ` $Datum: (config) => /** @type {Cast<${datumTypes[0]}, ${datumTypes[1]}>} */ (new Cast(${JSON.stringify(v.Datum)}, config)),\n` : ""}`
224
283
  )
225
284
 
226
285
  this.declaration.write(
@@ -234,8 +293,8 @@ ${datumTypes ? ` $Datum: new Cast(${JSON.stringify(v.Datum)}),\n` : ""}`
234
293
  .map((d) => `typeof ${d}`)
235
294
  .join(", ")}]
236
295
  $dependsOnOwnHash: boolean
237
- $Redeemer: Cast<${redeemerTypes[0]}, ${redeemerTypes[1]}>
238
- ${datumTypes ? ` $Datum: Cast<${datumTypes[0]}, ${datumTypes[1]}>,\n` : ""}`
296
+ $Redeemer: ConfigurableCast<${redeemerTypes[0]}, ${redeemerTypes[1]}>
297
+ ${datumTypes ? ` $Datum: ConfigurableCast<${datumTypes[0]}, ${datumTypes[1]}>,\n` : ""}`
239
298
  )
240
299
 
241
300
  this.combined.write(
@@ -243,11 +302,11 @@ ${datumTypes ? ` $Datum: Cast<${datumTypes[0]}, ${datumTypes[1]}>,\n` : ""}`
243
302
  $name: "${v.name}" as const,
244
303
  $purpose: "${v.purpose}" as const,
245
304
  $sourceCode: ${JSON.stringify(v.sourceCode)} as string,
246
- $dependencies: [${v.moduleDepedencies.join(", ")}],
305
+ $dependencies: [${v.moduleDepedencies.join(", ")}] as const,
247
306
  $hashDependencies: [${v.hashDependencies.filter((d) => d != v.name).join(", ")}],
248
307
  $dependsOnOwnHash: ${v.hashDependencies.some((d) => d == v.name)} as boolean,
249
- $Redeemer: new Cast<${redeemerTypes[0]}, ${redeemerTypes[1]}>(${JSON.stringify(v.Redeemer)}),
250
- ${datumTypes ? ` $Datum: new Cast<${datumTypes[0]}, ${datumTypes[1]}>(${JSON.stringify(v.Datum)}),\n` : ""}`
308
+ $Redeemer: (config: CastConfig) => new Cast<${redeemerTypes[0]}, ${redeemerTypes[1]}>(${JSON.stringify(v.Redeemer)}, config),
309
+ ${datumTypes ? ` $Datum: (config: CastConfig) => new Cast<${datumTypes[0]}, ${datumTypes[1]}>(${JSON.stringify(v.Datum)}, config),\n` : ""}`
251
310
  )
252
311
 
253
312
  this.writeTypes(v.types)
@@ -1,5 +1,11 @@
1
1
  import { Cast } from "../cast/Cast.js"
2
2
 
3
+ /**
4
+ * @template TStrict
5
+ * @template TPermissive
6
+ * @typedef {import("../cast/Cast.js").CastLike<TStrict, TPermissive>} CastLike
7
+ */
8
+
3
9
  /**
4
10
  * @typedef {import("./LoadedModule.js").LoadedModule} LoadedModule
5
11
  */
@@ -8,16 +14,34 @@ import { Cast } from "../cast/Cast.js"
8
14
  * @typedef {{
9
15
  * $name: string
10
16
  * $sourceCode: string
11
- * $dependencies: LoadedModule[]
12
- * $hashDependencies: LoadedValidator[]
17
+ * $dependencies: ReadonlyArray<LoadedModule>
18
+ * $hashDependencies: ReadonlyArray<LoadedValidator>
13
19
  * $dependsOnOwnHash: boolean
14
- * $Redeemer: Cast
20
+ * $types: {[name: string]: CastLike<any, any>}
21
+ * $Redeemer: CastLike<any, any>
15
22
  * } & (
16
23
  * {
17
24
  * $purpose: "spending"
18
- * $Datum: Cast
25
+ * $Datum: CastLike<any, any>
19
26
  * } | {
20
27
  * $purpose: "minting" | "certifying" | "rewarding" | "staking"
21
28
  * }
22
29
  * )} LoadedValidator
23
30
  */
31
+
32
+ /**
33
+ * @template {{$name: string}} N
34
+ * @typedef {{
35
+ * [name in N["$name"]]: Extract<N, {"$name": name}>
36
+ * }} NamedDependencyToObject
37
+ */
38
+
39
+ /**
40
+ * @template {ReadonlyArray<{$name: string}>} N
41
+ * @typedef {NamedDependencyToObject<N extends ReadonlyArray<infer M> ? M : never>} NamedDependenciesToObject
42
+ */
43
+
44
+ /**
45
+ * @template {LoadedValidator} V
46
+ * @typedef {NamedDependenciesToObject<V["$dependencies"]>} ExtractDependencies
47
+ */
@@ -6,6 +6,7 @@ import {
6
6
  import { Cast } from "../cast/index.js"
7
7
 
8
8
  /**
9
+ * @typedef {import("../codegen/index.js").LoadedModule} LoadedModule
9
10
  * @typedef {import("../codegen/index.js").LoadedValidator} LoadedValidator
10
11
  */
11
12
 
@@ -30,12 +31,18 @@ import { Cast } from "../cast/index.js"
30
31
  */
31
32
 
32
33
  /**
33
- * @template {Cast} C
34
+ * @template TStrict
35
+ * @template TPermissive
36
+ * @typedef {import("../cast/index.js").CastLike<TStrict, TPermissive>} CastLike
37
+ */
38
+
39
+ /**
40
+ * @template {CastLike<any, any>} C
34
41
  * @typedef {import("../cast/index.js").PermissiveType<C>} PermissiveType
35
42
  */
36
43
 
37
44
  /**
38
- * @template {Cast} C
45
+ * @template {CastLike<any, any>} C
39
46
  * @typedef {import("../cast/index.js").StrictType<C>} StrictType
40
47
  */
41
48
 
@@ -44,35 +51,59 @@ import { Cast } from "../cast/index.js"
44
51
  * ValidatorHash<SpendingContext<any, any, any, any>> |
45
52
  * MintingPolicyHash<MintingContext<any, any>> |
46
53
  * StakingValidatorHash<StakingContext<any, any>>
47
- * )} AnyContractValidatorHash
54
+ * )} AnyContractValidatorContext
48
55
  */
49
56
 
50
57
  /**
51
58
  * @template {LoadedValidator} V
52
- * @typedef {V extends {"$purpose": "spending", "$Datum": Cast} ? ValidatorHash<
53
- * SpendingContext<
54
- * StrictType<V["$Datum"]>,
55
- * PermissiveType<V["$Datum"]>,
56
- * StrictType<V["$Redeemer"]>,
57
- * PermissiveType<V["$Redeemer"]>
59
+ * @typedef {V extends {"$purpose": "spending", "$Datum": CastLike<any, any>, "$Redeemer": CastLike<any, any>} ? {
60
+ * $hash: ValidatorHash<
61
+ * SpendingContext<
62
+ * V["$Datum"],
63
+ * PermissiveType<V["$Datum"]>,
64
+ * StrictType<V["$Redeemer"]>,
65
+ * PermissiveType<V["$Redeemer"]>
66
+ * >
58
67
  * >
59
- * > :
60
- * V extends {"$purpose": "minting"} ? MintingPolicyHash<
61
- * MintingContext<
62
- * StrictType<V["$Redeemer"]>,
63
- * PermissiveType<V["$Redeemer"]>
68
+ * } & ContractTypesContext<V["$types"]> :
69
+ * V extends {"$purpose": "minting", "$Redeemer": CastLike<any, any>} ? {
70
+ * $hash: MintingPolicyHash<
71
+ * MintingContext<
72
+ * StrictType<V["$Redeemer"]>,
73
+ * PermissiveType<V["$Redeemer"]>
74
+ * >
64
75
  * >
65
- * > :
66
- * V extends {"$purpose": ("certifying" | "rewarding" | "staking")} ? StakingValidatorHash<
67
- * StakingContext<
68
- * StrictType<V["$Redeemer"]>,
69
- * PermissiveType<V["$Redeemer"]>
76
+ * } & ContractTypesContext<V["$types"]> :
77
+ * V extends {"$purpose": ("certifying" | "rewarding" | "staking"), "$Redeemer": CastLike<any, any>} ? {
78
+ * $hash: StakingValidatorHash<
79
+ * StakingContext<
80
+ * StrictType<V["$Redeemer"]>,
81
+ * PermissiveType<V["$Redeemer"]>
82
+ * >
70
83
  * >
71
- * > : never
72
- * } ContractValidatorHash
84
+ * } & ContractTypesContext<V["$types"]> :
85
+ * never
86
+ * } ContractValidatorContexts
87
+ */
88
+
89
+ /**
90
+ * @template {LoadedModule} V
91
+ * @typedef {ContractTypesContext<V["$types"]>} ContractModuleContexts
92
+ */
93
+
94
+ /**
95
+ * @template {{[typeName:string]: CastLike<any, any>}} T
96
+ * @typedef {{
97
+ * [K in keyof T]: Cast<StrictType<T[K]>, PermissiveType<T[K]>>
98
+ * }} ContractTypesContext
73
99
  */
74
100
 
75
101
  /**
76
- * @template {{[name: string]: LoadedValidator}} T
77
- * @typedef {{[K in keyof T]: ContractValidatorHash<T[K]>}} ContractContext
102
+ * @template {{[name: string]: LoadedValidator}} Vs
103
+ * @template {{[name: string]: LoadedModule}} Ms
104
+ * @typedef {{
105
+ * [K in keyof Vs]: ContractValidatorContexts<Vs[K]>
106
+ * } & {
107
+ * [K in keyof Ms]: ContractModuleContexts<Ms[K]>
108
+ * }} ContractContext
78
109
  */
@@ -1,69 +1,157 @@
1
1
  import { None } from "@helios-lang/type-utils"
2
2
  import { loadCompilerLib } from "../compiler/index.js"
3
3
  import { DagCompiler } from "./DagCompiler.js"
4
+ import { configureCast } from "../cast/Cast.js"
4
5
 
5
6
  /**
7
+ * @typedef {import("../cast/index.js").CastConfig} CastConfig
6
8
  * @typedef {import("../codegen/LoadedModule.js").LoadedModule} LoadedModule
7
9
  * @typedef {import("../codegen/LoadedValidator.js").LoadedValidator} LoadedValidator
8
10
  * @typedef {import("../compiler/index.js").CompilerLib} CompilerLib
9
11
  */
10
12
 
11
13
  /**
12
- * @template {{[name: string]: LoadedValidator}} T
13
- * @typedef {import("./ContractContext.js").ContractContext<T>} ContractContext
14
+ * @template {LoadedValidator} V
15
+ * @typedef {import("../codegen/LoadedValidator.js").ExtractDependencies<V>} ExtractDependencies
14
16
  */
15
17
 
16
18
  /**
17
- * @template {{[name: string]: LoadedValidator}} T
19
+ * @template {{[name: string]: LoadedValidator}} Vs
20
+ * @template {{[name: string]: LoadedModule}} Ms
21
+ * @typedef {import("./ContractContext.js").ContractContext<Vs, Ms>} ContractContext
22
+ */
23
+
24
+ /**
25
+ * @typedef {{
26
+ * isMainnet: boolean
27
+ * expectedHashes?: {[name: string]: string}
28
+ * }} ContractContextBuildProps
29
+ */
30
+
31
+ /**
32
+ * @template {{[name: string]: LoadedValidator}} Vs
33
+ * @template {{[name: string]: LoadedModule}} Ms
18
34
  */
19
35
  export class ContractContextBuilder {
20
36
  /**
21
37
  * @private
22
38
  * @readonly
23
- * @type {T}
39
+ * @type {Vs}
24
40
  */
25
41
  validators
26
42
 
43
+ /**
44
+ * @readonly
45
+ * @type {Ms}
46
+ */
47
+ modules
48
+
27
49
  /**
28
50
  * @private
29
- * @param {T} validators
51
+ * @param {Vs} validators
52
+ * @param {Ms} modules
30
53
  */
31
- constructor(validators) {
54
+ constructor(validators, modules) {
32
55
  this.validators = validators
56
+ this.modules = modules
33
57
  }
34
58
 
35
59
  /**
36
- * @returns {ContractContextBuilder<{}>}
60
+ * @returns {ContractContextBuilder<{}, {}>}
37
61
  */
38
62
  static new() {
39
- return new ContractContextBuilder({})
63
+ return new ContractContextBuilder({}, {})
40
64
  }
41
65
 
42
66
  /**
43
67
  * @template {LoadedValidator} V
44
68
  * @param {V} validator
45
- * @returns {ContractContextBuilder<T & {
46
- * [K in V["$name"]]: V
47
- * }>}
69
+ * @returns {ContractContextBuilder<
70
+ * Vs & {[K in V["$name"]]: V},
71
+ * Ms & ExtractDependencies<V>
72
+ * >}
48
73
  */
49
74
  with(validator) {
50
- return new ContractContextBuilder({
51
- ...this.validators,
52
- [validator.$name]: validator
53
- })
75
+ return new ContractContextBuilder(
76
+ /** @type {any} */ ({
77
+ ...this.validators,
78
+ [validator.$name]: validator
79
+ }),
80
+ /** @type {any} */ ({
81
+ ...this.modules,
82
+ ...Object.fromEntries(
83
+ validator.$dependencies.map((d) => [d.$name, d])
84
+ )
85
+ })
86
+ )
87
+ }
88
+
89
+ /**
90
+ * @template {LoadedModule} M
91
+ * @param {M} m
92
+ * @returns {ContractContextBuilder<
93
+ * Vs,
94
+ * Ms & {[K in M["$name"]]: M}
95
+ * >}
96
+ */
97
+ withModule(m) {
98
+ return new ContractContextBuilder(
99
+ this.validators,
100
+ /** @type {any} */ ({
101
+ ...this.modules,
102
+ [m.$name]: m
103
+ })
104
+ )
54
105
  }
55
106
 
56
107
  /**
57
- * @param {Option<{[name: string]: string}>} expectedHashes
58
- * @returns {ContractContext<T>}
108
+ * @param {ContractContextBuildProps} props
109
+ * @returns {ContractContext<Vs, Ms>}
59
110
  */
60
- build(expectedHashes = None) {
111
+ build(props) {
112
+ const castConfig = { isMainnet: props.isMainnet }
61
113
  const lib = loadCompilerLib()
62
114
 
63
- const dagCompiler = new DagCompiler(lib)
115
+ const dagCompiler = new DagCompiler(lib, castConfig)
64
116
 
65
- return /** @type {any} */ (
66
- dagCompiler.build(Object.values(this.validators), expectedHashes)
117
+ /**
118
+ * @type {any}
119
+ */
120
+ const hashes = dagCompiler.build(
121
+ Object.values(this.validators),
122
+ props.expectedHashes
67
123
  )
124
+
125
+ /**
126
+ * @type {any}
127
+ */
128
+ const res = {}
129
+
130
+ for (let name in this.modules) {
131
+ res[name] = Object.fromEntries(
132
+ Object.entries(this.modules[name].$types).map(
133
+ ([typeName, castLike]) => [
134
+ typeName,
135
+ configureCast(castLike, castConfig)
136
+ ]
137
+ )
138
+ )
139
+ }
140
+
141
+ for (let name in this.validators) {
142
+ res[name] = {
143
+ ...Object.fromEntries(
144
+ Object.entries(this.validators[name].$types).map(
145
+ ([typeName, castLike]) => [
146
+ typeName,
147
+ configureCast(castLike, castConfig)
148
+ ]
149
+ )
150
+ ),
151
+ $hash: hashes[name]
152
+ }
153
+ }
154
+
155
+ return res
68
156
  }
69
157
  }
@@ -6,11 +6,13 @@ import {
6
6
  } from "@helios-lang/ledger"
7
7
  import { None } from "@helios-lang/type-utils"
8
8
  import { UplcProgramV2 } from "@helios-lang/uplc"
9
+ import { configureCast } from "../cast/index.js"
9
10
 
10
11
  /**
12
+ * @typedef {import("../cast/index.js").CastConfig} CastConfig
11
13
  * @typedef {import("../codegen/LoadedValidator.js").LoadedValidator} LoadedValidator
12
14
  * @typedef {import("../compiler/index.js").CompilerLib} CompilerLib
13
- * @typedef {import("./ContractContext.js").AnyContractValidatorHash} AnyContractValidatorHash
15
+ * @typedef {import("./ContractContext.js").AnyContractValidatorContext} AnyContractValidatorContext
14
16
  */
15
17
 
16
18
  /**
@@ -27,24 +29,33 @@ export class DagCompiler {
27
29
  /**
28
30
  * @private
29
31
  * @readonly
30
- * @type {{[name: string]: AnyContractValidatorHash}}
32
+ * @type {CastConfig}
33
+ */
34
+ config
35
+
36
+ /**
37
+ * @private
38
+ * @readonly
39
+ * @type {{[name: string]: AnyContractValidatorContext}}
31
40
  */
32
41
  cache
33
42
 
34
43
  /**
35
44
  * @param {CompilerLib} lib
45
+ * @param {CastConfig} castConfig
36
46
  */
37
- constructor(lib) {
47
+ constructor(lib, castConfig) {
38
48
  this.lib = lib
49
+ this.castConfig = castConfig
39
50
  this.cache = {}
40
51
  }
41
52
 
42
53
  /**
43
54
  * @param {LoadedValidator[]} validators
44
55
  * @param {Option<{[name: string]: string}>} expectedHashes
45
- * @returns {{[name: string]: AnyContractValidatorHash}}
56
+ * @returns {{[name: string]: AnyContractValidatorContext}}
46
57
  */
47
- build(validators, expectedHashes = None) {
58
+ build(validators, castConfig, expectedHashes = None) {
48
59
  const hashTypes = this.getHashTypes(validators)
49
60
 
50
61
  /**
@@ -132,14 +143,14 @@ export class DagCompiler {
132
143
  */
133
144
  addToCache(validator, program, hash) {
134
145
  const name = validator.$name
135
- const redeemer = validator.$Redeemer
146
+ const redeemer = configureCast(validator.$Redeemer, this.castConfig)
136
147
 
137
148
  switch (validator.$purpose) {
138
149
  case "spending":
139
150
  this.cache[name] = new ValidatorHash(hash, {
140
151
  program,
141
152
  redeemer,
142
- datum: validator.$Datum
153
+ datum: configureCast(validator.$Datum, this.castConfig)
143
154
  })
144
155
  break
145
156
  case "minting":
package/src/index.js CHANGED
@@ -1,15 +1,5 @@
1
- export { Cast } from "./cast/index.js"
1
+ export * from "./cast/index.js"
2
2
  export { ContractContextBuilder } from "./context/index.js"
3
3
  export { typeCheckFiles, typeCheckScripts } from "./compiler/ops.js"
4
4
  export { LoadedScriptsWriter } from "./codegen/LoadedScriptsWriter.js"
5
5
  export { loadCompilerLib } from "./compiler/ops.js"
6
-
7
- /**
8
- * @template {import("./cast/index.js").Cast} C
9
- * @typedef {import("./cast/index.js").StrictType<C>} StrictType
10
- */
11
-
12
- /**
13
- * @template {import("./cast/index.js").Cast} C
14
- * @typedef {import("./cast/index.js").PermissiveType<C>} PermissiveType
15
- */