DocsSmart Contracts
Smart Contracts

Smart contracts on QuanChain

Channel 2 hosts the contract layer. Familiar Solidity syntax with quantum-aware primitives — composite signatures, level-gated entry points, and rotation-safe storage.

Intermediate15 min read
Channel 2

The smart-contract channel

Contract calls settle on Channel 2 with a 2-second finality budget and a 512 KB payload cap. The execution layer is a QuanChain-flavoured EVM with extensions for composite-signature verification, Merkle-proof primitives, and per-level address parsing.

EVM-compatible

Solidity 0.8.x compiles directly. Most existing tooling works after a network switch.

Quantum-aware ops

Native opcodes: VERIFYCOMPOSITE, PARSELEVEL, MERKLEVERIFY.

2s finality

Channel 2 finalises in roughly 2 seconds. Use Channel 1 for sub-second UX.

Toolchain

Languages and tooling

Solidity is the canonical language. A Rust-based DSL (qcr) is available for contracts that need tighter control over composite verification.

Solidity

Versions 0.8.20+ supported. Hardhat + Foundry plugins on npm under @quanchain/.

qcr (Rust DSL)

For composite-signature workflows and gas-tight loops. Beta.

Walkthrough

Hello QuanChain

The minimum viable contract is a counter that can only be incremented by addresses at Level 10 or above.

solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@quanchain/contracts/QuanAddress.sol";

contract Counter {
    using QuanAddress for address;

    uint256 public count;

    event Bump(address indexed by, uint256 newCount, uint8 level);

    modifier minLevel(uint8 lvl) {
        require(msg.sender.level() >= lvl, "level too low");
        _;
    }

    function bump() external minLevel(10) {
        count += 1;
        emit Bump(msg.sender, count, msg.sender.level());
    }
}
Build

Compile and deploy

The Hardhat plugin ships a quanchain network preset.

bash
npm install --save-dev hardhat @quanchain/hardhat
npx hardhat init
# add 'quanchain' network in hardhat.config.ts
npx hardhat compile
npx hardhat deploy --network quanchain-testnet
typescript
// hardhat.config.ts
import '@quanchain/hardhat';

export default {
  solidity: '0.8.24',
  networks: {
    'quanchain-testnet': {
      url: 'https://rpc-testnet.quanchain.ai',
      accounts: [process.env.SEED!],
      channel: 2,
    },
  },
};
Higher assurance

Composite-signed entry points

For irreversible administrative actions, gate a function behind a Level 20 composite signature. The runtime exposes VERIFYCOMPOSITE natively.

solidity
import "@quanchain/contracts/QuanComposite.sol";

contract Treasury {
    using QuanComposite for bytes;

    address public owner;       // QC20_ parent
    address public delegate;

    function setDelegate(address newDelegate, bytes calldata sig) external {
        bytes32 msgHash = keccak256(abi.encodePacked(newDelegate, block.chainid));
        require(sig.verifyCompositeFor(owner, msgHash), "bad sig");
        delegate = newDelegate;
    }
}
Gas cost
Composite verification costs ~480k gas. Reserve it for owner / governance flows, not per-user transactions.
Storage model

Quantum-safe storage

Storage slots that hold public keys carry the level prefix automatically. Reading an address from storage returns it parsed as a structured tuple:

solidity
import "@quanchain/contracts/QuanAddress.sol";

contract Registry {
    mapping(bytes32 => address) public records;

    function ownerOf(bytes32 id) external view
        returns (uint8 level, bytes memory hash)
    {
        address a = records[id];
        return (a.level(), a.hash());
    }
}

For large opaque blobs (e.g. compressed Merkle proofs), use Channel 3 storage and reference the blob hash from Channel 2.

Verification

Testing locally

A local devnet ships with the SDK and produces all three channels in a single process.

bash
npx @quanchain/devnet start            # starts on :8545 (chain 8453021)
npx hardhat test --network localhost
Before mainnet

Security checklist

  • Gate irreversible actions behind composite-signed entry points.
  • Validate msg.sender.level() on functions that move value at scale.
  • Never store a private key or seed in contract storage — even encrypted.
  • Use Channel 3 for blobs over 64 KB; reference the hash from Channel 2 contracts.
  • Run the audit harness at /docs/api before mainnet deploy.

Want help?

Join the developer Discord, open a GitHub issue, or read the whitepaper.