Skip to content

The counter example is a Compact contract and a CLI environment to deploy and interact with the contract that can be used as a template to start building on Midnight

License

Notifications You must be signed in to change notification settings

midnightntwrk/example-counter

Counter DApp

Generic badge
Generic badge

Prerequisites

  1. You must have NodeJS version 22 installed.

  2. Download the latest version of the Compact compiler from the compiler release page and follow the instructions to install it (in particular the instructions regarding permissions that must be set to compile the contracts).

  3. Create a directory for the compiler executables, and unzip the downloaded file into that directory.

  4. Add the directory to your shell's $PATH.

    For example, if you unzipped the Compact compiler in $HOME/bin/compactc:

    export PATH=$PATH:$HOME/bin/compactc
  5. Run npm install to install all the necessary packages.

  6. Compile and build the code in the contract folder before running the code in the counter-cli folder.
    In the contract folder, run this command:

    npm run compact && npm run build

    Follow the instructions in the documentation to install and launch the proof server.

  7. Switch to the counter-cli folder and run this command:

    npm run start-testnet-remote

    If you do not have a wallet yet, you will be given the option to create a new one. After getting your address, you can use the official faucet to request coins to deploy a contract on testnet and interact with it.

The counter contract

The contract subdirectory contains:

The source code

The contract contains a declaration of state stored publicly on the blockchain:

export ledger round: Counter;

and a single transition function to change the state:

export circuit increment(): [] {
  round.increment(1);
}

To verify that the smart contract operates as expected, we've provided some unit tests in contract/src/test/counter.test.ts.

We've also provided tests that use a simple simulator, which illustrates how to initialize and call the smart contract code locally without running a node in contract/src/test/counter-simulator.ts

Building the smart contract

  1. Install dependencies:

    cd contract
    npm install
  2. Compile the contract:

    npm run compact

    You should see the following output from npm and the Compact compiler:

    > compact
    > compactc --skip-zk src/counter.compact src/managed/counter
    
    Compactc version: 0.22.0

    The compiler will complete very quickly because we've instructed it to skip ZK key generation with the option --skip-zk. The compiler's output files will be placed in the directory contract/src/managed/counter.

  3. Build the TypeScript source files:

    npm run build

    This creates the contract/dist directory.

  4. Start unit tests:

    npm run test

CLI

After building the smart contract you can deploy it using the project in the subdirectory counter-cli:

cd ../counter-cli

Install dependencies:

npm install

Build from source code:

npm run build

Import the contract code:

npm run compile-contract

Run the DApp:

npm run testnet-remote

If you want to launch all these steps at once, you can use this command:

npm run start-testnet-remote

The preceding entry point assumes you already have a proof server running locally. If you want one to be started automatically for you, use instead:

npm run testnet-remote-ps

Then follow the instructions from the CLI.

If you did not previously create and fund a Midnight Lace wallet, you will need to do so. Funds for testing can be requested from the official faucet.

You can find more information in part 2 of the Midnight developer tutorial.

About

The counter example is a Compact contract and a CLI environment to deploy and interact with the contract that can be used as a template to start building on Midnight

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •