Skip to content

Commit 2276841

Browse files
committed
Replace lottery logic with ERC20 token implementation
Removed lottery-specific functionality and refactored the contract to implement a basic ERC20 token with minting, transfer, approval, and allowance features. The contract now supports standard ERC20 operations and no longer manages lottery rounds or ticket purchases.
1 parent 2540fc3 commit 2276841

File tree

1 file changed

+41
-69
lines changed

1 file changed

+41
-69
lines changed

src/lotteryERC20.sol

Lines changed: 41 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,64 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

4-
interface IERC20 {
5-
function transferFrom(address from, address to, uint256 amount) external returns (bool);
6-
}
7-
8-
contract Lottery{
9-
address[] public players;
10-
uint256 public roundId;
4+
contract LotteryERC20 {
115
address public owner;
6+
string public name;
7+
string public symbol;
8+
uint8 public decimals = 18;
9+
uint256 public totalSupply;
1210

13-
IERC20 public ticketToken;
14-
uint256 public ticketPrice;
11+
mapping(address => uint256) public balanceOf;
12+
mapping(address => mapping(address => uint256)) public allowance;
1513

16-
uint256 public constant TICKET_PRICE = 0.01 ether;
17-
uint256 public fee = 500;
14+
event Transfer(address indexed from, address indexed to, uint amount);
15+
event Approval(address indexed owner, address indexed spender, uint amount);
1816

19-
struct Round {
20-
address winner;
21-
uint256 potTokens;
22-
uint256 playersCount;
23-
uint256 timestamtp;
17+
constructor(string memory _name, string memory _symbol, uint initialSupply) {
18+
owner = msg.sender;
19+
name = _name;
20+
symbol = _symbol;
21+
_mint(msg.sender, initialSupply);
2422
}
2523

26-
mapping(uint256 => Round) public rounds;
27-
28-
event Enter(address indexed player, uint256 tickets, uint256 value, uint256 roundId);
29-
event WinnerPicked(address indexed winner, uint256 prize, uint256 roundId);
30-
3124
modifier onlyOwner() {
32-
require(msg.sender == owner, "Only owner");
25+
require(msg.sender == owner, "Not owner");
3326
_;
3427
}
3528

36-
constructor(address _token, uint256 _ticketPrice){
37-
owner = msg.sender;
38-
roundId = 1;
39-
ticketToken = IERC20(_token);
40-
ticketPrice = _ticketPrice;
29+
function mint(address to, uint amount) external onlyOwner {
30+
_mint(to, amount);
4131
}
4232

43-
function enter() external payable{
44-
require(msg.value >= TICKET_PRICE, 'not enough eth, pay 0.01 eth');
45-
require(msg.value % TICKET_PRICE == 0, 'Send multiple of ticket price');
46-
47-
uint256 tickets = msg.value / TICKET_PRICE;
48-
for(uint256 i = 0; i < tickets; i++){
49-
players.push(msg.sender);
50-
}
51-
52-
emit Enter(msg.sender, tickets, msg.value, roundId);
33+
function transfer(address to, uint amount) external returns (bool){
34+
require(balanceOf[msg.sender] >= amount, 'Not enough balance');
35+
balanceOf[msg.sender] -= amount;
36+
balanceOf[to] += amount;
37+
emit Transfer(msg.sender, to, amount);
38+
return true;
5339
}
5440

55-
function pickWinner() external onlyOwner returns(address winner){
56-
require(players.length > 0, 'Not enough players');
57-
58-
uint256 pot = address(this).balance;
59-
uint256 newFee = (pot * fee) / 10_000;
60-
uint256 prize = pot - newFee;
61-
62-
uint256 randomIndex = _random() % players.length;
63-
winner = players[randomIndex];
64-
65-
66-
67-
delete players;
68-
roundId++;
69-
70-
if(newFee > 0){
71-
(bool feeSent, ) = owner.call{value: newFee}("");
72-
require(feeSent, 'Fee transfer failed');
73-
}
74-
75-
(bool prizeSent, ) = winner.call{value: prize}("");
76-
require(prizeSent, 'Prize transfer fail');
77-
78-
emit WinnerPicked(winner, prize, roundId - 1);
41+
function approve(address spender, uint amount) external returns (bool){
42+
allowance[msg.sender][spender] = amount;
43+
emit Approval(msg.sender, spender, amount);
44+
return true;
7945
}
8046

81-
function _random() private view returns (uint256){
82-
return uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender, players.length, address(this).balance)));
83-
}
47+
function transferFrom(address from, address to, uint amount) external returns (bool){
48+
require(balanceOf[msg.sender] >= amount, 'Not enough balance');
49+
require(allowance[from][msg.sender] >= amount, 'Not allowed');
8450

85-
function playersCount() external view returns (uint256){
86-
return players.length;
51+
allowance[from][msg.sender] -= amount;
52+
balanceOf[from] -= amount;
53+
balanceOf[to] += amount;
54+
55+
emit Transfer(from, to, amount);
56+
return true;
8757
}
8858

89-
function contractBalance() external view returns (uint256) {
90-
return address(this).balance;
59+
function _mint(address to, uint amount) internal{
60+
totalSupply += amount;
61+
balanceOf[to] += amount;
62+
emit Transfer(address(0), to, amount);
9163
}
92-
}
64+
}

0 commit comments

Comments
 (0)