Closed Keith-CY closed 1 year ago
Here's the tutorial of samples/mvp-dapp with contract.
Please have a review @zhengjianhui @yanguoyu @Daryl-L @PainterPuppets
Ref: Add demonstration of how to integrate contract development within the mvp
$ yarn bootstrap
to perform a dependency installation first
yarn bootstrap
➜ yarn bootstrap
yarn run v1.22.10
error Command "bootstrap" not found.
After lerna
update to v7, only need to run yarn
or npm i
to replace yarn bootstrap
.
https://github.com/ckb-js/kuai/pull/310
更新到 v7后
lerna
,只需运行yarn
或npm i
替换yarn bootstrap
. #310
ok
cd packages/cli
lerna notice cli v7.0.0
✔ @ckb-js/kuai-typeorm:build (3s)
✖ @ckb-js/kuai-common:build
$ tsc src/util.ts(2,26): error TS2307: Cannot find module '@ckb-lumos/lumos' or its corresponding type declarations. error Command failed with exit code 2.
cd packages/cli
lerna notice cli v7.0.0 ✔ @ckb-js/kuai-typeorm:build (3s) ✖ @ckb-js/kuai-common:build $ tsc src/util.ts(2,26): error TS2307: Cannot find module '@ckb-lumos/lumos' or its corresponding type declarations. error Command failed with exit code 2.
The dependency @ckb-lumos/lumos
was a shadow dependency missing in kuai/packages/common/package.json
, it's installed by lerna
but ignored by npm/yarn
So @ckb-lumos/lumos
should be added in kuai/packages/common/package.json
@ckb-lumos/lumos
$ npm install @ckb-lumos/lumos --spoce @ckb-js/kuai-common
It is recommended to use a tag to fix the version
@ckb-lumos/lumos
$ npm install @ckb-lumos/lumos --spoce @ckb-js/kuai-common
It's fixed by https://github.com/ckb-js/kuai/pull/313
It is recommended to use a tag to fix the version
Lerna
is version locked at https://github.com/ckb-js/kuai/blob/develop/package.json#L30
The shadow dependencies are not installed because lerna
is opted out during the installation.
https://github.com/ckb-js/kuai/tree/develop/packages/samples/mvp-dapp#getting-started This document doesn't seem complete enough. We can add how to start dev and add docs on how to build dependence(kuai).
develop
/packages/samples/mvp-dapp#getting-started This document doesn't seem complete enough. We can add how to start dev and add docs on how to build dependence(kuai).
What if merging this document(https://github.com/ckb-js/kuai/issues/306) into https://github.com/ckb-js/kuai/tree/develop/docs and add a link in https://github.com/ckb-js/kuai/blob/develop/packages/samples/mvp-dapp/README.md with a concise description
develop
/packages/samples/mvp-dapp#getting-started This document doesn't seem complete enough. We can add how to start dev and add docs on how to build dependence(kuai).What if merging this document(#306) into https://github.com/ckb-js/kuai/tree/develop/docs and add a link in https://github.com/ckb-js/kuai/blob/develop/packages/samples/mvp-dapp/README.md with a concise description
How about using https://github.com/ckb-js/kuai/pull/326 to improve it?
develop
/packages/samples/mvp-dapp#getting-started This document doesn't seem complete enough. We can add how to start dev and add docs on how to build dependence(kuai).What if merging this document(#306) into
develop
/docs and add a link indevelop
/packages/samples/mvp-dapp/README.md with a concise descriptionHow about using #326 to improve it?
It would be great
Will be updated by https://github.com/ckb-js/kuai/pull/331
Build a profile DApp with Kuai
A user-oriented DApp typically requires the development of frontend, backend, and smart contracts. As Kuai is a contract and server development framework specific for Nervos CKB, this article will not cover tutorials on front-end development.
This article describes how to develop a Profile Application using Kuai. The application will collect users' blank Omnilock cells as storage space, write their profile information, and provide basic read-write APIs. Simple validation will be performed on the profile to demonstrate contract verification.
Before we begin
A node.js CLI tool typically needs to be installed via npm before it can be used. Since Kuai is still in the development phase and has not been published to the npm registry, we need to clone Kuai Repository to our local machine and use npm link to install kuai-cli.
Follow the steps[^1] below to install kuai-cli locally
Initialize the project
The project template will be available as follows:
src/main.ts is the entry point of profile application. It registers services before the application starts; src/app.controller.ts is the router, which defines APIs exposed to users; src/actors/ is the directory to define actor models based on specific patterns. Learn more about actor models in Kuai;
Contract
Add contract module
After then
is ready for contract development.
Add contract source code
Contract template named kuai-mvp-contract will be generated in profile/contract/contracts
Fill the kuai-mvp-contract we've implemented into profile/contract/contracts/kuai-mvp-contract/src.
Contract Libs
One unwritten norm is to abstract business logic into a lib([types](https://github.com/yanguoyu/kuai/tree/c1a25782234c95ea2ddd86449f07ca38e15559dc/packages/samples/mvp-dapp/contract/types)in our case), which relates to the skills of contract development and will not be elaborated here.Build and deploy contract
Contract artifacts will be generated in profile/contract/build/release/ for deployment.
The contract will be deployed by the default signer, ckb-cli[^3], in this case, so we need to create an account for signing transactions.
Go to CKB Testnet Faucet[^4] and claim 300,000 CKB for contract deployment.
Deploy contracts with kuai-cli[^5]
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
.Backend
Actor Models
An actor model is an abstract of a bundle of cells matched by specific patterns. By defining an actor model in
Kuai
, cells will be collected and decoded automatically to read and write.There're two actor models mapped from the cells:
These two models are the core of the entire backend application, and they are defined in profile/src/actors/
Omnilock Model
At first, the built-in model named JSONStore should be imported as the basic model, and decorated by patterns as follows
Pay attention to the decorators above OmnilockModel
By prepending all these decorators, an OmnilockModel instance represents cells owned by
OmniLockAddress(args)
as an entire object during runtime.After then, we can add custom methods according to the business logic. Here we add
meta
to get capacity of the model andclaim
to transform plain omnilock cells into a profile-hold cell.Record Model
Similarly, the RecordModel can be decorated to limit its cells by
omnilock
andtype script of profile contract
Define
update
to change profile, andclear
to wipe profile outView
Notice that the responses of OmnilockModel and RecordModel consist of inputs, outputs, and cellDeps. A wrapper is required to transform them into a valid transaction. This step can be done anywhere. In this case, a view module is introduced to wrap them into a transaction.
Controller
Finally, requests from a client should be routed to the correct models; routes are defined in the generated app.controller.ts file. For instance,
Chain Source
In conclusion, the modules introduced above are the critical components of the entire backend application, as they constitute the overall logic of the entire application. Please visit samples/mvp-dapp to learn all the details.
Ref:
[^1]: Kuai Installation: https://github.com/ckb-js/kuai/blob/c2845169de81817fd2fd397032672f79bf73aebd/README.md#kuai-installation [^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