107 lines
4.0 KiB
Solidity
107 lines
4.0 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.20;
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
|
|
import "@openzeppelin/contracts/access/Ownable.sol";
|
|
|
|
contract PollCelo is ReentrancyGuard, Ownable {
|
|
IERC20 public immutable cUSD;
|
|
uint256 public constant PROTOCOL_FEE = 5; // 5%
|
|
|
|
struct Poll {
|
|
string id;
|
|
uint256 endTime;
|
|
uint256 totalPot;
|
|
uint256 winningOption;
|
|
bool finalized;
|
|
mapping(uint256 => uint256) optionTotals;
|
|
mapping(address => Vote) votes;
|
|
}
|
|
|
|
struct Vote {
|
|
uint256 option;
|
|
uint256 amount;
|
|
bool claimed;
|
|
}
|
|
|
|
mapping(string => Poll) public polls;
|
|
string[] public pollIds;
|
|
|
|
event Voted(string indexed pollId, address indexed voter, uint256 option, uint256 amount);
|
|
event PollFinalized(string indexed pollId, uint256 winningOption, uint256 totalPot);
|
|
event RewardClaimed(string indexed pollId, address indexed voter, uint256 amount);
|
|
|
|
constructor(address _cUSD) {
|
|
cUSD = IERC20(_cUSD);
|
|
}
|
|
|
|
function createPoll(string memory pollId, uint256 duration) external onlyOwner {
|
|
require(polls[pollId].endTime == 0, "Poll exists");
|
|
|
|
Poll storage poll = polls[pollId];
|
|
poll.id = pollId;
|
|
poll.endTime = block.timestamp + duration;
|
|
pollIds.push(pollId);
|
|
}
|
|
|
|
function vote(string memory pollId, uint256 option, uint256 amount) external nonReentrant {
|
|
Poll storage poll = polls[pollId];
|
|
require(poll.endTime > 0, "Poll not found");
|
|
require(block.timestamp < poll.endTime, "Poll ended");
|
|
require(poll.votes[msg.sender].amount == 0, "Already voted");
|
|
require(amount > 0, "Invalid amount");
|
|
|
|
require(cUSD.transferFrom(msg.sender, address(this), amount), "Transfer failed");
|
|
|
|
poll.votes[msg.sender] = Vote(option, amount, false);
|
|
poll.optionTotals[option] += amount;
|
|
poll.totalPot += amount;
|
|
|
|
emit Voted(pollId, msg.sender, option, amount);
|
|
}
|
|
|
|
function finalizePoll(string memory pollId, uint256 winningOption) external onlyOwner {
|
|
Poll storage poll = polls[pollId];
|
|
require(poll.endTime > 0, "Poll not found");
|
|
require(block.timestamp >= poll.endTime, "Poll not ended");
|
|
require(!poll.finalized, "Already finalized");
|
|
|
|
poll.winningOption = winningOption;
|
|
poll.finalized = true;
|
|
|
|
uint256 protocolAmount = (poll.totalPot * PROTOCOL_FEE) / 100;
|
|
require(cUSD.transfer(owner(), protocolAmount), "Protocol fee failed");
|
|
|
|
emit PollFinalized(pollId, winningOption, poll.totalPot);
|
|
}
|
|
|
|
function claimReward(string memory pollId) external nonReentrant {
|
|
Poll storage poll = polls[pollId];
|
|
require(poll.finalized, "Poll not finalized");
|
|
|
|
Vote storage userVote = poll.votes[msg.sender];
|
|
require(userVote.amount > 0, "No vote found");
|
|
require(!userVote.claimed, "Already claimed");
|
|
require(userVote.option == poll.winningOption, "Not a winner");
|
|
|
|
uint256 winnersPot = (poll.totalPot * 95) / 100;
|
|
uint256 reward = (userVote.amount * winnersPot) / poll.optionTotals[poll.winningOption];
|
|
|
|
userVote.claimed = true;
|
|
require(cUSD.transfer(msg.sender, reward), "Transfer failed");
|
|
|
|
emit RewardClaimed(pollId, msg.sender, reward);
|
|
}
|
|
|
|
function getUserVote(string memory pollId, address user) external view returns (uint256 option, uint256 amount, bool claimed) {
|
|
Vote memory userVote = polls[pollId].votes[user];
|
|
return (userVote.option, userVote.amount, userVote.claimed);
|
|
}
|
|
|
|
function getPollInfo(string memory pollId) external view returns (uint256 endTime, uint256 totalPot, bool finalized, uint256 winningOption) {
|
|
Poll storage poll = polls[pollId];
|
|
return (poll.endTime, poll.totalPot, poll.finalized, poll.winningOption);
|
|
}
|
|
}
|