Back to Browse

Use with your AI agent

  1. Install — run npx skills add Kali-Decoder/Midnight-skills --skill example-counter for this skill, or npx skills add Kali-Decoder/Midnight-skills for all
  2. Prompt — copy the example prompt and paste it into Cursor, Claude Code, or Codex
  3. Build — the agent loads this skill and scaffolds your Midnight work

Example Counter

Full-Fledged TemplatesIntermediateReviewedUpdated 10 days agov1.0.0

Agent picks this when: Complete DApp reference: wallet, deploy, interact (use as template)

Complete Midnight DApp reference — headless wallet, CLI, counter contract, DUST generation, deploy, interaction.

Good for

  • Complete DApp reference: headless wallet, CLI, counter contract.
  • DUST generation, deploy, interaction, standalone mode.

Preview in agent

Suggested prompts

Complete DApp reference: wallet, deploy, interact (use as template). Use the "example-counter" skill from MIDSKILLS. Follow Compact smart-contract best practices, wire wallet flows where needed, and explain each step as you implement. Prefer the runnable counter-dapp template when scaffolding the app. Target Midnight Preprod unless I specify otherwise.

Loaded skill example-counter

What the skill guides the agent to do:

  1. Complete DApp reference: headless wallet, CLI, counter contract.
  2. DUST generation, deploy, interaction, standalone mode.

Illustrative — based on the skill's description, not recorded agent output.

Try it without installing: npx skills use Kali-Decoder/Midnight-skills@example-counter

What changed in v1.0.16

All registry changes

No changes in v1.0.16 — last updated in v1.0.14.

On this page · 15 sections
SKILL.md

Example Counter DApp Skill

This skill is the walkthrough for templates/counter-dapp/, the runnable midnightntwrk/example-counter reference DApp (v2.1.1) ported to Ledger 9 in this repo (21 Nov 2026). Upstream had not published a Ledger 9 release at the time: its main still pins runtime 0.15 / Midnight.js 4 / setNetworkId. The template holds every source, config and Docker file; this page explains them. Do not retype the files from here: copy the template, then adapt it (§13).

Get the template:

git clone --depth 1 --filter=blob:none --sparse https://github.com/Kali-Decoder/Midnight-Skills.git
cd Midnight-Skills && git sparse-checkout set templates/counter-dapp
cp -R templates/counter-dapp ../my-counter && cd ../my-counter
npm install
cd contract && npm run compact && npm run build && cd ..

Without a checkout, read any file at https://raw.githubusercontent.com/Kali-Decoder/Midnight-Skills/main/templates/counter-dapp/<path>.

Verified: the contract compiles with compact compile +0.36.1, contract type-checks and builds, and counter-cli type-checks against the installed Ledger 9 packages. The CLI has not been run against a live network.

Primary references:

  • github.com/midnightntwrk/example-counter — official reference repo
  • docs.midnight.network/guides/deploy-mn-app — official deploy guide
  • docs.midnight.network/guides/interact-with-mn-app — official interact guide
  • docs.midnight.network/relnotes/support-matrix — authoritative version compatibility matrix

Package versions below are the Ledger 9.1 set (checked against npm and the support matrix on 21 Nov 2026). Resolve the current tested set from the support matrix before pinning — see references/volatile.md. Never install @latest.


1) Project Structure

templates/counter-dapp/
├── package.json                    # npm/yarn workspaces: contract, counter-cli
├── package-lock.json
├── compose.yml                     # Docker: full local stack (localhost-only)
├── contract/
│   ├── package.json                # @midnight-ntwrk/counter-contract (exports dist/)
│   ├── tsconfig.json, tsconfig.build.json, vitest.config.ts
│   └── src/
│       ├── counter.compact         # the contract
│       ├── witnesses.ts            # CounterPrivateState + witnesses
│       ├── index.ts                # re-exports managed contract + witnesses
│       └── managed/                # compiler output (gitignored)
└── counter-cli/
    ├── package.json                # @midnight-ntwrk/counter-cli
    ├── tsconfig.json, tsconfig.build.json, vitest.config.ts, vitest.setup.ts
    ├── proof-server.yml            # Docker: proof server only (preprod/preview)
    ├── standalone.yml              # Docker: node + indexer + proof server
    ├── standalone.env.example
    └── src/
        ├── api.ts                  # wallet, providers, deploy/join, increment, DUST
        ├── cli.ts                  # interactive menus
        ├── config.ts               # network config classes
        ├── common-types.ts, logger-utils.ts, index.ts
        └── preprod.ts, preview.ts, standalone.ts, *-start-proof-server.ts

Not included: upstream's test suites (contract/src/test/*, counter-cli/src/test/*) and its eslint/prettier/VS Code configs. The vitest configs are kept so tests can be added.


2) Prerequisites

# Node.js 22.15+ required
node --version

# Yarn 1.x (classic) required
yarn --version

# Docker required (proof server + optional local stack)
docker --version

# Install Compact compiler
curl --proto '=https' --tlsv1.2 -sSf \
  https://github.com/midnightntwrk/compact/releases/latest/download/compact-installer.sh | sh
source $HOME/.local/bin/env
compact update 0.36.1
compact compile --version

3) Template Files

FileRole
package.jsonRoot workspace (contract, counter-cli); scripts test, test:local, env:up/env:down, validate; pins the shared Ledger 9 packages
contract/package.jsoncompact (compact compile +0.36.1 src/counter.compact src/managed/counter), build (tsc + copies managed/ into dist/), typecheck. main/types point at dist/, so build the contract before running the CLI
counter-cli/package.jsonpreprod, preview, standalone, *-ps (auto-start proof server), typecheck, build
*/tsconfig*.jsonESM, strict; tsconfig.build.json emits dist/
compose.yml, counter-cli/standalone.ymlFull local stack (node, indexer, proof server) on localhost
counter-cli/proof-server.ymlProof server only, for preprod/preview
counter-cli/standalone.env.exampleCopy to standalone.env for the local stack

Image tags and package versions are the references/versions.json set.


4) contract/src/counter.compact

pragma language_version >= 0.28.0;

import CompactStandardLibrary;

export ledger round: Counter;

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

Compile it:

# From the contract/ directory
cd contract && npm run compact

Expected output (exact k/rows depend on the toolchain; 0.36.x emits ZKIR v3):

Compiling 1 circuits:
  circuit "increment" ...

contract/src/witnesses.ts

export type CounterPrivateState = {
  privateCounter: number;
};

export const witnesses = {};

contract/src/index.ts

export * as Counter from "./managed/counter/contract/index.js";
export * from "./witnesses";

5) counter-cli/src/config.ts

Network configuration. Ledger 9 / Midnight.js 5: there is no setNetworkId, so each config carries its networkId. Preprod is served by Blockfrost.

Config is implemented by StandaloneConfig (local Docker stack, undeployed), PreviewConfig and PreprodConfig. Each carries networkId, logDir, indexer HTTP/WS URLs, node URL, proof server URL and an optional faucet. contractConfig holds the privateStateStoreName for the LevelDB private-state provider and the zkConfigPath of the compiled contract (contract/src/managed/counter).

Note: local and preview use indexer API v4 (/api/v4/graphql). The old "all networks use v3, do not change to v4" advice is obsolete. indexer.preprod.midnight.network was retired on 9 Oct 2026 (HTTP 410), and rpc.preprod.midnight.network shut down on 18 Nov 2026. Blockfrost URLs contain your token, so don't log them.


6) counter-cli/src/api.ts

Core API — wallet creation (Wallet SDK 2.x), contract deploy/join, increment, DUST management. Ported to Ledger 9; type-checked against Midnight.js 5.0.2 / Wallet SDK 2.0.1.

ExportWhat it does
buildWalletAndWaitForFunds(config, seed) / buildFreshWallet(config)WalletFacade from a seed (or a new one), sync, wait for funds, register NIGHT for DUST
createWalletAndMidnightProvider(ctx)Era-aware wallet and midnight providers from the facade
configureProviders(ctx, config)Full MidnightProviders: LevelDB private state, indexer, node ZK config, HTTP proof provider, config: { networkId, ttlSeconds }
deploy(providers, privateState) / joinContract(providers, address)deployContract / findDeployedContract with counterCompiledContract
increment(contract)callTx.increment()
getCounterLedgerState(providers, address) / displayCounterValue(...)Read and decode the ledger with Counter.ledger(contractState.data)
waitForSync, waitForFunds, getDustBalance, monitorDustBalanceWallet and DUST helpers
withStatus, setLoggerCLI progress output and logging

Key points:

  • globalThis.WebSocket = WebSocket is required for GraphQL subscriptions (wallet sync) in Node.js
  • WalletFacade.init() with WalletSeeds.fromMasterSeed(seed); the facade is started with the seeds (wallet.start(seeds))
  • Era-aware providers via createLedger9WalletProvider / createLedger9MidnightProvider; the old signTransactionIntents workaround is gone
  • providers.config = { networkId, ttlSeconds } replaces setNetworkId
  • DUST balance is state.dust.balance(time); the registration signer returns a Promise (SignSegment)

7) counter-cli/src/cli.ts

Interactive CLI with wallet setup, deploy/join, and counter interaction menus. Supports Docker lifecycle via testcontainers. run(config, logger, dockerEnv?) drives three menus: wallet (create, restore from seed), contract (deploy, join by address), and counter (increment, read). Entry points pick the config and call run.


8) Entry Points (counter-cli/src/)

FileNetworkNotes
preprod.tspreprodBlockfrost endpoints: export BLOCKFROST_PROJECT_ID first
preview.tspreviewNo token needed
standalone.tsundeployed (local)Starts standalone.yml through testcontainers; uses the genesis wallet
preprod-start-proof-server.ts, preview-start-proof-server.tspreprod / previewAlso start proof-server.yml
index.tsn/aRe-exports api and cli


9) First Run — Preprod

# 1. Install deps
yarn

# 2. Compile and build the contract (the CLI imports its dist/)
cd contract && npm run compact && npm run build && cd ..
# → circuit "increment" (compiled by `compact compile +0.36.1`)

# 3. Start proof server (keep this terminal open)
cd counter-cli && docker compose -f proof-server.yml pull && docker compose -f proof-server.yml up

# 4. In another terminal, run the CLI (preprod endpoints are Blockfrost: export a preprod token first)
export BLOCKFROST_PROJECT_ID=nightpreprod...
cd counter-cli && tsx src/preprod.ts
# or with auto-started proof server:
cd counter-cli && yarn preprod-ps

Faucet: https://midnight-tmnight-preprod.nethermind.dev/ (preprod) or https://faucet.preview.midnight.network/ (preview, tsx src/preview.ts, no token needed). Preprod cleared non-cNIGHT DUST registrations on 9 Nov 2026, so wallets register again on first run.

The CLI will:

  1. Present a wallet menu — choose [1] to create a new wallet
  2. Save the seed phrase shown
  3. Wait for sync, then display the unshielded address
  4. Prompt you to fund via the faucet (paste the address)
  5. Wait for funds and register NIGHT UTXOs for DUST generation
  6. Enter the contract menu — choose [1] to deploy
  7. After deployment, enter the counter menu — choose [1] to increment, [2] to read

10) First Run — Local (standalone)

# 1. Start full local stack with testcontainers (auto-managed)
cd counter-cli && yarn standalone

# 2. This will:
#    - Pull Docker images
#    - Start node + indexer + proof server
#    - Use the genesis wallet (pre-funded)
#    - Deploy contract and enter the interaction loop

Note: Standalone mode uses the genesis mint wallet seed (0000...0001) automatically. No faucet needed.


11) Running Tests

The template keeps the vitest configs but not upstream's test files. After adding tests:

# Contract unit tests (from contract/ directory)
cd contract && npm run test:compile

# API integration tests against the local stack (from counter-cli/ directory)
cd counter-cli && yarn test-api

# Full validation (from root)
yarn validate

12) Troubleshooting

ErrorCauseFix
compact: command not foundPATH not setsource $HOME/.local/bin/env
ETARGET No matching version foundVersion not in the tested setResolve current versions from the support matrix — see references/volatile.md. Do not install @latest.
ECONNREFUSED 127.0.0.1:6300Proof server not runningStart proof server: docker compose -f proof-server.yml up
WalletFacade.init is not a functionOld WalletFacade usageUse WalletFacade.init() static factory, not new WalletFacade()
MidnightBech32m.encode is not a functionMissing address-format importImport from @midnightntwrk/wallet-sdk-address-format
Failed to clone intentWallet SDK 1.x signing bugGone in Wallet SDK 2.x (signRecipe signs per stage); upgrade instead of patching
DUST = 0 after failed deployDUST coins lockedRestart the process — wallet.stop() then rerun
0 balance after faucetWallet not synced yetWait for sync; check unshielded address was used
Cannot find moduleContract not compiled or not builtcd contract && npm run compact && npm run build (the CLI imports dist/)
prove: no SRS params for k=6Circuit too small for proverAdd dummy ledger fields to increase circuit size
Old address fails after recompileVerifier key changedRedeploy contract

13) Adapting This Template

Copy templates/counter-dapp/ first and get it running unchanged. Then, to replace the counter with your own contract:

  1. Replace contract/src/counter.compact with your contract
  2. Run cd contract && npm run compact && npm run build
  3. Update contract/src/witnesses.ts with your private state type
  4. Update contract/src/index.ts to export your contract symbols
  5. In counter-cli/src/common-types.ts — update type aliases for your circuits
  6. In counter-cli/src/api.ts:
    • Update imports to use your contract package
    • Update counterCompiledContract with your contract reference
    • Update counterContractInstance, deploy, joinContract as needed
    • Replace increment with your circuit calls
    • Replace Counter.ledger(contractState.data) with your ledger field
  7. Rename privateStateStoreName in config.ts (contractConfig) to avoid LevelDB collisions
  8. Replace the private-state password before production: configureProviders derives it from the wallet's coin public key, so anyone who knows that key can reproduce it. Ask the user for a password or derive it from secret wallet material

Start in 2 steps

  1. 1. Install — one skill or the full set
  2. 2. Prompt — paste the example into your agent

Install all skills (recommended)

npx skills add Kali-Decoder/Midnight-skills

One command adds every Midnight skill to Cursor, Claude Code, and similar agents.

Install this skill only

npx skills add Kali-Decoder/Midnight-skills --skill example-counter

Installs only "example-counter" into every detected agent (Cursor, Claude Code, Codex, …).

Example agent prompt

Complete DApp reference: wallet, deploy, interact (use as template). Use the "example-counter" skill from MIDSKILLS. Follow Compact smart-contract best practices, wire wallet flows where needed, and explain each step as you implement. Prefer the runnable counter-dapp template when scaffolding the app. Target Midnight Preprod unless I specify otherwise.

Paste into Cursor, Claude Code, or Codex after installing.

Try runnable template
Author
Tusharpamnani
Category
Full-Fledged Templates
Difficulty
intermediate
Version
v1.0.0

Tags

CounterCLIHeadless

Files included

SKILL.md
Create Social CardLive HackathonsGallery
Mock Demo
Example Counter