Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate development workflow of contract #52

Closed
Keith-CY opened this issue Nov 30, 2022 · 2 comments
Closed

Integrate development workflow of contract #52

Keith-CY opened this issue Nov 30, 2022 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@Keith-CY
Copy link
Member

Keith-CY commented Nov 30, 2022

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:

capsule build

2.2 Deploy & Upgrade

There are several ways to deploy or upgrade contracts.

@Keith-CY Keith-CY added the documentation Improvements or additions to documentation label Nov 30, 2022
@Keith-CY Keith-CY moved this to 📋 Backlog in CKB JS Backlog Nov 30, 2022
@Keith-CY Keith-CY added this to the 2022/12/01 - 2022/12/08 milestone Dec 1, 2022
@Keith-CY Keith-CY moved this from 📋 Backlog to 🔖 Ready in CKB JS Backlog Dec 1, 2022
@Keith-CY Keith-CY assigned PainterPuppets and unassigned felicityin Dec 8, 2022
@Keith-CY
Copy link
Member Author

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

@Keith-CY Keith-CY moved this from 🔖 Ready to 🏗 In progress in CKB JS Backlog Dec 14, 2022
@PainterPuppets
Copy link
Contributor

PainterPuppets commented Dec 15, 2022

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

@Keith-CY Keith-CY moved this from 🏗 In progress to ✅ Done in CKB JS Backlog Jan 4, 2023
@Keith-CY Keith-CY moved this from ✅ Done to Hold on in CKB JS Backlog Jan 4, 2023
@Keith-CY Keith-CY moved this from Hold on to ✅ Done in CKB JS Backlog Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Archived in project
Development

No branches or pull requests

3 participants