区块链-简单合约部署

简单合约代码

VoteStorage.sol

// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.0;

contract Voting {

address public owner; // 合约拥有者
mapping(address => bool) public voters; // 存储已投票的地址
mapping(uint256 => Candidate) public candidates; // 存储候选人信息
uint256 public candidatesCount; // 当前候选人数量

// 候选人结构体
struct Candidate {
    uint256 id;
    string name;
    uint256 voteCount;
}

// 事件:当一个新的候选人被添加时触发
event CandidateAdded(uint256 candidateId, string candidateName);
// 事件:当有人投票时触发
event Voted(address voter, uint256 candidateId);

// 构造函数,设置合约拥有者
constructor() {
    owner = msg.sender;
}

// 仅合约拥有者可以执行的修饰符
modifier onlyOwner() {
    require(msg.sender == owner, "You are not the owner");
    _;
}

// 1. 添加候选人函数:只有合约拥有者可以调用
function addCandidate(string memory _name) public onlyOwner {
    candidatesCount++;
    candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
    emit CandidateAdded(candidatesCount, _name); // 触发事件
}

// 2. 投票函数:用户只能投一次票
function vote(uint256 _candidateId) public {
    require(!voters[msg.sender], "You have already voted!"); // 防止重复投票
    require(_candidateId > 0 && _candidateId <= candidatesCount, "Invalid candidate!"); // 确保候选人有效

    voters[msg.sender] = true; // 标记该地址为已投票
    candidates[_candidateId].voteCount++; // 增加候选人的得票数

    emit Voted(msg.sender, _candidateId); // 触发投票事件
}

// 3. 获取候选人信息函数
function getCandidate(uint256 _candidateId) public view returns (string memory name, uint256 voteCount) {
    require(_candidateId > 0 && _candidateId <= candidatesCount, "Invalid candidate!");
    Candidate memory c = candidates[_candidateId];
    return (c.name, c.voteCount);
}

// 4. 获取所有候选人数量函数
function getCandidatesCount() public view returns (uint256) {
    return candidatesCount;
}

}

运行部署合约:

安装node, npm,truffle,
创建项目:
mkdir vote && cd vote && truffle init (创建truffle项目目录)

运行合约:
ganache --port 8545 --networkId 5777

配置网络
truffle-config.js 文件中配置网络
module.exports = {
networks: {
development: {
host: “127.0.0.1”,
port: 8545,
network_id: “*”, // Match any network id
},
},
compilers: {
solc: {
version: “0.8.0”, // 确保与你的合约版本一致
},
},
};
部署合约:
在 migrations/ 目录下创建一个迁移文件,例如 2_deploy_contracts.js
const Voting = artifacts.require(“Voting”);

module.exports = function (deployer) {
  deployer.deploy(Voting);
};

然后在终端中运行:

truffle migrate --network development

与合约交互:
truffle console --network development

let voting = await Voting.deployed();
await voting.addCandidate("Alice", { from: accounts[0] });
查询候选人信息:

javascript
let candidate = await voting.getCandidate(1);
console.log(candidate);

hardhat:

npx hardhat init 生成hardhatdemo(目录结构)

npx hardhat --version 版本

npx hardhat accounts 执行 hardhat.config.js 定义的 task

npx hardhat compile 编译

npx hardhat flatten 合并solidity文件

npx hardhat node 启动节点

npx hardhat test 运行测试

npx hardhat run scripts/deploy.js 部署合约

npx hardhat run scripts/deploy.js --network localhost 部署合约

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值