Quick Start (5min)
We recommend using offCKB to quickly set up a local development environment using our pre-built boilerplates. This streamlined approach allows you to jumpstart your project efficiently without the hassle of manual configuration.
Install OffCKB
OffCKB provides a one-line command to start a local blockchain (CKB Devnet) with pre-funded test accounts and built-in Scripts
npm install -g @offckb/cli
Throughout this documentation, we use offckb/cli version >=0.3.0. You can always run the above command to update to the latest version.
Create a New Project
To setup a boilerplate for a full-stacked dApp, run the following command. Replace <my-dapp-project>
with any name you like.
offckb create <my-dapp-project>
When prompted to select a bare template, use your arrow keys to select the frontend framework you prefer. If you wish to use a frontend framework not listed, please refer to this section.
- Command
- Response
? Select a bare template
Remix-Vite Bare Templates
> Next.js Bare Templates
---
A full-stack template with Next.js framework and ckb-script-templates,
[next.js,tailwindcss,ckb-script-templates,typescript,rust]
Cloning into '/Users/Desktop/offckb/templates/temp-clone-folder'...
Folder examples/next-js-template downloaded successfully from https://github.com/nervosnetwork/docs.nervos.org and moved to /Users/nervosDocs/Desktop/offckb/my-dapp-project
How to create a Script-only project?
To create a project focused solely on Script development without any frontend work, you can run the following command. Replace <my-script-project>
with any name you like.
offckb create --script <my-script-project>
Note that offckb
essentially utilizes an underlying tool called ckb-script-templates
to initialize Script projects. You can directly engage ckb-script-templates
for more specific Script development tasks. For further details, refer to the Script tutorial.
Start the Devnet
- Command
- Response
offckb node
/bin/sh: /Users/nervosDocs/.nvm/versions/node/v18.12.1/lib/node_modules/@offckb/cli/target/ckb/ckb: No such file or directory
/Users/nervosDocs/.nvm/versions/node/v18.12.1/lib/node_modules/@offckb/cli/target/ckb/ckb not found, download and install the new version 0.113.1..
CKB installed successfully.
init Devnet config folder: /Users/nervosDocs/.nvm/versions/node/v18.12.1/lib/node_modules/@offckb/cli/target/devnet
modified /Users/nervosDocs/.nvm/versions/node/v18.12.1/lib/node_modules/@offckb/cli/target/devnet/ckb-miner.toml
CKB output: 2024-03-20 07:56:44.765 +00:00 main INFO sentry sentry is disabled
CKB output: 2024-03-20 07:56:44.766 +00:00 main INFO ckb_bin::helper raise_fd_limit newly-increased limit: 61440
CKB output: 2024-03-20 07:56:44.854 +00:00 main INFO ckb_bin::subcommand::run ckb version: 0.113.1 (95ad24b 2024-01-31)
CKB output: 2024-03-20 07:56:45.320 +00:00 main INFO ckb_db_migration Init database version 20230206163640
CKB output: 2024-03-20 07:56:45.329 +00:00 main INFO ckb_launcher Touch chain spec hash: Byte32(0x3036c73473a371f3aa61c588c38924a93fb8513e481fa7c8d884fc4cf5fd368a)
Your local CKB blockchain is currently active with the RPC endpoint at localhost:8114. To stop the chain, press CTRL+C
in the terminal. To resume where you left off, simply run offckb node
again.
If you need to start fresh with a new chain, run the following command before starting the node:
- Command
- Response
offckb clean
Chain data cleaned.
Run the DApp Project
The dApp is based on various JavaScript web frontend frameworks, such as Remix-Vite
and Next.js
, and is integrated with the CKB JavaScript framework, Lumos. All Lumos-related code is defined in the frontend/offckb.config.ts
file.
To start development, navigate to the frontend workspace:
cd frontend
Now, run the following command to install dependencies and start the dApp:
npm i && npm run dev
Once the server is up and running, you can view the dApp by visiting localhost:3000. You can start editing the page by modifying app/page.tsx
. As you make changes to the file, the page will automatically update to reflect your edits. For detailed instructions on how to start the dApp and explore additional config options, please see the README.md
file.
DApp Project Structure
The boilerplate project for a full-stack CKB dApp comprises two main components:
- The frontend, which utilizes the CKB JavaScript framework, CCC.
- The Scripts, which leverage the ckb-script-templates.
By default, the Next.js template comes with a simple Script hello-world
under contracts/hello-world/src/main.rs
.
- Make (≥v4.3), Sed (≥v4.7), Bash (≥v5.0), sha256sum (≥v9.0)
- Rust (≥v.1.71.1) and
riscv64
target - Clang (≥v18)
- cargo-generate (≥0.17.0)
For detailed installation steps, refer to our Installation Guide
Script-Related Commands
- Add a new Script:
make generate
- Build Script:
make build
- Run unit tests:
make test
Deploy Scripts to Blockchain
After building your Scripts, you can navigate to your frontend workspace and deploy your Scripts to the Devnet or Testnet by using the following command:
- Devnet
- Testnet
offckb deploy --network devnet
offckb deploy --network testnet
Use Scripts in Frontend
If the deployment is successful, the offckb.config.ts
file will contain the necessary details to import and utilize your Scripts in your frontend dApp. Here's how you can use your Scripts directly:
import offckb from "offckb.config";
const myScriptDeps: CellDep[] = offCKB.myScripts["YOUR_SCRIPT_NAME"]!.cellDeps;
const myScript: Script = {
codeHash: offCKB.myScripts["hash-lock"]!.codeHash,
hashType: offCKB.myScripts["hash-lock"]!.hashType,
args: lockArgs,
};
Use a Different Frontend Framework?
The frontend world is filled with numerous (perhaps too many) frameworks. New frameworks come out every day, and different developers have different preferences among these frameworks. So if you don't use the framework in the pre-defined boilerplate, like remix-vite
and next.js
, don't worry! You can still use offckb
with the inject-config
command.
First, simply initialize your dApp project with your preferred framework, such as CRA:
- Command
- Response
npx create-react-app my-cra-dapp --template typescript
Creating a new React app in /Users/nervosDocs/Desktop/offckb/my-cra-dapp.
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template-typescript...
added 1489 packages in 2m
Next, navigate to your dApp directory and initialize the offckb
config within your framework:
- Command
- Response
cd my-cra-dapp
offckb inject-config
All good. You can now use it in your project like:
import offCKB from "offckb.config";
const myScriptCodeHash = offCKB.myScripts['script-name'].codeHash;
const omnilockScriptCodeHash = offCKB.systemScripts['omnilock'].codeHash;
Check example at https://github.com/nervosnetwork/docs.nervos.org/tree/develop/examples/simple-transfer
Now, when you deploy your Scripts with offckb deploy --network devnet --target <your-folder-to-your-script-binaries>
, run the following command in your frontend root path where the offckb.config.ts
file is located to update the deployed Scripts info for your frontend.
- Command
- Response
offckb sync-scripts
scripts json config updated.
For switching between different networks, you might need to update the readEnvNetwork
method in offckb.config.ts
according to your framework.
function readEnvNetwork(): "devnet" | "testnet" | "mainnet" {
// you may need to update the env method
// according to your frontend framework
const network = process.env.NETWORK;
const defaultNetwork = "devnet";
if (!network) return defaultNetwork;
if (!["devnet", "testnet", "mainnet"].includes(network)) {
return defaultNetwork;
}
return network as "devnet" | "testnet" | "mainnet";
}
Debug a transaction
If you are using the offckb dApp template to create your project, one of the beneficial is that all the failed transactions will be dumped and recorded so you can debug them later.
Everytime you run a transaction, you can debug it with the transaction hash:
offckb debug <transaction-hash>
It will verify all the scripts in the transaction and print the detailed info in the terminal.
offckb debug --tx-hash 0x64c936ee78107450d49e57b7453dce9031ce68b056b2f1cdad5c2218ab7232ad
Dump transaction successfully
******************************
****** Input[0].Lock ******
hello, this is new add!
Hashed 1148 bytes in sighash_all
sighash_all = 5d9b2340738ee28729fc74eba35e6ef969878354fe556bd89d5b6f62642f6e50
event = {"pubkey":"45c41f21e1cf715fa6d9ca20b8e002a574db7bb49e96ee89834c66dac5446b7a","tags":[["ckb_sighash_all","5d9b2340738ee28729fc74eba35e6ef969878354fe556bd89d5b6f62642f6e50"]],"created_at":1725339769,"kind":23334,"content":"Signing a CKB transaction\n\nIMPORTANT: Please verify the integrity and authenticity of connected Nostr client before signing this message\n","id":"90af298075ac878901282e23ce35b24e584b7727bc545e149fc259875a23a7aa","sig":"b505e7d5b643d2e6b1f0e5581221bbfe3c37f17534715e51eecf5ff97a2e1b828a3d767eb712555c78a8736e9085b4960458014fa171d5d169a1b267b186d2f3"}
verify_signature costs 3654 k cycles
Run result: 0
Total cycles consumed: 4013717(3.8M)
Transfer cycles: 44947(43.9K), running cycles: 3968770(3.8M)
******************************
****** Output[0].Type ******
verify_signature costs 3654 k cycles
Run result: 0
Total cycles consumed: 3916670(3.7M)
Transfer cycles: 43162(42.2K), running cycles: 3873508(3.7M)
If you want to debug a single cell script in the transaction, you can use the following command:
offckb debug <transaction-hash> --single-script <single-cell-script-option>
The single-cell-script-option
format is <cell-type>[<cell-index>].<script-type>
, eg: "input[0].lock"
cell-type
could beinput
oroutput
, refers to the cell typecell-index
is the index of the cell in the transactionscript-type
could belock
ortype
, refers to the script type
Or you can replace the script with a binary file in your single cell script debug session:
offckb debug <transaction-hash> --single-script <single-cell-script-option> --bin <path/to/binary/file>
All the debug utils are borrowed from ckb-debugger.
About offckb.config.ts
offckb.config.ts
is a straightforward TypeScript file that encapsulates basic devnet information. Everything is explicitly defined, making it easy to modify. This minimalistic approach ensures that offckb
does not impose limitations on the tech selections and development processes of developers.
If you have any feedback you would like to share with us, feel free to contact us at github or discord.