Skip to content

Commit

Permalink
chore: update contract doc (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets authored Sep 15, 2023
1 parent 70d2b1a commit ea7aedd
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 4 deletions.
157 changes: 157 additions & 0 deletions docs/tutorials/kuai-cli-deploy-contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# How to deploy contracts with kuai-cli

## deploy contract workflow by builtin signer (directly deploy):

1. Create a new conrtact, skip this step if the contract already exists ([How to import existent contract](#how-to-import-contract-to-kuai-project))

- `kuai contract new --name [contract-name]`

```bash
$ kuai contract new --name always-success

# New contract "always-success"
# Created binary (application) `always-success` package
# Rewrite Cargo.toml
# Rewrite capsule.toml
# Done
```

2. Build contract

- `kuai contract build --name [contract-name] --release`

```bash
$ kuai contract build --name always-success --release
# Building contract always-success
# Compiling always-success v0.1.0 (/code/contracts/always-success)
# Done
```

3. Deploy contract

- `kuai contract deploy --name [contract-name] --from [0x.....] --signer ckb-cli`

```bash
$ kuai contract deploy --name always-success --from ckt1qyqw8yx5hx6vwcm7eqren0d0v39wvfwdhy3q2807pp --signer ckb-cli
# [warn] ANYONE_CAN_PAY script info not found in config!
# [warn] OMNILOCK script info not found in config!
# [warn] `config` changed, regenerate lockScriptInfos!
# The address format generated by generateAddress or scriptToAddress will be deprecated, please migrate to encodeToAddress to generate the new ckb2021 full format address as soon as possible
# Input ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0rjr2tndx8vdlvspuehkhkgjhxyhxmjgsqw8hsk's password for sign messge by ckb-cli:
# deploy success, txHash: 0x7507b59b5f715c0fcd83d55df5569d72ff1cb05cd399afe1f366e03918827a58
```

## deploy contract workflow by export tx:

1. `kuai contract deploy --name [contract-name] --from [0x.....] --export [transaction json file]`

```bash
$ kuai contract deploy --name always-success --from ckt1qyqw8yx5hx6vwcm7eqren0d0v39wvfwdhy3q2807pp --export ./tx.json
# [warn] ANYONE_CAN_PAY script info not found in config!
# [warn] OMNILOCK script info not found in config!
# [warn] `config` changed, regenerate lockScriptInfos!
```

2. `ckb-cli tx sign-inputs --tx-file [transaction json file] --from-account 0xe390d4b9b4c7637ec80799bdaf644ae625cdb922 --add-signatures`

```bash
$ ckb-cli tx sign-inputs --tx-file ./tx.json --from-account 0xe390d4b9b4c7637ec80799bdaf644ae625cdb922 --add-signatures
# Password:
# - lock-arg: 0xe390d4b9b4c7637ec80799bdaf644ae625cdb922
# signature: 0xf3af55aa4c9d6923852465a33d625ff2b5489409908259d79d6aa5bf15a407f10ec32cdd9a9ebff837cdccc2850fc533cab8f08a4c701b57e75530c329f56be201
```

3. `ckb-cli tx send --tx-file [transaction json file]`

```bash
$ ckb-cli tx send --tx-file ./tx.json
# 0xfd79d9c3e562b2c5c51df39e49fbb906c63cf2e33e5dbe2bbff12345fa4e6c72
```

## export tx workflow with multisig

1. `kuai contract deploy --name [contract-name] --from multisig [R] [M] [...args] --export [transaction json file]`

```bash
$ kuai contract deploy --name always-success --from multisig 0 2 0xe390d4b9b4c7637ec80799bdaf644ae625cdb922 0xb6ddba87bb5af5f053b1ae5bcbc7f4de03479f7e --export ./tx.json
# [warn] ANYONE_CAN_PAY script info not found in config!
# [warn] OMNILOCK script info not found in config!
# [warn] `config` changed, regenerate lockScriptInfos!
# The address format generated by generateAddress or scriptToAddress will be deprecated, please migrate to encodeToAddress to generate the new ckb2021 full format address as soon as possible
```

2. Sign transactions according to multi-signature address: `ckb-cli tx sign-inputs --tx-file [transaction json file] --from-account [args] --add-signatures`

```bash
# sign by 0xe390d4b9b4c7637ec80799bdaf644ae625cdb922
$ ckb-cli tx sign-inputs --tx-file ./tx.json --from-account 0xe390d4b9b4c7637ec80799bdaf644ae625cdb922 --add-signatures
# Password:
# - lock-arg: 0x9ab40977fbad98c42cb0688cb10557a76f32c7ff
# signature: 0x2395d9d3fde764a6ba78b26699dab410d3609c0303c436768d24add978a64a8c4c4653051fe88124e9f2acb139179f291a40f79ebad93dac3433ad10b39415dd00
```

```bash
# sign by 0xb6ddba87bb5af5f053b1ae5bcbc7f4de03479f7e
$ ckb-cli tx sign-inputs --tx-file ./tx.json --from-account 0xb6ddba87bb5af5f053b1ae5bcbc7f4de03479f7e --add-signatures
# Password:
# - lock-arg: 0x9ab40977fbad98c42cb0688cb10557a76f32c7ff
# signature: 0x6efc1f6359b34fc6bc7edb32059947b61b9914272bda2ed7a7b1aeb0d7d272db7121fcab8cbe25ef8947a0ab1019dcaae3c5f83a9c0f6f486a81fe9787bc16cd01
```

3. `ckb-cli tx send --tx-file [transaction json file]`

```bash
$ ckb-cli tx send --tx-file ./tx.json
# 0xa0fad2950093488baf41b25f3459482b5c7870b21f8d2bb709a731417cb99e33
```

### How to import contract to kuai project

Typically, the file directory of a ckb contract looks something like this:

```bash
.
├── Cargo.toml
└── src
├── entry.rs
├── error.rs
└── main.rs
```

1. Copy all of this directory directly into the kuai's convention directory where the contracts are placed, which by default should be`/contract/contracts/`
2. Edit the `/contract/cargo.toml` file to add information about the contract to `[workspace]`, e.g.
```toml
[workspace]
members = ["tests", "contracts/kuai-mvp-contract", "types", "contracts/always-success"]
```
### How to deploy contract to other network
- `kuai contract deploy --name [contract-name] --from [0x.....] --network [networkname]`
- or set network to `.env` file
### Tips: add more network config
Kuai currently has three default settings for networks,` testnet` / `mainnet` / `devnet`, and users can also add custom network
- edit `kuai.config.js` of project and add network tor `networks` field
```typescript
networks: {
[networkname: string]: {
rpcUrl: string
prefix: string
scripts?: Record<string, ScriptConfig>
}
}
```
8 changes: 5 additions & 3 deletions docs/tutorials/mvp-dapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@ Deploy contracts with kuai-cli[^5]

```bash
# cd to /profile
# kuai contract deploy --name <contract name> --from <deployer address> --netwrok <chain type> --send --signer <signer>
$ kuai contract deploy --name kuai-mvp-contract --from ckt***************************************slzz --network testnet --send --signer ckb-cli
# kuai contract deploy --name <contract name> --from <deployer address> --netwrok <chain type> --signer <signer>
$ kuai contract deploy --name kuai-mvp-contract --from ckt***************************************slzz --network testnet --signer ckb-cli

# [warn] `config` changed, regenerate lockScriptInfos!
# Input ckt1q*****************************************************************************************gt9's password for sign messge by ckb-cli:
# deploy success, txHash: 0x1ed********************************************************fbd3c
```

Deploy contracts by tx file

So far, we've completed the development&deployment of contracts.

The deployment information should be generated automatically as a facility for the backend. It will be implemented soon, now we have to set it manually at `profile/contract/deployed/contracts.json`.
Expand Down Expand Up @@ -325,4 +327,4 @@ In conclusion, the modules introduced above are the critical components of the e
[^2]: Capsule and its prerequisites: https://github.com/nervosnetwork/capsule#prerequisites
[^3]: CKB CLI: https://github.com/nervosnetwork/ckb-cli
[^4]: CKB Testnet Faucet: https://faucet.nervos.org/
[^5]: Deploy contracts by kuai-cli: https://github.com/ckb-js/kuai/pull/242
[^5]: Deploy contracts by kuai-cli: [kuai-cli-deploy-contract.md](./kuai-cli-deploy-contract.md)
2 changes: 1 addition & 1 deletion packages/samples/mvp-dapp/contract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ $ capsule build --release
## Contract deploy

```shell
$ kuai contract deploy --name kuai-mvp-contract --from ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq25dhcqh3x2zd8008c8re2khlsy9nxdjss6xne37 --network testnet --send --signer ckb-cli
$ kuai contract deploy --name kuai-mvp-contract --from ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq25dhcqh3x2zd8008c8re2khlsy9nxdjss6xne37 --network testnet --signer ckb-cli
```

0 comments on commit ea7aedd

Please sign in to comment.