Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.0 kB
3
Indexable
Never
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

//import "@openzeppelin/contracts/utils/Random.sol";

contract Ticket is ERC1155, Ownable {
    IERC20 public feeToken;
    uint256 public feeAmount = 2 * 10**6;
    mapping(uint256 => bool) public mintedTokens;

    // Winning numbers
    uint256[] public winningNumbers;
    // Mapping to track who minted which tokens
    mapping(uint256 => address) public tokenMinters;

    event TokensMinted(address indexed user, uint256[] tokenIds);
    event TokensWithdrawn(address indexed user, uint256[] tokenIds);
    event USDTWithdrawn(address indexed user, uint256 amount);
    event WinningNumbersAnnounced(uint256[] numbers);
    event TokensClaimed(address indexed user, uint256[] tokenIds);

    constructor(address _feeToken, address owner)
        ERC1155("Your Game Tokens URI")
        Ownable(owner)
    {
        feeToken = IERC20(_feeToken);
    }

    // Reward multiplier variable
    uint256 public rewardMultiplier = 1;

    // Function for admin to set the reward multiplier
    function setRewardMultiplier(uint256 multiplier) external onlyOwner {
        rewardMultiplier = multiplier;
    }

    function mintTickets(uint256[] memory tokenIds) external {
        require(tokenIds.length == 3, "Exactly 3 tokens must be selected");
        require(
            feeToken.transferFrom(msg.sender, address(this), feeAmount * 3),
            "Fee transfer failed"
        );

        for (uint256 i = 0; i < 3; i++) {
            _mint(msg.sender, tokenIds[i], 1, "");
            mintedTokens[tokenIds[i]] = true;
            tokenMinters[tokenIds[i]] = msg.sender;
        }

        emit TokensMinted(msg.sender, tokenIds);
    }

    function generateRandomNumbers() public view returns (uint256[] memory) {
        uint256[] memory randomNumbers = new uint256[](3);

        for (uint256 i = 0; i < 3; i++) {
            // Use block timestamp, block difficulty, and block hash to generate randomness
            uint256 randomNumber = uint256(
                keccak256(
                    abi.encodePacked(
                        block.timestamp,
                        block.difficulty,
                        blockhash(block.number - i)
                    )
                )
            );
            // Map the random number to the range [1, 30]
            randomNumbers[i] = (randomNumber % 30) + 1;
        }

        return randomNumbers;
    }

    function claimWinningTokens() external {
        uint256[] memory randomNumbers = generateRandomNumbers();
        require(randomNumbers.length == 3, "Invalid random numbers generated");

        bool hasWon = true;
        for (uint256 i = 0; i < randomNumbers.length; i++) {
            if (balanceOf(msg.sender, randomNumbers[i]) == 0) {
                hasWon = false;
                break;
            }
        }

        require(hasWon, "You didn't mint all the winning tokens");

        uint256 reward = feeAmount * rewardMultiplier;
        // Transfer the reward tokens to the user
        require(
            feeToken.transfer(msg.sender, reward),
            "Reward transfer failed"
        );

        // Emit an event for token claiming
        emit TokensClaimed(msg.sender, randomNumbers);
    }

    function withdrawUSDT(uint256 amount) external onlyOwner {
        require(amount > 0, "Amount must be greater than 0");
        require(
            feeToken.balanceOf(address(this)) >= amount,
            "Insufficient USDT balance in the contract"
        );

        require(feeToken.transfer(msg.sender, amount), "USDT transfer failed");

        emit USDTWithdrawn(msg.sender, amount);
    }

    // Function to allow users to check if tokens have been minted
    function checkTokenMinted(uint256 tokenId) external view returns (bool) {
        return mintedTokens[tokenId];
    }
}
Leave a Comment