@helios-lang/contract-utils 0.1.40 → 0.2.0
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/examples/multi-validator/contract.js +11 -7
- package/examples/multi-validator/index.d.ts +19 -9
- package/examples/multi-validator/index.js +38 -14
- package/examples/multi-validator/package-lock.json +79 -5
- package/examples/multi-validator/package.json +1 -0
- package/package.json +5 -5
- package/src/cast/Cast.js +57 -13
- package/src/cast/PermissiveType.js +15 -3
- package/src/cast/StrictType.js +15 -3
- package/src/cast/index.js +19 -2
- package/src/codegen/LoadedModule.js +9 -1
- package/src/codegen/LoadedScriptsWriter.js +90 -31
- package/src/codegen/LoadedValidator.js +28 -4
- package/src/context/ContractContext.js +54 -23
- package/src/context/ContractContextBuilder.js +109 -21
- package/src/context/DagCompiler.js +18 -7
- package/src/index.js +1 -11
- package/types/cast/Cast.d.ts +42 -4
- package/types/cast/Cast.d.ts.map +1 -1
- package/types/cast/PermissiveType.d.ts +3 -2
- package/types/cast/PermissiveType.d.ts.map +1 -1
- package/types/cast/StrictType.d.ts +3 -2
- package/types/cast/StrictType.d.ts.map +1 -1
- package/types/cast/index.d.ts +6 -2
- package/types/cast/index.d.ts.map +1 -1
- package/types/codegen/LoadedModule.d.ts +6 -1
- package/types/codegen/LoadedModule.d.ts.map +1 -1
- package/types/codegen/LoadedScriptsWriter.d.ts +13 -0
- package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -1
- package/types/codegen/LoadedValidator.d.ts +19 -5
- package/types/codegen/LoadedValidator.d.ts.map +1 -1
- package/types/context/ContractContext.d.ts +28 -11
- package/types/context/ContractContext.d.ts.map +1 -1
- package/types/context/ContractContextBuilder.d.ts +58 -19
- package/types/context/ContractContextBuilder.d.ts.map +1 -1
- package/types/context/DagCompiler.d.ts +17 -7
- package/types/context/DagCompiler.d.ts.map +1 -1
- package/types/index.d.ts +1 -3
- package/types/index.d.ts.map +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ContractContextBuilder } from "@helios-lang/contract-utils"
|
|
2
|
-
import { Address, AssetClass
|
|
2
|
+
import { Address, AssetClass } from "@helios-lang/ledger"
|
|
3
|
+
import { TxBuilder } from "@helios-lang/tx-utils"
|
|
3
4
|
import { match_string, match_string_policy } from "./index.js"
|
|
4
5
|
|
|
5
6
|
export function createApp() {
|
|
@@ -7,17 +8,20 @@ export function createApp() {
|
|
|
7
8
|
.with(match_string)
|
|
8
9
|
.with(match_string_policy)
|
|
9
10
|
.build({
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
isMainnet: false,
|
|
12
|
+
expectedHashes: {
|
|
13
|
+
match_string_policy:
|
|
14
|
+
"f7c8a87011f1aec5967dd3cd68b7a2c26135d750867bfd957b4d8967" // expected hash for match_string_policy
|
|
15
|
+
}
|
|
12
16
|
})
|
|
13
17
|
|
|
14
|
-
console.log(context.match_string_policy.toHex())
|
|
18
|
+
console.log(context.match_string_policy.$hash.toHex())
|
|
15
19
|
|
|
16
|
-
const assetClass = new AssetClass(context.match_string_policy, [])
|
|
20
|
+
const assetClass = new AssetClass(context.match_string_policy.$hash, [])
|
|
17
21
|
|
|
18
|
-
const address = Address.fromHash(context.match_string)
|
|
22
|
+
const address = Address.fromHash(false, context.match_string.$hash)
|
|
19
23
|
|
|
20
|
-
let b = new
|
|
24
|
+
let b = TxBuilder.new({ isMainnet: false }).mint(assetClass, 10, true)
|
|
21
25
|
|
|
22
26
|
let c = b
|
|
23
27
|
.pay(address, 1_000_000n, { hash: { One: { message: "hello" } } })
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import type {UplcData} from "@helios-lang/ledger";
|
|
2
|
-
import { Address, AssetClass, DatumHash, MintingPolicyHash, PubKey, PubKeyHash, SpendingCredential, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value } from "@helios-lang/ledger";
|
|
3
1
|
import { Cast } from "@helios-lang/contract-utils";
|
|
2
|
+
import { Address, AssetClass, DatumHash, MintingPolicyHash, PubKey, PubKeyHash, SpendingCredential, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value } from "@helios-lang/ledger";
|
|
3
|
+
import type { IntLike } from "@helios-lang/codec-utils";
|
|
4
|
+
import type { CastConfig } from "@helios-lang/contract-utils";
|
|
5
|
+
import type { ConfigurableCast } from "@helios-lang/contract-utils";
|
|
6
|
+
import type { TimeLike } from "@helios-lang/ledger";
|
|
7
|
+
import type { UplcData } from "@helios-lang/ledger";
|
|
4
8
|
export const utils: {
|
|
5
|
-
$name:
|
|
6
|
-
$purpose:
|
|
9
|
+
$name: "utils"
|
|
10
|
+
$purpose: "module"
|
|
7
11
|
$sourceCode: string
|
|
8
12
|
$dependencies: [],
|
|
9
|
-
|
|
13
|
+
$types: {
|
|
14
|
+
MyUtilType: ConfigurableCast<{hello: bigint}, {hello: IntLike}>,
|
|
15
|
+
},
|
|
10
16
|
}
|
|
11
17
|
export const match_string_policy: {
|
|
12
18
|
$name: "match_string_policy"
|
|
@@ -15,7 +21,9 @@ export const match_string_policy: {
|
|
|
15
21
|
$dependencies: []
|
|
16
22
|
$hashDependencies: []
|
|
17
23
|
$dependsOnOwnHash: boolean
|
|
18
|
-
$Redeemer:
|
|
24
|
+
$Redeemer: ConfigurableCast<boolean, boolean>
|
|
25
|
+
$types: {
|
|
26
|
+
},
|
|
19
27
|
}
|
|
20
28
|
export const match_string: {
|
|
21
29
|
$name: "match_string"
|
|
@@ -24,7 +32,9 @@ export const match_string: {
|
|
|
24
32
|
$dependencies: [typeof utils]
|
|
25
33
|
$hashDependencies: [typeof match_string_policy]
|
|
26
34
|
$dependsOnOwnHash: boolean
|
|
27
|
-
$Redeemer:
|
|
28
|
-
$Datum:
|
|
29
|
-
|
|
35
|
+
$Redeemer: ConfigurableCast<string, string>
|
|
36
|
+
$Datum: ConfigurableCast<{One: {message: string}} | {Two: {code: bigint}}, {One: {message: string}} | {Two: {code: IntLike}}>,
|
|
37
|
+
$types: {
|
|
38
|
+
Datum: ConfigurableCast<{One: {message: string}} | {Two: {code: bigint}}, {One: {message: string}} | {Two: {code: IntLike}}>,
|
|
39
|
+
},
|
|
30
40
|
}
|
|
@@ -1,28 +1,52 @@
|
|
|
1
1
|
import { Cast } from "@helios-lang/contract-utils";
|
|
2
|
+
import { Address, AssetClass, DatumHash, MintingPolicyHash, PubKey, PubKeyHash, SpendingCredential, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value } from "@helios-lang/ledger";
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import("@helios-lang/codec-utils").IntLike} IntLike
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import("@helios-lang/contract-utils").CastConfig} CastConfig
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @template TStrict
|
|
11
|
+
* @template TPermissive
|
|
12
|
+
* @typedef {import("@helios-lang/contract-utils").ConfigurableCast<TStrict, TPermissive>} ConfigurableCast
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {import("@helios-lang/ledger").TimeLike} TimeLike
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {import("@helios-lang/ledger").UplcData} UplcData
|
|
19
|
+
*/
|
|
2
20
|
export const utils = {
|
|
3
|
-
$name: "utils",
|
|
4
|
-
$purpose: "module",
|
|
21
|
+
$name: /** @type {const} */ ("utils"),
|
|
22
|
+
$purpose: /** @type {const} */ ("module"),
|
|
5
23
|
$sourceCode: "module utils\n\nstruct MyUtilType {\n hello: Int\n}\n\nconst my_assetclass: AssetClass = AssetClass::new(Scripts::match_string_policy, #)\n\nconst my_hash: ValidatorHash = Scripts::match_string\n\nfunc compare(a: String, b: String) -> Bool {\n a == b\n}",
|
|
6
|
-
$dependencies: [],
|
|
7
|
-
|
|
24
|
+
$dependencies: /** @type {const} */ ([]),
|
|
25
|
+
$types: {
|
|
26
|
+
MyUtilType: (config) => /** @type {Cast<{hello: bigint}, {hello: IntLike}>} */ (new Cast({"structFieldTypes":[{"name":"hello","type":{"primitiveType":"Int"}}]}, config)),
|
|
27
|
+
},
|
|
8
28
|
}
|
|
9
29
|
export const match_string_policy = {
|
|
10
|
-
$name: "match_string_policy",
|
|
11
|
-
$purpose: "minting",
|
|
30
|
+
$name: /** @type {const} */ ("match_string_policy"),
|
|
31
|
+
$purpose: /** @type {const} */ ("minting"),
|
|
12
32
|
$sourceCode: "minting match_string_policy\n\nfunc main(redeemer: Bool, _) -> Bool {\n redeemer\n}",
|
|
13
|
-
$dependencies: [],
|
|
33
|
+
$dependencies: /** @type {const} */ ([]),
|
|
14
34
|
$hashDependencies: [],
|
|
15
35
|
$dependsOnOwnHash: false,
|
|
16
|
-
$Redeemer: new Cast({"primitiveType":"Bool"}),
|
|
36
|
+
$Redeemer: (config) => /** @type {Cast<boolean, boolean>} */ (new Cast({"primitiveType":"Bool"}, config)),
|
|
37
|
+
$types: {
|
|
38
|
+
},
|
|
17
39
|
}
|
|
18
40
|
export const match_string = {
|
|
19
|
-
$name: "match_string",
|
|
20
|
-
$purpose: "spending",
|
|
41
|
+
$name: /** @type {const} */ ("match_string"),
|
|
42
|
+
$purpose: /** @type {const} */ ("spending"),
|
|
21
43
|
$sourceCode: "spending match_string\n\nimport { compare, my_assetclass } from utils\n\nenum Datum {\n One {\n message: String\n }\n\n Two {\n code: Int\n }\n}\n\nfunc main(datum: Datum, redeemer: String, ctx: ScriptContext) -> Bool {\n compare(datum.switch{d: One => d.message, d: Two => d.code.show()}, redeemer) &&\n ctx.tx.minted.get(my_assetclass) > 0\n}",
|
|
22
|
-
$dependencies: [utils],
|
|
44
|
+
$dependencies: /** @type {const} */ ([utils]),
|
|
23
45
|
$hashDependencies: [match_string_policy],
|
|
24
46
|
$dependsOnOwnHash: true,
|
|
25
|
-
$Redeemer: new Cast({"primitiveType":"String"}),
|
|
26
|
-
$Datum: new Cast({"enumVariantTypes":[{"name":"One","fieldTypes":[{"name":"message","type":{"primitiveType":"String"}}]},{"name":"Two","fieldTypes":[{"name":"code","type":{"primitiveType":"Int"}}]}]}),
|
|
27
|
-
|
|
47
|
+
$Redeemer: (config) => /** @type {Cast<string, string>} */ (new Cast({"primitiveType":"String"}, config)),
|
|
48
|
+
$Datum: (config) => /** @type {Cast<{One: {message: string}} | {Two: {code: bigint}}, {One: {message: string}} | {Two: {code: IntLike}}>} */ new Cast({"enumVariantTypes":[{"name":"One","fieldTypes":[{"name":"message","type":{"primitiveType":"String"}}]},{"name":"Two","fieldTypes":[{"name":"code","type":{"primitiveType":"Int"}}]}]}, config),
|
|
49
|
+
$types: {
|
|
50
|
+
Datum: (config) => /** @type {Cast<{One: {message: string}} | {Two: {code: bigint}}, {One: {message: string}} | {Two: {code: IntLike}}>} */ (new Cast({"enumVariantTypes":[{"name":"One","fieldTypes":[{"name":"message","type":{"primitiveType":"String"}}]},{"name":"Two","fieldTypes":[{"name":"code","type":{"primitiveType":"Int"}}]}]}, config)),
|
|
51
|
+
},
|
|
28
52
|
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
"name": "spending_example",
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@helios-lang/contract-utils": "file:../..",
|
|
10
|
+
"@helios-lang/tx-utils": "^0.1.38",
|
|
10
11
|
"@hyperionbt/helios": "^0.16.7"
|
|
11
12
|
},
|
|
12
13
|
"devDependencies": {
|
|
@@ -15,14 +16,15 @@
|
|
|
15
16
|
},
|
|
16
17
|
"../..": {
|
|
17
18
|
"name": "@helios-lang/contract-utils",
|
|
18
|
-
"version": "0.1.
|
|
19
|
+
"version": "0.1.41",
|
|
19
20
|
"license": "BSD-3-Clause",
|
|
20
21
|
"dependencies": {
|
|
21
|
-
"@helios-lang/
|
|
22
|
-
"@helios-lang/
|
|
23
|
-
"@helios-lang/
|
|
22
|
+
"@helios-lang/cli-utils": "^0.1.4",
|
|
23
|
+
"@helios-lang/codec-utils": "^0.1.29",
|
|
24
|
+
"@helios-lang/compiler-utils": "^0.1.22",
|
|
25
|
+
"@helios-lang/ledger": "^0.2.1",
|
|
24
26
|
"@helios-lang/type-utils": "^0.1.8",
|
|
25
|
-
"@helios-lang/uplc": "^0.1.
|
|
27
|
+
"@helios-lang/uplc": "^0.1.27"
|
|
26
28
|
},
|
|
27
29
|
"bin": {
|
|
28
30
|
"hl2ts": "src/hl2ts.js"
|
|
@@ -36,10 +38,82 @@
|
|
|
36
38
|
"@helios-lang/compiler": "*"
|
|
37
39
|
}
|
|
38
40
|
},
|
|
41
|
+
"node_modules/@helios-lang/cbor": {
|
|
42
|
+
"version": "0.1.18",
|
|
43
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/cbor/-/cbor-0.1.18.tgz",
|
|
44
|
+
"integrity": "sha512-DgHJ4PQJqGKBLSnRD3PkZ0QjSzYrQIinEuVyK5XsdLS/s95z5/2rkPxd9v/VvbfAHdZlYT1fsV3Ei/pblxuJXw==",
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@helios-lang/codec-utils": "^0.1.29",
|
|
47
|
+
"@helios-lang/type-utils": "^0.1.8"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"node_modules/@helios-lang/codec-utils": {
|
|
51
|
+
"version": "0.1.29",
|
|
52
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/codec-utils/-/codec-utils-0.1.29.tgz",
|
|
53
|
+
"integrity": "sha512-Faa9AlB61BSed3fZ8T5t/hljeJP7tWdqPOuuAKMJPoyDIPdNhMesux0jZMRTyQByKEXE2DSq3TFvQCeAsE6zng=="
|
|
54
|
+
},
|
|
55
|
+
"node_modules/@helios-lang/compiler-utils": {
|
|
56
|
+
"version": "0.1.22",
|
|
57
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/compiler-utils/-/compiler-utils-0.1.22.tgz",
|
|
58
|
+
"integrity": "sha512-y2BEe5Gnnag7UGFTj+mOb2GdlYvuLaaBwekgHe+tFf1HYuyhfIFdSOyRdALYnh+LjQhcFhxgHzob55uyfTxdHQ==",
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@helios-lang/codec-utils": "^0.1.29",
|
|
61
|
+
"@helios-lang/type-utils": "^0.1.8"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
39
64
|
"node_modules/@helios-lang/contract-utils": {
|
|
40
65
|
"resolved": "../..",
|
|
41
66
|
"link": true
|
|
42
67
|
},
|
|
68
|
+
"node_modules/@helios-lang/crypto": {
|
|
69
|
+
"version": "0.1.8",
|
|
70
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/crypto/-/crypto-0.1.8.tgz",
|
|
71
|
+
"integrity": "sha512-06ak82XadShfEguohj9bgnlI9Dxol1Hq9s1njP7F6UJwEL3WIvm/DrMGvHzx0+jg1s50WHl2SqZnA5CC+GLdKA==",
|
|
72
|
+
"dependencies": {
|
|
73
|
+
"@helios-lang/codec-utils": "^0.1.29"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"node_modules/@helios-lang/ledger": {
|
|
77
|
+
"version": "0.2.1",
|
|
78
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/ledger/-/ledger-0.2.1.tgz",
|
|
79
|
+
"integrity": "sha512-un7pUstnpiboq3fnuIkls4tedO42febiY8s4tH/qEgVLtYz3TV3gicjwg36Bc8ff//CoUabhS3syOcgK0QNKXQ==",
|
|
80
|
+
"dependencies": {
|
|
81
|
+
"@helios-lang/cbor": "^0.1.18",
|
|
82
|
+
"@helios-lang/codec-utils": "^0.1.29",
|
|
83
|
+
"@helios-lang/crypto": "^0.1.8",
|
|
84
|
+
"@helios-lang/type-utils": "^0.1.8",
|
|
85
|
+
"@helios-lang/uplc": "^0.1.27"
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"node_modules/@helios-lang/tx-utils": {
|
|
89
|
+
"version": "0.1.38",
|
|
90
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/tx-utils/-/tx-utils-0.1.38.tgz",
|
|
91
|
+
"integrity": "sha512-Qbcgtpv8UpbL3bqBEfNG8DMx1ts8TK1KHMRlp9HlBkTMPfWvyoV1R+BLicCLN0fcMFxeG+pVuOgZFK9dG7TQ2Q==",
|
|
92
|
+
"dependencies": {
|
|
93
|
+
"@helios-lang/codec-utils": "^0.1.29",
|
|
94
|
+
"@helios-lang/crypto": "^0.1.8",
|
|
95
|
+
"@helios-lang/ledger": "^0.2.1",
|
|
96
|
+
"@helios-lang/type-utils": "^0.1.8",
|
|
97
|
+
"@helios-lang/uplc": "^0.1.27"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"node_modules/@helios-lang/type-utils": {
|
|
101
|
+
"version": "0.1.8",
|
|
102
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/type-utils/-/type-utils-0.1.8.tgz",
|
|
103
|
+
"integrity": "sha512-zVGgJvNQHWuFfK1HLybynp6RAO31yIPkEp/ztdJJcdKS3TOVaUVAr3XHaFY8Hk2Pr7qO0lD2rvfl3Hkk+GApwQ=="
|
|
104
|
+
},
|
|
105
|
+
"node_modules/@helios-lang/uplc": {
|
|
106
|
+
"version": "0.1.27",
|
|
107
|
+
"resolved": "https://registry.npmjs.org/@helios-lang/uplc/-/uplc-0.1.27.tgz",
|
|
108
|
+
"integrity": "sha512-WqFMpc2/SvZyX0ubsw/uNI+kMIK539QbeCAOXIVdDkotqC8qqXf+MeFqRepSe5T2luY1dhTnUqlRrYExd6dmvg==",
|
|
109
|
+
"dependencies": {
|
|
110
|
+
"@helios-lang/cbor": "^0.1.18",
|
|
111
|
+
"@helios-lang/codec-utils": "^0.1.29",
|
|
112
|
+
"@helios-lang/compiler-utils": "^0.1.22",
|
|
113
|
+
"@helios-lang/crypto": "^0.1.8",
|
|
114
|
+
"@helios-lang/type-utils": "^0.1.8"
|
|
115
|
+
}
|
|
116
|
+
},
|
|
43
117
|
"node_modules/@hyperionbt/helios": {
|
|
44
118
|
"version": "0.16.7",
|
|
45
119
|
"resolved": "https://registry.npmjs.org/@hyperionbt/helios/-/helios-0.16.7.tgz",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helios-lang/contract-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"@helios-lang/compiler": "*"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@helios-lang/cli-utils": "^0.1.
|
|
38
|
+
"@helios-lang/cli-utils": "^0.1.4",
|
|
39
39
|
"@helios-lang/codec-utils": "^0.1.29",
|
|
40
|
-
"@helios-lang/compiler-utils": "^0.1.
|
|
41
|
-
"@helios-lang/ledger": "^0.1
|
|
40
|
+
"@helios-lang/compiler-utils": "^0.1.22",
|
|
41
|
+
"@helios-lang/ledger": "^0.2.1",
|
|
42
42
|
"@helios-lang/type-utils": "^0.1.8",
|
|
43
|
-
"@helios-lang/uplc": "^0.1.
|
|
43
|
+
"@helios-lang/uplc": "^0.1.27"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/cast/Cast.js
CHANGED
|
@@ -38,8 +38,27 @@ import {
|
|
|
38
38
|
*/
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* @
|
|
42
|
-
*
|
|
41
|
+
* @typedef {{
|
|
42
|
+
* isMainnet: boolean
|
|
43
|
+
* }} CastConfig
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @template TStrict
|
|
48
|
+
* @template TPermissive
|
|
49
|
+
* @typedef {(config: CastConfig) => Cast<TStrict, TPermissive>} ConfigurableCast
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* StrictType and PermissiveType work for both Cast and ConfigurableCast
|
|
54
|
+
* @template TStrict
|
|
55
|
+
* @template TPermissive
|
|
56
|
+
* @typedef {Cast<TStrict, TPermissive> | ConfigurableCast<TStrict, TPermissive>} CastLike
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @template TStrict
|
|
61
|
+
* @template TPermissive
|
|
43
62
|
*/
|
|
44
63
|
export class Cast {
|
|
45
64
|
/**
|
|
@@ -48,11 +67,19 @@ export class Cast {
|
|
|
48
67
|
*/
|
|
49
68
|
schema
|
|
50
69
|
|
|
70
|
+
/**
|
|
71
|
+
* @readonly
|
|
72
|
+
* @type {CastConfig}
|
|
73
|
+
*/
|
|
74
|
+
config
|
|
75
|
+
|
|
51
76
|
/**
|
|
52
77
|
* @param {TypeSchema} schema
|
|
78
|
+
* @param {CastConfig} config
|
|
53
79
|
*/
|
|
54
|
-
constructor(schema) {
|
|
80
|
+
constructor(schema, config) {
|
|
55
81
|
this.schema = schema
|
|
82
|
+
this.config = config
|
|
56
83
|
}
|
|
57
84
|
|
|
58
85
|
/**
|
|
@@ -60,7 +87,7 @@ export class Cast {
|
|
|
60
87
|
* @returns {TStrict}
|
|
61
88
|
*/
|
|
62
89
|
fromUplcData(data) {
|
|
63
|
-
return uplcToSchema(this.schema, data)
|
|
90
|
+
return uplcToSchema(this.schema, data, this.config)
|
|
64
91
|
}
|
|
65
92
|
|
|
66
93
|
/**
|
|
@@ -196,13 +223,14 @@ function schemaToUplc(schema, x) {
|
|
|
196
223
|
* This should fail when deviating
|
|
197
224
|
* @param {TypeSchema} schema
|
|
198
225
|
* @param {UplcData} data
|
|
226
|
+
* @param {CastConfig} config
|
|
199
227
|
* @returns {any}
|
|
200
228
|
*/
|
|
201
|
-
function uplcToSchema(schema, data) {
|
|
229
|
+
function uplcToSchema(schema, data, config) {
|
|
202
230
|
if ("primitiveType" in schema) {
|
|
203
231
|
switch (schema.primitiveType) {
|
|
204
232
|
case "Address":
|
|
205
|
-
return Address.fromUplcData(data)
|
|
233
|
+
return Address.fromUplcData(config.isMainnet, data)
|
|
206
234
|
case "Any":
|
|
207
235
|
// TODO: should this throw an error?
|
|
208
236
|
return null
|
|
@@ -257,19 +285,19 @@ function uplcToSchema(schema, data) {
|
|
|
257
285
|
}
|
|
258
286
|
} else if ("listItemType" in schema) {
|
|
259
287
|
return ListData.expect(data).items.map((x) =>
|
|
260
|
-
uplcToSchema(schema.listItemType, x)
|
|
288
|
+
uplcToSchema(schema.listItemType, x, config)
|
|
261
289
|
)
|
|
262
290
|
} else if ("mapKeyType" in schema) {
|
|
263
291
|
return new Map(
|
|
264
292
|
MapData.expect(data).items.map(([k, v]) => [
|
|
265
|
-
uplcToSchema(schema.mapKeyType, k),
|
|
266
|
-
uplcToSchema(schema.mapValueType, v)
|
|
293
|
+
uplcToSchema(schema.mapKeyType, k, config),
|
|
294
|
+
uplcToSchema(schema.mapValueType, v, config)
|
|
267
295
|
])
|
|
268
296
|
)
|
|
269
297
|
} else if ("optionSomeType" in schema) {
|
|
270
298
|
const optionData = decodeOptionData(data)
|
|
271
299
|
return optionData
|
|
272
|
-
? uplcToSchema(schema.optionSomeType, optionData)
|
|
300
|
+
? uplcToSchema(schema.optionSomeType, optionData, config)
|
|
273
301
|
: None
|
|
274
302
|
} else if ("structFieldTypes" in schema) {
|
|
275
303
|
const nExpected = schema.structFieldTypes.length
|
|
@@ -278,7 +306,8 @@ function uplcToSchema(schema, data) {
|
|
|
278
306
|
return {
|
|
279
307
|
[schema.structFieldTypes[0].name]: uplcToSchema(
|
|
280
308
|
schema.structFieldTypes[0].type,
|
|
281
|
-
data
|
|
309
|
+
data,
|
|
310
|
+
config
|
|
282
311
|
)
|
|
283
312
|
}
|
|
284
313
|
} else {
|
|
@@ -293,7 +322,7 @@ function uplcToSchema(schema, data) {
|
|
|
293
322
|
return Object.fromEntries(
|
|
294
323
|
fields.map((field, i) => [
|
|
295
324
|
schema.structFieldTypes[i].name,
|
|
296
|
-
uplcToSchema(schema.structFieldTypes[i].type, field)
|
|
325
|
+
uplcToSchema(schema.structFieldTypes[i].type, field, config)
|
|
297
326
|
])
|
|
298
327
|
)
|
|
299
328
|
}
|
|
@@ -318,7 +347,7 @@ function uplcToSchema(schema, data) {
|
|
|
318
347
|
[variantSchema.name]: Object.fromEntries(
|
|
319
348
|
fields.map((f, i) => [
|
|
320
349
|
variantSchema.fieldTypes[i].name,
|
|
321
|
-
uplcToSchema(variantSchema.fieldTypes[i].type, f)
|
|
350
|
+
uplcToSchema(variantSchema.fieldTypes[i].type, f, config)
|
|
322
351
|
])
|
|
323
352
|
)
|
|
324
353
|
}
|
|
@@ -328,3 +357,18 @@ function uplcToSchema(schema, data) {
|
|
|
328
357
|
)
|
|
329
358
|
}
|
|
330
359
|
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* @template TStrict
|
|
363
|
+
* @template TPermissive
|
|
364
|
+
* @param {CastLike<TStrict, TPermissive>} cast
|
|
365
|
+
* @param {CastConfig} config
|
|
366
|
+
* @returns {Cast<TStrict, TPermissive>}
|
|
367
|
+
*/
|
|
368
|
+
export function configureCast(cast, config) {
|
|
369
|
+
if (cast instanceof Cast) {
|
|
370
|
+
return cast
|
|
371
|
+
} else {
|
|
372
|
+
return cast(config)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
export {}
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @template
|
|
5
|
-
* @
|
|
4
|
+
* @template TStrict
|
|
5
|
+
* @template TPermissive
|
|
6
|
+
* @typedef {import("./Cast.js").ConfigurableCast<TStrict, TPermissive>} ConfigurableCast
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @template TStrict
|
|
11
|
+
* @template TPermissive
|
|
12
|
+
* @typedef {import("./Cast.js").CastLike<TStrict, TPermissive>} CastLike
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @template {CastLike<any, any>} C
|
|
17
|
+
* @typedef {C extends CastLike<any, infer TPermissive> ? TPermissive : never} PermissiveType
|
|
6
18
|
*/
|
package/src/cast/StrictType.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
export {}
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @template
|
|
5
|
-
* @
|
|
4
|
+
* @template TStrict
|
|
5
|
+
* @template TPermissive
|
|
6
|
+
* @typedef {import("./Cast.js").ConfigurableCast<TStrict, TPermissive>} ConfigurableCast
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @template TStrict
|
|
11
|
+
* @template TPermissive
|
|
12
|
+
* @typedef {import("./Cast.js").CastLike<TStrict, TPermissive>} CastLike
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @template {CastLike<any, any>} C
|
|
17
|
+
* @typedef {C extends CastLike<infer TStrict, any> ? TStrict : never} StrictType
|
|
6
18
|
*/
|
package/src/cast/index.js
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
import { Cast } from "./Cast.js"
|
|
2
2
|
export { Cast }
|
|
3
|
+
export { configureCast } from "./Cast.js"
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* @
|
|
6
|
+
* @typedef {import("./Cast.js").CastConfig} CastConfig
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @template TStrict
|
|
11
|
+
* @template TPermissive
|
|
12
|
+
* @typedef {import("./Cast.js").ConfigurableCast<TStrict, TPermissive>} ConfigurableCast
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @template TStrict
|
|
17
|
+
* @template TPermissive
|
|
18
|
+
* @typedef {import("./Cast.js").CastLike<TStrict, TPermissive>} CastLike
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @template {CastLike<any, any>} C
|
|
6
23
|
* @typedef {import("./PermissiveType.js").PermissiveType<C>} PermissiveType
|
|
7
24
|
*/
|
|
8
25
|
|
|
9
26
|
/**
|
|
10
|
-
* @template {
|
|
27
|
+
* @template {CastLike<any, any>} C
|
|
11
28
|
* @typedef {import("./StrictType.js").StrictType<C>} StrictType
|
|
12
29
|
*/
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
export {}
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @template TStrict
|
|
5
|
+
* @template TPermissive
|
|
6
|
+
* @typedef {import("../cast/Cast.js").CastLike<TStrict, TPermissive>} CastLike
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
/**
|
|
4
10
|
* @typedef {{
|
|
5
11
|
* $name: string
|
|
12
|
+
* $purpose: "module"
|
|
6
13
|
* $sourceCode: string
|
|
7
|
-
* $dependencies: LoadedModule
|
|
14
|
+
* $dependencies: ReadonlyArray<LoadedModule>
|
|
15
|
+
* $types: {[name: string]: CastLike<any, any>}
|
|
8
16
|
* }} LoadedModule
|
|
9
17
|
*/
|