// File: PublicSale.sol



// SPDX-License-Identifier: MIT
//https://www.boundfinance.co.uk/
//https://boundfinance.app/
//Twitter - @BoundFinance
//Discord - https://discord.com/invite/kBDEWndd7m
//Public sale Contract for BCKGOV Tokens


pragma solidity ^0.8.19;

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";


interface IERC20Token {
    function balanceOf(address owner) external returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function decimals() external returns (uint8);
    function transferFrom(address user1, address user2, uint256 amount) external returns (bool);
}

contract TokenSale is ReentrancyGuard {
    IERC20Token public tokenContract;  // the token being sold
    uint256 public price;              // the price, in wei, per token
    address public owner;
    uint256 public tokensSold;

    event Sold(address indexed buyer, uint256 amount);
    event BoughtBack(address indexed seller, uint256 amount);
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    modifier onlyOwner() {
        require(msg.sender == owner, "You are not the owner");
        _;
    }


    constructor(IERC20Token _tokenContract, uint256 _price) {
        owner = msg.sender;
        tokenContract = _tokenContract;
        price = _price;
    }

    function buyTokens(uint256 numberOfTokens) public payable nonReentrant {
        require(msg.value == ((numberOfTokens * price) / 1E18), "Mismatched value sent");

        require(tokenContract.balanceOf(address(this)) >= numberOfTokens, "Insufficient tokens");

        tokensSold += numberOfTokens;

        emit Sold(msg.sender, numberOfTokens);

        require(tokenContract.transfer(msg.sender, numberOfTokens), "Token transfer failed");
    }

    function sellTokens(uint256 numberOfTokens) public nonReentrant {

        require(tokenContract.balanceOf(msg.sender) >= numberOfTokens, "Insufficient tokens to sell");

        require(address(this).balance >= ((numberOfTokens * price) / 1E18), "Insufficient ETH in contract");

        tokensSold -= numberOfTokens;

        emit BoughtBack(msg.sender, numberOfTokens);

        require(tokenContract.transferFrom(msg.sender, address(this), numberOfTokens), "Token transfer failed");

        payable(msg.sender).transfer(((numberOfTokens * price) / 1E18));
    }

    function endSale() public onlyOwner nonReentrant {
        // Send unsold tokens to the owner.
        require(tokenContract.transfer(owner, tokenContract.balanceOf(address(this))), "Transfer failed");

        // Transfer the balance to the owner
        payable(owner).transfer(address(this).balance);
    }

    receive() external payable {
    
    }

    function transferOwnership(address newOwner) public onlyOwner  {
        require(newOwner != address(0), "New owner is the zero address");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// 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;
    }
}