Tutorial: Run JavaScript Code on CKB
Tutorial Overview
- Rust and riscv64 target:
rustup target add riscv64imac-unknown-none-elf
The High-Level Ideaβ
As we have learned before, you can use any programming language to write a Script (Smart contract) for CKB. But does it really work in reality? This tutorial will show a full example of using JavaScript to write Scripts and execute them in the CKB-VM.
The process is as follows: first we port a JavaScript engine as a base Script to CKB. Then, we write the business logic in JavaScript and execute this JS-powered Script within the base Script on top of CKB-VM.
It sounds like a of work. But thanks to the CKB VM team, we already have a fully runnable JavaScript engine called ckb-js-vm. It is ported from quick.js so that it is compatible with running on CKB-VM. We just need to take the ckb-js-vm and deploy it on-chain before we can run our JS Script.
Below is a step-by-step guide, and you can also clone the full code example from the Github repo.