// 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);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @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;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
// \\
// Created by Clodron
// If you want to create a new project, please contact me @
// info@clodron.com
// \\
contract TokenOnChainWinPTA is ReentrancyGuard {
address public owner;
address public rewardToken;
uint256 public rewardAmount;
mapping(address => uint256) public entryCount;
address[] public winners;
address[] public players;
address[] private playerSelector;
bool public raffleStatus;
uint256 public entryFee;
uint256 public entryFeeUSDT;
uint256 public entryFeeUSDC;
uint256 public totalEntries;
uint256 public targetPrizeAmount;
uint256 public raffleStartTime;
uint256 public raffleDuration;
address constant USDT = 0xf55BEC9cafDbE8730f096Aa55dad6D22d44099Df; // USDT token
address constant USDC = 0x06eFdBFf2a14a7c8E15944D1F4A48F9F95F663A4; // USDC token
uint256 private raffleStartDuration;
event NewEntry(address indexed player, uint256 numberOfEntries);
event RaffleStarted(
uint256 targetPrizeAmount,
uint256 startTime,
uint256 duration
);
event RaffleEnded(
address winner,
uint256 targetPrizeAmount,
address rewardToken
);
constructor() {
owner = msg.sender;
raffleStatus = false;
}
modifier onlyOwner() {
require(msg.sender == owner, "Just owner!");
_;
}
function setupRaffle(
uint256 _targetPrizeAmount,
uint256 _durationInMinutes,
uint256 _entryFee,
uint256 _entryFeeUSDT,
uint256 _entryFeeUSDC,
address _rewardToken
) public onlyOwner {
require(!raffleStatus, "Raffle already in progress.");
require(
totalEntries == 0,
"Cannot start a new raffle until the previous one has ended."
);
// require(_targetPrizeAmount >= _entryFee, "Invalid entry fee for ETH.");
require(_durationInMinutes > 0, "Invalid duration.");
require(_entryFee > 0, "Invalid entry fee for ETH.");
require(_targetPrizeAmount > 0, "More than 0");
targetPrizeAmount = _targetPrizeAmount;
raffleDuration = _durationInMinutes * 1 minutes;
entryFeeUSDT = _entryFeeUSDT;
entryFeeUSDC = _entryFeeUSDC;
entryFee = _entryFee;
raffleStatus = true;
raffleStartTime = block.timestamp;
rewardToken = _rewardToken;
raffleStartDuration = _durationInMinutes;
emit RaffleStarted(targetPrizeAmount, raffleStartTime, raffleDuration);
}
function buyEntry(uint256 _numberOfEntries) public payable {
require(raffleStatus, "Raffle has not started or finished yet.");
uint256 totalCostWithCommission = (entryFee * _numberOfEntries * 105) /
100; // %5
require(
msg.value >= totalCostWithCommission,
"You need to pay much more."
);
for (uint256 i = 0; i < _numberOfEntries; i++) {
playerSelector.push(msg.sender);
}
entryCount[msg.sender] += _numberOfEntries;
totalEntries += _numberOfEntries;
emit NewEntry(msg.sender, _numberOfEntries);
if (getRemainingTimeSec() == 0) {
raffleStatus = false;
selectWinners();
}
}
function buyEntryUSDT(uint256 _numberOfEntries) public payable {
require(raffleStatus, "Raffle has not started or finished yet.");
uint256 totalCostWithUSDTCommission = (entryFeeUSDT *
_numberOfEntries *
105) / 100; // %5
require(totalCostWithUSDTCommission > 0, "Invalid entry fee for USDT.");
require(
IERC20(USDT).transferFrom(
msg.sender,
address(this),
totalCostWithUSDTCommission
),
"USDT transfer failed. Make sure you have approved the contract to spend your tokens."
);
for (uint256 i = 0; i < _numberOfEntries; i++) {
playerSelector.push(msg.sender);
}
entryCount[msg.sender] += _numberOfEntries;
totalEntries += _numberOfEntries;
emit NewEntry(msg.sender, _numberOfEntries);
if (getRemainingTimeSec() == 0) {
raffleStatus = false;
selectWinners();
}
}
function buyEntryUSDC(uint256 _numberOfEntries) public payable {
require(raffleStatus, "Raffle has not started or finished yet.");
uint256 totalCostWithUSDCCommission = (entryFeeUSDC *
_numberOfEntries *
105) / 100; // %5
require(totalCostWithUSDCCommission > 0, "Invalid entry fee for USDC.");
require(
IERC20(USDC).transferFrom(
msg.sender,
address(this),
totalCostWithUSDCCommission
),
"USDC transfer failed. Make sure you have approved the contract to spend your tokens."
);
for (uint256 i = 0; i < _numberOfEntries; i++) {
playerSelector.push(msg.sender);
}
entryCount[msg.sender] += _numberOfEntries;
totalEntries += _numberOfEntries;
emit NewEntry(msg.sender, _numberOfEntries);
if (getRemainingTimeSec() == 0) {
raffleStatus = false;
selectWinners();
}
}
function selectWinners() private {
if (
raffleStatus &&
(block.timestamp >= raffleStartTime + raffleDuration)
) {
raffleStatus = false;
}
require(
block.timestamp >= raffleStartTime + raffleDuration,
"Raffle is not yet ready to end."
);
require(playerSelector.length > 0, "No entries in the raffle.");
require(
!raffleStatus,
"Raffle must be ended before selecting a winner."
);
uint256 winnerIndex = random() % playerSelector.length;
address winner = playerSelector[winnerIndex];
IERC20(rewardToken).transfer(winner, targetPrizeAmount);
emit RaffleEnded(winner, targetPrizeAmount, rewardToken);
winners.push(winner);
resetsContracts();
}
function getPlayers() public view returns (address[] memory) {
return playerSelector;
}
function getNumberOfTicketsPerPlayer(
address player
) public view returns (uint256) {
return entryCount[player];
}
function hardReset() public onlyOwner {
resetsContract();
}
function resetsContracts() private {
for (uint256 i = 0; i < playerSelector.length; i++) {
entryCount[playerSelector[i]] = 0;
}
delete playerSelector;
delete players;
raffleStatus = false;
totalEntries = 0;
entryFee = 0;
entryFeeUSDC = 0;
entryFeeUSDT = 0;
targetPrizeAmount = 0;
}
function resetsContract() private onlyOwner {
for (uint256 i = 0; i < playerSelector.length; i++) {
entryCount[playerSelector[i]] = 0;
}
delete playerSelector;
delete players;
raffleStatus = false;
totalEntries = 0;
entryFee = 0;
entryFeeUSDC = 0;
entryFeeUSDT = 0;
}
function withdrawBalance() public onlyOwner {
require(!raffleStatus, "Cannot withdraw before raffle has ended.");
uint256 balance = address(this).balance;
require(balance > 0, "No balance to withdraw.");
payable(owner).transfer(balance);
}
function withdrawTokenBalance(address token) public onlyOwner {
require(!raffleStatus, "Cannot withdraw before raffle has ended.");
require(
token == address(USDT) || token == address(USDC),
"Invalid token address."
);
uint256 balance = IERC20(token).balanceOf(address(this));
require(balance > 0, "No token balance to withdraw.");
require(
IERC20(token).transfer(owner, balance),
"Token transfer failed."
);
}
function random() private view returns (uint256) {
return
uint256(
keccak256(
abi.encodePacked(
block.timestamp,
block.prevrandao,
players.length,
address(this)
)
)
);
}
// Utility
function isThereRewardBalance() public view returns (uint256) {
return IERC20(rewardToken).balanceOf(address(this));
}
function getTotalEntries() public view returns (uint256) {
return totalEntries;
}
function initialDeposit() external payable onlyOwner {}
function usdtDeposit(uint256 _amount) external payable onlyOwner {
IERC20 usdtToken = IERC20(USDT);
require(
usdtToken.transferFrom(msg.sender, address(this), _amount),
"USDT transfer failed."
);
}
function usdcDeposit(uint256 _amount) external payable onlyOwner {
IERC20 usdcToken = IERC20(USDC);
require(
usdcToken.transferFrom(msg.sender, address(this), _amount),
"USDC transfer failed."
);
}
function tokenDeposit(
address token,
uint256 _amount
) external payable onlyOwner {
IERC20 tokenContract = IERC20(token);
require(
tokenContract.transferFrom(msg.sender, address(this), _amount),
"Token transfer failed."
);
}
function getRemainingTimeSec() public view returns (uint256) {
if (!raffleStatus) {
return 0;
} else {
uint256 endTime = raffleStartTime + raffleDuration;
if (block.timestamp >= endTime) {
return 0;
} else {
return endTime - block.timestamp;
}
}
}
function getRemainingTimeMinutes() public view returns (uint256) {
if (!raffleStatus) {
return 0;
} else {
uint256 endTime = raffleStartTime + raffleDuration;
if (block.timestamp >= endTime) {
return 0;
} else {
return (endTime - block.timestamp) / 60;
}
}
}
function getRemainingTimeHours() public view returns (uint256) {
if (!raffleStatus) {
return 0;
} else {
uint256 endTime = raffleStartTime + raffleDuration;
if (block.timestamp >= endTime) {
return 0;
} else {
return (endTime - block.timestamp) / 3600;
}
}
}
function getWinnerCount() public view returns (uint256) {
return winners.length;
}
function getWinnerByIndex(uint256 index) public view returns (address) {
require(index < winners.length, "No winner found.");
return winners[index];
}
function getContractETHBalance() public view returns (uint256) {
return address(this).balance;
}
function getTokenBalance(address token) public view returns (uint256) {
return IERC20(token).balanceOf(address(this));
}
function getUSDCBalance() public view returns (uint256) {
return IERC20(USDC).balanceOf(address(this));
}
function getUSDTBalance() public view returns (uint256) {
return IERC20(USDT).balanceOf(address(this));
}
function getRaffleStartDuration() public view returns (uint256) {
return raffleStartDuration;
}
function getRewardToken() public view returns (address) {
return rewardToken;
}
}
{
"compilationTarget": {
"contracts/TokenPaidTimer.sol": "TokenOnChainWinPTA"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1000
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"player","type":"address"},{"indexed":false,"internalType":"uint256","name":"numberOfEntries","type":"uint256"}],"name":"NewEntry","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"winner","type":"address"},{"indexed":false,"internalType":"uint256","name":"targetPrizeAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"rewardToken","type":"address"}],"name":"RaffleEnded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"targetPrizeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"startTime","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"duration","type":"uint256"}],"name":"RaffleStarted","type":"event"},{"inputs":[{"internalType":"uint256","name":"_numberOfEntries","type":"uint256"}],"name":"buyEntry","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_numberOfEntries","type":"uint256"}],"name":"buyEntryUSDC","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_numberOfEntries","type":"uint256"}],"name":"buyEntryUSDT","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"entryCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"entryFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"entryFeeUSDC","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"entryFeeUSDT","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getContractETHBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"player","type":"address"}],"name":"getNumberOfTicketsPerPlayer","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPlayers","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRaffleStartDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRemainingTimeHours","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRemainingTimeMinutes","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRemainingTimeSec","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRewardToken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"getTokenBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTotalEntries","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUSDCBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUSDTBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"getWinnerByIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getWinnerCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"hardReset","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"initialDeposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"isThereRewardBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"players","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"raffleDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"raffleStartTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"raffleStatus","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardToken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_targetPrizeAmount","type":"uint256"},{"internalType":"uint256","name":"_durationInMinutes","type":"uint256"},{"internalType":"uint256","name":"_entryFee","type":"uint256"},{"internalType":"uint256","name":"_entryFeeUSDT","type":"uint256"},{"internalType":"uint256","name":"_entryFeeUSDC","type":"uint256"},{"internalType":"address","name":"_rewardToken","type":"address"}],"name":"setupRaffle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"targetPrizeAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"tokenDeposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"totalEntries","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"usdcDeposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"usdtDeposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"winners","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"withdrawBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"withdrawTokenBalance","outputs":[],"stateMutability":"nonpayable","type":"function"}]