From 3ed5ef125d1a43e1e64b9d83bc8ba188f75e0326 Mon Sep 17 00:00:00 2001 From: oemerfurkan Date: Tue, 21 Nov 2023 19:50:15 +0300 Subject: [PATCH] File initialazation with zk file --- src/Add.test.ts | 67 ------------------------------ src/Add.ts | 25 ------------ src/Square.test.ts | 7 ++++ src/Square.ts | 0 src/interact.ts | 100 --------------------------------------------- src/main.ts | 0 6 files changed, 7 insertions(+), 192 deletions(-) delete mode 100644 src/Add.test.ts delete mode 100644 src/Add.ts create mode 100644 src/Square.test.ts create mode 100644 src/Square.ts delete mode 100644 src/interact.ts create mode 100644 src/main.ts diff --git a/src/Add.test.ts b/src/Add.test.ts deleted file mode 100644 index 7c6672c..0000000 --- a/src/Add.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Add } from './Add'; -import { Field, Mina, PrivateKey, PublicKey, AccountUpdate } from 'o1js'; - -/* - * This file specifies how to test the `Add` example smart contract. It is safe to delete this file and replace - * with your own tests. - * - * See https://docs.minaprotocol.com/zkapps for more info. - */ - -let proofsEnabled = false; - -describe('Add', () => { - let deployerAccount: PublicKey, - deployerKey: PrivateKey, - senderAccount: PublicKey, - senderKey: PrivateKey, - zkAppAddress: PublicKey, - zkAppPrivateKey: PrivateKey, - zkApp: Add; - - beforeAll(async () => { - if (proofsEnabled) await Add.compile(); - }); - - beforeEach(() => { - const Local = Mina.LocalBlockchain({ proofsEnabled }); - Mina.setActiveInstance(Local); - ({ privateKey: deployerKey, publicKey: deployerAccount } = - Local.testAccounts[0]); - ({ privateKey: senderKey, publicKey: senderAccount } = - Local.testAccounts[1]); - zkAppPrivateKey = PrivateKey.random(); - zkAppAddress = zkAppPrivateKey.toPublicKey(); - zkApp = new Add(zkAppAddress); - }); - - async function localDeploy() { - const txn = await Mina.transaction(deployerAccount, () => { - AccountUpdate.fundNewAccount(deployerAccount); - zkApp.deploy(); - }); - await txn.prove(); - // this tx needs .sign(), because `deploy()` adds an account update that requires signature authorization - await txn.sign([deployerKey, zkAppPrivateKey]).send(); - } - - it('generates and deploys the `Add` smart contract', async () => { - await localDeploy(); - const num = zkApp.num.get(); - expect(num).toEqual(Field(1)); - }); - - it('correctly updates the num state on the `Add` smart contract', async () => { - await localDeploy(); - - // update transaction - const txn = await Mina.transaction(senderAccount, () => { - zkApp.update(); - }); - await txn.prove(); - await txn.sign([senderKey]).send(); - - const updatedNum = zkApp.num.get(); - expect(updatedNum).toEqual(Field(3)); - }); -}); diff --git a/src/Add.ts b/src/Add.ts deleted file mode 100644 index f0e810b..0000000 --- a/src/Add.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Field, SmartContract, state, State, method } from 'o1js'; - -/** - * Basic Example - * See https://docs.minaprotocol.com/zkapps for more info. - * - * The Add contract initializes the state variable 'num' to be a Field(1) value by default when deployed. - * When the 'update' method is called, the Add contract adds Field(2) to its 'num' contract state. - * - * This file is safe to delete and replace with your own contract. - */ -export class Add extends SmartContract { - @state(Field) num = State(); - - init() { - super.init(); - this.num.set(Field(1)); - } - - @method update() { - const currentState = this.num.getAndAssertEquals(); - const newState = currentState.add(2); - this.num.set(newState); - } -} diff --git a/src/Square.test.ts b/src/Square.test.ts new file mode 100644 index 0000000..7c88998 --- /dev/null +++ b/src/Square.test.ts @@ -0,0 +1,7 @@ +// import { Square } from './Square'; + +describe('Square.js', () => { + describe('Square()', () => { + it.todo('should be correct'); + }); +}); diff --git a/src/Square.ts b/src/Square.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/interact.ts b/src/interact.ts deleted file mode 100644 index edcd115..0000000 --- a/src/interact.ts +++ /dev/null @@ -1,100 +0,0 @@ -/** - * This script can be used to interact with the Add contract, after deploying it. - * - * We call the update() method on the contract, create a proof and send it to the chain. - * The endpoint that we interact with is read from your config.json. - * - * This simulates a user interacting with the zkApp from a browser, except that here, sending the transaction happens - * from the script and we're using your pre-funded zkApp account to pay the transaction fee. In a real web app, the user's wallet - * would send the transaction and pay the fee. - * - * To run locally: - * Build the project: `$ npm run build` - * Run with node: `$ node build/src/interact.js `. - */ -import fs from 'fs/promises'; -import { Mina, PrivateKey } from 'o1js'; -import { Add } from './Add.js'; - -// check command line arg -let deployAlias = process.argv[2]; -if (!deployAlias) - throw Error(`Missing argument. - -Usage: -node build/src/interact.js -`); -Error.stackTraceLimit = 1000; - -// parse config and private key from file -type Config = { - deployAliases: Record< - string, - { - url: string; - keyPath: string; - fee: string; - feepayerKeyPath: string; - feepayerAlias: string; - } - >; -}; -let configJson: Config = JSON.parse(await fs.readFile('config.json', 'utf8')); -let config = configJson.deployAliases[deployAlias]; -let feepayerKeysBase58: { privateKey: string; publicKey: string } = JSON.parse( - await fs.readFile(config.feepayerKeyPath, 'utf8') -); - -let zkAppKeysBase58: { privateKey: string; publicKey: string } = JSON.parse( - await fs.readFile(config.keyPath, 'utf8') -); - -let feepayerKey = PrivateKey.fromBase58(feepayerKeysBase58.privateKey); -let zkAppKey = PrivateKey.fromBase58(zkAppKeysBase58.privateKey); - -// set up Mina instance and contract we interact with -const Network = Mina.Network(config.url); -const fee = Number(config.fee) * 1e9; // in nanomina (1 billion = 1.0 mina) -Mina.setActiveInstance(Network); -let feepayerAddress = feepayerKey.toPublicKey(); -let zkAppAddress = zkAppKey.toPublicKey(); -let zkApp = new Add(zkAppAddress); - -let sentTx; -// compile the contract to create prover keys -console.log('compile the contract...'); -await Add.compile(); -try { - // call update() and send transaction - console.log('build transaction and create proof...'); - let tx = await Mina.transaction({ sender: feepayerAddress, fee }, () => { - zkApp.update(); - }); - await tx.prove(); - console.log('send transaction...'); - sentTx = await tx.sign([feepayerKey]).send(); -} catch (err) { - console.log(err); -} -if (sentTx?.hash() !== undefined) { - console.log(` -Success! Update transaction sent. - -Your smart contract state will be updated -as soon as the transaction is included in a block: -${getTxnUrl(config.url, sentTx.hash())} -`); -} - -function getTxnUrl(graphQlUrl: string, txnHash: string | undefined) { - const txnBroadcastServiceName = new URL(graphQlUrl).hostname - .split('.') - .filter((item) => item === 'minascan' || item === 'minaexplorer')?.[0]; - const networkName = new URL(graphQlUrl).hostname - .split('.') - .filter((item) => item === 'berkeley' || item === 'testworld')?.[0]; - if (txnBroadcastServiceName && networkName) { - return `https://minascan.io/${networkName}/tx/${txnHash}?type=zk-tx`; - } - return `Transaction hash: ${txnHash}`; -} diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..e69de29