Smart contracts you can read

"A VM that runs JavaScript" is an abstract claim, so here is the concrete version: fourteen working contract templates, from a two-party escrow to an automated market maker, each one plain JavaScript, MIT-licensed, tested against the real VM, and documented down to the attacks it considers. Fork one instead of starting from a blank file.

Live today onBitcoin·Litecoin·Dogecoin·more chains coming

The shape of a contract

Plain JavaScript, honest custody

A contract is a JavaScript module: initialize is its constructor, every other exported function is a method someone can call with EXECUTE, and tokens move in and out of its custody with DEPOSIT and WITHDRAW. No new language, no compiler, no toolchain to install first.

What the excerpt shows

This is the settlement path of the escrow template, abridged from the real 215-line contract. Three habits every template shares are visible right here:

  • Never trust a caller-supplied amount. The contract reads its own on-chain balance and settles the whole of it, so nothing can be stranded or under-paid.
  • State is explicit. The escrow marks itself terminal before it pays, so a second call finds a settled contract, not a second payout.
  • The ledger is out of reach. The contract cannot credit a balance; it emits a SEND, and the same validated handler that checks every user transaction checks this one.

Every template's README walks through its custody model the same way, and closes with the attacks it was written to survive.

// escrow.js (abridged): pay the seller. The buyer can release,
// or the arbiter can, to settle a dispute.
release: function (xchain) {
    settle(xchain, ['buyer', 'arbiter'], 'seller', 'RELEASED');
},

function settle(xchain, allowedRoles, payeeRole, terminalStatus) {
    xchain.require(xchain.state.get('status') === 'FUNDED',
        'escrow not funded / already settled');

    // Only the roles fixed at deploy time may settle.
    var caller = xchain.getSourceAddress();
    var ok = isAllowed(caller, allowedRoles);
    xchain.require(ok, 'not authorized');

    // Never trust a caller-supplied amount: read the chain.
    var tick = xchain.state.get('tick');
    var addr = xchain.getContractAddress();
    var held = xchain.getBalance(addr, tick);
    xchain.require(xchain.math.gt(held, '0'), 'nothing to settle');

    // Mark terminal BEFORE paying: no second payout.
    xchain.state.set('status', terminalStatus);

    // The contract can't touch the ledger. It emits a validated SEND,
    // checked by the same handlers as every user transaction.
    xchain.emit.send({
        destination: xchain.state.get(payeeRole),
        tick: tick,
        quantity: held
    });
}

The library

Fourteen templates, ready to fork

Each template lives in its own folder with the contract, its test suite, and a README that explains the custody model and the attacks it considers. All of it is MIT-licensed, so a fork can become your product. Click any card to read the code.

Escrow ↗

A buyer locks tokens for a seller, with an arbiter and a deadline for disputes. The recommended first read.

English auction ↗

Ascending bids, where each new high bid refunds the outbid bidder in the same transaction.

Dutch auction ↗

The price falls linearly to a floor; the first buyer willing to pay wins at that price.

Automated market maker ↗

A constant-product AMM with a 0.3% fee whose LP shares are real, tradeable tokens the contract issues itself.

Crowdsale ↗

A capped token sale with a soft cap and deadline, where the contract creates and issues its own token.

Vesting ↗

Linear release with a cliff, measured in blocks, with optional revocation for the unvested remainder.

Governed treasury ↗

A poll-governed treasury with a timelock and a guardian veto, hardened against low-turnout raids.

Card dispenser ↗

Random card packs drawn from the contract's real inventory, with rarity set by what is actually in stock.

Price bet ↗

A two-party binary option settled by the PRICE oracle at an agreed round, with deterministic settlement anyone can trigger.

Price bet, timed ↗

The same bet settled by the first oracle round at or after an agreed clock time instead of a round number.

Stable vault ↗

An over-collateralized stablecoin engine with oracle staleness gating and permissionless liquidation.

Delivery escrow ↗

Escrow that releases itself when a carrier's tracking page attests delivery: no manual settlement call.

URL oracle ↗

Read an HTTPS endpoint from a contract through validator attestation, without breaking determinism.

Counterparty bridge ↗

Burn-to-mint migration for Counterparty assets, minting only after attestation confirms the burn is irreversible.

More than examples

Start from a scaffold, not a blank file

The repository is also the starter kit: shared building blocks, a generator for the most common contract, and the same checks the chain will run at deploy time.

Reusable patterns ↗

Access control, pausable, safe transfer, state machine, and validation: five building blocks, each small enough to paste into your own contract.

Token rules without code

Describe pausable, freeze, allowlist, and royalty rules in a JSON policy, and the generator emits a deploy-ready controller contract. No JavaScript required.

A scaffold and a linter

npx xchain-contracts scaffold starts a new contract from any template, and the linter runs the deploy-time rules before the chain does.

Tested against the real VM

Every template ships its own test suite, executed in the same sandboxed VM that validators run, not in a lookalike harness.

Go deeper

Where to take it next