@helios-lang/contract-utils 0.3.5 → 0.3.7

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 (56) hide show
  1. package/package.json +1 -1
  2. package/src/artifacts/Artifact.js +14 -0
  3. package/src/artifacts/ArtifactWriter.js +199 -0
  4. package/src/artifacts/ChildArtifactWriter.js +118 -0
  5. package/src/artifacts/FunctionArtifact.js +90 -0
  6. package/src/artifacts/ModuleArtifact.js +83 -0
  7. package/src/artifacts/RootArtifact.js +105 -0
  8. package/src/artifacts/TypeArtifact.js +104 -0
  9. package/src/artifacts/ValidatorArtifact.js +158 -0
  10. package/src/artifacts/index.js +1 -0
  11. package/src/artifacts/symbols.js +16 -0
  12. package/src/cast/Cast.js +45 -9
  13. package/src/cast/UserFunc.js +182 -131
  14. package/src/cast/index.js +12 -3
  15. package/src/codegen/LoadedScriptsWriter.js +35 -11
  16. package/src/codegen/TypeSchema.js +126 -0
  17. package/src/context/index.js +1 -0
  18. package/src/hl2ts.js +0 -4
  19. package/src/index.js +36 -8
  20. package/src/internal.js +12 -0
  21. package/types/artifacts/Artifact.d.ts +11 -0
  22. package/types/artifacts/Artifact.d.ts.map +1 -0
  23. package/types/artifacts/ArtifactWriter.d.ts +64 -0
  24. package/types/artifacts/ArtifactWriter.d.ts.map +1 -0
  25. package/types/artifacts/ChildArtifactWriter.d.ts +35 -0
  26. package/types/artifacts/ChildArtifactWriter.d.ts.map +1 -0
  27. package/types/artifacts/FunctionArtifact.d.ts +14 -0
  28. package/types/artifacts/FunctionArtifact.d.ts.map +1 -0
  29. package/types/artifacts/ModuleArtifact.d.ts +29 -0
  30. package/types/artifacts/ModuleArtifact.d.ts.map +1 -0
  31. package/types/artifacts/RootArtifact.d.ts +11 -0
  32. package/types/artifacts/RootArtifact.d.ts.map +1 -0
  33. package/types/artifacts/TypeArtifact.d.ts +17 -0
  34. package/types/artifacts/TypeArtifact.d.ts.map +1 -0
  35. package/types/artifacts/ValidatorArtifact.d.ts +17 -0
  36. package/types/artifacts/ValidatorArtifact.d.ts.map +1 -0
  37. package/types/artifacts/index.d.ts +2 -0
  38. package/types/artifacts/index.d.ts.map +1 -0
  39. package/types/artifacts/symbols.d.ts +8 -0
  40. package/types/artifacts/symbols.d.ts.map +1 -0
  41. package/types/cast/Cast.d.ts +31 -0
  42. package/types/cast/Cast.d.ts.map +1 -1
  43. package/types/cast/UserFunc.d.ts +39 -0
  44. package/types/cast/UserFunc.d.ts.map +1 -1
  45. package/types/cast/index.d.ts +2 -2
  46. package/types/codegen/LoadedScriptsWriter.d.ts +18 -0
  47. package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -1
  48. package/types/codegen/TypeSchema.d.ts +5 -0
  49. package/types/codegen/TypeSchema.d.ts.map +1 -1
  50. package/types/context/writeContractContextArtifacts.d.ts +30 -0
  51. package/types/context/writeContractContextArtifacts.d.ts.map +1 -0
  52. package/types/hl2ts.d.ts.map +1 -1
  53. package/types/index.d.ts +30 -4
  54. package/types/index.d.ts.map +1 -1
  55. package/types/internal.d.ts +5 -0
  56. package/types/internal.d.ts.map +1 -0
@@ -0,0 +1,104 @@
1
+ import { collectBuiltinTypes, genTypes } from "../codegen/TypeSchema.js"
2
+ import { ChildArtifactWriter } from "./ChildArtifactWriter.js"
3
+ import { writeFunctionArtifact } from "./FunctionArtifact.js"
4
+
5
+ /**
6
+ * @import { TypeSchema } from "@helios-lang/type-utils"
7
+ * @import { Artifact } from "./Artifact.js"
8
+ * @import { FunctionDetails, TypeDetails } from "./symbols.js"
9
+ */
10
+
11
+ /**
12
+ *
13
+ * @param {Artifact} parent
14
+ * @param {string} name
15
+ * @param {TypeDetails} props
16
+ * @param {Record<string, FunctionDetails>} members
17
+ */
18
+ export function writeTypeArtifact(parent, name, props, members) {
19
+ const artifact = new TypeArtifact(parent, name)
20
+
21
+ const schema = props.schema
22
+
23
+ artifact.writeSchema(schema)
24
+ artifact.writeType(schema)
25
+ artifact.writeConverters()
26
+
27
+ for (let memberName in members) {
28
+ writeFunctionArtifact(artifact, memberName, members[memberName])
29
+ }
30
+
31
+ // also write enum members
32
+ if (schema.kind == "enum") {
33
+ for (let variant of schema.variantTypes) {
34
+ writeTypeArtifact(artifact, variant.name, { schema: variant }, {})
35
+ }
36
+ }
37
+
38
+ artifact.save()
39
+
40
+ parent.writeAggregateExport(name)
41
+ }
42
+
43
+ class TypeArtifact extends ChildArtifactWriter {
44
+ /**
45
+ * @param {Artifact} parent
46
+ * @param {string} name
47
+ */
48
+ constructor(parent, name) {
49
+ super(parent, name)
50
+ }
51
+
52
+ /**
53
+ * @param {TypeSchema} schema
54
+ */
55
+ writeSchema(schema) {
56
+ this.addImport("TypeSchema", "@helios-lang/type-utils", true)
57
+
58
+ this.writeDeclLine(`export const $schema: TypeSchema`)
59
+ this.writeDefLine(
60
+ `/**
61
+ * @type {import("@helios-lang/type-utils").TypeSchema}
62
+ */
63
+ export const $schema = ${JSON.stringify(schema, undefined, 4)}`
64
+ )
65
+ }
66
+
67
+ /**
68
+ * @param {TypeSchema} schema
69
+ */
70
+ writeType(schema) {
71
+ const types = genTypes(schema)
72
+
73
+ this.writeDeclLine(`export type $StrictType = ${types[0]}`)
74
+ this.writeDeclLine(`export type $PermissiveType = ${types[1]}`)
75
+
76
+ const internalTypes = collectBuiltinTypes(schema)
77
+
78
+ Array.from(internalTypes.entries()).forEach(([name, from]) => {
79
+ this.addImport(name, from, true)
80
+ })
81
+ }
82
+
83
+ writeConverters() {
84
+ this.addImport("convertToUplcData", "@helios-lang/contract-utils")
85
+ this.addImport("convertFromUplcData", "@helios-lang/contract-utils")
86
+ this.addImport("UplcData", "@helios-lang/uplc", true)
87
+
88
+ this.writeDeclLine(
89
+ `export function $toUplcData(x: $PermissiveType): UplcData`
90
+ )
91
+ this.writeDeclLine(
92
+ `export function $fromUplcData(data: UplcData): $StrictType`
93
+ )
94
+
95
+ this.writeDefLine(`export function $toUplcData(x) {
96
+ return convertToUplcData($schema, x)
97
+ }`)
98
+ this.writeDefLine(`export function $fromUplcData(data) {
99
+ return convertFromUplcData($schema, data, {
100
+ isMainnet: ${this.isMainnet}
101
+ })
102
+ }`)
103
+ }
104
+ }
@@ -0,0 +1,158 @@
1
+ import { genTypes } from "../codegen/TypeSchema.js"
2
+ import { ModuleArtifact } from "./ModuleArtifact.js"
3
+
4
+ /**
5
+ * @import { MintingContext, ScriptHash, SpendingContext, StakingContext } from "@helios-lang/ledger"
6
+ * @import { Cast } from "../index.js"
7
+ * @import { Artifact } from "./Artifact.js"
8
+ * @import { ModuleSymbols } from "./symbols.js"
9
+ */
10
+
11
+ /**
12
+ * @param {Artifact} parent
13
+ * @param {string} name
14
+ * @param {ScriptHash} hash
15
+ * @param {ModuleSymbols} symbols
16
+ */
17
+ export function writeValidatorArtifact(parent, name, hash, symbols) {
18
+ const artifact = new ValdiatorArtifact(parent, name)
19
+
20
+ artifact.writeHash(hash)
21
+
22
+ artifact.writeSymbols(symbols)
23
+
24
+ artifact.save()
25
+
26
+ parent.writeAggregateExport(name)
27
+ }
28
+
29
+ /**
30
+ * @implements {Artifact}
31
+ */
32
+ class ValdiatorArtifact extends ModuleArtifact {
33
+ /**
34
+ * @param {Artifact} parent
35
+ * @param {string} name
36
+ */
37
+ constructor(parent, name) {
38
+ super(parent, name)
39
+ }
40
+
41
+ /**
42
+ * @param {ScriptHash} hash
43
+ */
44
+ writeHash(hash) {
45
+ // TODO: SpendingContext, MintingContext, StakingContext
46
+ if (hash.kind == "MintingPolicyHash") {
47
+ this.addImport(
48
+ "MintingPolicyHash",
49
+ "@helios-lang/ledger",
50
+ true
51
+ ).addImport("makeMintingPolicyHash", "@helios-lang/ledger")
52
+
53
+ if (!hash.context) {
54
+ this.writeDeclLine(
55
+ `export const $hash: MintingPolicyHash`
56
+ ).writeDefLine(
57
+ `export const $hash = makeMintingPolicyHash("${hash.toHex()}")`
58
+ )
59
+ } else {
60
+ this.addImport("makeCast", "@helios-lang/contract-utils")
61
+ this.addImport("MintingContext", "@helios-lang/ledger", true)
62
+
63
+ const context =
64
+ /** @type {MintingContext<unknown, unknown>} */ (
65
+ hash.context
66
+ )
67
+ const program = context.program
68
+ const redeemer = /** @type {Cast<unknown, unknown>} */ (
69
+ context.redeemer
70
+ )
71
+ const types = genTypes(redeemer.schema)
72
+
73
+ this.writeProgram("$program", program).writeDeclLine(
74
+ `export const $hash: MintingPolicyHash<MintingContext<${types[0]}, ${types[1]}>>`
75
+ )
76
+ .writeDefLine(`export const $hash = makeMintingPolicyHash("${hash.toHex()}", {
77
+ program: $program,
78
+ redeemer: makeCast(${JSON.stringify(redeemer.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}})
79
+ })`)
80
+ }
81
+ } else if (hash.kind == "StakingValidatorHash") {
82
+ this.addImport(
83
+ "StakingValidatorHash",
84
+ "@helios-lang/ledger",
85
+ true
86
+ ).addImport("makeStakingValidatorHash", "@helios-lang/ledger")
87
+
88
+ if (!hash.context) {
89
+ this.writeDeclLine(
90
+ `export const $hash: StakingValidatorHash`
91
+ ).writeDefLine(
92
+ `export const $hash = makeStakingValidatorHash("${hash.toHex()}")`
93
+ )
94
+ } else {
95
+ this.addImport("makeCast", "@helios-lang/contract-utils")
96
+ this.addImport("StakingContext", "@helios-lang/ledger", true)
97
+
98
+ const context =
99
+ /** @type {StakingContext<unknown, unknown>} */ (
100
+ hash.context
101
+ )
102
+ const program = context.program
103
+ const redeemer = /** @type {Cast<unknown, unknown>} */ (
104
+ context.redeemer
105
+ )
106
+ const types = genTypes(redeemer.schema)
107
+
108
+ this.writeProgram("$program", program).writeDeclLine(
109
+ `export const $hash: StakingValidatorHash<StakingContext<${types[0]}, ${types[1]}>>`
110
+ )
111
+ .writeDefLine(`export const $hash = makeStakingValidatorHash("${hash.toHex()}", {
112
+ program: $program,
113
+ redeemer: makeCast(${JSON.stringify(redeemer.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}})
114
+ })`)
115
+ }
116
+ } else if (hash.kind == "ValidatorHash") {
117
+ this.addImport(
118
+ "ValidatorHash",
119
+ "@helios-lang/ledger",
120
+ true
121
+ ).addImport("makeValidatorHash", "@helios-lang/ledger")
122
+
123
+ if (!hash.context) {
124
+ this.writeDeclLine(
125
+ `export const $hash: ValidatorHash`
126
+ ).writeDefLine(
127
+ `export const $hash = makeValidatorHash("${hash.toHex()}")`
128
+ )
129
+ } else {
130
+ this.addImport("makeCast", "@helios-lang/contract-utils")
131
+ this.addImport("SpendingContext", "@helios-lang/ledger", true)
132
+
133
+ const context =
134
+ /** @type {SpendingContext<unknown, unknown, unknown, unknown>} */ (
135
+ hash.context
136
+ )
137
+ const program = context.program
138
+ const datum = /** @type {Cast<unknown, unknown>} */ (
139
+ context.datum
140
+ )
141
+ const redeemer = /** @type {Cast<unknown, unknown>} */ (
142
+ context.redeemer
143
+ )
144
+ const dTypes = genTypes(datum.schema)
145
+ const rTypes = genTypes(redeemer.schema)
146
+
147
+ this.writeProgram("$program", program).writeDeclLine(
148
+ `export const $hash: ValidatorHash<SpendingContext<${dTypes[0]}, ${dTypes[1]}, ${rTypes[0]}, ${rTypes[1]}>>`
149
+ )
150
+ .writeDefLine(`export const $hash = makeValidatorHash("${hash.toHex()}", {
151
+ program: $program,
152
+ datum: makeCast(${JSON.stringify(datum.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}}),
153
+ redeemer: makeCast(${JSON.stringify(redeemer.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}})
154
+ })`)
155
+ }
156
+ }
157
+ }
158
+ }
@@ -0,0 +1 @@
1
+ export { writeContractContextArtifacts } from "./RootArtifact.js"
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @import { TypeSchema } from "@helios-lang/type-utils"
3
+ * @import { Cast, UserFunc } from "../index.js"
4
+ */
5
+
6
+ /**
7
+ * @typedef {UserFunc<any, any>} FunctionDetails
8
+ */
9
+
10
+ /**
11
+ * @typedef {{schema: TypeSchema}} TypeDetails
12
+ */
13
+
14
+ /**
15
+ * @typedef {Record<string, FunctionDetails | TypeDetails>} ModuleSymbols
16
+ */
package/src/cast/Cast.js CHANGED
@@ -125,12 +125,11 @@ class CastImpl {
125
125
  * @returns {TStrict}
126
126
  */
127
127
  fromUplcData(data, dataPath = "") {
128
- return convertUplcDataToSchemaDataWithDataPath(this.schema, data, {
128
+ return convertFromUplcData(this.schema, data, {
129
129
  isMainnet: this.config.isMainnet,
130
130
  unwrapSingleFieldEnumVariants:
131
131
  !!this.config.unwrapSingleFieldEnumVariants,
132
- dataPath,
133
- defs: {}
132
+ dataPath
134
133
  })
135
134
  }
136
135
 
@@ -146,17 +145,54 @@ class CastImpl {
146
145
  * @returns {UplcData}
147
146
  */
148
147
  toUplcData(x, dataPath = "") {
149
- const t = convertSchemaDataToUplcDataWithDataPath(this.schema, x, {
150
- defs: {},
148
+ return convertToUplcData(this.schema, x, {
149
+ dataPath,
151
150
  unwrapSingleFieldEnumVariants:
152
- !!this.config.unwrapSingleFieldEnumVariants,
153
- dataPath
151
+ !!this.config.unwrapSingleFieldEnumVariants
154
152
  })
155
- t.rawData = x
156
- return t
157
153
  }
158
154
  }
159
155
 
156
+ /**
157
+ * @template T
158
+ * @param {TypeSchema} schema
159
+ * @param {UplcData} data
160
+ * @param {{
161
+ * isMainnet: boolean,
162
+ * dataPath?: string,
163
+ * unwrapSingleFieldEnumVariants?: boolean
164
+ * }} options
165
+ * @returns {T}
166
+ */
167
+ export function convertFromUplcData(schema, data, options) {
168
+ return convertUplcDataToSchemaDataWithDataPath(schema, data, {
169
+ isMainnet: options.isMainnet,
170
+ unwrapSingleFieldEnumVariants: !!options.unwrapSingleFieldEnumVariants,
171
+ dataPath: options.dataPath ?? "",
172
+ defs: {}
173
+ })
174
+ }
175
+
176
+ /**
177
+ * @template T
178
+ * @param {TypeSchema} schema
179
+ * @param {T} x
180
+ * @param {{
181
+ * dataPath?: string,
182
+ * unwrapSingleFieldEnumVariants?: boolean
183
+ * }} [options]
184
+ * @returns {UplcData}
185
+ */
186
+ export function convertToUplcData(schema, x, options = {}) {
187
+ const t = convertSchemaDataToUplcDataWithDataPath(schema, x, {
188
+ defs: {},
189
+ unwrapSingleFieldEnumVariants: !!options.unwrapSingleFieldEnumVariants,
190
+ dataPath: options.dataPath ?? ""
191
+ })
192
+ t.rawData = x
193
+ return t
194
+ }
195
+
160
196
  /**
161
197
  * @param {TypeSchema} schema
162
198
  * @param {any} x - data