Skip to content

Commit 0e8565a

Browse files
committed
refactor(zkappWorker.ts): reorganize and clean up the code structure for better readability and maintainability
1 parent b6b157b commit 0e8565a

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

examples/zkapps/04-zkapp-browser-ui/ui/src/pages/zkappWorker.ts

+19-33
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,51 @@ import type { Add } from '../../../contracts/src/Add';
44

55
type Transaction = Awaited<ReturnType<typeof Mina.transaction>>;
66

7-
// ---------------------------------------------------------------------------------------
8-
9-
107
const state = {
118
Add: null as null | typeof Add,
129
zkapp: null as null | Add,
1310
transaction: null as null | Transaction,
1411
};
1512

16-
// ---------------------------------------------------------------------------------------
17-
18-
// Define the worker's methods
19-
export const api = {
20-
setActiveInstanceToDevnet: async (args: {}) => {
21-
const Network = Mina.Network(
22-
'https://api.minascan.io/node/devnet/v1/graphql'
23-
);
24-
console.log('Devnet network instance configured.');
13+
const api = {
14+
async setActiveInstanceToDevnet() {
15+
const Network = Mina.Network('https://api.minascan.io/node/devnet/v1/graphql');
2516
Mina.setActiveInstance(Network);
2617
},
27-
loadContract: async (args: {}) => {
18+
async loadContract() {
2819
const { Add } = await import('../../../contracts/build/src/Add.js');
2920
state.Add = Add;
3021
},
31-
compileContract: async (args: {}) => {
22+
async compileContract() {
3223
await state.Add!.compile();
3324
},
34-
fetchAccount: async (publicKey: PublicKey) => {
35-
// console.log('args', args )
36-
// const publicKey = PublicKey.fromBase58(args.publicKey58);
37-
return await fetchAccount(publicKey);
25+
async fetchAccount(publicKey58: string) {
26+
console.log(`fetchAccount Received publicKey58: ${publicKey58}`);
27+
28+
const publicKey = PublicKey.fromBase58(publicKey58);
29+
return fetchAccount({ publicKey });
3830
},
39-
initZkappInstance: async (publicKey: PublicKey) => {
40-
console.log(publicKey)
41-
// const publicKey = PublicKey.fromBase58(args.publicKey58);
42-
// const pk = PublicKey.toBase58(publicKey)
43-
// console.log('pk', pk)
44-
console.log(state.Add)
31+
async initZkappInstance(publicKey58: string) {
32+
console.log(`initZkappInstance Received publicKey58: ${publicKey58}`);
33+
const publicKey = PublicKey.fromBase58(publicKey58);
4534
state.zkapp = new state.Add!(publicKey);
4635
},
47-
getNum: async (args: {}) => {
36+
async getNum() {
4837
const currentNum = await state.zkapp!.num.get();
4938
return JSON.stringify(currentNum.toJSON());
5039
},
51-
createUpdateTransaction: async (args: {}) => {
52-
const transaction = await Mina.transaction(async () => {
40+
async createUpdateTransaction() {
41+
state.transaction = await Mina.transaction(async () => {
5342
await state.zkapp!.update();
5443
});
55-
state.transaction = transaction;
5644
},
57-
proveUpdateTransaction: async (args: {}) => {
45+
async proveUpdateTransaction() {
5846
await state.transaction!.prove();
5947
},
60-
getTransactionJSON: async (args: {}) => {
48+
async getTransactionJSON() {
6149
return state.transaction!.toJSON();
6250
},
6351
};
6452

65-
// ---------------------------------------------------------------------------------------
66-
67-
// Expose the API to be used by the main thread
53+
// Expose the API to be used by the main thread
6854
Comlink.expose(api);

0 commit comments

Comments
 (0)