/** * 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 { Mina, PrivateKey, shutdown } from 'snarkyjs'; import fs from 'fs/promises'; import { Add } from './Add.js'; // check command line arg let network = process.argv[2]; let userKeyPath = "keys/user.json" if (!network) throw Error(`Missing argument. Usage: node build/src/interact.js Example: node build/src/interact.js berkeley `); Error.stackTraceLimit = 1000; // parse config and private key from file type Config = { deployAliases: Record }; let configJson: Config = JSON.parse(await fs.readFile('config.json', 'utf8')); let config = configJson.deployAliases[network]; let key: { privateKey: string } = JSON.parse( await fs.readFile(config.keyPath, 'utf8') ); let zkAppKey = PrivateKey.fromBase58(key.privateKey); let userKeyPair :{ privateKey: string } = JSON.parse( await fs.readFile(userKeyPath, 'utf8') ); let userPrivateKey = PrivateKey.fromBase58(userKeyPair.privateKey) let userPublicKey = userPrivateKey.toPublicKey() // set up Mina instance and contract we interact with const Network = Mina.Network(config.url); Mina.setActiveInstance(Network); let zkAppAddress = zkAppKey.toPublicKey(); let zkApp = new Add(zkAppAddress); // compile the contract to create prover keys console.log('compile the contract...'); await Add.compile(); // call update() and send transaction console.log('build transaction and create proof...'); let tx = await Mina.transaction({ sender: userPublicKey, fee: 0.1e9 }, () => { zkApp.update(); }); await tx.prove(); console.log('send transaction...'); let sentTx = await tx.sign([userPrivateKey]).send(); 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()} `); } shutdown();