@proto-kit/module 0.1.1-develop.153

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.
Files changed (75) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +114 -0
  3. package/dist/chain/Chain.d.ts +109 -0
  4. package/dist/chain/Chain.d.ts.map +1 -0
  5. package/dist/chain/Chain.js +229 -0
  6. package/dist/index.d.ts +13 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +12 -0
  9. package/dist/method/MethodExecutionContext.d.ts +73 -0
  10. package/dist/method/MethodExecutionContext.d.ts.map +1 -0
  11. package/dist/method/MethodExecutionContext.js +112 -0
  12. package/dist/method/MethodParameterDecoder.d.ts +20 -0
  13. package/dist/method/MethodParameterDecoder.d.ts.map +1 -0
  14. package/dist/method/MethodParameterDecoder.js +30 -0
  15. package/dist/method/RuntimeMethodExecutionContext.d.ts +57 -0
  16. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +1 -0
  17. package/dist/method/RuntimeMethodExecutionContext.js +92 -0
  18. package/dist/method/assert.d.ts +12 -0
  19. package/dist/method/assert.d.ts.map +1 -0
  20. package/dist/method/assert.js +20 -0
  21. package/dist/method/decorator.d.ts +45 -0
  22. package/dist/method/decorator.d.ts.map +1 -0
  23. package/dist/method/decorator.js +140 -0
  24. package/dist/method/runtimeMethod.d.ts +18 -0
  25. package/dist/method/runtimeMethod.d.ts.map +1 -0
  26. package/dist/method/runtimeMethod.js +114 -0
  27. package/dist/module/decorator.d.ts +8 -0
  28. package/dist/module/decorator.d.ts.map +1 -0
  29. package/dist/module/decorator.js +15 -0
  30. package/dist/runtime/Runtime.d.ts +71 -0
  31. package/dist/runtime/Runtime.d.ts.map +1 -0
  32. package/dist/runtime/Runtime.js +185 -0
  33. package/dist/runtime/RuntimeModule.d.ts +30 -0
  34. package/dist/runtime/RuntimeModule.d.ts.map +1 -0
  35. package/dist/runtime/RuntimeModule.js +44 -0
  36. package/dist/state/InMemoryStateService.d.ts +14 -0
  37. package/dist/state/InMemoryStateService.d.ts.map +1 -0
  38. package/dist/state/InMemoryStateService.js +21 -0
  39. package/dist/state/State.d.ts +65 -0
  40. package/dist/state/State.d.ts.map +1 -0
  41. package/dist/state/State.js +114 -0
  42. package/dist/state/StateMap.d.ts +37 -0
  43. package/dist/state/StateMap.d.ts.map +1 -0
  44. package/dist/state/StateMap.js +56 -0
  45. package/dist/state/StateServiceProvider.d.ts +10 -0
  46. package/dist/state/StateServiceProvider.d.ts.map +1 -0
  47. package/dist/state/StateServiceProvider.js +34 -0
  48. package/dist/state/decorator.d.ts +7 -0
  49. package/dist/state/decorator.d.ts.map +1 -0
  50. package/dist/state/decorator.js +42 -0
  51. package/jest.config.cjs +1 -0
  52. package/package.json +36 -0
  53. package/src/index.ts +12 -0
  54. package/src/method/MethodParameterDecoder.ts +55 -0
  55. package/src/method/RuntimeMethodExecutionContext.ts +111 -0
  56. package/src/method/assert.test.ts +49 -0
  57. package/src/method/assert.ts +23 -0
  58. package/src/method/decorator.test.ts +46 -0
  59. package/src/method/runtimeMethod.ts +192 -0
  60. package/src/module/decorator.ts +21 -0
  61. package/src/runtime/Runtime.ts +304 -0
  62. package/src/runtime/RuntimeModule.ts +68 -0
  63. package/src/state/InMemoryStateService.ts +29 -0
  64. package/src/state/State.ts +154 -0
  65. package/src/state/StateMap.ts +69 -0
  66. package/src/state/StateServiceProvider.ts +24 -0
  67. package/src/state/decorator.ts +65 -0
  68. package/test/Runtime.test.ts +37 -0
  69. package/test/modules/Admin.ts +19 -0
  70. package/test/modules/Balances.test.ts +340 -0
  71. package/test/modules/Balances.ts +51 -0
  72. package/test/runtimeMethod.test.ts +43 -0
  73. package/test/transaction.test.ts +82 -0
  74. package/tsconfig.json +8 -0
  75. package/tsconfig.test.json +9 -0
@@ -0,0 +1,51 @@
1
+ import { Bool, PublicKey, UInt64 } from "snarkyjs";
2
+ import { Option } from "@proto-kit/protocol";
3
+ import { Presets } from "@proto-kit/common";
4
+
5
+ import { State } from "../../src/state/State.js";
6
+ import { state } from "../../src/state/decorator.js";
7
+ import { StateMap } from "../../src/state/StateMap.js";
8
+ import { RuntimeModule, runtimeMethod, runtimeModule } from "../../src";
9
+
10
+ import { Admin } from "./Admin.js";
11
+
12
+ interface BalancesConfig {
13
+ test: Bool;
14
+ }
15
+
16
+ @runtimeModule()
17
+ export class Balances extends RuntimeModule<BalancesConfig> {
18
+ /**
19
+ * We use `satisfies` here in order to be able to access
20
+ * presets by key in a type safe way.
21
+ */
22
+ public static presets = {} satisfies Presets<BalancesConfig>;
23
+
24
+ @state() public totalSupply = State.from<UInt64>(UInt64);
25
+
26
+ @state() public balances = StateMap.from<PublicKey, UInt64>(
27
+ PublicKey,
28
+ UInt64
29
+ );
30
+
31
+ public constructor(public admin: Admin) {
32
+ super();
33
+ }
34
+
35
+ @runtimeMethod()
36
+ public getTotalSupply() {
37
+ this.totalSupply.get();
38
+ }
39
+
40
+ @runtimeMethod()
41
+ public setTotalSupply() {
42
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
43
+ this.totalSupply.set(UInt64.from(20));
44
+ this.admin.isAdmin(this.transaction.sender);
45
+ }
46
+
47
+ @runtimeMethod()
48
+ public getBalance(address: PublicKey): Option<UInt64> {
49
+ return this.balances.get(address);
50
+ }
51
+ }
@@ -0,0 +1,43 @@
1
+ import "reflect-metadata";
2
+ import { Bool, PublicKey } from "snarkyjs";
3
+
4
+ import { InMemoryStateService, Runtime } from "../src";
5
+ import { MethodParameterDecoder } from "../src/method/MethodParameterDecoder";
6
+
7
+ import { Balances } from "./modules/Balances";
8
+
9
+ describe("runtimeMethod", () => {
10
+ const parameters = [PublicKey.empty()];
11
+
12
+ it("should create correct param types", () => {
13
+ // eslint-disable-next-line jest/prefer-expect-assertions
14
+ expect.assertions(1 + parameters.length);
15
+
16
+ const runtime = Runtime.from({
17
+ state: new InMemoryStateService(),
18
+
19
+ modules: {
20
+ Balances,
21
+ },
22
+
23
+ config: {
24
+ Balances: {
25
+ test: Bool(true),
26
+ },
27
+ },
28
+ });
29
+
30
+ const module = runtime.resolve("Balances");
31
+
32
+ const decoder = MethodParameterDecoder.fromMethod(module, "getBalance");
33
+ const recodedParameters = decoder.fromFields(
34
+ parameters.flatMap((x) => x.toFields())
35
+ );
36
+
37
+ expect(parameters).toHaveLength(recodedParameters.length);
38
+
39
+ parameters.forEach((parameter, index) => {
40
+ expect(parameter).toStrictEqual(recodedParameters[index]);
41
+ });
42
+ });
43
+ });
@@ -0,0 +1,82 @@
1
+ import "reflect-metadata";
2
+ import { PrivateKey, Proof, PublicKey, UInt64 } from "snarkyjs";
3
+ import { container } from "tsyringe";
4
+ import {
5
+ InMemoryStateService,
6
+ MethodPublicOutput,
7
+ Runtime,
8
+ runtimeMethod,
9
+ RuntimeMethodExecutionContext,
10
+ } from "../src";
11
+ import { runtimeModule } from "../src/module/decorator";
12
+ import { RuntimeModule } from "../src/runtime/RuntimeModule";
13
+ import { state } from "../src/state/decorator";
14
+ import { StateMap } from "../src/state/StateMap";
15
+
16
+ interface BalancesConfig {}
17
+ @runtimeModule()
18
+ class Balances extends RuntimeModule<BalancesConfig> {
19
+ @state() public balances = StateMap.from<PublicKey, UInt64>(
20
+ PublicKey,
21
+ UInt64
22
+ );
23
+
24
+ @runtimeMethod()
25
+ public setBalance(to: PublicKey, amount: UInt64) {
26
+ this.balances.set(to, amount);
27
+ }
28
+
29
+ @runtimeMethod()
30
+ public transfer(from: PublicKey, to: PublicKey, amount: UInt64) {
31
+ const fromBalance = this.balances.get(from);
32
+ const toBalance = this.balances.get(to);
33
+
34
+ this.balances.set(from, fromBalance.value.sub(amount));
35
+ this.balances.set(to, toBalance.value.add(amount));
36
+ }
37
+ }
38
+
39
+ describe("transaction", () => {
40
+ it("should update the state", async () => {
41
+ expect.assertions(0);
42
+
43
+ const runtime = Runtime.from({
44
+ modules: {
45
+ Balances,
46
+ },
47
+
48
+ config: {
49
+ Balances: {},
50
+ },
51
+
52
+ state: new InMemoryStateService(),
53
+ });
54
+
55
+ const alice = PrivateKey.random().toPublicKey();
56
+ const bob = PrivateKey.random().toPublicKey();
57
+
58
+ const balances = runtime.resolve("Balances");
59
+
60
+ runtime.transaction(() => {
61
+ balances.setBalance(alice, UInt64.from(1000));
62
+ });
63
+
64
+ runtime.transaction(() => {
65
+ balances.transfer(alice, bob, UInt64.from(100));
66
+ });
67
+
68
+ const executionContext = container.resolve<RuntimeMethodExecutionContext>(
69
+ RuntimeMethodExecutionContext
70
+ );
71
+
72
+ const balanceAlice = balances.balances.get(alice);
73
+ const balanceBob = balances.balances.get(bob);
74
+
75
+ console.log("balances", {
76
+ // 900
77
+ alice: balanceAlice.value.toString(),
78
+ // 100
79
+ bob: balanceBob.value.toString(),
80
+ });
81
+ });
82
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist"
5
+ },
6
+ "include": ["./src", "src/index.ts"],
7
+ "exclude": ["./dist/**/*.ts", "./**/*.test.ts", "./test/**/*.ts"]
8
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./../../tsconfig.json",
3
+ "include": [
4
+ "./src/**/*.test.ts",
5
+ "./test/**/*.ts",
6
+ "./test/*.ts",
7
+ "./src/**/*.ts"
8
+ ]
9
+ }