@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.
- package/LICENSE.md +201 -0
- package/README.md +114 -0
- package/dist/chain/Chain.d.ts +109 -0
- package/dist/chain/Chain.d.ts.map +1 -0
- package/dist/chain/Chain.js +229 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/method/MethodExecutionContext.d.ts +73 -0
- package/dist/method/MethodExecutionContext.d.ts.map +1 -0
- package/dist/method/MethodExecutionContext.js +112 -0
- package/dist/method/MethodParameterDecoder.d.ts +20 -0
- package/dist/method/MethodParameterDecoder.d.ts.map +1 -0
- package/dist/method/MethodParameterDecoder.js +30 -0
- package/dist/method/RuntimeMethodExecutionContext.d.ts +57 -0
- package/dist/method/RuntimeMethodExecutionContext.d.ts.map +1 -0
- package/dist/method/RuntimeMethodExecutionContext.js +92 -0
- package/dist/method/assert.d.ts +12 -0
- package/dist/method/assert.d.ts.map +1 -0
- package/dist/method/assert.js +20 -0
- package/dist/method/decorator.d.ts +45 -0
- package/dist/method/decorator.d.ts.map +1 -0
- package/dist/method/decorator.js +140 -0
- package/dist/method/runtimeMethod.d.ts +18 -0
- package/dist/method/runtimeMethod.d.ts.map +1 -0
- package/dist/method/runtimeMethod.js +114 -0
- package/dist/module/decorator.d.ts +8 -0
- package/dist/module/decorator.d.ts.map +1 -0
- package/dist/module/decorator.js +15 -0
- package/dist/runtime/Runtime.d.ts +71 -0
- package/dist/runtime/Runtime.d.ts.map +1 -0
- package/dist/runtime/Runtime.js +185 -0
- package/dist/runtime/RuntimeModule.d.ts +30 -0
- package/dist/runtime/RuntimeModule.d.ts.map +1 -0
- package/dist/runtime/RuntimeModule.js +44 -0
- package/dist/state/InMemoryStateService.d.ts +14 -0
- package/dist/state/InMemoryStateService.d.ts.map +1 -0
- package/dist/state/InMemoryStateService.js +21 -0
- package/dist/state/State.d.ts +65 -0
- package/dist/state/State.d.ts.map +1 -0
- package/dist/state/State.js +114 -0
- package/dist/state/StateMap.d.ts +37 -0
- package/dist/state/StateMap.d.ts.map +1 -0
- package/dist/state/StateMap.js +56 -0
- package/dist/state/StateServiceProvider.d.ts +10 -0
- package/dist/state/StateServiceProvider.d.ts.map +1 -0
- package/dist/state/StateServiceProvider.js +34 -0
- package/dist/state/decorator.d.ts +7 -0
- package/dist/state/decorator.d.ts.map +1 -0
- package/dist/state/decorator.js +42 -0
- package/jest.config.cjs +1 -0
- package/package.json +36 -0
- package/src/index.ts +12 -0
- package/src/method/MethodParameterDecoder.ts +55 -0
- package/src/method/RuntimeMethodExecutionContext.ts +111 -0
- package/src/method/assert.test.ts +49 -0
- package/src/method/assert.ts +23 -0
- package/src/method/decorator.test.ts +46 -0
- package/src/method/runtimeMethod.ts +192 -0
- package/src/module/decorator.ts +21 -0
- package/src/runtime/Runtime.ts +304 -0
- package/src/runtime/RuntimeModule.ts +68 -0
- package/src/state/InMemoryStateService.ts +29 -0
- package/src/state/State.ts +154 -0
- package/src/state/StateMap.ts +69 -0
- package/src/state/StateServiceProvider.ts +24 -0
- package/src/state/decorator.ts +65 -0
- package/test/Runtime.test.ts +37 -0
- package/test/modules/Admin.ts +19 -0
- package/test/modules/Balances.test.ts +340 -0
- package/test/modules/Balances.ts +51 -0
- package/test/runtimeMethod.test.ts +43 -0
- package/test/transaction.test.ts +82 -0
- package/tsconfig.json +8 -0
- 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