DFINITY
DFINITY AKA ICP 是世界上第一个以网络速度运行并且可以无限扩容的区块链,其目标是为企业级应用提供去中心化的网络服务。2021年5月7日,ICP 主网正式上线,于此同时官方也为开发者提供了详细的技术文档,方便大家学习和在ICP网络上进行应用开发,下面让我们来一步一步的搭建我们自己的应用吧~!
Step 1
首先搭建ICP 开发环境,首先需要一台Linux 或者MacOs 系统(本人使用MacOs 系统,下面步骤为MacOs上开发步骤)的电脑,然后确认你的电脑网络连接正常并且已安装 Node.js.
Step2
打开Terminal,运行以下代码安装DFINITY Canister SDK 包:
DFX_VERSION=0.7.0 sh -ci "$(curl -sSL https://sdk.dfinity.org/install.sh)"
注:此命令可指定下载最新0.7.0 SDK,否则采用默认下载路径会安装0.6版本SDK,导致项目部署失败。安装完成后,为确认SDK 已安装好,可在命令行运行:
dfx --version
如安装成功,则命令行提示dfx 安装版本:
dfx 0.7.0
Step3
创建工作目录,可在主目录下创建名为 ic-projects 的工作目录。
Step4
好啦,开发环境已经部署好,下面开始我们的第一个ICP项目吧~!
在命令行中进入创建的工作目录,然后新建一个名叫 hello_world的项目
dfx new hello_world
等待项目新建完成,将看到系统提示:
Step5
打开电脑文件夹工作目录,可以看到hello_world项目文件夹已经生成,打开文件夹可以看到项目文件夹中有以下文件:
hello_world
├── README.md #默认项目文档
├── dfx.json #项目配置文件
├── node_modules #node.js 依赖包
├── package-lock.json
├── package.json
├── src # 项目源文件目录
│ ├── hello_world
│ │ └── main.mo #motoko 代码
│ └── hello_worold_assets #前端文件
│ ├── assets #前端静态资源
│ │ ├── logo.png
│ │ ├── main.css
│ │ └── sample-asset.txt
│ └── src #前端代码文件
│ ├── index.html
│ └── index.js
└── webpack.config.js
Step6
打开另一个Terminal, 进入项目目录,在后台运行一个ICP网络副本,并保持后台运行。
dfx start
这时本地ICP网络已经运行,可打开终端提示网址查看ICP网络副本运行状态:
Step7
在ICP网络副本上创建项目容器,输入命令:
dfx canister create hello_world --all
系统会创建一个Wallet 容器和一个名叫hello_world 的 Actor 容器:
Creating a wallet canister on the local network.
The wallet canister on the "local" network for user "default" is "rwlgt-iiaaa-aaaaa-aaaaa-cai"
Creating canister "hello_world"...
"hello_world" canister created with canister id: "rrkah-fqaaa-aaaaa-aaaaq-cai"
Step8
在ICP网络副本上部署项目
dfx deploy
系统会向ICP网络副本部署创建的容器,同时生成并在ICP网络副本上传前端资源到容器。
Deploying all canisters.
Creating canisters...
Creating canister "hello_world_assets"...
"hello_world_assets" canister created with canister id: "ryjl3-tyaaa-aaaaa-aaaba-cai"
Building canisters...
Building frontend...
Installing canisters...
Creating UI canister on the local network.
The UI canister on the "local" network is "r7inp-6aaaa-aaaaa-aaabq-cai"
Installing code for canister hello_world, with canister_id rrkah-fqaaa-aaaaa-aaaaq-cai
Installing code for canister hello_world_assets, with canister_id ryjl3-tyaaa-aaaaa-aaaba-cai
Authorizing our identity (default) to the asset canister...
Uploading assets to asset canister...
/index.html 1/1 (478 bytes)
/index.html (gzip) 1/1 (319 bytes)
/index.js 1/1 (260229 bytes)
/index.js (gzip) 1/1 (87771 bytes)
/main.css 1/1 (484 bytes)
/main.css (gzip) 1/1 (263 bytes)
/sample-asset.txt 1/1 (24 bytes)
/logo.png 1/1 (25397 bytes)
/index.js.map 1/1 (843004 bytes)
/index.js.map (gzip) 1/1 (228442 bytes)
/index.js.LICENSE.txt 1/1 (499 bytes)
/index.js.LICENSE.txt (gzip) 1/1 (285 bytes)
Deployed canisters.
Step9
此时项目已经就已经创建并部署完成。让我们一起来看看效果吧~~!
在网页中打开默认地址和端口号,127.0.0.1:8000,追加/?canisterId=和hello_world_assets标识的URL。如果没有记下容器标识符,可以运行以下命令:
dfx canister id hello_world_assets
输入你的名字,LiLei
系统会响应弹出欢迎文字。
虽然只是hello world的功能,但是我们看到整个项目没有后台服务器,不需要数据库,所有资源全部部署在ICP 本地网络副本上。
更多关于ICP 上应用开发相关,让我们来一起探索吧~!
发表评论 取消回复