@helios-lang/contract-utils 0.2.34 → 0.2.35

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.2.34",
3
+ "version": "0.2.35",
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",
@@ -4,6 +4,7 @@ import { Cast } from "./Cast.js"
4
4
 
5
5
  /**
6
6
  * @typedef {import("@helios-lang/type-utils").TypeSchema} TypeSchema
7
+ * @typedef {import("@helios-lang/uplc").CekResult} CekResult
7
8
  * @typedef {import("@helios-lang/uplc").UplcData} UplcData
8
9
  * @typedef {import("@helios-lang/uplc").UplcProgram} UplcProgram
9
10
  * @typedef {import("@helios-lang/uplc").UplcValue} UplcValue
@@ -102,6 +103,22 @@ export class UserFunc {
102
103
  * @returns {UplcData}
103
104
  */
104
105
  evalUnsafe(namedArgs) {
106
+ const result = this.profile(namedArgs).result
107
+
108
+ if (isLeft(result)) {
109
+ throw new Error(result.left.error)
110
+ } else if (result.right instanceof UplcDataValue) {
111
+ return result.right.value
112
+ } else {
113
+ throw new Error(`${result.right.toString()} isn't a UplcDataValue`)
114
+ }
115
+ }
116
+
117
+ /**
118
+ * @param {UnsafeArgsT<ArgsT>} namedArgs
119
+ * @returns {CekResult}
120
+ */
121
+ profile(namedArgs) {
105
122
  /**
106
123
  * @type {UplcData[]}
107
124
  */
@@ -113,10 +130,7 @@ export class UserFunc {
113
130
  return new ConstrData(1, [])
114
131
  }
115
132
  } else {
116
- const rawArg = expectSome(namedArgs[name])
117
-
118
- // TODO: apply type conversion
119
- return rawArg
133
+ return expectSome(namedArgs[name])
120
134
  }
121
135
  })
122
136
 
@@ -132,32 +146,50 @@ export class UserFunc {
132
146
  const index = expectSome(
133
147
  expectSome(this.props.validatorIndices)[currentScriptName]
134
148
  )
149
+
135
150
  args.push(new ConstrData(index, []))
136
151
  }
137
152
 
138
153
  const argValues = args.map((a) => new UplcDataValue(a))
139
154
 
140
- const result = this.uplc.eval(argValues).result
155
+ const cekResult = this.uplc.eval(argValues)
141
156
 
142
157
  if ("alt" in this.uplc && this.uplc.alt instanceof UplcProgramV2) {
143
- const resultUnoptim = this.uplc.alt.eval(argValues).result
158
+ const cekResultUnoptim = this.uplc.alt.eval(argValues)
159
+ const resultUnoptim = cekResultUnoptim.result
144
160
  const resultUnoptimStr = evalResultToString(resultUnoptim)
145
- const resultStr = evalResultToString(result)
161
+ const resultStr = evalResultToString(cekResult.result)
146
162
 
147
163
  if (resultStr != resultUnoptimStr) {
148
164
  throw new Error(
149
165
  `Critical error: contact Helios maintainers, expected ${resultUnoptimStr}, got ${resultStr}`
150
166
  )
151
167
  }
152
- }
153
168
 
154
- if (isLeft(result)) {
155
- throw new Error(result.left.error)
156
- } else if (result.right instanceof UplcDataValue) {
157
- return result.right.value
158
- } else {
159
- throw new Error(`${result.right.toString()} isn't a UplcDataValue`)
169
+ // also make sure the cost is an improvement
170
+ if (cekResult.cost.mem > cekResultUnoptim.cost.mem) {
171
+ throw new Error(
172
+ `Critical error: optimizer worsened memory cost of ${this.props.name}`
173
+ )
174
+ }
175
+
176
+ if (cekResult.cost.cpu > cekResultUnoptim.cost.cpu) {
177
+ throw new Error(
178
+ `Critical error: optimizer worsened cpu cost of ${this.props.name}`
179
+ )
180
+ }
181
+
182
+ if (
183
+ cekResult.cost.mem == cekResultUnoptim.cost.mem &&
184
+ cekResult.cost.cpu == cekResultUnoptim.cost.cpu
185
+ ) {
186
+ console.error(
187
+ `Warning: optimizer didn't improve cost of ${this.props.name}`
188
+ )
189
+ }
160
190
  }
191
+
192
+ return cekResult
161
193
  }
162
194
  }
163
195
 
@@ -6,6 +6,7 @@ export {}
6
6
 
7
7
  /**
8
8
  * @typedef {{
9
+ * name: string
9
10
  * requiresScriptContext: boolean
10
11
  * requiresCurrentScript: boolean
11
12
  * arguments: {
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * @typedef {import("@helios-lang/type-utils").TypeSchema} TypeSchema
3
+ * @typedef {import("@helios-lang/uplc").CekResult} CekResult
3
4
  * @typedef {import("@helios-lang/uplc").UplcData} UplcData
4
5
  * @typedef {import("@helios-lang/uplc").UplcProgram} UplcProgram
5
6
  * @typedef {import("@helios-lang/uplc").UplcValue} UplcValue
@@ -58,8 +59,14 @@ export class UserFunc<ArgsT extends {
58
59
  * @returns {UplcData}
59
60
  */
60
61
  evalUnsafe(namedArgs: UnsafeArgsT<ArgsT>): UplcData;
62
+ /**
63
+ * @param {UnsafeArgsT<ArgsT>} namedArgs
64
+ * @returns {CekResult}
65
+ */
66
+ profile(namedArgs: UnsafeArgsT<ArgsT>): CekResult;
61
67
  }
62
68
  export type TypeSchema = import("@helios-lang/type-utils").TypeSchema;
69
+ export type CekResult = import("@helios-lang/uplc").CekResult;
63
70
  export type UplcData = import("@helios-lang/uplc").UplcData;
64
71
  export type UplcProgram = import("@helios-lang/uplc").UplcProgram;
65
72
  export type UplcValue = import("@helios-lang/uplc").UplcValue;
@@ -1 +1 @@
1
- {"version":3,"file":"UserFunc.d.ts","sourceRoot":"","sources":["../../src/cast/UserFunc.js"],"names":[],"mappings":"AAIA;;;;;;GAMG;AAEH;;;;;;;;;;;;;;GAcG;AAEH;;;GAGG;AAEH;;;GAGG;AACH;;;IAeI;;;OAGG;IACH,kBAHW,WAAW,SACX,aAAa,EAKvB;IArBD;;;;OAIG;IACH,sBAAI;IAEJ;;;;OAIG;IACH,uBAAK;IAWL;;;OAGG;IACH,gBAHW,KAAK,GACH,IAAI,CAkChB;IAED;;;OAGG;IACH,sBAHW,YAAY,KAAK,CAAC,GAChB,QAAQ,CA2DpB;CACJ;yBA5JY,OAAO,yBAAyB,EAAE,UAAU;uBAC5C,OAAO,mBAAmB,EAAE,QAAQ;0BACpC,OAAO,mBAAmB,EAAE,WAAW;wBACvC,OAAO,mBAAm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EAAE,OAAO,CAAA;KACpB,EAAE,CAAA;IACP,OAAW,EAAE,UAAU,CAAA;IACvB,gBAAoB,CAAC,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7C,UAAc,EAAE,UAAU,CAAA;CACvB"}
@@ -1,5 +1,6 @@
1
1
  export type TypeSchema = import("./TypeSchema.js").TypeSchema;
2
2
  export type TypeCheckedUserFunc = {
3
+ name: string;
3
4
  requiresScriptContext: boolean;
4
5
  requiresCurrentScript: boolean;
5
6
  arguments: {
@@ -1 +1 @@
1
- {"version":3,"file":"TypeCheckedModule.d.ts","sourceRoot":"","sources":["../../src/codegen/TypeCheckedModule.js"],"names":[],"mappings":"yBAGa,OAAO,iBAAiB,EAAE,UAAU;kCAIpC;IACZ,qBAAyB,EAAE,OAAO,CAAA;IAClC,qBAAyB,EAAE,OAAO,CAAA;IAClC,SAAa,EAAE;QACf,MAAY,MAAM,CAAA;QAClB,IAAU,EAAE,UAAU,CAAA;QACtB,UAAgB,EAAE,OAAO,CAAA;KACpB,EAAE,CAAA;IACP,OAAW,EAAE,UAAU,CAAA;CACpB;gCAIS;IACZ,MAAU,MAAM,CAAA;IAChB,OAAW,EAAE,MAAM,CAAA;IACnB,UAAc,EAAE,MAAM,CAAA;IACtB,iBAAqB,EAAE,MAAM,EAAE,CAAA;IAC/B,KAAS,EAAE,OAAO,MAAM,EAAE,UAAU,CAAC,CAAA;IACrC,SAAa,CAAC,EAAE,OAAO,MAAM,EAAE,mBAAmB,CAAC,CAAA;CAChD"}
1
+ {"version":3,"file":"TypeCheckedModule.d.ts","sourceRoot":"","sources":["../../src/codegen/TypeCheckedModule.js"],"names":[],"mappings":"yBAGa,OAAO,iBAAiB,EAAE,UAAU;kCAIpC;IACZ,MAAU,MAAM,CAAA;IAChB,qBAAyB,EAAE,OAAO,CAAA;IAClC,qBAAyB,EAAE,OAAO,CAAA;IAClC,SAAa,EAAE;QACf,MAAY,MAAM,CAAA;QAClB,IAAU,EAAE,UAAU,CAAA;QACtB,UAAgB,EAAE,OAAO,CAAA;KACpB,EAAE,CAAA;IACP,OAAW,EAAE,UAAU,CAAA;CACpB;gCAIS;IACZ,MAAU,MAAM,CAAA;IAChB,OAAW,EAAE,MAAM,CAAA;IACnB,UAAc,EAAE,MAAM,CAAA;IACtB,iBAAqB,EAAE,MAAM,EAAE,CAAA;IAC/B,KAAS,EAAE,OAAO,MAAM,EAAE,UAAU,CAAC,CAAA;IACrC,SAAa,CAAC,EAAE,OAAO,MAAM,EAAE,mBAAmB,CAAC,CAAA;CAChD"}