Skip to content

Commit 71b3f09

Browse files
authored
Merge pull request #161 from mlabs-haskell/jared/typescript-codegen
Typescript codegen
2 parents b429b2c + 6784f75 commit 71b3f09

File tree

118 files changed

+58220
-20901
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+58220
-20901
lines changed

docs/typescript-plutus/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extra-dependencies/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module MySchema
2+
3+
import Plutus.V1
4+
import Prelude (Eq, Json)
5+
6+
sum Branchy a
7+
= Node PlutusData (Extended (Branchy a))
8+
| Leaf a
9+
10+
11+
derive Json (Branchy a)
12+
derive Eq (Branchy a)
13+
derive PlutusData (Branchy a)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
_:
2+
{
3+
perSystem = { config, ... }:
4+
let
5+
tsFlake =
6+
config.lbf-nix.lbfPlutusTypescript {
7+
name = "lbf-plutus-sample-project";
8+
src = ./.;
9+
files = [ "MySchema.lbf" ];
10+
};
11+
in
12+
{
13+
packages = {
14+
inherit (tsFlake.packages) lbf-plutus-sample-project-typescript lbf-plutus-sample-project-typescript-tgz;
15+
};
16+
};
17+
}

docs/typescript-plutus/build.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{ inputs, ... }:
2+
{
3+
imports = [ ./api/lbf/build.nix ];
4+
5+
perSystem = { config, system, ... }:
6+
let
7+
tsFlake =
8+
inputs.flake-lang.lib.${system}.typescriptFlake {
9+
name = "plutus-sample-project";
10+
src = ./.;
11+
12+
npmExtraDependencies = [
13+
config.packages.lbf-plutus-sample-project-typescript-tgz
14+
];
15+
16+
devShellTools = config.settings.shell.tools;
17+
devShellHook = config.settings.shell.hook;
18+
};
19+
in
20+
{
21+
packages = {
22+
inherit (tsFlake.packages) plutus-sample-project-typescript plutus-sample-project-typescript-tgz;
23+
};
24+
25+
inherit (tsFlake) devShells checks;
26+
};
27+
}

docs/typescript-plutus/package-lock.json

Lines changed: 173 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "lambda-buffers-prelude-sample-project",
3+
"version": "1.0.0",
4+
"description": "Sample project for LambdaBuffers",
5+
"type": "module",
6+
"exports": {
7+
".": "./dist/index.mjs",
8+
"./package.json": "./package.json"
9+
},
10+
"scripts": {
11+
"build": "npx tsc -b src/",
12+
"test": ":"
13+
},
14+
"author": "Jared Pon",
15+
"license": "ISC",
16+
"files": [
17+
"./dist/**/*",
18+
"./.extra-dependencies/**/*"
19+
],
20+
"devDependencies": {
21+
"typescript": "^5.3.3"
22+
},
23+
"dependencies": {
24+
"lbf-plutus": "file:.extra-dependencies/lbf-plutus-1.0.0.tgz",
25+
"lbf-plutus-sample-project": "file:.extra-dependencies/lbf-plutus-sample-project-1.0.0.tgz",
26+
"lbf-prelude": "file:.extra-dependencies/lbf-prelude-1.0.0.tgz",
27+
"lbr-plutus": "file:.extra-dependencies/lbr-plutus-1.0.0.tgz",
28+
"lbr-prelude": "file:.extra-dependencies/lbr-prelude-1.0.0.tgz",
29+
"plutus-ledger-api": "file:.extra-dependencies/plutus-ledger-api-1.0.0.tgz",
30+
"prelude": "file:.extra-dependencies/prelude-1.0.1.tgz"
31+
}
32+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Simple plutus sample project.
2+
// To run, type
3+
// ```
4+
// npm run build && node ./dist/src/index.mjs
5+
// ```
6+
import * as LbrPlutusV1 from "lbr-plutus/V1.js";
7+
import * as LbrPlutusPd from "lbr-plutus/PlutusData.js";
8+
import * as LbrPrelude from "lbr-prelude";
9+
import * as Prelude from "prelude";
10+
import * as MySchema from "lbf-plutus-sample-project/LambdaBuffers/MySchema.mjs";
11+
12+
const pubKeyHash: LbrPlutusV1.PubKeyHash = new Uint8Array([
13+
0,
14+
0,
15+
]) as LbrPlutusV1.PubKeyHash;
16+
17+
const cred: LbrPlutusV1.Credential = {
18+
name: "PubKeyCredential",
19+
fields: pubKeyHash,
20+
};
21+
22+
console.log(`Some samples of PubKeyHash and Credential`);
23+
console.log(pubKeyHash);
24+
console.log(cred);
25+
26+
// Printing the leafy type
27+
console.log(`The Leaf constructor`);
28+
const leaf: MySchema.Branchy<LbrPlutusV1.Credential> = {
29+
name: "Leaf",
30+
fields: cred,
31+
};
32+
const leafpd: LbrPlutusV1.PlutusData = LbrPlutusPd.IsPlutusData
33+
[MySchema.Branchy](
34+
LbrPlutusPd.IsPlutusData[LbrPlutusV1.Credential],
35+
).toData(leaf);
36+
37+
console.log(leafpd);
38+
console.log(
39+
`Prelude.Json: ${
40+
Prelude.stringify(
41+
LbrPrelude.Json[MySchema.Branchy](LbrPrelude.Json[LbrPlutusV1.Credential])
42+
.toJson(leaf),
43+
)
44+
}`,
45+
);
46+
console.log(
47+
`Prelude.Json: ${
48+
Prelude.stringify(LbrPrelude.Json[LbrPlutusV1.PlutusData].toJson(leafpd))
49+
}`,
50+
);
51+
52+
console.log("fromData:");
53+
console.log(
54+
LbrPlutusPd.IsPlutusData[MySchema.Branchy](
55+
LbrPlutusPd.IsPlutusData[LbrPlutusV1.Credential],
56+
).fromData(leafpd),
57+
);
58+
59+
// Printing the branchy
60+
console.log(`The Node constructor`);
61+
const branchy: MySchema.Branchy<LbrPlutusV1.Credential> = {
62+
name: "Node",
63+
fields: [leafpd, { name: "Finite", fields: leaf }],
64+
};
65+
const branchypd: LbrPlutusV1.PlutusData = LbrPlutusPd.IsPlutusData
66+
[MySchema.Branchy](LbrPlutusPd.IsPlutusData[LbrPlutusV1.Credential]).toData(
67+
branchy,
68+
);
69+
70+
console.log("toData:");
71+
console.log(branchypd);
72+
73+
console.log(
74+
`Prelude.Json: ${
75+
Prelude.stringify(
76+
LbrPrelude.Json[MySchema.Branchy](LbrPrelude.Json[LbrPlutusV1.Credential])
77+
.toJson(branchy),
78+
)
79+
}`,
80+
);
81+
console.log(
82+
`Prelude.Json: ${
83+
Prelude.stringify(LbrPrelude.Json[LbrPlutusV1.PlutusData].toJson(branchypd))
84+
}`,
85+
);
86+
87+
console.log("fromData:");
88+
console.log(
89+
LbrPlutusPd.IsPlutusData[MySchema.Branchy](
90+
LbrPlutusPd.IsPlutusData[LbrPlutusV1.Credential],
91+
).fromData(branchypd),
92+
);
93+
94+
// Printing the Eq
95+
console.log(
96+
`branchy is branchy: ${
97+
LbrPrelude.Eq[MySchema.Branchy](LbrPrelude.Eq[LbrPlutusV1.Credential]).eq(
98+
branchy,
99+
branchy,
100+
)
101+
}`,
102+
);
103+
console.log(
104+
`branchy is not branchy: ${
105+
LbrPrelude.Eq[MySchema.Branchy](LbrPrelude.Eq[LbrPlutusV1.Credential]).neq(
106+
branchy,
107+
branchy,
108+
)
109+
}`,
110+
);
111+
console.log(
112+
`branchy is leaf: ${
113+
LbrPrelude.Eq[MySchema.Branchy](LbrPrelude.Eq[LbrPlutusV1.Credential]).eq(
114+
branchy,
115+
leaf,
116+
)
117+
}`,
118+
);
119+
console.log(
120+
`branchy is not leaf: ${
121+
LbrPrelude.Eq[MySchema.Branchy](LbrPrelude.Eq[LbrPlutusV1.Credential]).neq(
122+
branchy,
123+
leaf,
124+
)
125+
}`,
126+
);

0 commit comments

Comments
 (0)