在本节中,我们将讨论如何使用Solidity编写基本的智能合约。课程将涵盖变量、函数和修饰符等基本内容,并以一个简单的代币合约示例来详细介绍实现过程。
变量、函数和修饰符
我们用Solidity来创建一个简单的代币合约,允许用户在多个账户之间转移代币并查看任一账户的代币余额。
TypeScript
pragma solidity ^0.8.0;
contract SimpleToken {
// Declare state variables
address public owner;
mapping(address => uint256) public balances;
// Initialize the token contract
constructor(uint256 initialSupply) {
owner = msg.sender;
balances[owner] = initialSupply;
}
// Function to transfer tokens
function transfer(address to, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
}
// Function to check the token balance of an account
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
}
该合约包含:
owner
和 balances
,分别用于存储合约所有者的地址和所有地址的代币余额。constructor
构造函数,用于设置代币的初始供应并将其分配给合约所有者。transfer
函数,允许用户将代币转移到其他账户。它使用require
语句来确保发送者有足够的代币可以转移。balanceOf
函数,用于返回指定帐户的代币余额。TypeScript
pragma solidity ^0.8.0;
contract VotingSystem {
mapping (bytes32 => uint256) public votesReceived;
bytes32[] public candidateList;
constructor(bytes32[] memory candidateNames) {
candidateList = candidateNames;
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate), "Invalid candidate.");
votesReceived[candidate] += 1;
}
function totalVotesFor(bytes32 candidate) public view returns (uint256) {
require(validCandidate(candidate), "Invalid candidate.");
return votesReceived[candidate];
}
function validCandidate(bytes32 candidate) public view returns (bool) {
for (uint256 i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}
在此示例中,VotingSystem
合约允许用户通过调用voteForCandidate
函数为候选人投票。合约使用voteForCandidate
映射统计每个候选人收到的票数,统计基于候选人的名称,值则是票数。CandateList
变量存储有效候选人列表。可以调用totalVotesFor
函数来查看特定候选人获得的票数,并通过validCandidate
函数检查候选人是否有效。通过这一示例,我们学习了如何使用Solidity构建具有复杂逻辑的去中心化应用。
要点
在本节中,我们将讨论如何使用Solidity编写基本的智能合约。课程将涵盖变量、函数和修饰符等基本内容,并以一个简单的代币合约示例来详细介绍实现过程。
变量、函数和修饰符
我们用Solidity来创建一个简单的代币合约,允许用户在多个账户之间转移代币并查看任一账户的代币余额。
TypeScript
pragma solidity ^0.8.0;
contract SimpleToken {
// Declare state variables
address public owner;
mapping(address => uint256) public balances;
// Initialize the token contract
constructor(uint256 initialSupply) {
owner = msg.sender;
balances[owner] = initialSupply;
}
// Function to transfer tokens
function transfer(address to, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
}
// Function to check the token balance of an account
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
}
该合约包含:
owner
和 balances
,分别用于存储合约所有者的地址和所有地址的代币余额。constructor
构造函数,用于设置代币的初始供应并将其分配给合约所有者。transfer
函数,允许用户将代币转移到其他账户。它使用require
语句来确保发送者有足够的代币可以转移。balanceOf
函数,用于返回指定帐户的代币余额。TypeScript
pragma solidity ^0.8.0;
contract VotingSystem {
mapping (bytes32 => uint256) public votesReceived;
bytes32[] public candidateList;
constructor(bytes32[] memory candidateNames) {
candidateList = candidateNames;
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate), "Invalid candidate.");
votesReceived[candidate] += 1;
}
function totalVotesFor(bytes32 candidate) public view returns (uint256) {
require(validCandidate(candidate), "Invalid candidate.");
return votesReceived[candidate];
}
function validCandidate(bytes32 candidate) public view returns (bool) {
for (uint256 i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}
在此示例中,VotingSystem
合约允许用户通过调用voteForCandidate
函数为候选人投票。合约使用voteForCandidate
映射统计每个候选人收到的票数,统计基于候选人的名称,值则是票数。CandateList
变量存储有效候选人列表。可以调用totalVotesFor
函数来查看特定候选人获得的票数,并通过validCandidate
函数检查候选人是否有效。通过这一示例,我们学习了如何使用Solidity构建具有复杂逻辑的去中心化应用。
要点