-
Notifications
You must be signed in to change notification settings - Fork 11
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
Tutorial to build a profile DApp with Kuai #306
Comments
Here's the tutorial of samples/mvp-dapp with contract. Please have a review @zhengjianhui @yanguoyu @Daryl-L @PainterPuppets Ref: |
to perform a dependency installation first |
➜ yarn bootstrap
yarn run v1.22.10
error Command "bootstrap" not found. |
After |
ok |
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 So |
$ npm install @ckb-lumos/lumos --spoce @ckb-js/kuai-common |
It is recommended to use a tag to fix the version |
It's fixed by #313 |
The shadow dependencies are not installed because |
https://github.com/ckb-js/kuai/tree/develop/packages/samples/mvp-dapp#getting-started |
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 #326 to improve it? |
It would be great |
Will be updated by #331 |
Build a profile DApp with Kuai
This tutorial is specific to commitsA 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 steps1 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
The complete source code of demo can be found at https://github.com/yanguoyu/kuai/tree/b988eedbb224e15f5bb1d374e8d0345b8a558dc7/packages/samples/mvp-dapp/contract
Add contract module
The contract module will be delivered in project template in the future once the best practice of contract module structure is confirmed.(nervosnetwork/capsule#124)
capsule2 is required for contract development. It will be installed on-demand automatically in the future.
Now it should be installed manually by
cargo install ckb-capsule
if rust is ready on your machine.After then
is ready for contract development.
Add contract source code
This step requires docker
# kuai contract new --name <contract name> $ kuai contract new --name kuai-mvp-contract
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(typesin our case), which relates to the skills of contract development and will not be elaborated here.
Build and deploy contract
# cd to /profile $ kuai contract build --release
Contract artifacts will be generated in profile/contract/build/release/ for deployment.
The contract will be deployed by the default signer, ckb-cli3, in this case, so we need to create an account for signing transactions.
Creating an account in ckb-cli will be supported by kuai-cli in the futureGo to CKB Testnet Faucet4 and claim 300,000 CKB for contract deployment.
Deploy contracts with kuai-cli5
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
.The deployment information of the online demo can be found at https://github.com/ckb-js/kuai/tree/ebffba1bc73255fa3ccf6d85300129c70bf88f47/packages/samples/mvp-dapp/contract/deployed_demo
Backend
The backend source code of demo can be found at samples/mvp-dapp
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
The source code of omnilock model can be found at samples/mvp-dapp/src/actors/omnilock.model.ts
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
The source code of record model can be found at samples/mvp-dapp/src/actors/record.model.ts
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
The Chain Source module synchronizes data from CKB Node to Actor Models. It will be supported internally in the future and can be skipped in the code tour now.
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:
Footnotes
Kuai Installation: https://github.com/ckb-js/kuai/blob/c2845169de81817fd2fd397032672f79bf73aebd/README.md#kuai-installation ↩
Capsule and its prerequisites: https://github.com/nervosnetwork/capsule#prerequisites ↩
CKB CLI: https://github.com/nervosnetwork/ckb-cli ↩
CKB Testnet Faucet: https://faucet.nervos.org/ ↩
Deploy contracts by kuai-cli: https://github.com/ckb-js/kuai/pull/242 ↩
The text was updated successfully, but these errors were encountered: