Skip to content

Testing Smart Contracts with MockChain#

@fleet-sdk/mock-chain lets you compile ErgoScript contracts, construct off-chain transactions, and verify both happy-path execution and adversarial failure cases locally in TypeScript without deploying to Testnet or running a full node.

By the end of this tutorial, you will set up a repeatable Vitest test suite that validates:

  1. Multi-party signature and block-height conditions (HEIGHT > refundDeadline) with recipient and fee-cap enforcement.
  2. Stateful self-replicating eUTXO covenants that conserve tokens and storage-rent reserves across partial purchases.
  3. Adversarial security checks ({ throw: false }), including defense against eUTXO double-satisfaction attacks and safe handling of tokenless dust boxes.

Prerequisites#

  • Node.js >= 18 and npm
  • Basic familiarity with ErgoScript box registers (R4–R9) and Fleet SDK transaction builders

1. Initialize the Test Project#

Create a fresh TypeScript project and install the Fleet SDK packages alongside vitest:

mkdir ergo-mockchain-tests && cd ergo-mockchain-tests
npm init -y
npm pkg set type="module" scripts.test="vitest run"
npm install --ignore-scripts -D [email protected] [email protected] @fleet-sdk/[email protected] @fleet-sdk/[email protected] @fleet-sdk/[email protected]

These examples use the pinned versions above and generated test keys. They are learning fixtures; review any adapted contract before deploying it with funds. Create a minimal tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "skipLibCheck": true
  }
}

2. Test a Multi-Party Escrow Contract with Timeouts#

Create tests/escrowDepositContract.spec.ts. In Ergo, multi-party spending paths compose naturally with Sigma propositions (&& and ||), while mockChain.jumpTo(height) lets you test block-height transitions deterministically.

Notice three practices in the contract below:

  • Named constants (buyerPK, sellerPK, arbiterPK, refundDeadline, maxMinerFee) are injected safely at compile time through compile(..., { map }) rather than string interpolation.
  • Both dispute branches (sellerAward and buyerAward) enforce OUTPUTS(0).propositionBytes and SELF.value - maxMinerFee so a co-signing arbiter cannot divert funds or siphon value via excessive fees.
  • The post-deadline timeoutRefund branch likewise enforces OUTPUTS(0).propositionBytes == buyerPK.propBytes and OUTPUTS(0).value >= SELF.value - maxMinerFee, ensuring automated refund transactions preserve the buyer's destination address and refund floor.
  • Only one input using this escrow script may be spent per transaction. This prevents two identical escrows from sharing one payout check; batching needs a separate design that binds each input to its own payout.
import { MockChain } from "@fleet-sdk/mock-chain";
import { compile } from "@fleet-sdk/compiler";
import {
  OutputBuilder,
  SGroupElement,
  SInt,
  SLong,
  SSigmaProp,
  TransactionBuilder
} from "@fleet-sdk/core";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

const ESCROW_DEPOSIT = 5_000_000_000n; // 5 ERG
const MAX_MINER_FEE = 1_100_000n; // 0.0011 ERG
const DIVERTED_AMOUNT = 1_000_000n; // 0.001 ERG

const ESCROW_DEPOSIT_SCRIPT = `{
  val uniqueEscrowInput = INPUTS.filter { (box: Box) =>
    box.propositionBytes == SELF.propositionBytes
  }.size == 1
  val cooperativeRelease = buyerPK && sellerPK

  val sellerAward =
    arbiterPK &&
    sellerPK &&
    sigmaProp(
      OUTPUTS.size >= 1 &&
      OUTPUTS(0).propositionBytes == sellerPK.propBytes &&
      OUTPUTS(0).value >= SELF.value - maxMinerFee
    )

  val buyerAward =
    arbiterPK &&
    buyerPK &&
    sigmaProp(
      OUTPUTS.size >= 1 &&
      OUTPUTS(0).propositionBytes == buyerPK.propBytes &&
      OUTPUTS(0).value >= SELF.value - maxMinerFee
    )

  val timeoutRefund =
    buyerPK &&
    sigmaProp(
      HEIGHT > refundDeadline &&
      OUTPUTS.size >= 1 &&
      OUTPUTS(0).propositionBytes == buyerPK.propBytes &&
      OUTPUTS(0).value >= SELF.value - maxMinerFee
    )

  sigmaProp(uniqueEscrowInput) &&
    (cooperativeRelease || sellerAward || buyerAward || timeoutRefund)
}`;

describe("Arbiter escrow deposit contract", () => {
  const mockChain = new MockChain({ height: 1_052_944 });
  const refundDeadline = mockChain.height + 720;

  const buyer = mockChain.newParty("Buyer");
  const seller = mockChain.newParty("Seller");
  const arbiter = mockChain.newParty("Arbiter");

  const ergoTree = compile(ESCROW_DEPOSIT_SCRIPT, {
    map: {
      buyerPK: SSigmaProp(SGroupElement(buyer.key.publicKey)),
      sellerPK: SSigmaProp(SGroupElement(seller.key.publicKey)),
      arbiterPK: SSigmaProp(SGroupElement(arbiter.key.publicKey)),
      refundDeadline: SInt(refundDeadline),
      maxMinerFee: SLong(MAX_MINER_FEE)
    }
  });

  const escrowContract = mockChain.addParty(ergoTree.toHex(), "Escrow Deposit Contract");

  beforeEach(() => {
    mockChain.jumpTo(1_052_944);
  });

  afterEach(() => {
    mockChain.reset();
  });

  it("Should allow cooperative release co-signed by Buyer and Seller", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, seller.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [buyer, seller] })).to.be.true;
    expect(seller.balance).to.be.deep.equal({ nanoergs: payout, tokens: [] });
  });

  it("Should allow Arbiter and Seller to resolve a dispute in favor of Seller", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, seller.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [arbiter, seller] })).to.be.true;
    expect(seller.balance).to.be.deep.equal({ nanoergs: payout, tokens: [] });
  });

  it("Should reject an Arbiter and Seller spend that diverts funds to Arbiter", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, arbiter.address))
      .payMinFee()
      .build();

    expect(
      mockChain.execute(transaction, { signers: [arbiter, seller], throw: false })
    ).to.be.false;
    expect(escrowContract.balance).to.be.deep.equal({ nanoergs: ESCROW_DEPOSIT, tokens: [] });
  });

  it("Should allow Arbiter and Buyer to resolve a dispute in favor of Buyer", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, buyer.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [arbiter, buyer] })).to.be.true;
    expect(buyer.balance).to.be.deep.equal({ nanoergs: payout, tokens: [] });
  });

  it("Should reject an Arbiter and Buyer spend that diverts funds or underpays Buyer", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const wrongRecipientTx = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, arbiter.address))
      .payMinFee()
      .build();

    expect(
      mockChain.execute(wrongRecipientTx, { signers: [arbiter, buyer], throw: false })
    ).to.be.false;

    const underpaidBuyerTx = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to([
        new OutputBuilder(payout - DIVERTED_AMOUNT, buyer.address),
        new OutputBuilder(DIVERTED_AMOUNT, arbiter.address)
      ])
      .payMinFee()
      .build();

    expect(
      mockChain.execute(underpaidBuyerTx, { signers: [arbiter, buyer], throw: false })
    ).to.be.false;
    expect(escrowContract.balance).to.be.deep.equal({ nanoergs: ESCROW_DEPOSIT, tokens: [] });
  });

  it("Should reject a unilateral refund by Buyer before the deadline", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, buyer.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [buyer], throw: false })).to.be.false;
    expect(escrowContract.balance).to.be.deep.equal({ nanoergs: ESCROW_DEPOSIT, tokens: [] });
  });

  it("Should reject two escrow inputs sharing a single award", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });

    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos, { ensureInclusion: true })
      .to(new OutputBuilder(ESCROW_DEPOSIT - MAX_MINER_FEE, seller.address))
      .sendChangeTo(arbiter.address)
      .payMinFee()
      .build();

    expect(
      mockChain.execute(transaction, { signers: [arbiter, seller], throw: false })
    ).to.be.false;
    expect(escrowContract.balance.nanoergs).to.equal(2n * ESCROW_DEPOSIT);
  });

  it("Should allow Buyer to reclaim funds to Buyer address within fee cap after deadline", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });
    mockChain.jumpTo(refundDeadline + 1);

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const transaction = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, buyer.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [buyer] })).to.be.true;
    expect(buyer.balance).to.be.deep.equal({ nanoergs: payout, tokens: [] });
  });

  it("Should reject a post-deadline Buyer spend directed to another party", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });
    mockChain.jumpTo(refundDeadline + 1);

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const thirdPartyTx = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to(new OutputBuilder(payout, seller.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(thirdPartyTx, { signers: [buyer], throw: false })).to.be.false;
    expect(escrowContract.balance).to.be.deep.equal({ nanoergs: ESCROW_DEPOSIT, tokens: [] });
  });

  it("Should reject a post-deadline Buyer refund that deducts more than maxMinerFee", () => {
    escrowContract.addBalance({ nanoergs: ESCROW_DEPOSIT });
    mockChain.jumpTo(refundDeadline + 1);

    const payout = ESCROW_DEPOSIT - MAX_MINER_FEE;
    const excessiveDeductionTx = new TransactionBuilder(mockChain.height)
      .from(escrowContract.utxos)
      .to([
        new OutputBuilder(payout - DIVERTED_AMOUNT, buyer.address),
        new OutputBuilder(DIVERTED_AMOUNT, arbiter.address)
      ])
      .payMinFee()
      .build();

    expect(
      mockChain.execute(excessiveDeductionTx, { signers: [buyer], throw: false })
    ).to.be.false;
    expect(escrowContract.balance).to.be.deep.equal({ nanoergs: ESCROW_DEPOSIT, tokens: [] });
  });
});

3. Test Stateful Covenants and Double-Satisfaction Defenses#

Many Ergo protocols use self-replicating boxes that allow anyone to purchase a slice of tokens as long as a continuation box preserves the remaining token supply and minimum nanoERG reserve.

When writing and testing open-order or pool contracts in the eUTXO model, always verify two adversarial edge cases:

  1. Double-satisfaction: If two contract boxes are spent in the same transaction and both merely check that OUTPUTS(0) pays sellerPK, an attacker can spend two boxes while paying the seller only once. Binding OUTPUTS(0).R4[Coll[Byte]].getOrElse(Coll[Byte]()) == SELF.id ties the seller payout box to a single unique input box ID.
  2. Tokenless dust inputs: Because val bindings in ErgoScript evaluate eagerly before sellerPK || ..., indexing SELF.tokens(0) without checking SELF.tokens.size >= 1 would throw an out-of-bounds error and block sellerPK from recovering accidental tokenless ERG boxes sent to the contract address.

Create tests/selfReplicatingTokenSaleContract.spec.ts:

import { MockChain } from "@fleet-sdk/mock-chain";
import { compile } from "@fleet-sdk/compiler";
import {
  OutputBuilder,
  SByte,
  SColl,
  SGroupElement,
  SLong,
  SSigmaProp,
  TransactionBuilder
} from "@fleet-sdk/core";
import { afterEach, describe, expect, it } from "vitest";

const SALE_TOKEN_ID = "1111111111111111111111111111111111111111111111111111111111111111";
const MIN_BOX_VALUE = 1_000_000n;
const PRICE_PER_TOKEN = 10_000_000n; // 0.01 ERG per token

function hexToBytes(hexString: string): Uint8Array {
  return Uint8Array.from({ length: hexString.length / 2 }, (_, index) =>
    Number.parseInt(hexString.slice(index * 2, index * 2 + 2), 16)
  );
}

const SELF_REPLICATING_TOKEN_SALE_SCRIPT = `{
  val saleCycleValid = if (SELF.tokens.size >= 1 && OUTPUTS.size >= 1) {
    val selfTokenId = SELF.tokens(0)._1
    val selfTokenAmount = SELF.tokens(0)._2

    val remainingTokens = if (
      OUTPUTS.size >= 2 &&
      OUTPUTS(1).propositionBytes == SELF.propositionBytes &&
      OUTPUTS(1).tokens.size == 1 &&
      OUTPUTS(1).tokens(0)._1 == selfTokenId
    ) {
      OUTPUTS(1).tokens(0)._2
    } else {
      0L
    }

    val continuationSafe = remainingTokens == 0L || OUTPUTS(1).value >= SELF.value
    val soldTokens = selfTokenAmount - remainingTokens

    val payoutValid =
      soldTokens > 0L &&
      OUTPUTS(0).propositionBytes == sellerPK.propBytes &&
      (if (pricePerToken > 0L) OUTPUTS(0).value / pricePerToken >= soldTokens else false) &&
      OUTPUTS(0).R4[Coll[Byte]].getOrElse(Coll[Byte]()) == SELF.id

    continuationSafe && payoutValid
  } else {
    false
  }

  sellerPK || sigmaProp(saleCycleValid)
}`;

describe("Self-replicating token sale contract", () => {
  const mockChain = new MockChain({ height: 1_052_944 });

  const seller = mockChain.newParty("Seller");
  const buyer = mockChain.newParty("Buyer");
  const attacker = mockChain.newParty("Attacker");

  const ergoTree = compile(SELF_REPLICATING_TOKEN_SALE_SCRIPT, {
    map: {
      sellerPK: SSigmaProp(SGroupElement(seller.key.publicKey)),
      pricePerToken: SLong(PRICE_PER_TOKEN)
    }
  });

  const saleContract = mockChain.addParty(ergoTree.toHex(), "Token Sale Contract");

  afterEach(() => {
    mockChain.reset();
  });

  it("Should allow a partial purchase when continuation box preserves remaining supply", () => {
    saleContract.addBalance({
      nanoergs: MIN_BOX_VALUE,
      tokens: [{ tokenId: SALE_TOKEN_ID, amount: 100n }]
    });
    buyer.addBalance({ nanoergs: 2_000_000_000n });

    const saleBox = saleContract.utxos.toArray()[0];
    const tokensToBuy = 25n;
    const remainingTokens = 75n;
    const sellerPayout = tokensToBuy * PRICE_PER_TOKEN;

    const transaction = new TransactionBuilder(mockChain.height)
      .from([saleBox, ...buyer.utxos.toArray()])
      .to([
        new OutputBuilder(sellerPayout, seller.address).setAdditionalRegisters({
          R4: SColl(SByte, hexToBytes(saleBox.boxId)).toHex()
        }),
        new OutputBuilder(MIN_BOX_VALUE, saleContract.address).addTokens({
          tokenId: SALE_TOKEN_ID,
          amount: remainingTokens
        }),
        new OutputBuilder(MIN_BOX_VALUE, buyer.address).addTokens({
          tokenId: SALE_TOKEN_ID,
          amount: tokensToBuy
        })
      ])
      .sendChangeTo(buyer.address)
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [buyer] })).to.be.true;
    expect(saleContract.balance).to.be.deep.equal({
      nanoergs: MIN_BOX_VALUE,
      tokens: [{ tokenId: SALE_TOKEN_ID, amount: remainingTokens }]
    });
  });

  it("Should reject a double-satisfaction attack across two concurrent sale boxes", () => {
    saleContract.addBalance({
      nanoergs: MIN_BOX_VALUE,
      tokens: [{ tokenId: SALE_TOKEN_ID, amount: 10n }]
    });
    saleContract.addBalance({
      nanoergs: MIN_BOX_VALUE,
      tokens: [{ tokenId: SALE_TOKEN_ID, amount: 10n }]
    });
    attacker.addBalance({ nanoergs: 2_000_000_000n });

    const [firstSaleBox, secondSaleBox] = saleContract.utxos.toArray();
    const singleBoxPayout = 10n * PRICE_PER_TOKEN;

    const transaction = new TransactionBuilder(mockChain.height)
      .from([firstSaleBox, secondSaleBox, ...attacker.utxos.toArray()])
      .to([
        new OutputBuilder(singleBoxPayout, seller.address).setAdditionalRegisters({
          R4: SColl(SByte, hexToBytes(firstSaleBox.boxId)).toHex()
        }),
        new OutputBuilder(MIN_BOX_VALUE, attacker.address).addTokens({
          tokenId: SALE_TOKEN_ID,
          amount: 20n
        })
      ])
      .sendChangeTo(attacker.address)
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [attacker], throw: false })).to.be.false;
  });

  it("Should allow seller to sweep a tokenless ERG box without failing", () => {
    saleContract.addBalance({ nanoergs: 5_000_000n, tokens: [] });

    const attackerSweep = new TransactionBuilder(mockChain.height)
      .from(saleContract.utxos)
      .to(new OutputBuilder(3_900_000n, attacker.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(attackerSweep, { signers: [attacker], throw: false })).to.be.false;

    const sellerRecovery = new TransactionBuilder(mockChain.height)
      .from(saleContract.utxos)
      .to(new OutputBuilder(3_900_000n, seller.address))
      .payMinFee()
      .build();

    expect(mockChain.execute(sellerRecovery, { signers: [seller] })).to.be.true;
    expect(saleContract.balance).to.be.deep.equal({ nanoergs: 0n, tokens: [] });
  });

  it("Should reject a purchase whose required payment exceeds a Long", () => {
    const enormousSupply = 1_000_000_000_000n;
    saleContract.addBalance({
      nanoergs: MIN_BOX_VALUE,
      tokens: [{ tokenId: SALE_TOKEN_ID, amount: enormousSupply }]
    });
    attacker.addBalance({ nanoergs: 10_000_000n });
    const saleBox = saleContract.utxos.toArray()[0];

    const transaction = new TransactionBuilder(mockChain.height)
      .from([saleBox, ...attacker.utxos.toArray()], { ensureInclusion: true })
      .to([
        new OutputBuilder(MIN_BOX_VALUE, seller.address).setAdditionalRegisters({
          R4: SColl(SByte, hexToBytes(saleBox.boxId)).toHex()
        }),
        new OutputBuilder(MIN_BOX_VALUE, attacker.address).addTokens({
          tokenId: SALE_TOKEN_ID, amount: enormousSupply
        })
      ])
      .sendChangeTo(attacker.address)
      .payMinFee()
      .build();

    expect(mockChain.execute(transaction, { signers: [attacker], throw: false })).to.be.false;
    expect(saleContract.balance.tokens[0].amount).to.equal(enormousSupply);
  });
});

4. Run the Suite#

Execute Vitest from the project root:

npm test

The tests run in memory through the Sigma interpreter embedded in @fleet-sdk/mock-chain; package installation requires network access. The sale predicate divides the nonnegative payout by a positive price instead of multiplying token quantity by price, avoiding signed-Long payment overflow.

Troubleshooting Common Pitfalls#

Symptom Root Cause Fix
Option.get evaluation failure on missing register Calling OUTPUTS(0).R4[Coll[Byte]].get when R4 is empty or holds another type Use .getOrElse(Coll[Byte]()) with a safe sentinel value instead of .get
IndexOutOfBounds blocks admin/seller fallback on tokenless box val bindings evaluate eagerly before sellerPK || ..., so SELF.tokens(0) fails if a dust box has no tokens Guard collection indexing with SELF.tokens.size >= 1 inside the if condition
Boundary height off-by-one during mockChain.execute() mockBlockchainStateContext() sets sigmaPreHeader one block ahead of sigmaLastHeaders, and mockChain.reset() clears UTxOs without rewinding mockChain.height Explicitly call mockChain.jumpTo(targetHeight) in height-sensitive tests and account for candidate block height
SColl(SByte, ...) rejects a hex string or undefined helper SColl(SByte, data) expects a Uint8Array (or byte array) Convert 32-byte hex IDs (such as boxId or tokenId) to Uint8Array before passing to SColl(SByte, ...)
Contract input omitted from built transaction TransactionBuilder.from() skips inputs not needed to cover output asset totals Pass .from(contractBox, { ensureInclusion: true }) when the contract box carries no unique required asset
Test throws instead of returning false on invalid spend mockChain.execute() throws script-reduction errors by default for debugging Pass { throw: false } when asserting that an unauthorized spend is rejected