Giter Club home page Giter Club logo

Comments (2)

Keith-CY avatar Keith-CY commented on August 29, 2024

Any update on the investigation of contract development workflow, tools, paradigms, and tricks?

from kuai.

PainterPuppets avatar PainterPuppets commented on August 29, 2024

Integrate development workflow of contract

1 Overview

  • Language

    C, Rust

  • Compile

    You can use any tool you like to compile the contract. However, capsule can make deployment easier.

  • Deploy & Upgrade

    You can use tools such as capsule and ckb-cli to deploy / upgrade the contract. You can also write your own tools using sdk such as lumos, ckb-sdk-rust, etc.

2 Examples

2.1 Compile

Using capsule:

The capsule project has this file structure

  • capsule.toml
  • deployment.toml
  • contracts 
  • tests
  • build 
  • migrations 

Where the contracts folder holds the source code of the contract. When running capsule build, capsule will compile the contract source code into a binary file and place it in the build/dev or build/release directory depending on the dev / release mode

> capsule build

Building contract sample-dapp
   Compiling sample-dapp v0.1.0 (/contracts/sample)
    Finished dev [unoptimized + debuginfo] target(s) in 4.97s
Done

2.2 Deploy & Upgrade

There are several ways to deploy or upgrade contracts.

capsule

deploy

When running capsule deploy, the binary contract code will be put into data of cell and published to the chain

Info: capsule deployment depends on ckb-cli, need to import the private key of the deployment contract account into ckb-cli

Info: There seems to be no way to deploy multiple contracts under the same capsule project

Steps for using the capsule deploy contract:

  1. Edit deployment.toml

    • cells describes which cells to be deployed.
      • name: Define the reference name used in the deployment configuration.
      • enable_type_id : If it is set to true means create a type_id for the cell.
      • location : Define the script binary path.
    • dep_groups describes which dep_groups to be created. Dep Group is a cell which bundles several cells as its members. When a dep group cell is used in cell_deps, it has the same effect as adding all its members into cell_deps.
    • lock describes the lock field of the new deployed cells.It is recommended to set lock to the deployer's address(an address that you can unlock) in the dev chain and in the testnet, which is easier to update the script.
    # [[cells]]  
    # name = "my_cell"  
    # enable_type_id = false  
    # location = { file = "build/release/my_cell" }  
      
    # # Dep group cells  
    # [[dep_groups]]  
    # name = "my_dep_group"  
    # cells = [  
    # "my_cell",  
    # "secp256k1_data"  
    # ]  
      
    # # Replace with your own lock if you want to unlock deployed cells.  
    # # The deployment code_hash is secp256k1 lock  
    # [lock]  
    # code_hash = "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"  
    # args = "0x0000000000000000000000000000000000000000"  
    # hash_type = "type"
  2. Run capsule deploy

> capsule deploy --address ckt1qyqv7p27n5k4plv5zg86dkvpw29fhe2mluasfrkzv2 --fee 26624

Deployment plan:
---
migrated_capacity: 0.0 (CKB)
new_occupied_capacity: 26221.0 (CKB)
txs_fee_capacity: 26624.0 (CKB)
total_occupied_capacity: 26221.0 (CKB)
recipe:
  cells:
    - name: sample-dapp-bob
      index: 0
      tx_hash: "0xaa674bdf14f126a1643a564b75106234316dd12746a8dbb8636a9e449efb48f0"
      occupied_capacity: 26221.0 (CKB)
      data_hash: "0x1e4e4744bc87c5164990d534d994caaace908ff1511c7fa84bb67081813cca36"
      type_id: ~
  dep_groups: []

Confirm deployment? (Yes/No)
yes
Password: 

(1/1) Sending tx aa674bdf14f126a1643a564b75106234316dd12746a8dbb8636a9e449efb48f0

Deployment complete

After a successful deployment, a json file with a timestamp as the name is added to the migrattions folder, recording the contract's transaction information

upgrade

When upgrading the contract, capsule will read the latest json file in the migrations directory and put the old contract as input and the new contract into the output, thus upgrading the contract

> capsule deploy --address ckt1qyqv7p27n5k4plv5zg86dkvpw29fhe2mluasfrkzv2 --fee 26624

ckb-cli

reference: nervosnetwork/ckb-cli#515

deploy

#### Init config
ckb-cli deploy init-config --deployment-config deployment.toml

# >>>> Then edit deployment.toml file

#### Generate intermedium information
ckb-cli deploy gen-txs \
    --deployment-config ./deployment.toml \
    --migration-dir ./migrations \
    --from-address ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37 \
    --sign-now \
    --info-file info.json

#### sign with key
ckb-cli deploy sign-txs \
    --from-account ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37 \
    --add-signatures \
    --info-file info.json 

#### sign with another key (support multisig and offline sign)
ckb-cli deploy sign-txs \
    --from-account ckt1qyqyqnuss20vpevzrt46n0886h5yrn5l07jsgz7kfp \
    --add-signatures \
    --info-file info.json 

#### Explain the deploy action
ckb-cli deploy explain-txs --info-file info.json
# ==== Cell transaction ====
# [cell] Changed   name:      my_cell, old-capacity: 43026.0, new-capacity: 43006.0
# [cell] Unchanged name: genesis_cell, old-capacity:   202.0, new-capacity:   202.0
# > old total capacity: 43228.0 (CKB) (removed items not included)
# > new total capacity: 43208.0 (CKB)
# ==== DepGroup transaction ====
# [dep_group] Changed   name: my_dep_group, old-capacity:   137.0, new-capacity:   137.0
# > old total capacity: 137.0 (CKB) (removed items not included)
# > new total capacity: 137.0 (CKB)

#### Set signatures to witnesses and send to CKB node
ckb-cli deploy apply-txs \
    --migration-dir ./migrations \
    --info-file info.json

upgrade

write your own tools using sdk

deploy & upgrade contract with lumos example:

import { readFileSync } from "fs";
import {
	generateDeployWithTypeIdTx,
	generateUpgradeTypeIdDataTx,
} from "@ckb-lumos/common-scripts/lib/deploy";
import { Script, Indexer, RPC, hd, Address, commons } from "@ckb-lumos/lumos";
import { sealTransaction } from "@ckb-lumos/helpers";

const CKB_RPC_URL = "https://testnet.ckb.dev/rpc";
const CKB_INDEXER_URL = "https://testnet.ckb.dev/indexer";

const rpc = new RPC(CKB_RPC_URL);
const indexer = new Indexer(CKB_INDEXER_URL, CKB_RPC_URL);

// for example
const DEPLOYER_PK = '0x.....'

async function deploy(
	deployer: Address,
	contractBinPath: string,
) {
	const contractBin = readFileSync(contractBinPath);
	
	let { txSkeleton } = await generateDeployWithTypeIdTx({
		cellProvider: indexer,
		fromInfo: deployer,
		scriptBinary: contractBin,
	});
	txSkeleton = await commons.common.payFee(txSkeleton, [deployer], 1000);
	
	// sign transaction
	txSkeleton = commons.common.prepareSigningEntries(txSkeleton);
	const message = txSkeleton.get("signingEntries").get(0)?.message;
	const Sig = hd.key.signRecoverable(message!, DEPLOYER_PK);
	const tx = sealTransaction(txSkeleton, [Sig]);
	
	// send transaction
	const txHash = await rpc.sendTransaction(tx, "passthrough");
}

// upgrade
async function upgrade(
	deployer: Address,
	typeId: Script,
	contractBinPath: string,
) {
	const contractBin = readFileSync(contractBinPath);
	let { txSkeleton } = await generateUpgradeTypeIdDataTx({
		typeId: typeId,
		cellProvider: indexer,
		fromInfo: deployer,
		scriptBinary: contractBin,
	});
	txSkeleton = await commons.common.payFee(txSkeleton, [deployer], 1000);
	
	// sign transaction
	txSkeleton = commons.common.prepareSigningEntries(txSkeleton);
	const message = txSkeleton.get("signingEntries").get(0)?.message;
	const Sig = hd.key.signRecoverable(message!, DEPLOYER_PK);
	const tx = sealTransaction(txSkeleton, [Sig]);
	
	// send transaction
	const txHash = await rpc.sendTransaction(tx, "passthrough");
}

3 How to Integrate Contract to Kuai

Add kuai contract command

Subcommand

kuai contract new
 --name 				* name of contract
 --template 				choices: c, rust
kuai contract compile
 --name 				contract name
 --template 				different compilation ways. default: capsule
kuai contract deploy
 --network				network name
 --name 				the name of the deployment contract
 --address				the address of deployer
 --template 				different signer ways. default: capsule

from kuai.

Related Issues (20)

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.