The Blockchain singleton simulates the OPNet blockchain environment. It manages contracts, blocks, addresses, and transaction context.
Import: import { Blockchain } from '@btc-vision/unit-test-framework'
async init(): Promise<void>Initializes the blockchain VM. Must be called before any contract interaction.
dispose(): voidResets the current execution state. Call in afterEach.
cleanup(): voidFull cleanup including VM bindings. Call in afterAll.
clearContracts(): voidUnregisters all contracts from the blockchain.
get blockNumber(): bigint
set blockNumber(blockNumber: bigint)Current block height. Default: 1n.
get medianTimestamp(): bigint
set medianTimestamp(timestamp: bigint)Median block timestamp.
mineBlock(): voidAdvances blockNumber by 1. Used to test block-boundary behavior like updates:
await contract.update(v2Address);
Blockchain.mineBlock();
// Update now takes effect
const value = await contract.getValue();get msgSender(): Address
set msgSender(sender: Address)The msg.sender for contract calls.
get txOrigin(): Address
set txOrigin(from: Address)The tx.origin for contract calls. Must be a valid tweaked public key.
get transaction(): Transaction | null
set transaction(tx: Transaction | null)The current Bitcoin transaction context (inputs/outputs).
generateRandomAddress(): AddressGenerates a cryptographically random OPNet address.
generateRandomWallet(): WalletGenerates a full wallet with both ECDSA and ML-DSA keypairs:
const wallet = Blockchain.generateRandomWallet();
wallet.address; // Address
wallet.keypair; // Secp256k1 keypair
wallet.mldsaKeypair; // ML-DSA keypair
wallet.quantumPublicKeyHex; // ML-DSA public key (hex)generateAddress(deployer: Address, salt: Buffer, from: Address): AddressDeterministic address generation (CREATE2-style).
readonly DEAD_ADDRESS: AddressThe dead/burn address (Address.dead()).
register(contract: ContractRuntime): voidRegisters a contract with the blockchain. Required for contract-to-contract calls:
const tokenA = new OP20({ ... });
const swap = new MySwapContract(deployer, swapAddr);
Blockchain.register(tokenA);
Blockchain.register(swap);unregister(contract: ContractRuntime): voidgetContract(address: Address): ContractRuntimeLooks up a registered contract by address.
isContract(address: Address): booleanReturns true if the address has a registered contract.
expandTo18Decimals(n: number): bigintConverts n to n * 10^18. Example: 100 -> 100_000_000_000_000_000_000n.
expandToDecimal(n: number, decimals: number): bigintConverts n to n * 10^decimals.
decodeFrom18Decimals(n: bigint): numberConverts n / 10^18 to a number.
decodeFromDecimal(n: bigint, decimals: number): numberencodePrice(reserve0: bigint, reserve1: bigint): [bigint, bigint]Encodes a price from AMM reserves (UQ112x112 format).
backupStates(): voidSaves the current state of all registered contracts.
restoreStates(): voidRestores previously backed-up states.
registerMLDSAPublicKey(address: Address, publicKey: Uint8Array): voidRegisters an ML-DSA public key for signature verification.
getMLDSAPublicKey(address: Address): Uint8Array | undefinedEnable tracing to get detailed output during test execution:
// Gas tracing - logs gas usage per call
Blockchain.traceGas = true;
Blockchain.enableGasTracking();
Blockchain.disableGasTracking();
// Storage pointer tracing
Blockchain.tracePointers = true;
Blockchain.enablePointerTracking();
Blockchain.disablePointerTracking();
// Contract call tracing
Blockchain.traceCalls = true;
Blockchain.enableCallTracking();
Blockchain.disableCallTracking();
// Deployment tracing
Blockchain.traceDeployments = true;simulateRealEnvironment: boolean // Default: falseWhen true, simulates a more realistic execution environment.
changeNetwork(network: Network): voidSwitch to a different Bitcoin network (e.g., mainnet, testnet).
| Property | Type | Default | Description |
|---|---|---|---|
network |
Network |
regtest |
Bitcoin network |
blockNumber |
bigint |
1n |
Current block height |
medianTimestamp |
bigint |
BigInt(Date.now()) |
Block median time |
msgSender |
Address |
Address.dead() |
Call sender |
txOrigin |
Address |
Address.dead() |
Transaction origin |
transaction |
Transaction | null |
null |
Current BTC transaction |
traceGas |
boolean |
false |
Gas logging |
tracePointers |
boolean |
false |
Storage pointer logging |
traceCalls |
boolean |
false |
Call logging |
traceDeployments |
boolean |
false |
Deployment logging |
simulateRealEnvironment |
boolean |
false |
Realistic mode |
DEAD_ADDRESS |
Address |
Address.dead() |
Burn address |