@proto-kit/module 0.1.1-develop.600 → 0.1.1-develop.833

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 (108) hide show
  1. package/LICENSE.md +201 -201
  2. package/dist/method/MethodParameterDecoder.d.ts +10 -10
  3. package/dist/method/MethodParameterDecoder.js +40 -40
  4. package/dist/method/MethodParameterEncoder.d.ts +15 -8
  5. package/dist/method/MethodParameterEncoder.d.ts.map +1 -1
  6. package/dist/method/MethodParameterEncoder.js +122 -48
  7. package/dist/method/runtimeMethod.d.ts +6 -5
  8. package/dist/method/runtimeMethod.d.ts.map +1 -1
  9. package/dist/method/runtimeMethod.js +13 -26
  10. package/dist/runtime/MethodIdResolver.d.ts +7 -0
  11. package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
  12. package/dist/runtime/MethodIdResolver.js +35 -2
  13. package/dist/runtime/Runtime.d.ts +6 -5
  14. package/dist/runtime/Runtime.d.ts.map +1 -1
  15. package/dist/runtime/Runtime.js +29 -22
  16. package/dist/runtime/RuntimeEnvironment.d.ts +2 -2
  17. package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -1
  18. package/dist/runtime/RuntimeModule.d.ts +2 -2
  19. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  20. package/dist/runtime/RuntimeModule.js +10 -8
  21. package/dist/src/factories/MethodIdFactory.d.ts +10 -0
  22. package/dist/src/factories/MethodIdFactory.d.ts.map +1 -0
  23. package/dist/src/factories/MethodIdFactory.js +10 -0
  24. package/dist/src/index.d.ts +11 -0
  25. package/dist/src/index.d.ts.map +1 -0
  26. package/dist/src/index.js +10 -0
  27. package/dist/src/method/MethodParameterEncoder.d.ts +22 -0
  28. package/dist/src/method/MethodParameterEncoder.d.ts.map +1 -0
  29. package/dist/src/method/MethodParameterEncoder.js +103 -0
  30. package/dist/src/method/runtimeMethod.d.ts +26 -0
  31. package/dist/src/method/runtimeMethod.d.ts.map +1 -0
  32. package/dist/src/method/runtimeMethod.js +164 -0
  33. package/dist/src/module/decorator.d.ts +8 -0
  34. package/dist/src/module/decorator.d.ts.map +1 -0
  35. package/dist/src/module/decorator.js +15 -0
  36. package/dist/src/runtime/MethodIdResolver.d.ts +20 -0
  37. package/dist/src/runtime/MethodIdResolver.d.ts.map +1 -0
  38. package/dist/src/runtime/MethodIdResolver.js +100 -0
  39. package/dist/src/runtime/Runtime.d.ts +71 -0
  40. package/dist/src/runtime/Runtime.d.ts.map +1 -0
  41. package/dist/src/runtime/Runtime.js +215 -0
  42. package/dist/src/runtime/RuntimeEnvironment.d.ts +10 -0
  43. package/dist/src/runtime/RuntimeEnvironment.d.ts.map +1 -0
  44. package/dist/src/runtime/RuntimeEnvironment.js +1 -0
  45. package/dist/src/runtime/RuntimeModule.d.ts +26 -0
  46. package/dist/src/runtime/RuntimeModule.d.ts.map +1 -0
  47. package/dist/src/runtime/RuntimeModule.js +85 -0
  48. package/dist/src/state/InMemoryStateService.d.ts +15 -0
  49. package/dist/src/state/InMemoryStateService.d.ts.map +1 -0
  50. package/dist/src/state/InMemoryStateService.js +28 -0
  51. package/dist/src/state/decorator.d.ts +7 -0
  52. package/dist/src/state/decorator.d.ts.map +1 -0
  53. package/dist/src/state/decorator.js +39 -0
  54. package/dist/state/InMemoryStateService.d.ts +10 -6
  55. package/dist/state/InMemoryStateService.d.ts.map +1 -1
  56. package/dist/state/InMemoryStateService.js +10 -8
  57. package/dist/state/decorator.d.ts.map +1 -1
  58. package/dist/state/decorator.js +0 -3
  59. package/dist/test/Runtime.test.d.ts +2 -0
  60. package/dist/test/Runtime.test.d.ts.map +1 -0
  61. package/dist/test/Runtime.test.js +24 -0
  62. package/dist/test/TestingRuntime.d.ts +7 -0
  63. package/dist/test/TestingRuntime.d.ts.map +1 -0
  64. package/dist/test/TestingRuntime.js +29 -0
  65. package/dist/test/method/runtimeMethod.test.d.ts +2 -0
  66. package/dist/test/method/runtimeMethod.test.d.ts.map +1 -0
  67. package/dist/test/method/runtimeMethod.test.js +30 -0
  68. package/dist/test/modules/Admin.d.ts +10 -0
  69. package/dist/test/modules/Admin.d.ts.map +1 -0
  70. package/dist/test/modules/Admin.js +29 -0
  71. package/dist/test/modules/Balances.d.ts +23 -0
  72. package/dist/test/modules/Balances.d.ts.map +1 -0
  73. package/dist/test/modules/Balances.js +98 -0
  74. package/dist/test/modules/Balances.test.d.ts +2 -0
  75. package/dist/test/modules/Balances.test.d.ts.map +1 -0
  76. package/dist/test/modules/Balances.test.js +201 -0
  77. package/dist/test/modules/MethodIdResolver.test.d.ts +2 -0
  78. package/dist/test/modules/MethodIdResolver.test.d.ts.map +1 -0
  79. package/dist/test/modules/MethodIdResolver.test.js +67 -0
  80. package/dist/test/modules/State.test.d.ts +2 -0
  81. package/dist/test/modules/State.test.d.ts.map +1 -0
  82. package/dist/test/modules/State.test.js +42 -0
  83. package/dist/test/runtimeMethod.test.d.ts +2 -0
  84. package/dist/test/runtimeMethod.test.d.ts.map +1 -0
  85. package/dist/test/runtimeMethod.test.js +50 -0
  86. package/package.json +5 -5
  87. package/src/method/MethodParameterEncoder.ts +192 -81
  88. package/src/method/runtimeMethod.ts +29 -44
  89. package/src/runtime/MethodIdResolver.ts +51 -1
  90. package/src/runtime/Runtime.ts +48 -29
  91. package/src/runtime/RuntimeEnvironment.ts +4 -7
  92. package/src/runtime/RuntimeModule.ts +13 -15
  93. package/src/state/InMemoryStateService.ts +13 -13
  94. package/src/state/decorator.ts +1 -3
  95. package/test/Runtime.test.ts +68 -41
  96. package/test/TestingRuntime.ts +45 -0
  97. package/test/method/MethodParameterEncoder.test.ts +152 -0
  98. package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +2 -2
  99. package/test/modules/Admin.ts +3 -3
  100. package/test/modules/Balances.test.ts +57 -61
  101. package/test/modules/Balances.ts +15 -18
  102. package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +14 -23
  103. package/test/modules/State.test.ts +41 -50
  104. package/test/runtimeMethod.test.ts +19 -32
  105. package/test/tsconfig.json +7 -0
  106. package/tsconfig.json +2 -2
  107. package/test/transaction.test.ts +0 -82
  108. package/tsconfig.test.json +0 -9
@@ -1,9 +1,9 @@
1
1
  import { PublicKey } from "o1js";
2
+ import { assert } from "@proto-kit/protocol";
2
3
 
3
4
  import { runtimeModule } from "../../src/module/decorator.js";
4
5
  import { RuntimeModule } from "../../src/runtime/RuntimeModule.js";
5
6
  import { runtimeMethod } from "../../src/method/runtimeMethod.js";
6
- import { assert } from "@proto-kit/protocol";
7
7
 
8
8
  interface AdminConfig {
9
9
  publicKey: string;
@@ -12,8 +12,8 @@ interface AdminConfig {
12
12
  @runtimeModule()
13
13
  export class Admin extends RuntimeModule<AdminConfig> {
14
14
  @runtimeMethod()
15
- public isAdmin(publicKey: PublicKey) {
16
- const admin = PublicKey.empty().toConstant();
15
+ public async isAdmin(publicKey: PublicKey) {
16
+ const admin = PublicKey.empty<typeof PublicKey>().toConstant();
17
17
  assert(admin.equals(publicKey));
18
18
  }
19
19
  }
@@ -1,25 +1,18 @@
1
- /* eslint-disable max-lines */
2
1
  import "reflect-metadata";
3
- import {
4
- Bool,
5
- Field,
6
- Poseidon,
7
- PrivateKey,
8
- Proof,
9
- PublicKey,
10
- UInt64,
11
- } from "o1js";
2
+ import { Field, Poseidon, PrivateKey, Proof, PublicKey, UInt64 } from "o1js";
12
3
  import { container } from "tsyringe";
13
4
  import {
14
5
  type ProvableStateTransition,
15
6
  Path,
16
7
  MethodPublicOutput,
17
- StateService,
8
+ SimpleAsyncStateService,
18
9
  RuntimeMethodExecutionContext,
10
+ RuntimeTransaction,
11
+ NetworkState,
19
12
  } from "@proto-kit/protocol";
20
13
 
21
- import { InMemoryStateService } from "../../src/state/InMemoryStateService.js";
22
14
  import { Runtime } from "../../src";
15
+ import { createTestingRuntime } from "../TestingRuntime";
23
16
 
24
17
  import { Balances } from "./Balances.js";
25
18
  import { Admin } from "./Admin.js";
@@ -27,19 +20,19 @@ import { Admin } from "./Admin.js";
27
20
  describe("balances", () => {
28
21
  let balances: Balances;
29
22
 
30
- let state: StateService;
23
+ let state: SimpleAsyncStateService;
31
24
 
32
25
  let runtime: Runtime<{
33
26
  Admin: typeof Admin;
34
27
  Balances: typeof Balances;
35
28
  }>;
36
29
 
37
- function getStateValue(path: Field | undefined) {
30
+ async function getStateValue(path: Field | undefined) {
38
31
  if (!path) {
39
32
  throw new Error("Path not found");
40
33
  }
41
34
 
42
- const stateValue = state.get(path);
35
+ const stateValue = await state.get(path);
43
36
 
44
37
  if (!stateValue) {
45
38
  throw new Error("stateValue is undefined");
@@ -49,47 +42,26 @@ describe("balances", () => {
49
42
  }
50
43
 
51
44
  function createChain() {
52
- state = new InMemoryStateService();
53
-
54
- runtime = Runtime.from({
55
- state,
56
-
57
- modules: {
58
- Admin,
45
+ ({ runtime, state } = createTestingRuntime(
46
+ {
59
47
  Balances,
48
+ Admin,
60
49
  },
61
- });
62
-
63
- runtime.dependencyContainer.register("AreProofsEnabled", {
64
- useValue: {
65
- areProofsEnabled: false,
66
-
67
- setProofsEnabled(areProofsEnabled: boolean) {
68
- this.areProofsEnabled = areProofsEnabled;
50
+ {
51
+ Admin: {
52
+ publicKey: PublicKey.empty<typeof PublicKey>().toBase58(),
69
53
  },
70
- },
71
- });
72
54
 
73
- runtime.configure({
74
- Admin: {
75
- publicKey: PublicKey.empty().toBase58(),
76
- },
77
-
78
- Balances: {
79
- test: Bool(true),
80
- },
81
- });
55
+ Balances: {},
56
+ }
57
+ ));
82
58
 
83
59
  balances = runtime.resolve("Balances");
84
60
 
85
- state.set(
86
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
87
- balances.totalSupply.path!,
88
- UInt64.from(10).toFields()
89
- );
61
+ state.set(balances.totalSupply.path!, UInt64.from(10).toFields());
90
62
  }
91
63
 
92
- describe("compile and prove", () => {
64
+ describe.skip("compile and prove", () => {
93
65
  beforeAll(createChain);
94
66
 
95
67
  // Disabled until we implement a mechanism to enable/disable compiling tests
@@ -99,13 +71,18 @@ describe("balances", () => {
99
71
  runtime.zkProgrammable.appChain?.setProofsEnabled(true);
100
72
 
101
73
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
74
+ executionContext.setup({
75
+ transaction: RuntimeTransaction.dummyTransaction(),
76
+ networkState: NetworkState.empty(),
77
+ });
78
+
102
79
  const expectedStateTransitionsHash =
103
80
  "1439144406936083177718146178121957896974210157062549589517697792374542035761";
104
81
  const expectedStatus = true;
105
82
 
106
83
  await runtime.zkProgrammable.zkProgram.compile();
107
84
 
108
- balances.getTotalSupply();
85
+ await balances.getTotalSupply();
109
86
 
110
87
  const { result } = executionContext.current();
111
88
 
@@ -130,11 +107,15 @@ describe("balances", () => {
130
107
  describe("state transitions", () => {
131
108
  let stateTransitions: ProvableStateTransition[];
132
109
 
133
- beforeEach(() => {
110
+ beforeEach(async () => {
134
111
  const executionContext = container.resolve(
135
112
  RuntimeMethodExecutionContext
136
113
  );
137
- balances.getTotalSupply();
114
+ executionContext.setup({
115
+ transaction: RuntimeTransaction.dummyTransaction(),
116
+ networkState: NetworkState.empty(),
117
+ });
118
+ await balances.getTotalSupply();
138
119
 
139
120
  stateTransitions = executionContext
140
121
  .current()
@@ -158,13 +139,13 @@ describe("balances", () => {
158
139
  );
159
140
  });
160
141
 
161
- it("should produce a from-only state transition", () => {
142
+ it("should produce a from-only state transition", async () => {
162
143
  expect.assertions(3);
163
144
 
164
145
  const [stateTransition] = stateTransitions;
165
146
 
166
147
  const value = UInt64.fromFields(
167
- getStateValue(balances.totalSupply.path)
148
+ await getStateValue(balances.totalSupply.path)
168
149
  );
169
150
  const treeValue = Poseidon.hash(value.toFields());
170
151
 
@@ -181,15 +162,20 @@ describe("balances", () => {
181
162
 
182
163
  beforeAll(() => {
183
164
  createChain();
184
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165
+
185
166
  state.set(balances.totalSupply.path!, undefined);
186
167
  });
187
168
 
188
- beforeEach(() => {
169
+ beforeEach(async () => {
189
170
  const executionContext = container.resolve(
190
171
  RuntimeMethodExecutionContext
191
172
  );
192
- balances.getTotalSupply();
173
+ executionContext.setup({
174
+ transaction: RuntimeTransaction.dummyTransaction(),
175
+ networkState: NetworkState.empty(),
176
+ });
177
+
178
+ await balances.getTotalSupply();
193
179
 
194
180
  stateTransitions = executionContext
195
181
  .current()
@@ -235,11 +221,16 @@ describe("balances", () => {
235
221
  describe("state transitions", () => {
236
222
  let stateTransitions: ProvableStateTransition[];
237
223
 
238
- beforeEach(() => {
224
+ beforeEach(async () => {
239
225
  const executionContext = container.resolve(
240
226
  RuntimeMethodExecutionContext
241
227
  );
242
- balances.setTotalSupply();
228
+ executionContext.setup({
229
+ transaction: RuntimeTransaction.dummyTransaction(),
230
+ networkState: NetworkState.empty(),
231
+ });
232
+
233
+ await balances.setTotalSupply();
243
234
 
244
235
  stateTransitions = executionContext
245
236
  .current()
@@ -263,12 +254,12 @@ describe("balances", () => {
263
254
  );
264
255
  });
265
256
 
266
- it("should produce a from-to state transition", () => {
257
+ it("should produce a from-to state transition", async () => {
267
258
  expect.assertions(4);
268
259
 
269
260
  const [stateTransition] = stateTransitions;
270
261
  const fromValue = UInt64.fromFields(
271
- getStateValue(balances.totalSupply.path)
262
+ await getStateValue(balances.totalSupply.path)
272
263
  );
273
264
  const fromTreeValue = Poseidon.hash(fromValue.toFields());
274
265
 
@@ -295,11 +286,16 @@ describe("balances", () => {
295
286
  let stateTransitions: ProvableStateTransition[];
296
287
  const address = PrivateKey.random().toPublicKey();
297
288
 
298
- beforeEach(() => {
289
+ beforeEach(async () => {
299
290
  const executionContext = container.resolve(
300
291
  RuntimeMethodExecutionContext
301
292
  );
302
- balances.getBalance(address);
293
+ executionContext.setup({
294
+ transaction: RuntimeTransaction.dummyTransaction(),
295
+ networkState: NetworkState.empty(),
296
+ });
297
+
298
+ await balances.getBalance(address);
303
299
 
304
300
  stateTransitions = executionContext
305
301
  .current()
@@ -1,14 +1,12 @@
1
- import { Bool, PublicKey, UInt64 } from "o1js";
2
- import { Option, State, StateMap } from "@proto-kit/protocol";
1
+ import { PublicKey, UInt64 } from "o1js";
2
+ import { State, StateMap } from "@proto-kit/protocol";
3
3
  import { Presets } from "@proto-kit/common";
4
4
 
5
5
  import { RuntimeModule, runtimeMethod, runtimeModule, state } from "../../src";
6
6
 
7
7
  import { Admin } from "./Admin.js";
8
8
 
9
- interface BalancesConfig {
10
- test: Bool;
11
- }
9
+ interface BalancesConfig {}
12
10
 
13
11
  @runtimeModule()
14
12
  export class Balances extends RuntimeModule<BalancesConfig> {
@@ -30,28 +28,27 @@ export class Balances extends RuntimeModule<BalancesConfig> {
30
28
  }
31
29
 
32
30
  @runtimeMethod()
33
- public getTotalSupply() {
34
- this.totalSupply.get();
31
+ public async getTotalSupply() {
32
+ await this.totalSupply.get();
35
33
  }
36
34
 
37
35
  @runtimeMethod()
38
- public setTotalSupply() {
39
- // eslint-disable-next-line @typescript-eslint/no-magic-numbers
40
- this.totalSupply.set(UInt64.from(20));
41
- this.admin.isAdmin(this.transaction.sender);
36
+ public async setTotalSupply() {
37
+ await this.totalSupply.set(UInt64.from(20));
38
+ await this.admin.isAdmin(this.transaction.sender.value);
42
39
  }
43
40
 
44
41
  @runtimeMethod()
45
- public getBalance(address: PublicKey): Option<UInt64> {
46
- return this.balances.get(address);
42
+ public async getBalance(address: PublicKey) {
43
+ (await this.balances.get(address)).orElse(UInt64.zero);
47
44
  }
48
45
 
49
46
  @runtimeMethod()
50
- public transientState() {
51
- const totalSupply = this.totalSupply.get();
52
- this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(100));
47
+ public async transientState() {
48
+ const totalSupply = await this.totalSupply.get();
49
+ await this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(100));
53
50
 
54
- const totalSupply2 = this.totalSupply.get();
55
- this.totalSupply.set(totalSupply2.orElse(UInt64.zero).add(100));
51
+ const totalSupply2 = await this.totalSupply.get();
52
+ await this.totalSupply.set(totalSupply2.orElse(UInt64.zero).add(100));
56
53
  }
57
54
  }
@@ -1,17 +1,14 @@
1
1
  import "reflect-metadata";
2
- import { Bool, Field } from "o1js";
3
- import { beforeAll, beforeEach } from "@jest/globals";
2
+ import { assert } from "@proto-kit/protocol";
3
+ import { Field } from "o1js";
4
+ import { beforeAll } from "@jest/globals";
5
+ import { container } from "tsyringe";
4
6
 
5
7
  import { Runtime } from "../../src/runtime/Runtime";
6
8
  import { MethodIdResolver } from "../../src/runtime/MethodIdResolver";
7
- import {
8
- assert,
9
- InMemoryStateService,
10
- runtimeMethod,
11
- RuntimeModule,
12
- runtimeModule,
13
- } from "../../src";
14
- import { container } from "tsyringe";
9
+ import { runtimeMethod, RuntimeModule, runtimeModule } from "../../src";
10
+ import { createTestingRuntime } from "../TestingRuntime";
11
+
15
12
  import { Balances } from "./Balances";
16
13
 
17
14
  interface AdminConfig {}
@@ -19,7 +16,7 @@ interface AdminConfig {}
19
16
  @runtimeModule()
20
17
  class Admin extends RuntimeModule<AdminConfig> {
21
18
  @runtimeMethod()
22
- public isAdminWithAVeryVeryVeryVeryLongName() {
19
+ public async isAdminWithAVeryVeryVeryVeryLongName() {
23
20
  assert(Field(1).equals(Field(1)));
24
21
  }
25
22
  }
@@ -31,22 +28,16 @@ describe("methodId", () => {
31
28
  beforeAll(() => {
32
29
  container.clearInstances();
33
30
 
34
- runtime = Runtime.from({
35
- modules: {
31
+ ({ runtime } = createTestingRuntime(
32
+ {
36
33
  Admin,
37
34
  Balance: Balances,
38
35
  },
39
-
40
- config: {
36
+ {
41
37
  Admin: {},
42
- Balance: {
43
- test: Bool(true),
44
- },
45
- },
46
-
47
- state: new InMemoryStateService(),
48
- });
49
- runtime.start();
38
+ Balance: {},
39
+ }
40
+ ));
50
41
 
51
42
  resolver =
52
43
  runtime.dependencyContainer.resolve<MethodIdResolver>("MethodIdResolver");
@@ -1,60 +1,41 @@
1
1
  import "reflect-metadata";
2
- import { Bool, PublicKey, UInt64 } from "o1js";
2
+ import { PublicKey, UInt64 } from "o1js";
3
3
  import { container } from "tsyringe";
4
4
  import {
5
5
  NetworkState,
6
6
  Option,
7
7
  RuntimeMethodExecutionContext,
8
8
  RuntimeTransaction,
9
- StateService,
10
9
  } from "@proto-kit/protocol";
10
+ import { expectDefined } from "@proto-kit/common";
11
11
 
12
- import { InMemoryStateService, Runtime } from "../../src";
12
+ import { Runtime } from "../../src";
13
+ import { createTestingRuntime } from "../TestingRuntime";
13
14
 
14
15
  import { Admin } from "./Admin";
15
16
  import { Balances } from "./Balances";
16
17
 
17
- describe("transient state", () => {
18
+ describe("state", () => {
18
19
  let balances: Balances;
19
20
 
20
- let state: StateService;
21
-
22
21
  let runtime: Runtime<{
23
22
  Admin: typeof Admin;
24
23
  Balances: typeof Balances;
25
24
  }>;
26
25
 
27
26
  function createChain() {
28
- state = new InMemoryStateService();
29
-
30
- runtime = Runtime.from({
31
- state,
32
-
33
- modules: {
27
+ ({ runtime } = createTestingRuntime(
28
+ {
34
29
  Admin,
35
30
  Balances,
36
31
  },
37
- });
38
-
39
- runtime.dependencyContainer.register("AreProofsEnabled", {
40
- useValue: {
41
- areProofsEnabled: false,
42
-
43
- setProofsEnabled(areProofsEnabled: boolean) {
44
- this.areProofsEnabled = areProofsEnabled;
32
+ {
33
+ Admin: {
34
+ publicKey: PublicKey.empty<typeof PublicKey>().toBase58(),
45
35
  },
46
- },
47
- });
48
-
49
- runtime.configure({
50
- Admin: {
51
- publicKey: PublicKey.empty().toBase58(),
52
- },
53
-
54
- Balances: {
55
- test: Bool(true),
56
- },
57
- });
36
+ Balances: {},
37
+ }
38
+ ));
58
39
 
59
40
  balances = runtime.resolve("Balances");
60
41
  }
@@ -63,28 +44,38 @@ describe("transient state", () => {
63
44
  createChain();
64
45
  });
65
46
 
66
- it("should track previously set state", () => {
67
- const executionContext = container.resolve(RuntimeMethodExecutionContext);
68
- executionContext.setup({
69
- networkState: new NetworkState({ block: { height: UInt64.zero } }),
70
- transaction: undefined as unknown as RuntimeTransaction,
47
+ describe("state decorator", () => {
48
+ it("should decorate state properties correctly", () => {
49
+ expectDefined(balances.totalSupply.path);
71
50
  });
72
- balances.transientState();
51
+ });
52
+
53
+ describe("transient state", () => {
54
+ it("should track previously set state", async () => {
55
+ expect.assertions(2);
73
56
 
74
- const stateTransitions = executionContext
75
- .current()
76
- .result.stateTransitions.map((stateTransition) =>
77
- stateTransition.toProvable()
78
- );
57
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
58
+ executionContext.setup({
59
+ networkState: NetworkState.empty(),
60
+ transaction: RuntimeTransaction.dummyTransaction(),
61
+ });
62
+ await balances.transientState();
79
63
 
80
- const expectedLastOption = Option.fromValue(
81
- UInt64.from(200),
82
- UInt64
83
- ).toProvable();
64
+ const stateTransitions = executionContext
65
+ .current()
66
+ .result.stateTransitions.map((stateTransition) =>
67
+ stateTransition.toProvable()
68
+ );
84
69
 
85
- const last = stateTransitions.at(-1);
70
+ const expectedLastOption = Option.fromValue(
71
+ UInt64.from(200),
72
+ UInt64
73
+ ).toProvable();
86
74
 
87
- expect(last).toBeDefined();
88
- expect(last!.to.value).toStrictEqual(expectedLastOption.value);
75
+ const last = stateTransitions.at(-1);
76
+
77
+ expect(last).toBeDefined();
78
+ expect(last!.to.value).toStrictEqual(expectedLastOption.value);
79
+ });
89
80
  });
90
81
  });
@@ -1,5 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { Bool, Field, PublicKey, UInt64 } from "o1js";
2
+ import { PublicKey } from "o1js";
3
3
  import {
4
4
  NetworkState,
5
5
  RuntimeMethodExecutionContext,
@@ -8,43 +8,37 @@ import {
8
8
  import { container } from "tsyringe";
9
9
  import { AreProofsEnabled, log } from "@proto-kit/common";
10
10
 
11
- import { InMemoryStateService, MethodIdResolver, Runtime } from "../src";
12
- import { MethodParameterEncoder } from "../src/method/MethodParameterEncoder";
11
+ import { Runtime, MethodParameterEncoder } from "../src";
13
12
 
14
13
  import { Balances } from "./modules/Balances";
14
+ import { createTestingRuntime } from "./TestingRuntime";
15
15
 
16
16
  describe("runtimeMethod", () => {
17
- const parameters = [PublicKey.empty()];
17
+ const parameters = [PublicKey.empty<typeof PublicKey>()];
18
18
 
19
19
  let runtime: Runtime<{ Balances: typeof Balances }>;
20
20
 
21
21
  beforeEach(() => {
22
22
  log.setLevel(log.levels.DEBUG);
23
- runtime = Runtime.from({
24
- state: new InMemoryStateService(),
25
-
26
- modules: {
23
+ ({ runtime } = createTestingRuntime(
24
+ {
27
25
  Balances,
28
26
  },
29
-
30
- config: {
31
- Balances: {
32
- test: Bool(true),
33
- },
34
- },
35
- });
36
- runtime.start();
27
+ {
28
+ Balances: {},
29
+ }
30
+ ));
37
31
  });
38
32
 
39
- it("should create correct param types", () => {
40
- // eslint-disable-next-line jest/prefer-expect-assertions
33
+ it("should create correct param types", async () => {
41
34
  expect.assertions(1 + parameters.length);
42
35
 
43
36
  const module = runtime.resolve("Balances");
44
37
 
45
38
  const decoder = MethodParameterEncoder.fromMethod(module, "getBalance");
46
- const recodedParameters = decoder.fromFields(
47
- parameters.flatMap((x) => x.toFields())
39
+ const recodedParameters = await decoder.decode(
40
+ parameters.flatMap((x) => x.toFields()),
41
+ []
48
42
  );
49
43
 
50
44
  expect(parameters).toHaveLength(recodedParameters.length);
@@ -65,24 +59,17 @@ describe("runtimeMethod", () => {
65
59
  } as AreProofsEnabled,
66
60
  });
67
61
 
68
- const transaction = new RuntimeTransaction({
69
- methodId: Field(0),
70
- nonce: UInt64.zero,
71
- argsHash: Field(0),
72
- sender: PublicKey.empty(),
73
- });
74
-
75
62
  context.setup({
76
- transaction,
77
- networkState: new NetworkState({ block: { height: UInt64.zero } }),
63
+ transaction: RuntimeTransaction.dummyTransaction(),
64
+ networkState: NetworkState.empty(),
78
65
  });
79
66
 
80
67
  const module = runtime.resolve("Balances");
81
- module.getBalance(PublicKey.empty());
68
+ await module.getBalance(PublicKey.empty<typeof PublicKey>());
82
69
 
83
70
  context.setup({
84
- transaction,
85
- networkState: new NetworkState({ block: { height: UInt64.zero } }),
71
+ transaction: RuntimeTransaction.dummyTransaction(),
72
+ networkState: NetworkState.empty(),
86
73
  });
87
74
 
88
75
  await expect(context.current().result.prover!()).rejects.toThrow(
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "experimentalDecorators": true
5
+ },
6
+ "include": ["../src/**/*.test.ts", "./**/*.ts", "./*.ts", "../src/**/*.ts"]
7
+ }
package/tsconfig.json CHANGED
@@ -3,6 +3,6 @@
3
3
  "compilerOptions": {
4
4
  "outDir": "./dist"
5
5
  },
6
- "include": ["./src", "src/index.ts"],
7
- "exclude": ["./dist/**/*.ts", "./**/*.test.ts", "./test/**/*.ts"]
6
+ "include": ["./src/index.ts"],
7
+ "exclude": ["./dist/**/*.ts"]
8
8
  }