CashScript Documentation

website·Indexed 33 days ago

https://cashscript.org/

Documentation for CashScript, a high-level language and toolset for writing smart contracts on Bitcoin Cash (BCH). Includes guides on the cashc compiler, the TypeScript SDK, and implementing covenants using introspection data. Covers contract deployment, transaction building with TransactionBuilder, network providers like ElectrumNetworkProvider, and integration with CashTokens and BCH WalletConnect.

Tokens
46.5K
Snippets
197
Records
326
Agent score
66%

What's inside CashScript

  1. Overview of the CashScript Python SDK

    The cashscript-py library is a community-maintained Python SDK used to build and send CashScript transactions on Bitcoin Cash (BCH). It is a port of the official TypeScript SDK and is intended for developers building server-side tools, bots, or Electron Cash plugins. It handles contract instantiation, transaction building, and network interaction, but requires contracts to be pre-compiled into artifact JSONs using cashc.
  2. Overview of CashScript NetworkProviders

    The CashScript SDK uses NetworkProvider to perform blockchain operations such as retrieving contract balances and sending transactions. Because the interface is standardized, providers can be easily swapped.

    Recommended providers:

    • ElectrumNetworkProvider: For general blockchain network functionality.
    • MockNetworkProvider: For local development.
  3. Overview of CashScript for Bitcoin Cash Smart Contracts

    CashScript is a high-level programming language designed for writing smart contracts on Bitcoin Cash (BCH). It provides a strong abstraction layer over BCH Script (the native virtual machine of Bitcoin Cash) and supports advanced features such as native introspection and CashTokens. While its syntax is based on Solidity (Ethereum), its functionality is tailored to the UTXO model of Bitcoin Cash.
  4. Overview of the CashScript TypeScript SDK

    The CashScript TypeScript SDK provides a set of tools for building smart contract transactions in browser or server environments. It offers standardized network providers for BCH blockchain information, advanced debugging tools, and a type-safe API for transaction building. While optimized for TypeScript to provide better safety guarantees and developer experience, it is also compatible with vanilla JavaScript.
  5. Overview of the CashScript TypeScript SDK

    The CashScript TypeScript SDK allows developers to build smart contract transactions in both browser and server environments. It provides full type-safety to ensure application reliability and quality.
  6. Use MockNetworkProvider for local contract testing

    The MockNetworkProvider allows developers to evaluate transactions locally without interacting with the Bitcoin Cash network. It eliminates the need for testnet balances, smart contract UTXO setup, and network latency, making it ideal for repeated automated tests and debugging. By default, it updates an in-memory UTXO set after transactions are sent (consuming spent UTXOs and creating new ones), though this can be disabled by setting the updateUtxoSet option to false.
    import { MockNetworkProvider } from 'cashscript';
    
    const provider = new MockNetworkProvider();
  7. Understand and mitigate UTXO contention in smart contracts

    In the UTXO model, a single UTXO can only be spent once. If multiple users attempt to interact with a contract that has only one UTXO simultaneously, only one transaction will propagate (due to the first-seen rule), and others will be rejected as conflicts. This is known as UTXO contention. To scale public contracts, developers must use design patterns that avoid shared UTXOs or distribute access across multiple UTXOs.
  8. Reference for transaction and input introspection variables

    The following variables provide metadata about the current transaction and its inputs during validation:
    // Transaction Level
    int tx.version; // Transaction version (1 or 2)
    int tx.locktime; // nLocktime field
    
    // Input Level (Current Evaluation Context)
    int this.activeInputIndex; // Index of the input currently being evaluated
    bytes this.activeBytecode; // Contract bytecode of the input currently being evaluated
    
    // Transaction Inputs Array
    int tx.inputs.length; // Total number of inputs
    
    // Specific Input Properties (where i is the index)
    int tx.inputs[i].value; // Value in satoshis
    bytes tx.inputs[i].lockingBytecode; // scriptPubKey
    bytes tx.inputs[i].unlockingBytecode; // scriptSig
    bytes32 tx.inputs[i].outpointTransactionHash; // Hash of the locking transaction
    int tx.inputs[i].outpointIndex; // Index in the locking transaction
    int tx.inputs[i].sequenceNumber; // nSequence number
    bytes tx.inputs[i].tokenCategory; // Token category (unreversed byte-order)
    bytes tx.inputs[i].nftCommitment; // NFT commitment data
    int tx.inputs[i].tokenAmount; // Amount of fungible tokens (max 64-bit int)
  9. Understand the Bitcoin Cash (BCH) Virtual Machine (CashVM)

    While Bitcoin Cash (BCH) shares fundamentals with Bitcoin (BTC) such as Proof-of-Work and the UTXO model, it has diverged significantly in its virtual machine, known as the CashVM. Unlike BTC, BCH does not use Segwit or Taproot. Instead, the CashVM has been upgraded to support native introspection, CashTokens, BigInts, and re-enabled opcodes to facilitate powerful smart contracts.
  10. Use introspection variables for covenant contracts

    Introspection variables allow contracts to constrain spending by inspecting the transaction's properties. This is the basis for 'covenants'.
  11. Use MockNetworkProvider for local contract testing

    The MockNetworkProvider allows you to evaluate transactions locally without interacting with the actual Bitcoin Cash network. It is primarily used for writing automated tests and debugging contracts. The network type for this provider is 'mocknet'.
  12. Legacy Simple Transaction Builder Overview

    The 'Simple Transaction Builder' was used to complete transactions returned by calling a contract function of a Contract object. It allowed developers to chain methods to define outputs, inputs, and fees before sending the transaction.

    Warning: This API was removed in v0.12. Users are strongly recommended to migrate to the new default transaction builder.