testing_adding_number
Advanced tools
| 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 = true; | ||
| 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.addOne(); | ||
| }); | ||
| await txn.prove(); | ||
| await txn.sign([senderKey]).send(); | ||
| const updatedNum = zkApp.num.get(); | ||
| expect(updatedNum).toEqual(Field(2)); | ||
| }); | ||
| }); |
+34
| import { Field, SmartContract, state, State, method, PublicKey } 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<Field>(); | ||
| @state(PublicKey) lastSender = State<PublicKey>(); | ||
| init() { | ||
| super.init(); | ||
| this.num.set(Field(1)); | ||
| } | ||
| @method addOne() { | ||
| const currentState = this.num.getAndAssertEquals(); | ||
| const newState = currentState.add(1); | ||
| this.num.set(newState); | ||
| this.lastSender.set(this.sender); | ||
| } | ||
| @method addNum(addNum: Field) { | ||
| const currentState = this.num.getAndAssertEquals(); | ||
| const newState = currentState.add(addNum); | ||
| this.num.set(newState); | ||
| this.lastSender.set(this.sender); | ||
| } | ||
| } |
| import { Add } from './Add.js'; | ||
| export { Add }; |
| /** | ||
| * 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 <deployAlias>`. | ||
| */ | ||
| import { Mina, PrivateKey } from 'o1js'; | ||
| import fs from 'fs/promises'; | ||
| import { Add } from './Add.js'; | ||
| // check command line arg | ||
| let deployAlias = process.argv[2]; | ||
| if (!deployAlias) | ||
| throw Error(`Missing <deployAlias> argument. | ||
| Usage: | ||
| node build/src/interact.js <deployAlias> | ||
| `); | ||
| 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.addOne(); | ||
| }); | ||
| 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: | ||
| https://berkeley.minaexplorer.com/transaction/${sentTx.hash()} | ||
| `); | ||
| } |
@@ -9,3 +9,3 @@ import { Add } from './Add'; | ||
| */ | ||
| let proofsEnabled = false; | ||
| let proofsEnabled = true; | ||
| describe('Add', () => { | ||
@@ -51,5 +51,5 @@ let deployerAccount, deployerKey, senderAccount, senderKey, zkAppAddress, zkAppPrivateKey, zkApp; | ||
| const updatedNum = zkApp.num.get(); | ||
| expect(updatedNum).toEqual(Field(3)); | ||
| expect(updatedNum).toEqual(Field(2)); | ||
| }); | ||
| }); | ||
| //# sourceMappingURL=Add.test.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"Add.test.js","sourceRoot":"","sources":["../../src/Add.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAC5B,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAa,aAAa,EAAE,MAAM,MAAM,CAAC;AAEzE;;;;;GAKG;AAEH,IAAI,aAAa,GAAG,KAAK,CAAC;AAE1B,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE;IACnB,IAAI,eAA0B,EAC5B,WAAuB,EACvB,aAAwB,EACxB,SAAqB,EACrB,YAAuB,EACvB,eAA2B,EAC3B,KAAU,CAAC;IAEb,SAAS,CAAC,KAAK,IAAI,EAAE;QACnB,IAAI,aAAa;YAAE,MAAM,GAAG,CAAC,OAAO,EAAE,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,KAAK,GAAG,IAAI,CAAC,eAAe,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE;YACtD,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;QACzB,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE;YAClD,KAAK,CAAC,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AC"} |
+17
-1
@@ -11,4 +11,20 @@ { | ||
| "smartContract": "Add" | ||
| }, | ||
| "addingnumbertestnet2": { | ||
| "url": "https://proxy.berkeley.minaexplorer.com/graphql", | ||
| "keyPath": "keys/addingnumbertestnet2.json", | ||
| "feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json", | ||
| "feepayerAlias": "myaccount", | ||
| "fee": "0.1", | ||
| "smartContract": "Add" | ||
| }, | ||
| "addingnumbertestnet3": { | ||
| "url": "https://proxy.berkeley.minaexplorer.com/graphql", | ||
| "keyPath": "keys/addingnumbertestnet3.json", | ||
| "feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json", | ||
| "feepayerAlias": "myaccount", | ||
| "fee": "0.1", | ||
| "smartContract": "Add" | ||
| } | ||
| } | ||
| } | ||
| } |
+2
-4
| { | ||
| "name": "testing_adding_number", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "", | ||
@@ -45,7 +45,5 @@ "author": "", | ||
| "ts-jest": "^27.0.7", | ||
| "typescript": "^4.7.2" | ||
| }, | ||
| "peerDependencies": { | ||
| "typescript": "^4.7.2", | ||
| "o1js": "0.13.*" | ||
| } | ||
| } |
38296
20.42%0
-100%32
14.29%514
56.23%14
7.69%4
33.33%