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 theTransactionBuilderAPI used belowA 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
Network name
mainnet
chipnet
Address type
p2sh32 (default)
p2sh32 (default)
GetBlock RPC
https://go.getblock.io/<ACCESS-TOKEN>/
Provisioned per dashboard (verify)
Deploy to chipnet before mainnet. Funds sent to a contract address compiled from incorrect source or constructor arguments can become permanently unspendable, because no key can satisfy a spending condition that no function encodes.
Never commit a private key or WIF to source control. Load signing keys from environment variables, and use a throwaway key for testing. Keys committed to a public repository are swept by automated bots within seconds.
How to Deploy a Smart Contract
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.
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.
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:
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.
chipnet.imaginary.cash faucet — dispenses chipnet BCH per request; requires a destination chipnet address (verify)
Last updated
Was this helpful?