@helios-lang/contract-utils 0.3.8 → 0.3.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helios-lang/contract-utils",
3
- "version": "0.3.8",
3
+ "version": "0.3.10",
4
4
  "description": "Convenience and type-safety utilities for using Helios validators from within Typescript",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -41,36 +41,49 @@ export class ChildArtifactWriter extends ArtifactWriter {
41
41
  /**
42
42
  * @param {string} name
43
43
  * @param {UplcProgram} program
44
+ * @param {boolean} includeDef
44
45
  * @returns {ChildArtifactWriter}
45
46
  */
46
- writeProgram(name, program) {
47
+ writeProgram(name, program, includeDef) {
47
48
  if (program.plutusVersion == "PlutusScriptV1") {
48
49
  this.addImport("UplcProgramV1", "@helios-lang/uplc", true)
49
- this.addImport("decodeUplcProgramV1FromCbor", "@helios-lang/uplc")
50
-
51
- this.writeDeclLine(
52
- `export const ${name}: UplcProgramV1`
53
- ).writeDefLine(
54
- `export const ${name} = /* @__PURE__ */ ${stringifyProgram("decodeUplcProgramV1FromCbor", program)}`
55
- )
50
+ this.writeDeclLine(`export const ${name}: UplcProgramV1`)
51
+
52
+ if (includeDef) {
53
+ this.addImport(
54
+ "decodeUplcProgramV1FromCbor",
55
+ "@helios-lang/uplc"
56
+ )
57
+ this.writeDefLine(
58
+ `export const ${name} = ${stringifyProgram("decodeUplcProgramV1FromCbor", program)}`
59
+ )
60
+ }
56
61
  } else if (program.plutusVersion == "PlutusScriptV2") {
57
62
  this.addImport("UplcProgramV2", "@helios-lang/uplc", true)
58
- this.addImport("decodeUplcProgramV2FromCbor", "@helios-lang/uplc")
59
-
60
- this.writeDeclLine(
61
- `export const ${name}: UplcProgramV2`
62
- ).writeDefLine(
63
- `export const ${name} = /* @__PURE__ */ ${stringifyProgram("decodeUplcProgramV2FromCbor", program)}`
64
- )
63
+ this.writeDeclLine(`export const ${name}: UplcProgramV2`)
64
+
65
+ if (includeDef) {
66
+ this.addImport(
67
+ "decodeUplcProgramV2FromCbor",
68
+ "@helios-lang/uplc"
69
+ )
70
+ this.writeDefLine(
71
+ `export const ${name} = ${stringifyProgram("decodeUplcProgramV2FromCbor", program)}`
72
+ )
73
+ }
65
74
  } else if (program.plutusVersion == "PlutusScriptV3") {
66
75
  this.addImport("UplcProgramV3", "@helios-lang/uplc", true)
67
- this.addImport("decodeUplcProgramV3FromCbor", "@helios-lang/uplc")
68
-
69
- this.writeDeclLine(
70
- `export const ${name}: UplcProgramV3`
71
- ).writeDefLine(
72
- `export const ${name} = /* @__PURE__ */ ${stringifyProgram("decodeUplcProgramV3FromCbor", program)}`
73
- )
76
+ this.writeDeclLine(`export const ${name}: UplcProgramV3`)
77
+
78
+ if (includeDef) {
79
+ this.addImport(
80
+ "decodeUplcProgramV3FromCbor",
81
+ "@helios-lang/uplc"
82
+ )
83
+ this.writeDefLine(
84
+ `export const ${name} = ${stringifyProgram("decodeUplcProgramV3FromCbor", program)}`
85
+ )
86
+ }
74
87
  } else {
75
88
  throw new Error("unhandled Plutus version")
76
89
  }
@@ -88,7 +101,7 @@ export class ChildArtifactWriter extends ArtifactWriter {
88
101
  function stringifyProgram(decoderName, program) {
89
102
  const sourceMap = makeUplcSourceMap({ term: program.root })
90
103
 
91
- return `${decoderName}(
104
+ return `/* @__PURE__ */ ${decoderName}(
92
105
  "${bytesToHex(program.toCbor())}",
93
106
  {${stringifyAlt("decodeUplcProgramV2FromCbor", program.alt)}
94
107
  sourceMap: ${JSON.stringify(sourceMap.toJsonSafe(), undefined, 4).split("\n").join("\n ")}
@@ -109,7 +122,7 @@ function stringifyAlt(decoderName, alt) {
109
122
  const sourceMap = makeUplcSourceMap({ term: alt.root })
110
123
 
111
124
  return `
112
- alt: ${decoderName}(
125
+ alt: /* @__PURE__ */ ${decoderName}(
113
126
  "${bytesToHex(alt.toCbor())}",
114
127
  {
115
128
  sourceMap: ${JSON.stringify(sourceMap.toJsonSafe(), undefined, 4).split("\n").join("\n ")}
@@ -22,7 +22,7 @@ export function writeFunctionArtifact(parent, name, details) {
22
22
  const program = details.uplc
23
23
  const props = details.props
24
24
 
25
- artifact.writeProgram("$program", program)
25
+ artifact.writeProgram("$program", program, true)
26
26
  artifact.writeProps(props)
27
27
  artifact.writeEvals(props)
28
28
 
@@ -16,6 +16,11 @@ export function writeContractContextArtifacts(context, options) {
16
16
 
17
17
  const root = new RootArtifact(options.fs, options.outDir, isMainnet)
18
18
 
19
+ /**
20
+ * @type {string[]}
21
+ */
22
+ let validatorNames = []
23
+
19
24
  for (let moduleName in context) {
20
25
  if (moduleName.startsWith("$")) {
21
26
  continue
@@ -30,11 +35,14 @@ export function writeContractContextArtifacts(context, options) {
30
35
  moduleDetails.$hash,
31
36
  moduleDetails
32
37
  )
38
+
39
+ validatorNames.push(moduleName)
33
40
  } else {
34
41
  writeModuleArtifact(root, moduleName, moduleDetails)
35
42
  }
36
43
  }
37
44
 
45
+ root.writeValidatorNameUtils(validatorNames)
38
46
  root.save()
39
47
  }
40
48
 
@@ -58,6 +66,27 @@ class RootArtifact extends ArtifactWriter {
58
66
 
59
67
  this.isMainnet = isMainnet
60
68
  }
69
+
70
+ /**
71
+ * @param {string[]} names
72
+ */
73
+ writeValidatorNameUtils(names) {
74
+ this.writeDeclLine(
75
+ `export type $ValidatorNameType = ${names.map((n) => `"${n}"`).join(" | ")}`
76
+ )
77
+ .writeDeclLine(
78
+ `export const $VALIDATOR_NAMES: $ValidatorNameType[]`
79
+ )
80
+ .writeDeclLine(
81
+ `export function $isValidatorName(name: string): name is $ValidatorNameType`
82
+ )
83
+ .writeDefLine(
84
+ `export const $VALIDATOR_NAMES = [${names.map((n) => `"${n}"`).join(", ")}]`
85
+ )
86
+ .writeDefLine(
87
+ `export function $isValidatorName(name) {return $VALIDATOR_NAMES.includes(name)}`
88
+ )
89
+ }
61
90
  }
62
91
 
63
92
  /**
@@ -17,10 +17,10 @@ import { ModuleArtifact } from "./ModuleArtifact.js"
17
17
  export function writeValidatorArtifact(parent, name, hash, symbols) {
18
18
  const artifact = new ValdiatorArtifact(parent, name)
19
19
 
20
- artifact.writeHash(hash)
21
-
22
20
  artifact.writeSymbols(symbols)
23
21
 
22
+ artifact.writeHash(hash)
23
+
24
24
  artifact.save()
25
25
 
26
26
  parent.writeAggregateExport(name)
@@ -30,19 +30,28 @@ export function writeValidatorArtifact(parent, name, hash, symbols) {
30
30
  * @implements {Artifact}
31
31
  */
32
32
  class ValdiatorArtifact extends ModuleArtifact {
33
+ /**
34
+ * @private
35
+ * @type {boolean}
36
+ */
37
+ hasMainFunction
38
+
33
39
  /**
34
40
  * @param {Artifact} parent
35
41
  * @param {string} name
36
42
  */
37
43
  constructor(parent, name) {
38
44
  super(parent, name)
45
+ this.hasMainFunction = false
39
46
  }
40
47
 
41
48
  /**
42
49
  * @param {ScriptHash} hash
43
50
  */
44
51
  writeHash(hash) {
45
- // TODO: SpendingContext, MintingContext, StakingContext
52
+ this.writeDeclLine(`export const $hashHex: string`)
53
+ this.writeDefLine(`export const $hashHex = "${hash.toHex()}"`)
54
+
46
55
  if (hash.kind == "MintingPolicyHash") {
47
56
  this.addImport(
48
57
  "MintingPolicyHash",
@@ -54,9 +63,15 @@ class ValdiatorArtifact extends ModuleArtifact {
54
63
  this.writeDeclLine(
55
64
  `export const $hash: MintingPolicyHash`
56
65
  ).writeDefLine(
57
- `export const $hash = /* @__PURE__ */ makeMintingPolicyHash("${hash.toHex()}")`
66
+ `export const $hash = /* @__PURE__ */ makeMintingPolicyHash($hashHex)`
58
67
  )
59
68
  } else {
69
+ this.writeDeclLine(
70
+ `export const $hashWithoutContext: MintingPolicyHash`
71
+ ).writeDefLine(
72
+ `export const $hashWithoutContext = /* @__PURE__ */ makeMintingPolicyHash($hashHex)`
73
+ )
74
+
60
75
  this.addImport("makeCast", "@helios-lang/contract-utils")
61
76
  this.addImport("MintingContext", "@helios-lang/ledger", true)
62
77
 
@@ -70,10 +85,14 @@ class ValdiatorArtifact extends ModuleArtifact {
70
85
  )
71
86
  const types = genTypes(redeemer.schema)
72
87
 
73
- this.writeProgram("$program", program).writeDeclLine(
74
- `export const $hash: MintingPolicyHash<MintingContext<${types[0]}, ${types[1]}>>`
88
+ this.writeProgram("$program", program, !this.hasMainFunction)
89
+
90
+ this.writeDeclLine(
91
+ `export type $ContextType = MintingContext<${types[0]}, ${types[1]}>`
92
+ ).writeDeclLine(
93
+ `export const $hash: MintingPolicyHash<$ContextType>`
75
94
  )
76
- .writeDefLine(`export const $hash = /* @__PURE__ */ makeMintingPolicyHash("${hash.toHex()}", {
95
+ .writeDefLine(`export const $hash = /* @__PURE__ */ makeMintingPolicyHash($hashHex, {
77
96
  program: $program,
78
97
  redeemer: /* @__PURE__*/ makeCast(${JSON.stringify(redeemer.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}})
79
98
  })`)
@@ -89,9 +108,15 @@ class ValdiatorArtifact extends ModuleArtifact {
89
108
  this.writeDeclLine(
90
109
  `export const $hash: StakingValidatorHash`
91
110
  ).writeDefLine(
92
- `export const $hash = /* @__PURE__*/ makeStakingValidatorHash("${hash.toHex()}")`
111
+ `export const $hash = /* @__PURE__*/ makeStakingValidatorHash($hashHex)`
93
112
  )
94
113
  } else {
114
+ this.writeDeclLine(
115
+ `export const $hashWithoutContext: StakingValidatorHash`
116
+ ).writeDefLine(
117
+ `export const $hashWithoutContext = /* @__PURE__*/ makeStakingValidatorHash($hashHex)`
118
+ )
119
+
95
120
  this.addImport("makeCast", "@helios-lang/contract-utils")
96
121
  this.addImport("StakingContext", "@helios-lang/ledger", true)
97
122
 
@@ -105,10 +130,14 @@ class ValdiatorArtifact extends ModuleArtifact {
105
130
  )
106
131
  const types = genTypes(redeemer.schema)
107
132
 
108
- this.writeProgram("$program", program).writeDeclLine(
109
- `export const $hash: StakingValidatorHash<StakingContext<${types[0]}, ${types[1]}>>`
133
+ this.writeProgram("$program", program, !this.hasMainFunction)
134
+
135
+ this.writeDeclLine(
136
+ `export type $ContextType = StakingContext<${types[0]}, ${types[1]}>`
137
+ ).writeDeclLine(
138
+ `export const $hash: StakingValidatorHash<$ContextType>`
110
139
  )
111
- .writeDefLine(`export const $hash = /* @__PURE__*/ makeStakingValidatorHash("${hash.toHex()}", {
140
+ .writeDefLine(`export const $hash = /* @__PURE__*/ makeStakingValidatorHash($hashHex, {
112
141
  program: $program,
113
142
  redeemer: /* @__PURE__ */ makeCast(${JSON.stringify(redeemer.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}})
114
143
  })`)
@@ -124,9 +153,15 @@ class ValdiatorArtifact extends ModuleArtifact {
124
153
  this.writeDeclLine(
125
154
  `export const $hash: ValidatorHash`
126
155
  ).writeDefLine(
127
- `export const $hash = /* @__PURE__ */ makeValidatorHash("${hash.toHex()}")`
156
+ `export const $hash = /* @__PURE__ */ makeValidatorHash($hashHex)`
128
157
  )
129
158
  } else {
159
+ this.writeDeclLine(
160
+ `export const $hashWithoutContext: ValidatorHash`
161
+ ).writeDefLine(
162
+ `export const $hashWithoutContext = /* @__PURE__ */ makeValidatorHash($hashHex)`
163
+ )
164
+
130
165
  this.addImport("makeCast", "@helios-lang/contract-utils")
131
166
  this.addImport("SpendingContext", "@helios-lang/ledger", true)
132
167
 
@@ -144,15 +179,39 @@ class ValdiatorArtifact extends ModuleArtifact {
144
179
  const dTypes = genTypes(datum.schema)
145
180
  const rTypes = genTypes(redeemer.schema)
146
181
 
147
- this.writeProgram("$program", program).writeDeclLine(
148
- `export const $hash: ValidatorHash<SpendingContext<${dTypes[0]}, ${dTypes[1]}, ${rTypes[0]}, ${rTypes[1]}>>`
182
+ this.writeProgram("$program", program, !this.hasMainFunction)
183
+
184
+ this.writeDeclLine(
185
+ `export type $ContextType = SpendingContext<${dTypes[0]}, ${dTypes[1]}, ${rTypes[0]}, ${rTypes[1]}>`
149
186
  )
150
- .writeDefLine(`export const $hash = /* @__PURE__ */ makeValidatorHash("${hash.toHex()}", {
187
+ .writeDeclLine(
188
+ `export const $hash: ValidatorHash<$ContextType>`
189
+ )
190
+ .writeDefLine(
191
+ `export const $hash = /* @__PURE__ */ makeValidatorHash($hashHex, {
151
192
  program: $program,
152
193
  datum: /* @__PURE__ */ makeCast(${JSON.stringify(datum.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}}),
153
194
  redeemer: /* @__PURE__ */ makeCast(${JSON.stringify(redeemer.schema, undefined, 4).split("\n").join("\n ")}, {isMainnet: ${this.isMainnet}})
154
- })`)
195
+ })`
196
+ )
155
197
  }
156
198
  }
157
199
  }
200
+
201
+ /**
202
+ * @param {string} name
203
+ * @returns {ValdiatorArtifact}
204
+ */
205
+ writeAggregateExport(name) {
206
+ super.writeAggregateExport(name)
207
+
208
+ if (name == "main") {
209
+ this.writeDefLine(
210
+ `import { $program } from "./${name}/index.js"`
211
+ ).writeDefLine(`export { $program }`)
212
+ this.hasMainFunction = true
213
+ }
214
+
215
+ return this
216
+ }
158
217
  }
@@ -25,9 +25,10 @@ export class ChildArtifactWriter extends ArtifactWriter {
25
25
  /**
26
26
  * @param {string} name
27
27
  * @param {UplcProgram} program
28
+ * @param {boolean} includeDef
28
29
  * @returns {ChildArtifactWriter}
29
30
  */
30
- writeProgram(name: string, program: UplcProgram): ChildArtifactWriter;
31
+ writeProgram(name: string, program: UplcProgram, includeDef: boolean): ChildArtifactWriter;
31
32
  }
32
33
  import { ArtifactWriter } from "./ArtifactWriter.js";
33
34
  import type { Artifact } from "./Artifact.js";
@@ -1 +1 @@
1
- {"version":3,"file":"ChildArtifactWriter.d.ts","sourceRoot":"","sources":["../../src/artifacts/ChildArtifactWriter.js"],"names":[],"mappings":"AAIA;;;GAGG;AAEH;IAaI;;;OAGG;IACH,oBAHW,QAAQ,QACR,MAAM,EAOhB;IArBD;;;OAGG;IACH,iBAFU,QAAQ,CAEZ;IAEN;;;OAGG;IACH,eAFU,MAAM,CAEZ;IAaJ;;OAEG;IACH,iBAFU,OAAO,CAIhB;IAED;;;;OAIG;IACH,mBAJW,MAAM,WACN,WAAW,GACT,mBAAmB,CAmC/B;CACJ;+BA7E8B,qBAAqB;8BAIvB,eAAe;iCADZ,mBAAmB"}
1
+ {"version":3,"file":"ChildArtifactWriter.d.ts","sourceRoot":"","sources":["../../src/artifacts/ChildArtifactWriter.js"],"names":[],"mappings":"AAIA;;;GAGG;AAEH;IAaI;;;OAGG;IACH,oBAHW,QAAQ,QACR,MAAM,EAOhB;IArBD;;;OAGG;IACH,iBAFU,QAAQ,CAEZ;IAEN;;;OAGG;IACH,eAFU,MAAM,CAEZ;IAaJ;;OAEG;IACH,iBAFU,OAAO,CAIhB;IAED;;;;;OAKG;IACH,mBALW,MAAM,WACN,WAAW,cACX,OAAO,GACL,mBAAmB,CA+C/B;CACJ;+BA1F8B,qBAAqB;8BAIvB,eAAe;iCADZ,mBAAmB"}
@@ -1 +1 @@
1
- {"version":3,"file":"RootArtifact.d.ts","sourceRoot":"","sources":["../../src/artifacts/RootArtifact.js"],"names":[],"mappings":"AAIA;;;GAGG;AAEH;;;GAGG;AACH,uDAHW,GAAG,WACH,oCAAoC,QA2B9C;0DAjCyJ,aAAa"}
1
+ {"version":3,"file":"RootArtifact.d.ts","sourceRoot":"","sources":["../../src/artifacts/RootArtifact.js"],"names":[],"mappings":"AAIA;;;GAGG;AAEH;;;GAGG;AACH,uDAHW,GAAG,WACH,oCAAoC,QAmC9C;0DAzCyJ,aAAa"}