Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 Updated the deprecated code in off-chain storage tutorial and file #864

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 117 additions & 3 deletions docs/zkapps/tutorials/06-offchain-storage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ For all projects, you run `zk` commands from the root of your project directory.
$ rm src/interact.ts
$ zk file src/NumberTreeContract
$ touch src/main.ts
$ touch src/utils.ts
```

1. Edit `index.ts` to import and export your new smart contract:
Expand All @@ -162,6 +163,119 @@ For all projects, you run `zk` commands from the root of your project directory.
export { NumberTreeContract };
```

1. Edit `utils.ts` to export required functions for `main.ts`:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @barriebyron ,

I am adding this utils.ts file, hope it should go here.
This file is required for main.ts, not sure why it was not mentioned in docs earlier.

I am still trying to make code work locally so will keep on pushing updates.

Thank You 😃


```ts
import { Field, Mina, PrivateKey, PublicKey, fetchAccount } from 'o1js';

export const loopUntilAccountExists = async ({
account,
eachTimeNotExist,
isZkAppAccount,
}: {
account: PublicKey;
eachTimeNotExist: () => void;
isZkAppAccount: boolean;
}) => {
for (;;) {
let response = await fetchAccount({ publicKey: account });
let accountExists = response.error == null;
if (isZkAppAccount && response.account?.zkapp) {
accountExists = accountExists && response.account.zkapp.appState != null;
}
if (!accountExists) {
await eachTimeNotExist();
await new Promise((resolve) => setTimeout(resolve, 5000));
} else {
return response.account!;
}
}
};

interface ToString {
toString: () => string;
}

type FetchedAccountResponse = Awaited<ReturnType<typeof fetchAccount>>;
type FetchedAccount = NonNullable<FetchedAccountResponse['account']>;

export const makeAndSendTransaction = async <State extends ToString>({
feePayerPrivateKey,
zkAppPublicKey,
mutateZkApp,
transactionFee,
getState,
statesEqual,
}: {
feePayerPrivateKey: PrivateKey;
zkAppPublicKey: PublicKey;
mutateZkApp: () => void;
transactionFee: number;
getState: () => State;
statesEqual: (state1: State, state2: State) => boolean;
}) => {
const initialState = getState();

await fetchAccount({ publicKey: feePayerPrivateKey.toPublicKey() });

let transaction = await Mina.transaction(
{ feePayerKey: feePayerPrivateKey, fee: transactionFee },
() => {
mutateZkApp();
}
);

console.log('Creating an execution proof...');
const time0 = Date.now();
await transaction.prove();
const time1 = Date.now();
console.log('creating proof took', (time1 - time0) / 1e3, 'seconds');

console.log('Sending the transaction...');
const res = await transaction.send();
const hash = await res.hash();
if (hash == null) {
console.log('error sending transaction (see above)');
} else {
console.log(
'See transaction at',
'https://berkeley.minaexplorer.com/transaction/' + hash
);
}

let state = getState();

let stateChanged = false;
while (!stateChanged) {
console.log(
'waiting for zkApp state to change... (current state: ',
state.toString() + ')'
);
await new Promise((resolve) => setTimeout(resolve, 5000));
await fetchAccount({ publicKey: zkAppPublicKey });
state = await getState();
stateChanged = !statesEqual(initialState, state);
}
};

export const zkAppNeedsInitialization = async ({
zkAppAccount,
}: {
zkAppAccount: FetchedAccount;
}) => {
console.warn(
'warning: using a `utils.ts` written before `isProved` made available. Check https://docs.minaprotocol.com/zkapps/tutorials/deploying-to-a-live-network for updates'
);

const allZeros = zkAppAccount.zkapp?.appState!.every((f: Field) =>
f.equals(Field(0)).toBoolean()
);
const needsInitialization = allZeros;
return needsInitialization;
};

```

1. Now, add the experimental library for the off-chain storage server:

```sh
Expand Down Expand Up @@ -280,13 +394,13 @@ Initialize the zkApp state for these three values by setting:
storedNewRootSignature: Signature
) {
const storedRoot = this.storageTreeRoot.get();
this.storageTreeRoot.assertEquals(storedRoot);
this.storageTreeRoot.requireEquals(storedRoot);

let storedNumber = this.storageNumber.get();
this.storageNumber.assertEquals(storedNumber);
this.storageNumber.requireEquals(storedNumber);

let storageServerPublicKey = this.storageServerPublicKey.get();
this.storageServerPublicKey.assertEquals(storageServerPublicKey);
this.storageServerPublicKey.requireEquals(storageServerPublicKey);

let leaf = [oldNum];
let newLeaf = [num];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export class NumberTreeContract extends SmartContract {
storedNewRootSignature: Signature
) {
const storedRoot = this.storageTreeRoot.get();
this.storageTreeRoot.assertEquals(storedRoot);
this.storageTreeRoot.requireEquals(storedRoot);

let storedNumber = this.storageNumber.get();
this.storageNumber.assertEquals(storedNumber);
this.storageNumber.requireEquals(storedNumber);

let storageServerPublicKey = this.storageServerPublicKey.get();
this.storageServerPublicKey.assertEquals(storageServerPublicKey);
this.storageServerPublicKey.requireEquals(storageServerPublicKey);

let leaf = [oldNum];
let newLeaf = [num];
Expand Down