这篇文章上次修改于 501 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

本文讲述如何将合约部署到测试网。
如果想部署到 dev net,可以参考 Run a CKB Dev Blockchain 先启动一条 dev chain,合约部署过程是一样的。

1 生成账户

根据密钥生成地址:https://ckb.tools/generator

或者使用 ckb-cli 生成账户:

ckb-cli account new

测试网充值:https://faucet.nervos.org/

2 将账户导入 ckb-cli

# 创建账户
echo 111111b054d5dd04013156dced8ba2ce4d8cc5973e10d905a228ea1abc267e60 >> privkey

# 将账户导入 ckb-cli
$ ckb-cli account import --privkey-path privkey
Password: 
address:
  mainnet: ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqw0q40f6t2slk2pyraxmxqh9z5mu4dl7wcykl27s
  testnet: ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqw0q40f6t2slk2pyraxmxqh9z5mu4dl7wc2y595g
address(deprecated):
  mainnet: ckb1qyqv7p27n5k4plv5zg86dkvpw29fhe2mluas5xgaqk
  testnet: ckt1qyqv7p27n5k4plv5zg86dkvpw29fhe2mluasfrkzv2
lock_arg: 0xcf055e9d2d50fd94120fa6d981728a9be55bff3b

# 查看余额
$ ckb-cli wallet get-capacity --address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqw0q40f6t2slk2pyraxmxqh9z5mu4dl7wc2y595g
total: 20000000000.0 (CKB)

3 切换 ckb-cli 的 url

将 ckb-cli 的 url 切换为测试网:

ckb-cli
config --url https://testnet.ckb.dev

4 编译合约

参考:https://github.com/axonweb3/axon-contract/tree/main

capsule build -n stake --release

其中,stake 是合约项目名。

5 部署合约

参考:https://github.com/wenyuanhust/axon-contract/blob/d1/deployment-stake.toml

在 capsule 根目录下添加文件 deploy.toml,添加如下内容

[[cells]]
name = "stake"
enable_type_id = true
location = { file = "build/release/stake" }

# Replace with your own lock if you want to unlock deployed cells.
# For example the secp256k1 lock
[lock]
code_hash = "0x1111116f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"
args = "0x111111aa92da8821d432700cb6be72d7a8971917"
hash_type = "type"

创建空目录 migrations-stake,

执行如下命令:

ckb-cli deploy gen-txs --deployment-config ./deployment-stake.toml --migration-dir ./migrations-stake --from-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdwcq424yk63qsagvnspjmtuukh4zt3j9cdgn4kv --info-file ./info-stake.json --sign-now

ckb-cli deploy sign-txs --from-account ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdwcq424yk63qsagvnspjmtuukh4zt3j9cdgn4kv --info-file ./info-stake.json --add-signatures

ckb-cli deploy apply-txs --info-file ./info-stake.json --migration-dir ./migrations-stake

6 升级合约

ckb-cli deploy gen-txs --deployment-config ./deployment-stake.toml --migration-dir ./migrations-stake --from-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdwcq424yk63qsagvnspjmtuukh4zt3j9cdgn4kv --info-file ./info-stake0.json --sign-now

ckb-cli deploy sign-txs --from-account ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdwcq424yk63qsagvnspjmtuukh4zt3j9cdgn4kv --info-file ./info-stake0.json --add-signatures

ckb-cli deploy apply-txs --info-file ./info-stake0.json --migration-dir ./migrations-stake

参考

How to use ckb-cli to deploy a contract
dapps-on-ckb-workshop-code