Skip to content

Commit

Permalink
Add support for mainnet. (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
shimkiv authored Feb 1, 2024
1 parent 6f276f6 commit e719742
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 160 deletions.
36 changes: 0 additions & 36 deletions .husky/_/husky.sh
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
#!/usr/bin/env sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}

readonly hook_name="$(basename -- "$0")"
debug "starting $hook_name..."

if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi

if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi

readonly husky_skip_init=1
export husky_skip_init
sh -e "$0" "$@"
exitCode="$?"

if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi

if [ $exitCode = 127 ]; then
echo "husky - command not found in PATH=$PATH"
fi

exit $exitCode
fi
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## Unreleased

## [0.16.1](https://github.com/o1-labs/zkapp-cli/compare/v16.0...v16.1) - 2024-01-XX

### Added

- Possibility to configure the target network to testnet or mainnet during the `zk config` process. [#564](https://github.com/o1-labs/zkapp-cli/pull/564)

## [0.16.0](https://github.com/o1-labs/zkapp-cli/compare/v15.2...v16.0) - 2023-12-11

### Added
Expand Down
220 changes: 110 additions & 110 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkapp-cli",
"version": "0.16.0",
"version": "0.16.1",
"description": "CLI to create zkApps (zero-knowledge apps) for Mina Protocol",
"homepage": "https://github.com/o1-labs/zkapp-cli/",
"keywords": [
Expand Down Expand Up @@ -63,7 +63,7 @@
"find-npm-prefix": "^1.0.2",
"fs-extra": "^11.2.0",
"gittar": "^0.1.1",
"mina-signer": "^2.1.1",
"mina-signer": "^2.1.2",
"o1js": "^0.15.*",
"opener": "^1.5.2",
"ora": "^8.0.1",
Expand All @@ -76,14 +76,14 @@
"@shimkiv/cli-testing-library": "^0.1.7",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.11",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-plugin-o1js": "^0.4.0",
"execa": "^8.0.1",
"fkill": "^9.0.0",
"husky": "^8.0.3",
"husky": "^9.0.7",
"jest": "^29.7.0",
"lint-staged": "^15.2.0",
"portfinder": "^1.0.32",
Expand Down
10 changes: 6 additions & 4 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ async function config() {
// If user presses "ctrl + c" during interactive prompt, exit.
let {
deployAliasName,
networkId,
url,
fee,
feepayer,
Expand All @@ -172,7 +173,7 @@ async function config() {
let feepayerKeyPair;
switch (feepayer) {
case 'create':
feepayerKeyPair = await createKeyPairStep(feepayerAlias);
feepayerKeyPair = await createKeyPairStep(feepayerAlias, networkId);
break;
case 'recover':
feepayerKeyPair = await recoverKeyPairStep(feepayerKey, feepayerAlias);
Expand All @@ -195,7 +196,7 @@ async function config() {
await step(
`Create zkApp key pair at keys/${deployAliasName}.json`,
async () => {
const keyPair = createKeyPair('testnet');
const keyPair = createKeyPair(networkId);
fs.outputJsonSync(`${DIR}/keys/${deployAliasName}.json`, keyPair, {
spaces: 2,
});
Expand All @@ -210,6 +211,7 @@ async function config() {
process.exit(1);
}
config.deployAliases[deployAliasName] = {
networkId,
url,
keyPath: `keys/${deployAliasName}.json`,
feepayerKeyPath: `${Constants.feePayerCacheDir}/${feepayerAlias}.json`,
Expand All @@ -235,7 +237,7 @@ async function config() {
}

// Creates a new feepayer key pair
async function createKeyPairStep(feepayerAlias) {
async function createKeyPairStep(feepayerAlias, networkId) {
if (!feepayerAlias) {
// No fee payer alias, return early to prevent generating key pair with undefined alias
log(chalk.red(`Invalid fee payer alias ${feepayerAlias}.`));
Expand All @@ -244,7 +246,7 @@ async function createKeyPairStep(feepayerAlias) {
return await step(
`Create fee payer key pair at ${Constants.feePayerCacheDir}/${feepayerAlias}.json`,
async () => {
const keyPair = createKeyPair('testnet');
const keyPair = createKeyPair(networkId);

fs.outputJsonSync(
`${Constants.feePayerCacheDir}/${feepayerAlias}.json`,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import path from 'path';
/**
* @typedef {'next' | 'svelte' | 'nuxt' | 'empty' | 'none'} UiType
* @typedef {'sudoku' | 'tictactoe'} ExampleType
* @typedef {'testnet' | 'mainnet'} NetworkId
* @typedef {'single-node' | 'multi-node'} LightnetMode
* @typedef {'fast' | 'real'} LightnetType
* @typedef {'none' | 'full'} LightnetProofLevel
* @typedef {'o1js-main' | 'berkeley' | 'develop'} LightnetMinaBranch
* @typedef {'Spam' | 'Trace' | 'Debug' | 'Info' | 'Warn' | 'Error' | 'Fatal'} LightnetMinaLogLevel
*
* @type {{ uiTypes: UiType[], exampleTypes: ExampleType[], feePayerCacheDir: string, lightnetWorkDir: string, lightnetModes: LightnetMode[], lightnetTypes: LightnetType[], lightnetProofLevels: LightnetProofLevel[], lightnetMinaBranches: LightnetMinaBranch[], lightnetProcessToLogFileMapping: Map<string, string>, lightnetMinaProcessesLogLevels: LightnetMinaLogLevel[] }}
* @type {{ uiTypes: UiType[], exampleTypes: ExampleType[], feePayerCacheDir: string, networkIds: NetworkId[], lightnetWorkDir: string, lightnetModes: LightnetMode[], lightnetTypes: LightnetType[], lightnetProofLevels: LightnetProofLevel[], lightnetMinaBranches: LightnetMinaBranch[], lightnetProcessToLogFileMapping: Map<string, string>, lightnetMinaProcessesLogLevels: LightnetMinaLogLevel[] }}
*/
const Constants = Object.freeze({
uiTypes: ['next', 'svelte', 'nuxt', 'empty', 'none'],
exampleTypes: ['sudoku', 'tictactoe'],
feePayerCacheDir: `${homedir()}/.cache/zkapp-cli/keys`,
networkIds: ['testnet', 'mainnet'],
lightnetWorkDir: path.resolve(`${homedir()}/.cache/zkapp-cli/lightnet`),
lightnetModes: ['single-node', 'multi-node'],
lightnetTypes: ['fast', 'real'],
Expand Down
10 changes: 9 additions & 1 deletion src/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import util from 'util';
import step from './helpers.js';

const log = console.log;
const DEFAULT_NETWORK_ID = 'testnet';
const DEFAULT_GRAPHQL = 'https://proxy.berkeley.minaexplorer.com/graphql'; // The endpoint used to interact with the network

/**
Expand Down Expand Up @@ -221,6 +222,10 @@ export async function deploy({ alias, yes }) {
}
let { PrivateKey, Mina, AccountUpdate } = await import(o1jsImportPath);

// We need to default to the testnet networkId if none is specified for this deploy alias in config.json
// This is to ensure the backward compatibility.
const networkId =
config.deployAliases[alias]?.networkId ?? DEFAULT_NETWORK_ID;
const graphQlUrl = config.deployAliases[alias]?.url ?? DEFAULT_GRAPHQL;

const { data: nodeStatus } = await sendGraphQL(
Expand Down Expand Up @@ -389,7 +394,10 @@ export async function deploy({ alias, yes }) {
}

let transaction = await step('Build transaction', async () => {
let Network = Mina.Network(graphQlUrl);
const Network = Mina.Network({
networkId,
mina: graphQlUrl,
});
Mina.setActiveInstance(Network);
let tx = await Mina.transaction({ sender: feepayerAddress, fee }, () => {
AccountUpdate.fundNewAccount(feepayerAddress);
Expand Down
23 changes: 23 additions & 0 deletions src/lib/prompts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chalk from 'chalk';
import { PrivateKey } from 'o1js';
import Constants from './constants.js';

function formatPrefixSymbol(state) {
// Shows a cyan question mark when not submitted.
Expand Down Expand Up @@ -42,6 +43,23 @@ const prompts = {
.trim()
.replace(/\s{1,}/g, '-'),
},
{
type: 'select',
name: 'networkId',
initial: 0, // 0 = testnet, 1 = mainnet, change it to 1 after the HF
choices: Constants.networkIds.map((networkId) => ({
name: capitalize(networkId),
value: networkId,
})),
message: (state) => {
const style =
state.submitted && !state.cancelled ? chalk.green : chalk.reset;
return style('Choose the target network:');
},
result() {
return this.focused.value;
},
},
{
type: 'input',
name: 'url',
Expand Down Expand Up @@ -272,4 +290,9 @@ function getFeepayorChoices(cachedFeepayerAliases) {

return choices;
}

function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

export default prompts;
3 changes: 2 additions & 1 deletion templates/project-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"typescript": "^4.7.2"
},
"peerDependencies": {
"o1js": "0.15.*"
"o1js": "0.15.*",
"mina-signer": "^2.1.2"
}
}
11 changes: 10 additions & 1 deletion templates/project-ts/src/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Run with node: `$ node build/src/interact.js <deployAlias>`.
*/
import fs from 'fs/promises';
import { NetworkId } from 'mina-signer';
import { Mina, PrivateKey } from 'o1js';
import { Add } from './Add.js';

Expand All @@ -25,12 +26,14 @@ Usage:
node build/src/interact.js <deployAlias>
`);
Error.stackTraceLimit = 1000;
const DEFAULT_NETWORK_ID = 'testnet';

// parse config and private key from file
type Config = {
deployAliases: Record<
string,
{
networkId?: string;
url: string;
keyPath: string;
fee: string;
Expand All @@ -53,7 +56,13 @@ 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 Network = Mina.Network({
// We need to default to the testnet networkId if none is specified for this deploy alias in config.json
// This is to ensure the backward compatibility.
networkId: (config.networkId ?? DEFAULT_NETWORK_ID) as NetworkId,
mina: config.url,
});
// 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();
Expand Down
Loading

0 comments on commit e719742

Please sign in to comment.