@helios-lang/contract-utils 0.2.23 → 0.2.24

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.23",
3
+ "version": "0.2.24",
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",
@@ -14,6 +14,7 @@ import { ConstrData, UplcDataValue } from "@helios-lang/uplc"
14
14
  * arguments: {
15
15
  * name: string
16
16
  * type: TypeSchema
17
+ * isOptional: boolean
17
18
  * }[]
18
19
  * returns: TypeSchema
19
20
  * validatorIndices?: Record<string, number>
@@ -56,12 +57,22 @@ export class UserFunc {
56
57
  /**
57
58
  * @type {UplcData[]}
58
59
  */
59
- const args = this.props.arguments.map(({ name, type }) => {
60
- const rawArg = expectSome(namedArgs[name])
60
+ const args = this.props.arguments.map(
61
+ ({ name, type, isOptional }) => {
62
+ if (isOptional) {
63
+ if (name in namedArgs) {
64
+ return new ConstrData(0, [namedArgs[name]])
65
+ } else {
66
+ return new ConstrData(1, [])
67
+ }
68
+ } else {
69
+ const rawArg = expectSome(namedArgs[name])
61
70
 
62
- // TODO: apply type conversion
63
- return rawArg
64
- })
71
+ // TODO: apply type conversion
72
+ return rawArg
73
+ }
74
+ }
75
+ )
65
76
 
66
77
  if (this.props.requiresScriptContext) {
67
78
  args.push(expectSome(namedArgs["$scriptContext"]))
@@ -312,7 +312,7 @@ export class LoadedScriptsWriter {
312
312
  `export const ${v.name} = {
313
313
  $name: /** @type {const} */ ("${v.name}"),
314
314
  $purpose: /** @type {const} */ ("${v.purpose}"),
315
- ${isSome(currentScriptIndex) ? ` $currentScriptIndex: /** @type {const} */ (${currentScriptIndex}),\n` : ""}
315
+ ${isSome(currentScriptIndex) ? ` $currentScriptIndex: /** @type {const} */ (${currentScriptIndex}),` : ""}
316
316
  $sourceCode: ${JSON.stringify(v.sourceCode)},
317
317
  $dependencies: /** @type {const} */ ([${v.moduleDepedencies.join(", ")}]),
318
318
  $hashDependencies: [${v.hashDependencies.filter((d) => d != v.name).join(", ")}],
@@ -325,7 +325,7 @@ ${datumTypes ? ` $Datum: (config) => /** @type {Cast<${datumTypes[0]}, ${datu
325
325
  `export const ${v.name}: {
326
326
  $name: "${v.name}"
327
327
  $purpose: "${v.purpose}"
328
- ${isSome(currentScriptIndex) ? ` $currentScriptIndex: ${currentScriptIndex},\n` : ""}
328
+ ${isSome(currentScriptIndex) ? ` $currentScriptIndex: ${currentScriptIndex}` : ""}
329
329
  $sourceCode: string
330
330
  $dependencies: [${v.moduleDepedencies.map((d) => `typeof ${d}`).join(", ")}]
331
331
  $hashDependencies: [${v.hashDependencies
@@ -341,7 +341,7 @@ ${datumTypes ? ` $Datum: ConfigurableCast<${datumTypes[0]}, ${datumTypes[1]}>
341
341
  `export const ${v.name} = {
342
342
  $name: "${v.name}" as const,
343
343
  $purpose: "${v.purpose}" as const,
344
- ${isSome(currentScriptIndex) ? ` $currentScriptIndex: ${currentScriptIndex} as const,\n` : ""}
344
+ ${isSome(currentScriptIndex) ? ` $currentScriptIndex: ${currentScriptIndex} as const,` : ""}
345
345
  $sourceCode: ${JSON.stringify(v.sourceCode)} as string,
346
346
  $dependencies: [${v.moduleDepedencies.join(", ")}] as const,
347
347
  $hashDependencies: [${v.hashDependencies.filter((d) => d != v.name).join(", ")}],
@@ -403,10 +403,8 @@ function genFuncType(fn) {
403
403
  fields.push("$currentScript: string")
404
404
  }
405
405
 
406
- fn.arguments.forEach(({ name, type: _type }, i) => {
407
- if (!name.startsWith("_")) {
408
- fields.push(`${name}: UplcData`)
409
- }
406
+ fn.arguments.forEach(({ name, type: _type, isOptional }, i) => {
407
+ fields.push(`${name}${isOptional ? "?" : ""}: UplcData`)
410
408
  })
411
409
 
412
410
  return `(args: {${fields.join(", ")}}) => UplcData`
@@ -11,6 +11,7 @@ export {}
11
11
  * arguments: {
12
12
  * name: string
13
13
  * type: TypeSchema
14
+ * isOptional: boolean
14
15
  * }[]
15
16
  * returns: TypeSchema
16
17
  * }} TypeCheckedUserFunc
@@ -5,6 +5,7 @@
5
5
  * arguments: {
6
6
  * name: string
7
7
  * type: TypeSchema
8
+ * isOptional: boolean
8
9
  * }[]
9
10
  * returns: TypeSchema
10
11
  * validatorIndices?: Record<string, number>
@@ -45,6 +46,7 @@ export type UserFuncProps = {
45
46
  arguments: {
46
47
  name: string;
47
48
  type: TypeSchema;
49
+ isOptional: boolean;
48
50
  }[];
49
51
  returns: TypeSchema;
50
52
  validatorIndices?: Record<string, number>;
@@ -1 +1 @@
1
- {"version":3,"file":"UserFunc.d.ts","sourceRoot":"","sources":["../../src/cast/UserFunc.js"],"names":[],"mappings":"AASA;;;;;;;;;;;GAWG;AAEH;;GAEG;AACH;IAeI;;;OAGG;IACH,kBAHW,WAAW,SACX,aAAa,EAKvB;IArBD;;;;OAIG;IACH,sBAAI;IAEJ;;;;OAIG;IACH,uBAAK;IAWL;;OAEG;IACH,oBA6CC;CACJ;yBAjGY,OAAO,yBAAyB,EAAE,UAAU;uBAC5C,OAAO,mBAAmB,EAAE,QAAQ;0BACpC,OAAO,mBAAmB,EAAE,WAAW;4BAOvC;IACZ,qBAAyB,EAAE,OAAO,CAAA;IAClC,qBAAyB,EAAE,OAAO,CAAA;IAClC,SAAa,EAAE;QACf,MAAY,MAAM,CAAA;QAClB,IAAU,EAAE,UAAU,CAAA;KACjB,EAAE,CAAA;IACP,OAAW,EAAE,UAAU,CAAA;IACvB,gBAAoB,CAAC,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC,CAAA;CAC1C"}
1
+ {"version":3,"file":"UserFunc.d.ts","sourceRoot":"","sources":["../../src/cast/UserFunc.js"],"names":[],"mappings":"AASA;;;;;;;;;;;;GAYG;AAEH;;GAEG;AACH;IAeI;;;OAGG;IACH,kBAHW,WAAW,SACX,aAAa,EAKvB;IArBD;;;;OAIG;IACH,sBAAI;IAEJ;;;;OAIG;IACH,uBAAK;IAWL;;OAEG;IACH,oBAuDC;CACJ;yBA5GY,OAAO,yBAAyB,EAAE,UAAU;uBAC5C,OAAO,mBAAmB,EAAE,QAAQ;0BACpC,OAAO,mBAAmB,EAAE,WAAW;4BAOvC;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;IACvB,gBAAoB,CAAC,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC,CAAA;CAC1C"}
@@ -5,6 +5,7 @@ export type TypeCheckedUserFunc = {
5
5
  arguments: {
6
6
  name: string;
7
7
  type: TypeSchema;
8
+ isOptional: boolean;
8
9
  }[];
9
10
  returns: TypeSchema;
10
11
  };
@@ -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;KACjB,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,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"}