以下是一个实现投票的智能合约的示例:

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

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Voting is Ownable {
    using SafeMath for uint256;
    
    struct Voter {
        uint256 timestamp;
        bool agree;
    }
    
    struct Proposal {
        uint256 id;
        uint256 timestamp;
        string content;
        address proposer;
        uint256 deadline;
        uint256 agreeCount;
        uint256 disagreeCount;
        bool passed;
    }
    
    mapping(address => Voter) private voters;
    Proposal private proposal;
    
    event Vote(address indexed voter, bool agree);
    event Result(bool passed);
    
    constructor(uint256 _deadline, string memory _content) {
        proposal.timestamp = block.timestamp;
        proposal.content = _content;
        proposal.proposer = msg.sender;
        proposal.deadline = _deadline;
    }
    
    function vote(bool _agree) public {
        require(block.timestamp <= proposal.deadline, "Voting has ended");
        require(voters[msg.sender].timestamp == 0, "Already voted");
        
        voters[msg.sender].timestamp = block.timestamp;
        voters[msg.sender].agree = _agree;
        
        if (_agree) {
            proposal.agreeCount = proposal.agreeCount.add(1);
        } else {
            proposal.disagreeCount = proposal.disagreeCount.add(1);
        }
        
        emit Vote(msg.sender, _agree);
    }
    
    function getResult() public {
        require(block.timestamp > proposal.deadline, "Voting has not ended yet");
        require(!proposal.passed, "Result has already been determined");
        
        uint256 totalVotes = proposal.agreeCount.add(proposal.disagreeCount);
        uint256 agreePercentage = proposal.agreeCount.mul(100).div(totalVotes);
        
        if (agreePercentage > 50) {
            proposal.passed = true;
        }
        
        emit Result(proposal.passed);
    }
    
    function getProposal() public view returns (Proposal memory) {
        return proposal;
    }
}

使用 Hardhat 进行测试的示例代码如下:

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

import "hardhat/console.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./Voting.sol";

contract VotingTest is Ownable {
    using SafeMath for uint256;
    using Strings for uint256;
    using Address for address payable;
    
    Voting private voting;
    
    constructor() {
        voting = new Voting(block.timestamp.add(3600), "Test Proposal");
    }
    
    function testVote(bool agree) public {
        voting.vote(agree);
    }
    
    function testGetResult() public {
        voting.getResult();
    }
    
    function testGetProposal() public view returns (string memory) {
        Voting.Proposal memory proposal = voting.getProposal();
        
        string memory agreeCountStr = proposal.agreeCount.toString();
        string memory disagreeCountStr = proposal.disagreeCount.toString();
        string memory passedStr = proposal.passed ? "true" : "false";
        
        string memory result = string(abi.encodePacked(
            "Content: ", proposal.content, "\n",
            "Proposer: ", Strings.toHexString(uint256(uint160(proposal.proposer))), "\n",
            "Deadline: ", Strings.toHexString(proposal.deadline), "\n",
            "Agree Count: ", agreeCountStr, "\n",
            "Disagree Count: ", disagreeCountStr, "\n",
            "Passed: ", passedStr
        ));
        
        return result;
    }
}

使用 Truffle 进行测试的示例代码如下:

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

import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./Voting.sol";

contract VotingTest is Ownable {
    using SafeMath for uint256;
    using Strings for uint256;
    using Address for address payable;
    
    Voting private voting;
    
    constructor() {
        voting = new Voting(block.timestamp.add(3600), "Test Proposal");
    }
    
    function testVote(bool agree) public {
        voting.vote(agree);
    }
    
    function testGetResult() public {
        voting.getResult();
    }
    
    function testGetProposal() public view returns (string memory) {
        Voting.Proposal memory proposal = voting.getProposal();
        
        string memory agreeCountStr = proposal.agreeCount.toString();
        string memory disagreeCountStr = proposal.disagreeCount.toString();
        string memory passedStr = proposal.passed ? "true" : "false";
        
        string memory result = string(abi.encodePacked(
            "Content: ", proposal.content, "\n",
            "Proposer: ", Strings.toHexString(uint256(uint160(proposal.proposer))), "\n",
            "Deadline: ", Strings.toHexString(proposal.deadline), "\n",
            "Agree Count: ", agreeCountStr, "\n",
            "Disagree Count: ", disagreeCountStr, "\n",
            "Passed: ", passedStr
        ));
        
        return result;
    }
}

请注意,在运行测试之前,您需要使用以下命令安装必要的依赖项:

npm install --save-dev hardhat
npm install @nomiclabs/hardhat-waffle ethereum-waffle chai
npm install --save-dev truffle
npm install @openzeppelin/contracts

然后,您可以使用 Hardhat 运行测试:

npx hardhat test

或者,您可以使用 Truffle 运行测试:

npx truffle test

希望对您有帮助

编写⼀个投票的智能合约在⼀定时间能进⾏投票超过时间投票截⽌并投赞同票超过50则为通过。并通过hardhat或truff测试使⽤safeMath库使⽤Owner 第三⽅库拥有参与投票权的⽤户在创建合约时确定 Voter 结构 要有时间戳、投票是否同意等; struct 结构体Proposal 提案提案编号、提案时间、提案内容、提案⼈、提案截⽌时间、结果等。Voter 投票投票时间 、态度等也可以增加

原文地址: https://www.cveoy.top/t/topic/iAwt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录