Skip to content

Instantly share code, notes, and snippets.

@Akagi201
Created October 11, 2022 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akagi201/f626bcc7dcaf570d1d4348ef6cd73abf to your computer and use it in GitHub Desktop.
Save Akagi201/f626bcc7dcaf570d1d4348ef6cd73abf to your computer and use it in GitHub Desktop.

DFINITY 课程作业讲解 - Mulander

第1课 - 使⽤ SDK 搭建简易⽹站

课程回顾

1.安装SDK

安装指引

# 如果下载中途提示是否继续下载,请输入y即可
sh -ci "$(curl -fsSL https://smartcontracts.org/install.sh)"

# 将 dfx 添加至bin目录,方便快速引用
export PATH=/home/$USER$/bin:$PATH

2.第一个实例:部署静态网站

领取 Cycle | 官方水龙头 :Cycle Faucet | currently getting reworked

#1.确保已成功安装dfx
dfx --version
#2.查看自己的 principal
dfx identity get-principal
#2.P.S 如果已拥有账号,欲导入已有的principal
# <name> 账号昵名, <pem path> 私钥文件地址
dfx identiy imoprt <name> <pem path>
#3.通过dfx以当前身份调用主网的水龙头服务
# 格式:dfx cansiter --network=ic call 服务容器ID 函数名 传入参数
dfx canister --network=ic \ call fg7gi-vyaaa-aaaal-qadca-cai redeem '("xxxxx-yyyyy-zzzzz")'
#3. Return 钱包容器ID WALLET_CANISTER_ID
# !!!此钱包容器ID需自己备份保存!!!
(principal "*****-*****-*****-*****-cai")
#4. 绑定钱包
dfx identity --network=ic set-wallet WALLET_CANISTER_ID
#5. 查看余额
dfx wallet --network=ic balance

部署应用

# 1.使用脚手架快速生成一个简单的项目模板
dfx new project_name
# 2.确认dfx.json文件
cat ./dfx.json
# 2.部署主网
dfx deploy --network=ic --with-cycles=400000000000
# 3.查看部署成功的canisterId
# 打包文件在./dfx/
cat ./canister_ids.json
# 4.访问部署在 IC ⽹络的项⽬
# 将以下命令中<canister_id>替换成⾃⼰应⽤的前端项目canister_id 
https://<canister_id>.ic0.app

3.ICP技术优势

  • 48字节 ChainKey 公钥验证,
  • 不让⽤户安装任何插件 毫秒级查询响应,2秒交易最终确认;
  • 简化了区块链 DAPP 架构;
  • Canister,轻量级容器封装代码和数据;
  • JS Agent 库提供底层⽀持;
  • 对安全性的需求:数据安全,计算可信 ------> 去中⼼化

作业点评

第1课共有 4 道题⽬

(1 组 17 位学员,有 12 位提交了作业,满分的有 6 位。)

  1. 安装并使用 SDK 在本机搭建一个简易网站 。(2 分,请提交源代码链接)
  2. 使用优惠码领取 cycles 钱包(2 分,请提交领取成功的 wallet canister ID)
  3. 将网站部署到 ic0.app 主网。 (3 分,请提交部署的网址链接)
  4. 思考题:假如开发团队不再维护代码了,用户该怎么办?(3 分,请以文字描述,本题为开放题)

思考题回答:

  1. 根源/诱因:团队跑路、容器Cycle耗尽。(1分)
  2. 解决方案(2-3分)
  • 问题导向 | 问题已发生如何解决
    • 如果未冻结,可由用户或第三方对其容器捐赠cycle解冻
    • 开源代码重新部署|存在信用背书和数据恢复问题
  • 目标导向 | 从将来出发如何规划
    • 代码中预留cycle接收和查询的接口,定期维护与告警
    • 集成合理的权限管理机制或DAO,使得canister的所有权可流转
  • 社区提议(bonus) 翻译原文
    • 系统事件将为canister提供一个通用机制,以对生命周期事件做出反应,如耗尽cycle
    • 备份/恢复将有助于将数据保存在canister之外,以便在数据因任何原因丢失的情况下能够恢复。
    • Canister WHOIS和dfx top将为社区提供基础设施,以建立更好的canister管理仪表盘。
    • 周期预测模型将使我们能够更好地预测周期使用情况和计费成本。

常见问题分析

  • dfx 下载安装失败: 去官方发行版本列表下载手动安装; 使用梯子代理; 修改host
  • github 源码文件不全或提交了多余的文件:请确认.gitignore合理配置
  • 提交了错误的主网应用地址:请区分本地地址与主网地址
  • 多个钱包时应用部署或钱包设置异常:请确认使用了正确的钱包地址
  • 本地运行失败:使用dfx start --clean清除缓存或者已存在重复的进程需要kill

补充资料

dfx常用命令

# get principal
dfx identity get-principal
# set wallet
dfx identity --network=ic set-wallet CANISTER_ID
# get wallet
dfx identity --network=ic get-wallet
# check balance
dfx wallet --network=ic balance

# start node
dfx start
# in backgaround
dfx start --background
# clean cahce
dfx start --clean

#build with check
dfx build --check

#local
dfx deploy
#mainnet
dfx deploy --network=ic --with-cycles=400000000000
#ReInstall
dfx deploy --network=ic -m reinstall <canister>

#Stop one
dfx canister --network=ic stop <canister>
#Stop all
dfx canister --network=ic stop --all

#Delete one
dfx canister --network=ic delete <canister>
#Delete all
dfx canister --network=ic delete --all

#Call
dfx canister call mysite fibonaci '(10)'
dfx canister --network=ic  call 4rl4k-uiaaa-aaaal-qaknq-cai get

# Deposit cycles 
# *Only the controllers of the canister
# Deposit cycles one
dfx canister deposit-cycles <amount> <canister>
# Deposit cycles for all by dfx.json
dfx canister deposit-cycles <amount> --all

#Cache
dfx cache show
#Path
export PATH=$(dfx cache show):$PATH

#Compiler Runtime
moc
#Compiler file
moc --package base $(dfx cache show)/base -r src/mysite/main.mo

i.e. Check Cycles Balance

import ExperimentalCycles "mo:base/ExperimentalCycles";

actor {
    public query func getCycles() : async Nat {

      Cycles.balance();
    };
};

引用链接:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment