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

Update template and example file structure to support publishing to npm #213

Merged
merged 7 commits into from
Jun 9, 2022
63 changes: 2 additions & 61 deletions examples/sudoku/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,3 @@
/**
* This file specifies how to run the `SudokuZkApp` smart contract locally using the `Mina.LocalBlockchain()` method.
* The `Mina.LocalBlockchain()` method specifies a ledger of accounts and contains logic for updating the ledger.
*
* Please note that this deployment is local and does not deploy to a live network.
* If you wish to deploy to a live network, please use the zkapp-cli to deploy.
*
* To run locally:
* Build the project: `$ npm run build`
* Run with node: `$ node build/src/index.js`.
*/
import {
deploy,
submitSolution,
getZkAppState,
createLocalBlockchain,
SudokuZkApp,
} from './sudoku.js';
import { cloneSudoku, generateSudoku, solveSudoku } from './sudoku-lib.js';
import { PrivateKey, shutdown } from 'snarkyjs';
import { SudokuZkApp } from './sudoku.js';

// setup
const account = createLocalBlockchain();
const sudoku = generateSudoku(0.5);
const zkAppPrivateKey = PrivateKey.random();
const zkAppAddress = zkAppPrivateKey.toPublicKey();
// create an instance of the smart contract
const zkAppInstance = new SudokuZkApp(zkAppAddress);

console.log('Deploying Sudoku...');
await deploy(zkAppInstance, zkAppPrivateKey, sudoku, account);

console.log('Is the sudoku solved?', getZkAppState(zkAppInstance).isSolved);

let solution = solveSudoku(sudoku);
if (solution === undefined) throw Error('cannot happen');

// submit a wrong solution
let noSolution = cloneSudoku(solution);
noSolution[0][0] = (noSolution[0][0] % 9) + 1;

console.log('Submitting wrong solution...');
try {
await submitSolution(
sudoku,
noSolution,
account,
zkAppAddress,
zkAppPrivateKey
);
} catch {
console.log('There was an error submitting the solution');
}
console.log('Is the sudoku solved?', getZkAppState(zkAppInstance).isSolved);

// submit the actual solution
console.log('Submitting solution...');
await submitSolution(sudoku, solution, account, zkAppAddress, zkAppPrivateKey);
console.log('Is the sudoku solved?', getZkAppState(zkAppInstance).isSolved);

// cleanup
await shutdown();
export { SudokuZkApp };
62 changes: 62 additions & 0 deletions examples/sudoku/ts/src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* This file specifies how to run the `SudokuZkApp` smart contract locally using the `Mina.LocalBlockchain()` method.
* The `Mina.LocalBlockchain()` method specifies a ledger of accounts and contains logic for updating the ledger.
*
* Please note that this deployment is local and does not deploy to a live network.
* If you wish to deploy to a live network, please use the zkapp-cli to deploy.
*
* To run locally:
* Build the project: `$ npm run build`
* Run with node: `$ node build/src/run.js`.
*/
import {
deploy,
submitSolution,
getZkAppState,
createLocalBlockchain,
SudokuZkApp,
} from './sudoku.js';
import { cloneSudoku, generateSudoku, solveSudoku } from './sudoku-lib.js';
import { PrivateKey, shutdown } from 'snarkyjs';

// setup
const account = createLocalBlockchain();
const sudoku = generateSudoku(0.5);
const zkAppPrivateKey = PrivateKey.random();
const zkAppAddress = zkAppPrivateKey.toPublicKey();
// create an instance of the smart contract
const zkAppInstance = new SudokuZkApp(zkAppAddress);

console.log('Deploying Sudoku...');
await deploy(zkAppInstance, zkAppPrivateKey, sudoku, account);

console.log('Is the sudoku solved?', getZkAppState(zkAppInstance).isSolved);

let solution = solveSudoku(sudoku);
if (solution === undefined) throw Error('cannot happen');

// submit a wrong solution
let noSolution = cloneSudoku(solution);
noSolution[0][0] = (noSolution[0][0] % 9) + 1;

console.log('Submitting wrong solution...');
try {
await submitSolution(
sudoku,
noSolution,
account,
zkAppAddress,
zkAppPrivateKey
);
} catch {
console.log('There was an error submitting the solution');
}
console.log('Is the sudoku solved?', getZkAppState(zkAppInstance).isSolved);

// submit the actual solution
console.log('Submitting solution...');
await submitSolution(sudoku, solution, account, zkAppAddress, zkAppPrivateKey);
console.log('Is the sudoku solved?', getZkAppState(zkAppInstance).isSolved);

// cleanup
await shutdown();
129 changes: 1 addition & 128 deletions examples/tictactoe/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,130 +1,3 @@
/**
* This file specifies how to run the `TicTacToe` smart contract locally using the `Mina.LocalBlockchain()` method.
* The `Mina.LocalBlockchain()` method specifies a ledger of accounts and contains logic for updating the ledger.
*
* Please note that this deployment is local and does not deploy to a live network.
* If you wish to deploy to a live network, please use the zkapp-cli to deploy.
*
* To run locally:
* Build the project: `$ npm run build`
* Run with node: `$ node build/src/index.js`.
*/

import { Field, PrivateKey, Mina, shutdown, isReady } from 'snarkyjs';
import { createLocalBlockchain, makeMove, deploy } from './tictactoe-lib.js';
import { TicTacToe, Board } from './tictactoe.js';

async function playTicTacToe() {
await isReady;

const [player1, player2] = createLocalBlockchain();
const player1Public = player1.toPublicKey();
const player2Public = player2.toPublicKey();

const zkAppPrivkey = PrivateKey.random();
const zkAppPubkey = zkAppPrivkey.toPublicKey();
const zkAppInstance = new TicTacToe(zkAppPubkey);

// Create a new instance of the contract
console.log('\n\n====== DEPLOYING ======\n\n');
await deploy(zkAppInstance, zkAppPrivkey, player1);

console.log('after transaction');

// initial state
let b = await Mina.getAccount(zkAppPubkey);
console.log('initial state of the zkApp');
for (const i in [0, 1, 2, 3, 4, 5, 6, 7]) {
console.log('state', i, ':', b.zkapp?.appState[i].toString());
}

console.log('\ninitial board');
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== FIRST MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player1,
player1Public,
player2Public,
Field.zero,
Field.zero
);

// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== SECOND MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player2,
player1Public,
player2Public,
Field.one,
Field.zero
);
// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== THIRD MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player1,
player1Public,
player2Public,
Field.one,
Field.one
);
// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== FOURTH MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player2,
player1Public,
player2Public,
Field(2),
Field.one
);

// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== FIFTH MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player1,
player1Public,
player2Public,
Field(2),
Field(2)
);

// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();
console.log(
'did someone win?',
b.zkapp?.appState[2].toString() ? 'Player 1!' : 'Player 2!'
);

// cleanup
await shutdown();
}

playTicTacToe();
export { TicTacToe, Board };
130 changes: 130 additions & 0 deletions examples/tictactoe/ts/src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* This file specifies how to run the `TicTacToe` smart contract locally using the `Mina.LocalBlockchain()` method.
* The `Mina.LocalBlockchain()` method specifies a ledger of accounts and contains logic for updating the ledger.
*
* Please note that this deployment is local and does not deploy to a live network.
* If you wish to deploy to a live network, please use the zkapp-cli to deploy.
*
* To run locally:
* Build the project: `$ npm run build`
* Run with node: `$ node build/src/run.js`.
*/

import { Field, PrivateKey, Mina, shutdown, isReady } from 'snarkyjs';
import { createLocalBlockchain, makeMove, deploy } from './tictactoe-lib.js';
import { TicTacToe, Board } from './tictactoe.js';

async function playTicTacToe() {
await isReady;

const [player1, player2] = createLocalBlockchain();
const player1Public = player1.toPublicKey();
const player2Public = player2.toPublicKey();

const zkAppPrivkey = PrivateKey.random();
const zkAppPubkey = zkAppPrivkey.toPublicKey();
const zkAppInstance = new TicTacToe(zkAppPubkey);

// Create a new instance of the contract
console.log('\n\n====== DEPLOYING ======\n\n');
await deploy(zkAppInstance, zkAppPrivkey, player1);

console.log('after transaction');

// initial state
let b = await Mina.getAccount(zkAppPubkey);
console.log('initial state of the zkApp');
for (const i in [0, 1, 2, 3, 4, 5, 6, 7]) {
console.log('state', i, ':', b.zkapp?.appState[i].toString());
}

console.log('\ninitial board');
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== FIRST MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player1,
player1Public,
player2Public,
Field.zero,
Field.zero
);

// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== SECOND MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player2,
player1Public,
player2Public,
Field.one,
Field.zero
);
// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== THIRD MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player1,
player1Public,
player2Public,
Field.one,
Field.one
);
// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== FOURTH MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player2,
player1Public,
player2Public,
Field(2),
Field.one
);

// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();

// play
console.log('\n\n====== FIFTH MOVE ======\n\n');
await makeMove(
zkAppInstance,
zkAppPrivkey,
player1,
player1Public,
player2Public,
Field(2),
Field(2)
);

// debug
b = await Mina.getAccount(zkAppPubkey);
new Board(b.zkapp?.appState?.[0]!).printState();
console.log(
'did someone win?',
b.zkapp?.appState[2].toString() ? 'Player 1!' : 'Player 2!'
);

// cleanup
await shutdown();
}

playTicTacToe();
3 changes: 3 additions & 0 deletions templates/project-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Add } from './Add.js';

export { Add };