Other Network Providers
The CashScript SDK needs to connect to the BCH network to perform certain operations, like retrieving the contract's balance, or sending transactions.
MockNetworkProvider
new MockNetworkProvider(options?: { updateUtxoSet: boolean })
The MockNetworkProvider is a special network provider that allows you to evaluate transactions locally without interacting with the Bitcoin Cash network. This is useful when writing automated tests for your contracts, or when debugging your contract locally.
The MockNetworkProvider has extra methods to enable this local emulation such as .addUtxo() and .setBlockHeight().
You can read more about the MockNetworkProvider and automated tests on the testing setup page.
The updateUtxoSet option is used to determine whether the UTXO set should be updated after a transaction is sent. If updateUtxoSet is true (default), the UTXO set will be updated to reflect the new state of the mock network. If updateUtxoSet is false, the UTXO set will not be updated.
Example
const provider = new MockNetworkProvider();
const newUtxo = randomUtxo({satoshis: 10_000n})
provider.addUtxo(contractAddress, newUtxo);
The network type of the MockNetworkProvider is 'mocknet'.
Other NetworkProviders
There are two alternative network providers implemented. Currently neither supports CashTokens, so it is recommended to use the ElectrumNetworkProvider.
FullStackNetworkProvider
new FullStackNetworkProvider(network: Network, bchjs: BCHJS)
The FullStackNetworkProvider uses FullStack.cash' infrastructure to connect to the BCH network. FullStack.cash' offers dedicated infrastructure and support plans for larger projects. Both network and bchjs parameters are mandatory, where bchjs is an instance of FullStack.cash' BCHJS.
The FullStackNetworkProvider does not currently support CashTokens. If you want to use CashTokens, please use the ElectrumNetworkProvider instead.
Example
import BCHJS from '@psf/bch-js';
import { FullStackNetworkProvider } from 'cashscript';
const restURL = 'https://api.fullstack.cash/v3/';
const apiToken = 'eyJhbGciO...'; // Your JWT token here.
const bchjs = new BCHJS({ restURL, apiToken });
const provider = new FullStackNetworkProvider('mainnet', bchjs);
BitcoinRpcNetworkProvider
new BitcoinRpcNetworkProvider(network: Network, url: string, options?: any)
The BitcoinRpcNetworkProvider uses a direct connection to a BCH node. Note that a regular node does not have indexing, so any address of interest (e.g. the contract address) need to be registered by the node before sending any funds to those addresses. Because of this it is recommended to use a different network provider unless you have a specific reason to use the RPC provider.
The BitcoinRpcNetworkProvider does not currently support CashTokens. If you want to use CashTokens, please use the ElectrumNetworkProvider instead.
Example
import { BitcoinRpcNetworkProvider } from 'cashscript';
const provider = new BitcoinRpcNetworkProvider('mainnet', 'http://localhost:8332');