// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function burn(uint256 amount) external;
function burnFrom(address account, uint256 amount) external;
}
/**
* SPDX-License-Identifier: Apache 2.0
*
*
* /$$ /$$ /$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$
*| $$ | $$| $$ | $$ |__/ | $$ | $$__ $$ /$$__ $$| $$
*| $$ | $$| $$ /$$$$$$ /$$ /$$$$$$/$$$$ /$$$$$$ /$$$$$$ /$$$$$$ | $$ \ $$ /$$$$$$ | $$ \ $$| $$
*| $$ | $$| $$|_ $$_/ | $$| $$_ $$_ $$ |____ $$|_ $$_/ /$$__ $$| $$ | $$ /$$__ $$| $$$$$$$$| $$
*| $$ | $$| $$ | $$ | $$| $$ \ $$ \ $$ /$$$$$$$ | $$ | $$$$$$$$| $$ | $$| $$$$$$$$| $$__ $$| $$
*| $$ | $$| $$ | $$ /$$| $$| $$ | $$ | $$ /$$__ $$ | $$ /$$| $$_____/| $$ | $$| $$_____/| $$ | $$| $$
*| $$$$$$/| $$ | $$$$/| $$| $$ | $$ | $$| $$$$$$$ | $$$$/| $$$$$$$| $$$$$$$/| $$$$$$$| $$ | $$| $$
* \______/ |__/ \___/ |__/|__/ |__/ |__/ \_______/ \___/ \_______/|_______/ \_______/|__/ |__/|__/
*
* its not i its little L
*
* /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$ /$$ /$$ /$$
* /$$__ $$ | $$ | $$ | $$ |__/ /$$__ $$| $$|__/ | $$
*| $$ \ $$ /$$$$$$$| $$$$$$$ /$$$$$$ /$$$$$$ | $$ | $$ /$$$$$$ /$$ /$$$$$$/$$$$ | $$ \__/| $$ /$$ /$$$$$$$| $$ /$$
*| $$ | $$ /$$_____/| $$__ $$ /$$__ $$ /$$__ $$ | $$$$$$$$ |____ $$| $$| $$_ $$_ $$ | $$ /$$$$| $$| $$ /$$_____/| $$ /$$/
*| $$ | $$| $$$$$$ | $$ \ $$| $$$$$$$$| $$ \__/ | $$__ $$ /$$$$$$$| $$| $$ \ $$ \ $$ | $$|_ $$| $$| $$| $$ | $$$$$$/
*| $$ | $$ \____ $$| $$ | $$| $$_____/| $$ | $$ | $$ /$$__ $$| $$| $$ | $$ | $$ | $$ \ $$| $$| $$| $$ | $$_ $$
*| $$$$$$/ /$$$$$$$/| $$ | $$| $$$$$$$| $$ | $$ | $$| $$$$$$$| $$| $$ | $$ | $$ | $$$$$$/| $$| $$| $$$$$$$| $$ \ $$
* \______/ |_______/ |__/ |__/ \_______/|__/ |__/ |__/ \_______/|__/|__/ |__/ |__/ \______/ |__/|__/ \_______/|__/ \__/
*
*
*
*
*
*
*
**/
pragma solidity ^0.8.26;
import "contracts/IERC20.sol";
import "contracts/ReentrancyGuard.sol";
interface IInvoice {
function verifyOwnershipByBarcode(address owner, string memory productBarcode) external view returns (bool);
}
contract LuckESH is ReentrancyGuard {
struct CityData {
uint256 totalTickets;
uint256 totalDeposited;
uint256 registrationCost;
address[] participants;
mapping(address => uint256) ticketsOwned;
mapping(address => uint256) amountDeposited;
}
IInvoice public invoices;
mapping(string => CityData) public cities;
string[] public cityNames;
IERC20 public token;
address public owner;
uint256 public constant MIN_CITY_COST = 65 * 1e18;
uint256 public constant MAX_CITY_COST = 90 * 1e18;
event Registered(address indexed participant, uint256 tickets, string city);
event LuckSpinned(address indexed winner, uint256 amount, string city);
event RegistrationCostUpdated(uint256 newCost, string city);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event TokenChanged(address indexed oldToken, address indexed newToken);
event Refunded(address indexed participant, uint256 amount, string city);
event CityInitialized(string city, uint256 initialCost);
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
constructor(IERC20 _token,address _invoices) {
token = _token;
owner = msg.sender;
invoices = IInvoice(_invoices);
}
function initializeCity(string memory _city) internal {
if (cities[_city].registrationCost == 0) {
uint256 initialCost = getRandomCost();
cities[_city].registrationCost = initialCost;
cityNames.push(_city);
emit CityInitialized(_city, initialCost);
}
}
function getRandomCost() internal view returns (uint256) {
uint256 range = MAX_CITY_COST - MIN_CITY_COST;
uint256 randomness = uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender)));
return MIN_CITY_COST + (randomness % (range + 1));
}
function calculatePrice(uint256 _tickets, string memory _city) public view returns (uint256) {
return _tickets * cities[_city].registrationCost;
}
function getTicketsOwned(string memory _city, address _participant) public view returns (uint256) {
return cities[_city].ticketsOwned[_participant];
}
function register(uint256 _tickets, string memory _city) public nonReentrant {
require(invoices.verifyOwnershipByBarcode(msg.sender,"LOTERRY"),"You Don't Have CitizenShip");
require(_tickets > 0, "Must buy at least one ticket");
initializeCity(_city);
uint256 cost = calculatePrice(_tickets, _city);
require(token.transferFrom(msg.sender, address(this), cost), "Transfer failed");
CityData storage cityData = cities[_city];
if (cityData.ticketsOwned[msg.sender] == 0) {
cityData.participants.push(msg.sender);
}
cityData.ticketsOwned[msg.sender] += _tickets;
cityData.amountDeposited[msg.sender] += cost;
cityData.totalTickets += _tickets;
cityData.totalDeposited += cost;
emit Registered(msg.sender, _tickets, _city);
}
function spinTheLuck(bytes32 randomSeed, string memory _city) public onlyOwner nonReentrant {
CityData storage cityData = cities[_city];
require(cityData.totalTickets > 0, "No tickets sold for this city");
uint256 winningTicket = getRandomNumber(randomSeed) % cityData.totalTickets;
address winner;
uint256 ticketCount = 0;
for (uint256 i = 0; i < cityData.participants.length; i++) {
address participant = cityData.participants[i];
ticketCount += cityData.ticketsOwned[participant];
if (ticketCount > winningTicket) {
winner = participant;
break;
}
}
uint256 totalDeposited = cityData.totalDeposited;
require(token.approve(address(this), totalDeposited), "Approval failed");
require(token.transfer(owner, totalDeposited / 3), "Transfer to owner failed");
require(token.transfer(winner, totalDeposited * 2 / 3), "Transfer to winner failed");
emit LuckSpinned(winner, totalDeposited, _city);
// Reset the city state
for (uint256 i = 0; i < cityData.participants.length; i++) {
delete cityData.ticketsOwned[cityData.participants[i]];
delete cityData.amountDeposited[cityData.participants[i]];
}
delete cityData.participants;
cityData.totalTickets = 0;
cityData.totalDeposited = 0;
}
function getRandomNumber(bytes32 randomSeed) internal view returns (uint256) {
uint256 randomNumber;
assembly {
let fp := mload(0x40)
mstore(fp, randomSeed)
mstore(add(fp, 32), timestamp())
mstore(add(fp, 64), prevrandao())
mstore(add(fp, 96), blockhash(sub(number(), 1)))
randomNumber := keccak256(fp, 128)
}
return randomNumber;
}
function setRegistrationCost(uint256 _newCost, string memory _city) public onlyOwner {
initializeCity(_city);
cities[_city].registrationCost = _newCost;
emit RegistrationCostUpdated(_newCost, _city);
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "New owner is the zero address");
address oldOwner = owner;
owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
function getParticipantsCount(string memory _city) public view returns (uint256) {
return cities[_city].participants.length;
}
function getCityTotalDeposited(string memory _city) public view returns (uint256) {
return cities[_city].totalDeposited;
}
function getCityPrice(string memory _city) public view returns (uint256) {
return cities[_city].registrationCost;
}
function changeToken(address _newToken) public onlyOwner nonReentrant {
require(_newToken != address(0), "New token is the zero address");
IERC20 oldToken = token;
token = IERC20(_newToken);
// Refund all participants with the old token for all cities
for (uint256 j = 0; j < cityNames.length; j++) {
string memory city = cityNames[j];
CityData storage cityData = cities[city];
for (uint256 i = 0; i < cityData.participants.length; i++) {
address participant = cityData.participants[i];
uint256 amount = cityData.amountDeposited[participant];
if (amount > 0) {
require(oldToken.transfer(participant, amount), "Refund transfer failed");
emit Refunded(participant, amount, city);
delete cityData.amountDeposited[participant];
delete cityData.ticketsOwned[participant];
}
}
delete cityData.participants;
cityData.totalTickets = 0;
cityData.totalDeposited = 0;
}
emit TokenChanged(address(oldToken), address(_newToken));
}
function getCityNames() public view returns (string[] memory) {
return cityNames;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.23;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
{
"compilationTarget": {
"contracts/LuckMachine.sol": "LuckESH"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IERC20","name":"_token","type":"address"},{"internalType":"address","name":"_invoices","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"city","type":"string"},{"indexed":false,"internalType":"uint256","name":"initialCost","type":"uint256"}],"name":"CityInitialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"winner","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"string","name":"city","type":"string"}],"name":"LuckSpinned","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"participant","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"string","name":"city","type":"string"}],"name":"Refunded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"participant","type":"address"},{"indexed":false,"internalType":"uint256","name":"tickets","type":"uint256"},{"indexed":false,"internalType":"string","name":"city","type":"string"}],"name":"Registered","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"newCost","type":"uint256"},{"indexed":false,"internalType":"string","name":"city","type":"string"}],"name":"RegistrationCostUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldToken","type":"address"},{"indexed":true,"internalType":"address","name":"newToken","type":"address"}],"name":"TokenChanged","type":"event"},{"inputs":[],"name":"MAX_CITY_COST","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MIN_CITY_COST","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tickets","type":"uint256"},{"internalType":"string","name":"_city","type":"string"}],"name":"calculatePrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_newToken","type":"address"}],"name":"changeToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"cities","outputs":[{"internalType":"uint256","name":"totalTickets","type":"uint256"},{"internalType":"uint256","name":"totalDeposited","type":"uint256"},{"internalType":"uint256","name":"registrationCost","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"cityNames","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCityNames","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_city","type":"string"}],"name":"getCityPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_city","type":"string"}],"name":"getCityTotalDeposited","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_city","type":"string"}],"name":"getParticipantsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_city","type":"string"},{"internalType":"address","name":"_participant","type":"address"}],"name":"getTicketsOwned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"invoices","outputs":[{"internalType":"contract IInvoice","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tickets","type":"uint256"},{"internalType":"string","name":"_city","type":"string"}],"name":"register","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_newCost","type":"uint256"},{"internalType":"string","name":"_city","type":"string"}],"name":"setRegistrationCost","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"randomSeed","type":"bytes32"},{"internalType":"string","name":"_city","type":"string"}],"name":"spinTheLuck","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]