Create an external, view function called isMember which takes an address and returns a bool indicating whether or not the address is a member. 创建一个名为isMember的外部视图函数,该函数接受一个地址</b1并返回一个布尔值</b2,用于指示该地址</b3是否为成员。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20;
contract Contract { mapping(address => bool) public members; function addMember(addressaddr)external { //如何通过在members映射中该地址对应的数据源位置存储true来实现这一点。 members[addr] = true; } function isMember(addressaddr)external view returns(bool) { //该函数接受一个地址</b1并返回一个布尔值</b2,用于指示该地址</b3是否为成员。 return members[addr]; } }
Your Goal: Remove Member 你的目标:移除成员
Create an external function called removeMember that will take an address and set its corresponding value in the members mapping to false. 创建一个名为removeMember的外部函数,该函数将接收一个地址</b1,并将members映射中与其对应的 value 设置为false。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20;
contract Contract { mapping(address => bool) public members; function addMember(addressaddr)external { //如何通过在members映射中该地址对应的数据源位置存储true来实现这一点。 members[addr] = true; } function isMember(addressaddr)external view returns(bool) { //该函数接受一个地址</b1并返回一个布尔值</b2,用于指示该地址</b3是否为成员。 return members[addr]; } function removeMember(addressaddr)external returns(bool) { return members[addr] = false; } }
Let’s create a new token where every new user will receive 100 tokens! 让我们创建一种新代币,每个新用户都将获得100个代币!
Create a public mapping called users that will map an address to a User struct. 创建一个名为users的公共映射,该映射将address映射到User结构体。 Create an external function called createUser. This function should create a new user and associate it to the msg.sender address in the users mapping. 创建一个名为createUser的外部函数。该函数应创建一个新用户,并将其关联到users映射中的msg.sender地址。 The balance of the new user should be set to 100 and the isActive boolean should be set to true. 新用户的balance应设置为100,且isActive布尔值应设置为true。
Create an external function called transfer which takes two parameters: an address for the recipient and a uint for the amount. 创建一个名为transfer的外部函数,该函数接受两个参数:一个用于接收者的address和一个用于金额的uint。 In this function, transfer the amount specified from the balance of the msg.sender to the balance of the recipient address. 在此函数中,将指定的amount从msg.sender的余额转移到接收者address的余额。
Create a public mapping called connections which will map an address to a mapping of an address to a ConnectionTypes enum value. 创建一个名为connections的公共映射,该映射会将一个address映射到另一个从address到ConnectionTypes枚举值的映射。 In theconnectWith function, create a connection from the msg.sender to the other address. 在connectWith函数中,创建一个从msg.sender到other地址的连接。
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20;
contract Contract { enum ConnectionTypes { Unacquainted, Friend, Family }
// TODO: create a public nested mapping `connections` //mapping(address => User) public connections; mapping(address => mapping(address => ConnectionTypes)) public connections;
functionconnectWith(address other, ConnectionTypes connectionType) external{ // TODO: make the connection from msg.sender => other => connectionType //connections[msg.sender][ConnectionTypes] = other; 错误的代码 require(other != msg.sender, "Cannot connect with yourself"); connections[msg.sender][other] = connectionType; } }
Voting Contract 投票合约
In this tutorial we’re going to build a voting contract! We’ll use the lessons learned here to understand how the Governor standard emerged 在本教程中,我们将构建一个投票合约!我们会运用在这里学到的知识,来理解Governor标准是如何形成的。
Proposals 提案 We’re going to focus on creating a voting contract that will allow members to create new proposals. This contract can contain many proposals which will be voted on by a group of members. Each proposal will keep track of its votes to decide when its time to execute. 我们将专注于创建一个投票合约,该合约将允许成员创建新提案。这个合约可以包含许多提案,供一群成员进行投票。每个提案都会记录其票数,以决定何时执行。
At execution time, these proposals will send calldata to a smart contract. The calldata could be anything! We could have a Voting system that allows 100 members to decide when to upgrade a protocol. The calldata might target a function with the signature upgrade(address) and send over the new protocol implementation. That would be a very cool use of your Voting contract! 在执行时,这些提案会将调用数据发送到智能合约。调用数据可以是任何内容!我们可以有一个投票系统,允许100名成员决定何时升级协议。调用数据可能会以签名upgrade(address)为目标函数,并发送新的协议实现。这将是对你的投票合约非常酷的一种使用方式!
Your Goal: Proposals 你的目标:提案
Create a public array of type Proposal and call it proposals. 创建一个类型为Proposal的公共数组,并将其命名为proposals。 Create an external function newProposal which takes two arguments: 创建一个外部函数newProposal,它接受两个参数: An address argument which will be the target address of the proposal. We’ll send some calldata to this address. 一个address参数,它将是提案的目标地址。我们会向这个地址发送一些调用数据。 A bytes argument which will be the calldata to eventually send to the smart contract when the proposal is executed. 一个bytes参数,它将是提案执行时最终发送给智能合约的调用数据。 In the newProposal function create a new Proposal with the arguments passed in and the yes/no vote counts are initialized at 0. 在newProposal函数中,使用传入的参数创建一个新的Proposal,赞成/反对票数初始化为0。 Add the new Proposal to the proposals array. 将新的Proposal添加到proposals数组中。
Create an external function castVote which takes a uint proposalId and a bool which indicates whether the vote supports the proposal (true for yes, false for no). 创建一个外部函数castVote,它接收一个uint类型的proposalId和一个bool类型的参数,该参数用于表示投票是否支持该提案(true表示赞成,false表示反对)。 For each vote cast, update the yesCount and noCount in the referenced proposal accordingly. 对于每一张所投的票,相应地更新所引用提案中的yesCount和noCount。