Get Started¶
Introduction¶
Chopsticks, developed by the Acala Foundation, is a versatile tool tailored for developers working on Polkadot SDK-based blockchains. With Chopsticks, you can fork live chains locally, replay blocks to analyze extrinsics, and simulate complex scenarios like XCM interactions all without deploying to a live network.
This guide walks you through installing Chopsticks and provides information on configuring a local blockchain fork. By streamlining testing and experimentation, Chopsticks empowers developers to innovate and accelerate their blockchain projects within the Polkadot ecosystem.
For additional support and information, please reach out through GitHub Issues.
Note
Chopsticks uses Smoldot light client, which only supports the native Polkadot SDK API. Consequently, a Chopsticks-based fork doesn't support Ethereum JSON-RPC calls, meaning you cannot use it to fork your chain and connect Metamask.
Prerequisites¶
Before you begin, ensure you have the following installed:
Install Chopsticks¶
You can install Chopsticks globally or locally in your project. Choose the option that best fits your development workflow.
Note
This documentation explains the features of Chopsticks version 0.13.1
. Make sure you're using the correct version to match these instructions.
Global Installation¶
To install Chopsticks globally, allowing you to use it across multiple projects, run:
Now, you should be able to run the chopsticks
command from your terminal.
Local Installation¶
To use Chopsticks in a specific project, first create a new directory and initialize a Node.js project:
Then, install Chopsticks as a local dependency:
Finally, you can run Chopsticks using the npx
command:
Configure Chopsticks¶
To run Chopsticks, you need to configure some parameters. This can be set either through using a configuration file or the command line interface (CLI). The parameters that can be configured are as follows:
genesis
- the link to a parachain's raw genesis file to build the fork from, instead of an endpointtimestamp
- timestamp of the block to fork fromendpoint
- the endpoint of the parachain to forkblock
- use to specify at which block hash or number to replay the forkwasm-override
- path of the Wasm to use as the parachain runtime, instead of an endpoint's runtimedb
- path to the name of the file that stores or will store the parachain's databaseconfig
- path or URL of the config fileport
- the port to expose an endpoint onbuild-block-mode
- how blocks should be built in the fork: batch, manual, instantimport-storage
- a pre-defined JSON/YAML storage path to override in the parachain's storageallow-unresolved-imports
- whether to allow Wasm unresolved imports when using a Wasm to build the parachainhtml
- include to generate storage diff preview between blocksmock-signature-host
- mock signature host so that any signature starts with0xdeadbeef
and filled by0xcd
is considered valid
Configuration File¶
The Chopsticks source repository includes a collection of YAML files that can be used to set up various Polkadot SDK chains locally. You can download these configuration files from the repository's configs
folder.
An example of a configuration file for Polkadot is as follows:
endpoint:
- wss://rpc.ibp.network/polkadot
- wss://polkadot-rpc.dwellir.com
mock-signature-host: true
block: ${env.POLKADOT_BLOCK_NUMBER}
db: ./db.sqlite
runtime-log-level: 5
import-storage:
System:
Account:
- - - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
- providers: 1
data:
free: '10000000000000000000'
ParasDisputes:
$removePrefix: ['disputes'] # those can makes block building super slow
The configuration file allows you to modify the storage of the forked network by rewriting the pallet, state component and value that you want to change. For example, Polkadot's file rewrites Alice's system.Account
storage so that the free balance is set to 10000000000000000000
.
CLI Flags¶
Alternatively, all settings (except for genesis and timestamp) can be configured via command-line flags, providing a comprehensive method to set up the environment.
WebSocket Commands¶
Chopstick's internal WebSocket server has special endpoints that allow the manipulation of the local Polkadot SDK chain.
These are the methods that can be invoked and their parameters:
-
dev_newBlock (newBlockParams) — generates one or more new blocks
newBlockParams
NewBlockParams - the parameters to build the new block with. Where theNewBlockParams
interface includes the following properties:count
number - the number of blocks to builddmp
{ msg: string, sentAt: number }[] - the downward messages to include in the blockhrmp
Record<string | number, { data: string, sentAt: number }[]> - the horizontal messages to include in the blockto
number - the block number to build totransactions
string[] - the transactions to include in the blockump
Record<number, string[]> - the upward messages to include in the blockunsafeBlockHeight
number - build block using a specific block height (unsafe)
-
dev_setBlockBuildMode (buildBlockMode) — sets block build mode
buildBlockMode
BuildBlockMode - the build mode. Can be any of the following modes:
-
dev_setHead (hashOrNumber) — sets the head of the blockchain to a specific hash or number
hashOrNumber
string | number - the block hash or number to set as head
-
dev_setRuntimeLogLevel (runtimeLogLevel) — sets the runtime log level
runtimeLogLevel
number - the runtime log level to set
-
dev_setStorage (values, blockHash) — creates or overwrites the value of any storage
values
object - JSON object resembling the path to a storage valueblockHash
string - the block hash to set the storage value
import { ApiPromise, WsProvider } from '@polkadot/api'; import { Keyring } from '@polkadot/keyring'; async function main() { const wsProvider = new WsProvider('ws://localhost:8000'); const api = await ApiPromise.create({ provider: wsProvider }); await api.isReady; const keyring = new Keyring({ type: 'ed25519' }); const bob = keyring.addFromUri('//Bob'); const storage = { System: { Account: [[[bob.address], { data: { free: 100000 }, nonce: 1 }]], }, }; await api.rpc('dev_setStorage', storage); } main();
-
dev_timeTravel (date) — sets the timestamp of the block to a specific date"
date
string - timestamp or date string to set. All future blocks will be sequentially created after this point in time
Where to Go Next¶
-
Tutorial Fork a Chain with Chopsticks
Visit this guide for step-by-step instructions for configuring and interacting with your forked chain.
| Created: November 22, 2024