文章目录
Day1课程
官方课程链接: https://learnnear.club/lessons/day-1-reading-web3-ncdl1c4/
https://hackmd.io/@nearly-learning
学习web 3
学习阅读控制金钱,身份和所有权的合约。
今天的目标是阅读尽可能多的合约。
有时人们不确定如何完成此活动。 这些技巧可能会有所帮助。
Try to figure out how to get to the contract code. Then move to quickly read the code. You want to see as much as you can. Ask yourself about the “purpose” of the contract (why is it?) and “code” (what is it?)
尝试弄清楚如何获得合约代码。 然后移动以快速阅读代码。 您希望看到尽可能多的东西。 问问自己合约的“目的”(why is it?)和“代码”(what is it?)
- AssemblyScript合约 是以.ts结尾的文件,通常是main.ts或index.ts
- Rust合约 始终位于名为lib.rs的文件中。
核心活动:
- 在进行其他操作之前,请先观看此视频
- 在下面的资源部分中,选择AssemblyScript或Rust
查看您的语言标记为“CORE Activity”的所有3份合约
奖励活动:
clone所选语言的 starter 库并按照说明进行操作
- AssemblyScript
- Rust (still under development)
Remember to focus on READING and avoid the temptation to build and run everything along the way because it’s a distraction at this point. You can build and run everything tomorrow.
你阅读得越多越好,如果可以的话,继续读下去。记住要把注意力集中在阅读上,避免一路上构建和运行所有东西的诱惑,因为这会分散你的注意力。你明天就可以建立和运行一切。
Read ALL the contracts for your language of choice, not just 3
阅读你所选择的语言的所有合约,而不仅仅是3种
Read all the contracts from the OTHER language as well.
还要阅读其他语言的所有合约。
Check out these little puzzles, they may be fun for you
查看这些小难题,它们可能对您很有趣
Going Deeper
If you’re feeling fearless, here’s about as far as you might take this road in a day
Review all core-contracts and watch related Live Contract Review videos
Review Illia’s contract repo
Review all Rainbow Hackathon contract submissions
资源
记住:今天的活动
- 您无需构建和测试每个合同
- 您无需了解每一行代码
- 您应该像看艺术或听音乐一样简单地阅读合约,而不必担心每个点或内容
AssemblyScript
如果您打算专注于AssemblyScript,请打开AssemblyScript合约列表
您应该至少阅读3个标记为CORE活动的合约。
Rust
如果您打算专注于Rust,请打开Rust合约列表
您应该至少阅读3个标记为CORE活动的合约。
阅读 web3 任务
Please submit:
- Contract name
- What did you find most excited about it
- What is the most challenging par to understand
根据原文提醒,填写如上信息到原文表单中,提交,任务完成。
相关项目如下:
Name | Notes | Code |
---|---|---|
SputnikDAO | This contract is a simple version of a DAO to give out tips, bounties and grants. Allows anyone to send a proposal to reward other people with funds and get a council to vote for it. The major difference between this and Moloch DAO design is that this contract would receive its function via donation and council has equal rights. | https://github.com/VitalPointAI/SputnikDao-AS-Contract/tree/main/sputnikdao-as |
Multisig | This contract provides a multisig scheme that allows making transfers, calling functions and adding / removing keys | https://github.com/VitalPointAI/Multisig-Contract |
Whitelist | The purpose of this contract is to maintain the whitelist of the staking pool contracts account IDs that are approved by NEAR Foundation. | https://github.com/VitalPointAI/Whitelist-Contract |
Counter | This contract implements simple counter backed by storage on blockchain. | https://github.com/near-examples/counter |
Guest Book | Sign in with NEAR and add a message to the guest book | https://github.com/near-examples/guest-book |
Cross Contract Calls | This repo is organized around a series of lessons about making cross contract calls | https://github.com/near-examples/cross-contract-calls |
NEAR Collection Examples | This repository serves to demonstrate differences in gas consumption when storing or retrieving data from the NEAR blockchain using AssemblyScript collection methods found in near-sdk-as. | https://github.com/near-examples/collection-examples-as |
NEAR AssemblyScript Workshop | This workshop includes several activities that introduce the basic concepts of writing contracts for NEAR Protocol using AssemblyScript | https://github.com/near-examples/workshop–exploring-assemblyscript-contracts |
NEAR Ice Cream | This dApp demonstrates buying ice cream with NEAR tokens | https://github.com/near/near-icecream |
Crypto Corgis | This dApp is an implementation of Crypto Kitties for NEAR | https://github.com/nearprotocol/corgis |
SputnikDAO Factory | This contract is a factory that creates instances of SputnikDAO | https://github.com/VitalPointAI/SputnikDao-AS-Contract/tree/main/sputnikdao-as-factory |
Voting | Port of NEAR core-contract as AssemblyScript | https://github.com/theophoric/near-core-contracts-as/tree/master/contracts/voting |
必看的3个合约:
-
SputnikDAO
这个合约是一个简单的DAO版本,用来分发小费、奖金和奖励。允许任何人发送一个提议,用资金奖励其他人,并得到理事会投票。这个和 Moloch DAO 设计的主要区别是这个合约将通过捐赠得到它的功能,并且理事会有平等的权利。 -
Multisig
This contract provides a multisig scheme that allows making transfers, calling functions and adding / removing keys
这个合约提供了一个multisig方案,允许进行传输、调用函数和添加/删除密钥 -
Whitelist
The purpose of this contract is to maintain the whitelist of the staking pool contracts account IDs that are approved by NEAR Foundation.
该合约的目的是维护由NEAR Foundation批准的权益池合约帐户ID的白名单。
SputnikDAO
https://github.com/VitalPointAI/SputnikDao-AS-Contract/tree/main/sputnikdao-as
github: https://github.com/VitalPointAI/SputnikDao-AS-Contract/tree/main/sputnikdao-as
SputnikDAO 帮助你在 NEAR 创建链上基金,并参与去中心化自治组织。
A smart contract written in AssemblyScript for an app initialized with create-near-app.
用AssemblyScript编写的针对使用create-near-app初始化的应用程序的智能合约。
多签合约(Multisig-Contract)
https://github.com/VitalPointAI/Multisig-Contract
相关参考: https://vitalpoint.ai/course/multisig-contract/
多重签名合约的整个前提是它可以实现要求多个人或实体在执行请求之前确认请求/提议的逻辑。这称为从N个多重签名方案中设置K个,其中N是参与者的总数,K是在执行请求或提议之前必须达成一致的数目。
此多重签名合约允许已添加到其中的密钥的所有者请求签名传输,函数调用或添加/删除现有或其他密钥。
没有时间限制。任何授权的访问密钥都可以进行确认,直至达到所需的确认数量。
让我们看一下整个过程的概述:
步骤1:初始化
- 确定在执行某些请求操作之前我们需要多少确认;和
- 我们要用来初始化的所有帐户的公钥(应该大于我们想要的确认数)。
例如,我要部署一个包含三个参与者(三个访问密钥)的多重签名,并且在执行任何操作之前,我想要两个单独的确认。换句话说,使用合同的三个人中的两个必须同意请求。
有了这些信息,我们可以部署调用它的initContract函数的合约。
当然,一个人可以分配任意数量的密钥,并增加或减少所需的确认数。一个人也可以稍后添加更多密钥或更改所需的确认数。
步骤2:FunctionCall存取键
NEAR具有两种访问键-fullAccess和functionCall访问。初始化时,我们设置了三个functionCall访问键,并赋予它们对methodNames数组标识的方法(“ add_request”,“ delete_request”和“ confirm”)的访问权限。因为它们是functionCall访问键-它们只能调用合约上的这三个更改功能(以及任何视图功能)。在上面的示例中,这三个键代表使用合约的三个人-A,B,C。
步骤3:提交请求
在星号3-用户C向合约提交多重签名请求。我们假设这是将资金转给某人的请求。附近。使用Near-CLI会采用以下形式:
转账要求
near call multisig.vitalpoint.ai add_request '{"request": {"receiver_id": "somebody.near", "actions": [{"type": "Transfer"}], "amount": "1000000000000000000000" }}' --accountId multisig.vitalpointai.testnet
向Multisig添加另一个密钥
near call multisig.vitalpoint.ai add_request '{"request": {"receiver_id": "multisig.vitalpointai.testnet", "actions": [{"type": "AddKey"}], "public_key": "<base58 of the key>"}}' --accountId multisig.vitalpointai.testnet
更改批准多重签名请求所需的确认数
near call multisig.vitalpoint.ai add_request '{"request": {"receiver_id": "multisig.vitalpointai.testnet", "actions": [{"type": "SetNumConfirmations"}], "num_confirmations": 2}}' --accountId multisig.vitalpointai.testnet
所有这些都返回request_id,可用于查询/确认详细信息。还请记住,您可以创建将多个操作链接在一起的更复杂的调用。
请注意,合约“ add_request_and_confirm”中有一种方法与“ add_request”具有相同的功能,但是它通过添加请求的键将请求提交与确认结合在一起。如果使用“ add_request”提交,则用户(在这种情况下为C)仍然需要确认。不要忘记在初始化时将’add_request_and_confirm’添加到methodNames数组中,以提供适当的权限来调用它到访问键。
合约也有许多视图请求,您可以调用这些视图请求以获取一些信息:
列出所有请求ID
near view multisig.vitalpointai.testnet list_request_ids
查看有关特定请求的信息
near view multisig.vitalpointai.testnet list_request_ids
查看特定请求的确认
near view multisig.vitalpointai.testnet get_request '{"request_id: 0"}'
任何request 所需的确认总数
near view multisig.vitalpointai.testnet get_confirmations '{"request_id: 0"}'
步骤4:确认
在星标4处-用户的B和C都在合约上调用“确认”方法,以表明他们对关联的request_id的支持。非常简单。看起来像这样:
near call multisig.vitalpointai.testnet confirm '{"request_id": 0}' --accountId multisig.vitalpointai.testnet
当我们初始化需要2/3确认的合约时-用B&C的确认,它会触发execute_request函数,该函数将我们带到最后一步。
步骤5:执行
执行正是听起来的样子。由于在步骤3提交的请求收到了必要的两次确认,因此execute_request函数将执行并执行属于请求的任何动作(假定所有相关参数都存在)。他们包括:
- 转移
- 创建帐号
- 部署帐户
- AddKey
- 删除键
- 函数调用
- SetNumConfirmations
MultiSig合约有什么好处呢?
发挥您的想象力-可能会想到许多情况,在这种情况下,提交请求并在参与人员中达成多数(或其他共识)是有意义的。
从本质上讲,这就是去中心化自治组织(DAO)的运行方式,在这种情况下,NEAR的帐户和访问密钥系统以及multisig合约为实现这些系统开辟了一些新颖的方式。
这种多重信号可能是超级健壮的DAO的开始。可以将合约扩展为包括其他各种请求类型,并且DAO的成员(如其访问键所示)可以指示对某些请求的支持,添加/删除其他成员或您能想到的任何其他内容。
它是可升级的
但如果您决定这样做,请务必小心-以下请求将在给定帐户上部署新的合同代码。您需要考虑数据并需要迁移(合同更新应包括向前迁移的数据)。
const fs = require('fs');
const account = await near.account("vitalpointai.testnet");
const contractName = "multisig.vitalpointai.testnet";
const requestArgs = {"request": [
{"receiver_id": "multisig.illia", "actions": [{"type": "DeployContract", "code":
fs.readFileSync("out/multisig.wasm")}]}
]};
const result = account.signAndSendTransaction(
contractName,
[
nearAPI.transactions.functionCall("add_request", Buffer.from(JSON.stringify(requestArgs)),
10000000000000, "0"),
]);
这仍然像是一个请求,并且需要通过合约的确认数量来进行确认。
Whitelist
https://github.com/VitalPointAI/Whitelist-Contract
The purpose of this contract is to maintain the whitelist of the staking pool contracts account IDs that are approved by NEAR Foundation.
该合约的目的是维护由NEAR Foundation批准的权益池合约帐户ID的白名单。
Counter
https://github.com/near-examples/counter
该合约实现了由存储在区块链上支持的简单计数器。