For the complete documentation index, see llms.txt. This page is also available as Markdown.

Deploy Smart Contract To Bitcon Cash

This guide covers writing, compiling, and deploying a smart contract to Bitcoin Cash using CashScript, and broadcasting contract transactions through a GetBlock node.

Bitcoin Cash contracts are compiled to Bitcoin Cash Script and locked to a Pay-to-Script-Hash (P2SH) address; deployment means funding that address, not registering code in a global contract account.

Bitcoin Cash smart contracts differ fundamentally from EVM contracts. A contract is a spending condition on a UTXO, not a stateful account. There is no persistent contract storage, no global contract account, and no Solidity, Foundry, Hardhat, or MetaMask wallet_addEthereumChain flow. The canonical reference for the CashScript language and SDK is maintained at cashscript.org; this page describes how to build and broadcast contracts with a GetBlock node behind the network layer.

Prerequisites

  • Node.js version 20 or later

  • The CashScript compiler (cashc) and SDK (cashscript), version 0.10.0 or later, which introduces the TransactionBuilder API used below

  • A funded Bitcoin Cash address to seed the contract and pay miner fees, on chipnet for testing (see Chipnet Faucet)

  • A GetBlock access token from the GetBlock dashboard, used in the endpoint https://go.getblock.io/<ACCESS-TOKEN>/

Network Details

Property
Mainnet
Chipnet (testing)

Network name

mainnet

chipnet

Address type

p2sh32 (default)

p2sh32 (default)

GetBlock RPC

https://go.getblock.io/<ACCESS-TOKEN>/

Provisioned per dashboard (verify)

How to Deploy a Smart Contract

  1. Install the Toolchain

Confirm the compiler version:

2. Write the Contract

Create TransferWithTimeout.cash. This contract lets a recipient claim funds with their signature, and lets the sender reclaim the funds after a timeout block height. It takes three constructor arguments and exposes two spending functions.

  1. Compile to an Artifact

The cashc compiler transpiles the .cash source to Bitcoin Cash Script and writes a JSON artifact that the SDK imports.

Inspect the compiled size and opcode count when optimizing:

4. Instantiate and Derive the Contract Address

Instantiating the contract with its constructor arguments produces the P2SH address that funds are sent to. Deriving the address is the deployment step; the contract exists on-chain once its address holds a UTXO.

  1. Fund the Contract

Send BCH from any wallet to the contract address printed above. On Chipnet, use the faucet listed below. Once the funding transaction confirms, the contract holds a spendable UTXO.

Verify the balance and UTXO set:

6. Connect Through GetBlock

The CashScript SDK reaches the network through a NetworkProvider. The interface exposes four operations: getUtxos, getBlockHeight, getRawTransaction, and sendRawTransaction. A custom provider can route transaction reads, block height, and broadcasting through a GetBlock node, which handles the node-level operations reliably without running local infrastructure.

Address-indexed UTXO retrieval (getUtxos) is not available on shared GetBlock endpoints, because Bitcoin Cash full nodes do not index outputs by address and the wallet and address methods are disallowed on shared nodes. The provider below routes getUtxos to an ElectrumNetworkProvider, which connects to an indexing server, and routes the remaining three operations through GetBlock.

Instantiate the contract with the GetBlock-backed provider:

  1. Spend From the Contract

Spending calls one of the contract functions and satisfies its require conditions. The TransactionBuilder adds the contract UTXO as an input, unlocked by the chosen function, and sends the balance to a destination address minus the miner fee. Broadcasting goes through the GetBlock-backed provider's sendRawTransaction.

Chipnet Faucet

Fund a chipnet address before deploying to the test network.

Last updated

Was this helpful?