Giter Site home page Giter Site logo

retricsu / ckb-hands-on-example Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 88 KB

A simple hash lock example to showcase the development of CKB with scripts and the frontend.

Makefile 17.83% Rust 23.36% CSS 1.29% TypeScript 55.12% JavaScript 1.03% Shell 1.37%

ckb-hands-on-example's Introduction

CKB-Hands-On

A simple hash lock example to showcase the development of CKB with scripts and the frontend.

Given a hash, you need to provide its preimage in order to unlock the Lock Script in the transaction

Example: Hash = blake2b_256(preimage)

offckb-template

This is a Minimal Template for CKB Full-Stack Dapps generated by offckb.

Offckb does not do the magic. It just wraps the new CKB smart contract template and the CKB javascript Dapp framework into one mono-repo. Under the hook, it uses:

Smart contract development

By default, this template comes with a single simple smart contract hello-world: contracts/hello-world/src/main.rs.

The smart contract is written in Rust lang. In order to develop, the following dependencies are required:

  • git, make, sed, bash, sha256sum and others Unix utilities.
  • Rust with riscv64 target: rustup target add riscv64imac-unknown-none-elf
  • Clang 16+
  • cargo-generate

Check out the ckb-script-templates for more detail

Usage

add a new smart-contract:

make generate

build smart-contract:

make build

run test:

make test

For more detail, check out ckb-script-templates

dApp frontend development

first, enter the frontend workspace:

cd frontend

start the app:

npm i && npm run dev

change the CKB blockchain network:

edit .env file:

NEXT_PUBLIC_NETWORK=devnet # devnet, testnet or mainnet

Deploy to devnet/testnet with offckb

Once you build your smart contracts, you can deploy them to CKB blockchain with ckb-cli or any other tools.

If you want to test them in devnet/testnet blockchain, then offckb might be the ideal selection.

offckb will look for the offckb.config.ts file to read config information. so you will need to enter the frontend workspace to do the instruction:

cd frontend
offckb deploy --network devnet

If successfully deployed, you will see the deploy script info for your smart contract recorded in the offckb.config.ts file. You can then directly import and use your smart contract in your frontend Dapp like this:

import offckb from 'offckb.config';
import { CellDep } from '@ckb-lumos/lumos';

const lumosConfig = offckb.lumosConfig;
const myContractDep: CellDep = {
  outPoint: {
    txHash: lumosConfig.SCRIPTS.YOUR_SCRIPT_NAME!.TX_HASH,
    index: lumosConfig.SCRIPTS.YOUR_SCRIPT_NAME!.INDEX,
  },
  depType: lumosConfig.SCRIPTS.YOUR_SCRIPT_NAME!.DEP_TYPE,
};

Every time you deploy a new version of your smart contracts, those script infos will be updated by offckb in the offckb.config.ts and work out-of-box in your frontend.

You can also deploy smart contracts to the CKB Testnet like this:

cd frontend
offckb deploy --network testnet

and start your frontend Dapp targeting Testnet:

edit .env file:

NEXT_PUBLIC_NETWORK=testnet # devnet, testnet or mainnet
cd frontend
npm run dev

Note that the mainnet network is not supported in offckb since offckb is focusing on building a friendly development environment for CKB. To gain better security, it is recommended to use production tools like ckb-cli to deploy smart contracts and do transactions for the CKB mainnet.

ckb-hands-on-example's People

Contributors

retricsu avatar

Stargazers

BlackPhoenix.eth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.