-
Notifications
You must be signed in to change notification settings - Fork 0
/
votedToken.sol
88 lines (76 loc) · 2.94 KB
/
votedToken.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pragma solidity ^0.8.1;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
mapping(address => bool) private _whitelist;
struct Proposal {
uint256 id;
address proposer;
uint256 amount;
address target;
bool executed;
uint256 endTime;
uint256 votesFor;
uint256 votesAgainst;
bool isMint; // true pour mint, false pour burn
}
uint256 public nextProposalId;
mapping(uint256 => Proposal) public proposals;
uint256 public voteDuration = 2 minutes; // Durée du vote
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
_mint(msg.sender, initialSupply);
addToWhitelist(msg.sender);
}
modifier onlyWhitelisted() {
require(_whitelist[msg.sender], "Vous n'etes pas autorise.");
_;
}
function addToWhitelist(address account) public onlyOwner {
_whitelist[account] = true;
}
function removeFromWhitelist(address account) public onlyOwner {
_whitelist[account] = false;
}
function createProposal(uint256 amount, address target, bool isMint) public onlyOwner {
proposals[nextProposalId] = Proposal(
nextProposalId,
msg.sender,
amount,
target,
false,
block.timestamp + voteDuration,
0,
0,
isMint
);
nextProposalId++;
}
function vote(uint256 proposalId, bool support) public {
require(balanceOf(msg.sender) > 0, "Vous devez posseder des tokens pour voter.");
Proposal storage proposal = proposals[proposalId];
require(block.timestamp < proposal.endTime, "Le temps de vote est ecoule.");
if (support) {
proposal.votesFor += balanceOf(msg.sender);
} else {
proposal.votesAgainst += balanceOf(msg.sender);
}
}
function executeProposal(uint256 proposalId) public {
Proposal storage proposal = proposals[proposalId];
require(block.timestamp >= proposal.endTime, "Le vote n'est pas encore fini.");
require(!proposal.executed, "La proposition a deja ete executee.");
uint256 totalVotes = proposal.votesFor + proposal.votesAgainst;
if (proposal.votesAgainst <= proposal.votesFor || proposal.votesAgainst < totalVotes / 2) {
if (proposal.isMint) {
_mint(proposal.target, proposal.amount);
} else {
_burn(proposal.target, proposal.amount);
}
proposal.executed = true;
}
}
function _transfer(address from, address to, uint256 amount) internal override onlyWhitelisted {
require(_whitelist[to], "Destinataire non autorise.");
super._transfer(from, to, amount);
}
}