@helios-lang/contract-utils 0.1.2 → 0.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helios-lang/contract-utils",
3
- "version": "0.1.2",
3
+ "version": "0.1.6",
4
4
  "description": "Convenience and type-safety utilities for using Helios validators from within Typescript",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -30,10 +30,10 @@
30
30
  "singleQuote": false
31
31
  },
32
32
  "dependencies": {
33
- "@helios-lang/codec-utils": "^0.1.23",
34
- "@helios-lang/compiler-utils": "^0.1.4",
35
- "@helios-lang/ledger": "^0.1.9",
36
- "@helios-lang/type-utils": "^0.1.3",
37
- "@helios-lang/uplc": "^0.1.0"
33
+ "@helios-lang/codec-utils": "^0.1.26",
34
+ "@helios-lang/compiler-utils": "^0.1.20",
35
+ "@helios-lang/ledger": "^0.1.10",
36
+ "@helios-lang/type-utils": "^0.1.8",
37
+ "@helios-lang/uplc": "^0.1.21"
38
38
  }
39
39
  }
@@ -46,6 +46,8 @@ export function genTypes(schema) {
46
46
  return ["number[]", "number[]"]
47
47
  case "Credential":
48
48
  return ["Credential", "Credential | PubKeyHash | ValidatorHash"]
49
+ case "Data":
50
+ return ["UplcData", "UplcData"]
49
51
  case "Duration":
50
52
  return ["bigint", "bigint | number"]
51
53
  case "Int":
@@ -79,7 +81,7 @@ export function genTypes(schema) {
79
81
  case "String":
80
82
  return ["string", "string"]
81
83
  case "Time":
82
- return ["Date", "Data | bigint | number"]
84
+ return ["Date", "Date | bigint | number"]
83
85
  case "TimeRange":
84
86
  return [
85
87
  "TimeRange",
@@ -98,6 +98,14 @@ export class TypescriptWriter {
98
98
  )
99
99
  }
100
100
 
101
+ writeHeaders() {
102
+ this.wjs.writeLine("import { Cast } from \"@helios-lang/contract-utils\";")
103
+
104
+ this.wdts.writeLine("import type {UplcData} from \"@helios-lang/ledger\";")
105
+ this.wdts.writeLine("import { Address, AssetClass, Credential, DatumHash, MintingPolicyHash, PubKeyHash, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value} from \"@helios-lang/ledger\";")
106
+ this.wdts.writeLine("import { Cast } from \"@helios-lang/contract-utils\";")
107
+ }
108
+
101
109
  /**
102
110
  * @param {{[name: string]: Module}} ms
103
111
  */
@@ -3,12 +3,6 @@ export { Writer } from "./Writer.js"
3
3
 
4
4
  /**
5
5
  * @typedef {import("./Module.js").Module} Module
6
- */
7
-
8
- /**
9
6
  * @typedef {import("./TypeSchema.js").TypeSchema} TypeSchema
10
- */
11
-
12
- /**
13
7
  * @typedef {import("./Validator.js").Validator} Validator
14
8
  */
package/src/hl2ts.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { promises, readFileSync } from "node:fs"
3
+ import { promises, readFileSync, writeFileSync } from "node:fs"
4
4
  import { dirname, join, resolve } from "node:path"
5
5
  import { readHeader, translateImportPaths } from "@helios-lang/compiler-utils"
6
6
  import { TypescriptWriter } from "./codegen/index.js"
@@ -8,23 +8,16 @@ import { loadLibrary } from "./lib/index.js"
8
8
 
9
9
  /**
10
10
  * @typedef {import("./lib/Lib.js").Lib} Lib
11
- */
12
-
13
- /**
14
11
  * @typedef {import("./lib/Lib.js").SourceDetails} SourceDetails
15
- */
16
-
17
- /**
18
12
  * @typedef {import("./lib/Lib.js").ModuleDetails} ModuleDetails
19
- */
20
-
21
- /**
22
13
  * @typedef {import("./lib/Lib.js").ValidatorDetails} ValidatorDetails
23
14
  */
24
15
 
25
16
  async function main() {
26
17
  console.log("hl2ts")
27
18
 
19
+ const { outDir } = parseArgs(process.argv)
20
+
28
21
  const lib = await loadLibrary()
29
22
  const filePaths = await listFiles(process.cwd(), ".hl")
30
23
  const files = readFiles(filePaths)
@@ -32,17 +25,37 @@ async function main() {
32
25
  const { modules, validators } = typeCheck(lib, sources)
33
26
 
34
27
  const w = new TypescriptWriter()
35
- console.log(modules, validators)
28
+
36
29
  w.writeModules(modules)
37
30
  w.writeValidators(validators)
38
31
  const [js, dts] = w.finalize()
39
32
 
40
- console.log(js)
41
- console.log(dts)
33
+ const jsPath = join(outDir, "index.js")
34
+ const dtsPath = join(outDir, "index.d.ts")
35
+
36
+ writeFileSync(jsPath, js)
37
+ writeFileSync(dtsPath, dts)
42
38
  }
43
39
 
44
40
  main()
45
41
 
42
+ /**
43
+ * @param {string[]} args
44
+ * @returns {{outDir: string}}
45
+ */
46
+ function parseArgs(args) {
47
+ let i = args.indexOf("-o")
48
+ if (i != -1) {
49
+ if (i == args.length - 1) {
50
+ throw new Error("expected argument after -o")
51
+ }
52
+
53
+ return { outDir: resolve(args[i + 1]) }
54
+ } else {
55
+ return { outDir: process.cwd() }
56
+ }
57
+ }
58
+
46
59
  /**
47
60
  * @param {string} dir
48
61
  * @param {string} ext
@@ -109,7 +122,7 @@ function preparseFiles(files) {
109
122
  const sourceCode = translateImportPaths(files[path], (relPath) => {
110
123
  const absPath = resolve(join(dirname(path), relPath))
111
124
 
112
- const d = partialSources[absPath]
125
+ const d = partialSources[absPath] ?? partialSources[absPath + ".hl"]
113
126
 
114
127
  if (!d) {
115
128
  throw new Error(`'${relPath}' not found`)
@@ -327,8 +327,16 @@ export class Lib_v0_16 {
327
327
  * @returns {InternalTypeDetails}
328
328
  */
329
329
  getInternalTypeDetails(dt) {
330
- const it = dt?.typeDetails?.internalType ?? { type: "Any" }
331
-
332
- return this.convertInternalType(it)
330
+ try {
331
+ const it = dt?.typeDetails?.internalType ?? { type: "Any" }
332
+
333
+ return this.convertInternalType(it)
334
+ } catch (e) {
335
+ if (e.message.includes("Data")) {
336
+ return { primitiveType: "Data" }
337
+ } else {
338
+ throw e
339
+ }
340
+ }
333
341
  }
334
342
  }
@@ -91,6 +91,8 @@ function schemaToUplc(schema, x) {
91
91
  case "Credential":
92
92
  case "PaymentCredential":
93
93
  return Credential.fromAlike(x).toUplcData()
94
+ case "Data":
95
+ return x
94
96
  case "DatumHash":
95
97
  return DatumHash.fromAlike(x).toUplcData()
96
98
  case "DCert":
@@ -199,6 +201,8 @@ function uplcToSchema(schema, data) {
199
201
  return ByteArrayData.expect(data).bytes
200
202
  case "Credential":
201
203
  return Credential.fromUplcData(data)
204
+ case "Data":
205
+ return data
202
206
  case "DCert":
203
207
  throw new Error(
204
208
  "can't convert UplcData back into DCert (significant loss of information)"