Skip to content

Commit

Permalink
feat(index.page.tsx): instantiate ZkappWorkerClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ymekuria committed Sep 19, 2024
1 parent 4ce9dd5 commit e883abe
Showing 1 changed file with 77 additions and 74 deletions.
151 changes: 77 additions & 74 deletions examples/zkapps/04-zkapp-browser-ui/ui/src/pages/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,81 +27,84 @@ export default function Home() {
// Do Setup

useEffect(() => {
async function timeout(seconds: number): Promise<void> {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, seconds * 1000);
});
}

const zkappWorkerClient = new ZkappWorkerClient();
// async function timeout(seconds: number): Promise<void> {
// return new Promise<void>((resolve) => {
// setTimeout(() => {
// resolve();
// }, seconds * 1000);
// });
// }

const zkappWorkerClient = new ZkappWorkerClient();
setState(prev => ({ ...prev, zkappWorkerClient }));

(async () => {
if (!state.hasBeenSetup) {
setDisplayText('Loading web worker...');
console.log('Loading web worker...');
const zkappWorkerClient = new ZkappWorkerClient();
await timeout(5);

setDisplayText('Done loading web worker');
console.log('Done loading web worker');

await zkappWorkerClient.setActiveInstanceToDevnet();

const mina = (window as any).mina;

if (mina == null) {
setState({ ...state, hasWallet: false });
return;
}

const publicKeyBase58: string = (await mina.requestAccounts())[0];
const publicKey = PublicKey.fromBase58(publicKeyBase58);

console.log(`Using key:${publicKey.toBase58()}`);
setDisplayText(`Using key:${publicKey.toBase58()}`);

setDisplayText('Checking if fee payer account exists...');
console.log('Checking if fee payer account exists...');

const res = await zkappWorkerClient.fetchAccount({
publicKey: publicKey!,
});
const accountExists = res.error == null;

await zkappWorkerClient.loadContract();

console.log('Compiling zkApp...');
setDisplayText('Compiling zkApp...');
await zkappWorkerClient.compileContract();
console.log('zkApp compiled');
setDisplayText('zkApp compiled...');

const zkappPublicKey = PublicKey.fromBase58(ZKAPP_ADDRESS);

await zkappWorkerClient.initZkappInstance(zkappPublicKey);

console.log('Getting zkApp state...');
setDisplayText('Getting zkApp state...');
await zkappWorkerClient.fetchAccount({ publicKey: zkappPublicKey });
const currentNum = await zkappWorkerClient.getNum();
console.log(`Current state in zkApp: ${currentNum.toString()}`);
setDisplayText('');

setState({
...state,
zkappWorkerClient,
hasWallet: true,
hasBeenSetup: true,
publicKey,
zkappPublicKey,
accountExists,
currentNum,
});
}
})();

const setup = async () => {

};
// (async () => {
// if (!state.hasBeenSetup) {
// setDisplayText('Loading web worker...');
// console.log('Loading web worker...');
// const zkappWorkerClient = new ZkappWorkerClient();
// await timeout(5);

// setDisplayText('Done loading web worker');
// console.log('Done loading web worker');

// await zkappWorkerClient.setActiveInstanceToDevnet();

// const mina = (window as any).mina;

// if (mina == null) {
// setState({ ...state, hasWallet: false });
// return;
// }

// const publicKeyBase58: string = (await mina.requestAccounts())[0];
// const publicKey = PublicKey.fromBase58(publicKeyBase58);

// console.log(`Using key:${publicKey.toBase58()}`);
// setDisplayText(`Using key:${publicKey.toBase58()}`);

// setDisplayText('Checking if fee payer account exists...');
// console.log('Checking if fee payer account exists...');

// const res = await zkappWorkerClient.fetchAccount({
// publicKey: publicKey!,
// });
// const accountExists = res.error == null;

// await zkappWorkerClient.loadContract();

// console.log('Compiling zkApp...');
// setDisplayText('Compiling zkApp...');
// await zkappWorkerClient.compileContract();
// console.log('zkApp compiled');
// setDisplayText('zkApp compiled...');

// const zkappPublicKey = PublicKey.fromBase58(ZKAPP_ADDRESS);

// await zkappWorkerClient.initZkappInstance(zkappPublicKey);

// console.log('Getting zkApp state...');
// setDisplayText('Getting zkApp state...');
// await zkappWorkerClient.fetchAccount({ publicKey: zkappPublicKey });
// const currentNum = await zkappWorkerClient.getNum();
// console.log(`Current state in zkApp: ${currentNum.toString()}`);
// setDisplayText('');

// setState({
// ...state,
// zkappWorkerClient,
// hasWallet: true,
// hasBeenSetup: true,
// publicKey,
// zkappPublicKey,
// accountExists,
// currentNum,
// });
// }
// })();
}, []);

// -------------------------------------------------------
Expand Down

0 comments on commit e883abe

Please sign in to comment.