// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
}
contract DailyTreasureEvent {
address public owner;
mapping(address => uint256) public lifetimeChestCount;
mapping(address => uint256) public lifetimePremiumChestCount;
mapping(address => bool) public premiumUsers;
IERC20 public token;
uint256 public chestOpenCost;
uint256 public premiumChestOpenCost;
event ChestOpened(address indexed user, uint256 timestamp);
event PremiumChestOpened(address indexed user, uint256 timestamp);
event PremiumUserAdded(address user);
event PremiumUserRemoved(address user);
event CostsUpdated(uint256 chestOpenCost, uint256 premiumChestOpenCost);
event FundsWithdrawn(address owner, uint256 amount);
constructor(address[] memory initialPremiumUsers, address tokenAddress) {
owner = msg.sender;
token = IERC20(tokenAddress);
chestOpenCost = 1000000000000000; // 0.001 tokens
premiumChestOpenCost = 1000000000000000; // 0.001 tokens
for (uint256 i = 0; i < initialPremiumUsers.length; i++) {
premiumUsers[initialPremiumUsers[i]] = true;
}
}
function openChest() public {
require(token.transferFrom(msg.sender, address(this), chestOpenCost), "Token transfer failed");
lifetimeChestCount[msg.sender]++;
emit ChestOpened(msg.sender, block.timestamp);
}
function openPremiumChest() public {
require(isPremiumUser(msg.sender), "Only premium users can open premium chests");
require(token.transferFrom(msg.sender, address(this), premiumChestOpenCost), "Token transfer failed");
lifetimePremiumChestCount[msg.sender]++;
emit PremiumChestOpened(msg.sender, block.timestamp);
}
function isPremiumUser(address user) public view returns (bool) {
return premiumUsers[user];
}
function addPremiumUser(address user) public {
require(msg.sender == owner, "Only the owner can add premium users");
premiumUsers[user] = true;
emit PremiumUserAdded(user);
}
function removePremiumUser(address user) public {
require(msg.sender == owner, "Only the owner can remove premium users");
premiumUsers[user] = false;
emit PremiumUserRemoved(user);
}
function updateCosts(uint256 newChestOpenCost, uint256 newPremiumChestOpenCost) public {
require(msg.sender == owner, "Only the owner can update costs");
chestOpenCost = newChestOpenCost;
premiumChestOpenCost = newPremiumChestOpenCost;
emit CostsUpdated(newChestOpenCost, newPremiumChestOpenCost);
}
function withdrawTokens(uint256 amount) public {
require(msg.sender == owner, "Only the owner can withdraw tokens");
uint256 contractBalance = token.balanceOf(address(this));
require(amount <= contractBalance, "Not enough tokens in the contract");
require(token.transfer(owner, amount), "Token transfer failed");
emit FundsWithdrawn(owner, amount);
}
}
{
"compilationTarget": {
"DailyTreasureEvent.sol": "DailyTreasureEvent"
},
"evmVersion": "cancun",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address[]","name":"initialPremiumUsers","type":"address[]"},{"internalType":"address","name":"tokenAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"timestamp","type":"uint256"}],"name":"ChestOpened","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"chestOpenCost","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"premiumChestOpenCost","type":"uint256"}],"name":"CostsUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"FundsWithdrawn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"timestamp","type":"uint256"}],"name":"PremiumChestOpened","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"user","type":"address"}],"name":"PremiumUserAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"user","type":"address"}],"name":"PremiumUserRemoved","type":"event"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"addPremiumUser","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"chestOpenCost","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"isPremiumUser","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lifetimeChestCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lifetimePremiumChestCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"openChest","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"openPremiumChest","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"premiumChestOpenCost","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"premiumUsers","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"removePremiumUser","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newChestOpenCost","type":"uint256"},{"internalType":"uint256","name":"newPremiumChestOpenCost","type":"uint256"}],"name":"updateCosts","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdrawTokens","outputs":[],"stateMutability":"nonpayable","type":"function"}]