@helios-lang/contract-utils 0.3.5 → 0.3.6
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/package.json +1 -1
- package/src/artifacts/Artifact.js +14 -0
- package/src/artifacts/ArtifactWriter.js +199 -0
- package/src/artifacts/ChildArtifactWriter.js +118 -0
- package/src/artifacts/FunctionArtifact.js +90 -0
- package/src/artifacts/ModuleArtifact.js +83 -0
- package/src/artifacts/RootArtifact.js +105 -0
- package/src/artifacts/TypeArtifact.js +104 -0
- package/src/artifacts/ValidatorArtifact.js +158 -0
- package/src/artifacts/index.js +1 -0
- package/src/artifacts/symbols.js +16 -0
- package/src/cast/Cast.js +45 -9
- package/src/cast/UserFunc.js +182 -131
- package/src/cast/index.js +12 -3
- package/src/codegen/LoadedScriptsWriter.js +35 -11
- package/src/codegen/TypeSchema.js +123 -0
- package/src/context/index.js +1 -0
- package/src/hl2ts.js +0 -4
- package/src/index.js +36 -8
- package/src/internal.js +12 -0
- package/types/artifacts/Artifact.d.ts +11 -0
- package/types/artifacts/Artifact.d.ts.map +1 -0
- package/types/artifacts/ArtifactWriter.d.ts +64 -0
- package/types/artifacts/ArtifactWriter.d.ts.map +1 -0
- package/types/artifacts/ChildArtifactWriter.d.ts +35 -0
- package/types/artifacts/ChildArtifactWriter.d.ts.map +1 -0
- package/types/artifacts/FunctionArtifact.d.ts +14 -0
- package/types/artifacts/FunctionArtifact.d.ts.map +1 -0
- package/types/artifacts/ModuleArtifact.d.ts +29 -0
- package/types/artifacts/ModuleArtifact.d.ts.map +1 -0
- package/types/artifacts/RootArtifact.d.ts +11 -0
- package/types/artifacts/RootArtifact.d.ts.map +1 -0
- package/types/artifacts/TypeArtifact.d.ts +17 -0
- package/types/artifacts/TypeArtifact.d.ts.map +1 -0
- package/types/artifacts/ValidatorArtifact.d.ts +17 -0
- package/types/artifacts/ValidatorArtifact.d.ts.map +1 -0
- package/types/artifacts/index.d.ts +2 -0
- package/types/artifacts/index.d.ts.map +1 -0
- package/types/artifacts/symbols.d.ts +8 -0
- package/types/artifacts/symbols.d.ts.map +1 -0
- package/types/cast/Cast.d.ts +31 -0
- package/types/cast/Cast.d.ts.map +1 -1
- package/types/cast/UserFunc.d.ts +39 -0
- package/types/cast/UserFunc.d.ts.map +1 -1
- package/types/cast/index.d.ts +2 -2
- package/types/codegen/LoadedScriptsWriter.d.ts +18 -0
- package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -1
- package/types/codegen/TypeSchema.d.ts +5 -0
- package/types/codegen/TypeSchema.d.ts.map +1 -1
- package/types/context/writeContractContextArtifacts.d.ts +30 -0
- package/types/context/writeContractContextArtifacts.d.ts.map +1 -0
- package/types/hl2ts.d.ts.map +1 -1
- package/types/index.d.ts +30 -4
- package/types/index.d.ts.map +1 -1
- package/types/internal.d.ts +5 -0
- 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
|
|
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
|
-
|
|
150
|
-
|
|
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
|