@helios-lang/contract-utils 0.3.10 → 0.3.11

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.10",
3
+ "version": "0.3.11",
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",
@@ -1,11 +1,13 @@
1
+ import { bytesToHex } from "@helios-lang/codec-utils"
2
+ import { expectDefined, isRight } from "@helios-lang/type-utils"
1
3
  import {
2
- genFuncType,
3
4
  genUserFuncArgsType,
4
5
  genUserFuncRetType
5
6
  } from "../codegen/LoadedScriptsWriter.js"
6
7
  import { ChildArtifactWriter } from "./ChildArtifactWriter.js"
7
8
 
8
9
  /**
10
+ * @import { UplcProgram } from "@helios-lang/uplc"
9
11
  * @import { UserFuncProps } from "../index.js"
10
12
  * @import { Artifact } from "./Artifact.js"
11
13
  * @import { FunctionDetails } from "./symbols.js"
@@ -24,7 +26,7 @@ export function writeFunctionArtifact(parent, name, details) {
24
26
 
25
27
  artifact.writeProgram("$program", program, true)
26
28
  artifact.writeProps(props)
27
- artifact.writeEvals(props)
29
+ artifact.writeEvals(program, props)
28
30
 
29
31
  artifact.save()
30
32
 
@@ -49,13 +51,15 @@ export const $props = ${JSON.stringify(props, undefined, 4)}`)
49
51
  }
50
52
 
51
53
  /**
54
+ * @param {UplcProgram} program
52
55
  * @param {UserFuncProps} props
53
56
  */
54
- writeEvals(props) {
57
+ writeEvals(program, props) {
55
58
  const safeArgsType = genUserFuncArgsType(props)
56
59
  const unsafeArgsType = genUserFuncArgsType(props, true)
57
60
  const retType = genUserFuncRetType(props)
58
61
  const unsafeRetType = genUserFuncRetType(props, true)
62
+ const isConst = safeArgsType == "{}"
59
63
 
60
64
  this.addImport("UplcLogger", "@helios-lang/uplc", true)
61
65
  this.addImport("UplcData", "@helios-lang/uplc", true)
@@ -63,28 +67,66 @@ export const $props = ${JSON.stringify(props, undefined, 4)}`)
63
67
 
64
68
  this.addImport("evalUserFunc", "@helios-lang/contract-utils")
65
69
  .writeDeclLine(
66
- `export function $eval(args${safeArgsType == "{}" ? "?" : ""}: ${safeArgsType}, logOptions?: UplcLogger | undefined): ${retType}`
70
+ `export function $eval(args${isConst ? "?" : ""}: ${safeArgsType}, logOptions?: UplcLogger | undefined): ${retType}`
67
71
  )
68
72
  .writeDefLine(
69
- `export function $eval(args${safeArgsType == "{}" ? " = {}" : ""}, logOptions = undefined) {
73
+ `export function $eval(args${isConst ? " = {}" : ""}, logOptions = undefined) {
70
74
  return evalUserFunc($program, $props, args, logOptions)
71
75
  }`
72
76
  )
73
77
  .addImport("evalUserFuncUnsafe", "@helios-lang/contract-utils")
74
78
  .writeDeclLine(
75
- `export function $evalUnsafe(args${unsafeArgsType == "{}" ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): ${unsafeRetType}`
79
+ `export function $evalUnsafe(args${isConst ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): ${unsafeRetType}`
76
80
  )
77
81
  .writeDefLine(
78
- `export function $evalUnsafe(args${unsafeArgsType == "{}" ? " = {}" : ""}, logOptions = undefined) {
82
+ `export function $evalUnsafe(args${isConst ? " = {}" : ""}, logOptions = undefined) {
79
83
  return evalUserFuncUnsafe($program, $props, args, logOptions)
80
84
  }`
81
85
  )
82
86
  .addImport("profileUserFunc", "@helios-lang/contract-utils")
83
87
  .writeDeclLine(
84
- `export function $profile(args${unsafeArgsType == "{}" ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): CekResult`
88
+ `export function $profile(args${isConst ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): CekResult`
85
89
  )
86
- .writeDefLine(`export function $profile(args${unsafeArgsType == "{}" ? " = {}" : ""}, logOptions = undefined) {
90
+ .writeDefLine(`export function $profile(args${isConst ? " = {}" : ""}, logOptions = undefined) {
87
91
  return profileUserFunc($program, $props, args, logOptions)
88
92
  }`)
93
+
94
+ if (isConst) {
95
+ // attempt to evaluate, and add $data
96
+ try {
97
+ const result = program.eval([])
98
+
99
+ if (
100
+ isRight(result.result) &&
101
+ typeof result.result.right != "string" &&
102
+ result.result.right.kind == "data"
103
+ ) {
104
+ const data = result.result.right.value
105
+
106
+ const uplcDataType = expectDefined(
107
+ {
108
+ int: "IntData",
109
+ bytes: "ByteArrayData",
110
+ constr: "ConstrData",
111
+ map: "MapData",
112
+ list: "ListData"
113
+ }[data.kind]
114
+ )
115
+
116
+ this.addImport(uplcDataType, "@helios-lang/uplc", true)
117
+ .writeDeclLine(
118
+ `export const $constData: ${uplcDataType}`
119
+ )
120
+ .addImport("decodeUplcData", "@helios-lang/uplc", false)
121
+ .writeDefLine(
122
+ `export const $constData = /* @__PURE__ */ decodeUplcData("${bytesToHex(data.toCbor())}")`
123
+ )
124
+
125
+ // TODO: $constValue
126
+ }
127
+ } catch (_e) {
128
+ // don't do anything, simply ignore
129
+ }
130
+ }
89
131
  }
90
132
  }
@@ -1,4 +1,5 @@
1
1
  /**
2
+ * @import { UplcProgram } from "@helios-lang/uplc"
2
3
  * @import { UserFuncProps } from "../index.js"
3
4
  * @import { Artifact } from "./Artifact.js"
4
5
  * @import { FunctionDetails } from "./symbols.js"
@@ -1 +1 @@
1
- {"version":3,"file":"FunctionArtifact.d.ts","sourceRoot":"","sources":["../../src/artifacts/FunctionArtifact.js"],"names":[],"mappings":"AAOA;;;;GAIG;AAEH;;;;GAIG;AACH,8CAJW,QAAQ,QACR,MAAM,WACN,eAAe,QAezB;8BAtB4B,eAAe;qCACR,cAAc"}
1
+ {"version":3,"file":"FunctionArtifact.d.ts","sourceRoot":"","sources":["../../src/artifacts/FunctionArtifact.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AAEH;;;;GAIG;AACH,8CAJW,QAAQ,QACR,MAAM,WACN,eAAe,QAezB;8BAtB4B,eAAe;qCACR,cAAc"}