creation_sourcecode: /**
   *Submitted for verification at mumbai.polygonscan.com on 2024-02-07
  */
  
  pragma solidity ^0.8.0;
  
  
  /**
   *Submitted for verification at Etherscan.io on 2023-06-08
  */
  
  /**
   *Submitted for verification at polygonscan.com on 2023-06-07
  */
  
  /**
   *Submitted for verification at polygonscan.com on 2023-05-25
  */
  
  // SPDX-License-Identifier: MIT
  
  // pragma solidity 0.8.19;
  
  interface IERC20 {
  
      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 `recipient`.
       *
       * Returns a boolean value indicating whether the operation succeeded.
       *
       * Emits a {Transfer} event.
       */
      function transfer(address recipient, 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 `sender` to `recipient` 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 sender, address recipient, uint256 amount) external returns (bool);
  
      /**
       * @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 Provides information about the current execution context, including the
   * sender of the transaction and its data. While these are generally available
   * via msg.sender and msg.data, they should not be accessed in such a direct
   * manner, since when dealing with meta-transactions the account sending and
   * paying for execution may not be the actual sender (as far as an application
   * is concerned).
   *
   * This contract is only required for intermediate, library-like contracts.
   */
  abstract contract Context {
      function _msgSender() internal view virtual returns (address) {
          return msg.sender;
      }
  
      function _msgData() internal view virtual returns (bytes calldata) {
          return msg.data;
      }
  }
  
  
  /**
   * @dev Contract module which provides a basic access control mechanism, where
   * there is an account (an owner) that can be granted exclusive access to
   * specific functions.
   *
   * By default, the owner account will be the one that deploys the contract. This
   * can later be changed with {transferOwnership}.
   *
   * This module is used through inheritance. It will make available the modifier
   * `onlyOwner`, which can be applied to your functions to restrict their use to
   * the owner.
   */
  abstract contract Ownable is Context {
      address private _owner;
  
      event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  
      /**
       * @dev Initializes the contract setting the deployer as the initial owner.
       */
      constructor() {
          _transferOwnership(msg.sender);
      }
  
      /**
       * @dev Throws if called by any account other than the owner.
       */
      modifier onlyOwner() {
          _checkOwner();
          _;
      }
  
      /**
       * @dev Returns the address of the current owner.
       */
      function owner() public view virtual returns (address) {
          return _owner;
      }
  
      /**
       * @dev Throws if the sender is not the owner.
       */
      function _checkOwner() internal view virtual {
          require(owner() == msg.sender, "Ownable: caller is not the owner");
      }
  
      /**
       * @dev Leaves the contract without owner. It will not be possible to call
       * `onlyOwner` functions anymore. Can only be called by the current owner.
       *
       * NOTE: Renouncing ownership will leave the contract without an owner,
       * thereby removing any functionality that is only available to the owner.
       */
      function renounceOwnership() public virtual onlyOwner {
          _transferOwnership(address(0));
      }
  
      /**
       * @dev Transfers ownership of the contract to a new account (`newOwner`).
       * Can only be called by the current owner.
       */
      function transferOwnership(address newOwner) public virtual onlyOwner {
          require(newOwner != address(0), "Ownable: new owner is the zero address");
          _transferOwnership(newOwner);
      }
  
      /**
       * @dev Transfers ownership of the contract to a new account (`newOwner`).
       * Internal function without access restriction.
       */
      function _transferOwnership(address newOwner) internal virtual {
          address oldOwner = _owner;
          _owner = newOwner;
          emit OwnershipTransferred(oldOwner, newOwner);
      }
  }
  
  interface IUniswapV2Factory {
      event PairCreated(address indexed token0, address indexed token1, address pair, uint);
  
      function feeTo() external view returns (address);
      function feeToSetter() external view returns (address);
  
      function getPair(address tokenA, address tokenB) external view returns (address pair);
      function allPairs(uint) external view returns (address pair);
      function allPairsLength() external view returns (uint);
  
      function createPair(address tokenA, address tokenB) external returns (address pair);
  
      function setFeeTo(address) external;
      function setFeeToSetter(address) external;
  }
  
  interface IUniswapV2Router01 {
      function factory() external pure returns (address);
      function WETH() external pure returns (address);
  
      function addLiquidity(
          address tokenA,
          address tokenB,
          uint amountADesired,
          uint amountBDesired,
          uint amountAMin,
          uint amountBMin,
          address to,
          uint deadline
      ) external returns (uint amountA, uint amountB, uint liquidity);
      function addLiquidityETH(
          address token,
          uint amountTokenDesired,
          uint amountTokenMin,
          uint amountETHMin,
          address to,
          uint deadline
      ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
      function removeLiquidity(
          address tokenA,
          address tokenB,
          uint liquidity,
          uint amountAMin,
          uint amountBMin,
          address to,
          uint deadline
      ) external returns (uint amountA, uint amountB);
      function removeLiquidityETH(
          address token,
          uint liquidity,
          uint amountTokenMin,
          uint amountETHMin,
          address to,
          uint deadline
      ) external returns (uint amountToken, uint amountETH);
      function removeLiquidityWithPermit(
          address tokenA,
          address tokenB,
          uint liquidity,
          uint amountAMin,
          uint amountBMin,
          address to,
          uint deadline,
          bool approveMax, uint8 v, bytes32 r, bytes32 s
      ) external returns (uint amountA, uint amountB);
      function removeLiquidityETHWithPermit(
          address token,
          uint liquidity,
          uint amountTokenMin,
          uint amountETHMin,
          address to,
          uint deadline,
          bool approveMax, uint8 v, bytes32 r, bytes32 s
      ) external returns (uint amountToken, uint amountETH);
      function swapExactTokensForTokens(
          uint amountIn,
          uint amountOutMin,
          address[] calldata path,
          address to,
          uint deadline
      ) external returns (uint[] memory amounts);
      function swapTokensForExactTokens(
          uint amountOut,
          uint amountInMax,
          address[] calldata path,
          address to,
          uint deadline
      ) external returns (uint[] memory amounts);
      function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
          external
          payable
          returns (uint[] memory amounts);
      function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
          external
          returns (uint[] memory amounts);
      function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
          external
          returns (uint[] memory amounts);
      function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
          external
          payable
          returns (uint[] memory amounts);
  
      function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
      function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
      function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
      function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
      function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
  }
  
  interface IUniswapV2Router02 is IUniswapV2Router01 {
      function removeLiquidityETHSupportingFeeOnTransferTokens(
          address token,
          uint liquidity,
          uint amountTokenMin,
          uint amountETHMin,
          address to,
          uint deadline
      ) external returns (uint amountETH);
      function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
          address token,
          uint liquidity,
          uint amountTokenMin,
          uint amountETHMin,
          address to,
          uint deadline,
          bool approveMax, uint8 v, bytes32 r, bytes32 s
      ) external returns (uint amountETH);
  
      function swapExactTokensForTokensSupportingFeeOnTransferTokens(
          uint amountIn,
          uint amountOutMin,
          address[] calldata path,
          address to,
          uint deadline
      ) external;
      function swapExactETHForTokensSupportingFeeOnTransferTokens(
          uint amountOutMin,
          address[] calldata path,
          address to,
          uint deadline
      ) external payable;
      function swapExactTokensForETHSupportingFeeOnTransferTokens(
          uint amountIn,
          uint amountOutMin,
          address[] calldata path,
          address to,
          uint deadline
      ) external;
  }
  
  /**
   * @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;
      }
  }
  
  pragma solidity ^0.8.0;
  
  // Import ERC20 Interface for handling tokens
  interface ERC20 {
      function transfer(address recipient, uint256 amount) external returns (bool);
      function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
      function balanceOf(address account) external view returns (uint256);
  }
  
  contract PrivateSale {
      // Mapping to store deposited amounts per user
      mapping(address => uint256) public deposits;
      mapping(address => uint256) public allocations;
      
      // Fixed token price and allocation per stablecoin
      uint256 public TOKEN_PRICE ; // Token price in stablecoin
     
      
      // Addresses of supported stablecoins
      address public constant USDC_ADDRESS = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; // Replace with actual USDC contract address
      address public constant USDT_ADDRESS = 0xdAC17F958D2ee523a2206206994597C13D831ec7; // Replace with actual USDT contract address
   
      // Token contract address
      address public tokenAddress; // Replace with actual token contract address
      
      // Sale status
      bool public saleActive;
      
      // Event to log successful deposits
      event Deposit(address indexed user, uint256 amount);
      event SaleStatusChanged(bool saleActive);
      
      // Minimum and maximum buying amounts
      uint256 public minBuyAmount;
      uint256 public maxBuyAmount;
      
      modifier onlyOwner {
          require(msg.sender == owner, "Only owner can perform this action");
          _;
      }
      
      modifier saleIsOpen {
          require(saleActive, "Sale is not active");
          _;
      }
      
      address  public  owner;
      
      constructor(address _tokenAddress, uint256 _minBuyAmount, uint256 _maxBuyAmount, uint256 _tokenPrice) {
          tokenAddress = _tokenAddress;
          owner = payable (msg.sender);
          TOKEN_PRICE = _tokenPrice * 10 ** 6;
          minBuyAmount = _minBuyAmount * 10 ** 6;
          maxBuyAmount = _maxBuyAmount * 10 ** 6;
      }
      
      // Function to start the private sale
      function startSale() external onlyOwner {
          require(!saleActive, "Sale is already active");
          saleActive = true;
          emit SaleStatusChanged(true);
      }
      
      // Function to end the private sale
      function endSale() external onlyOwner {
          require(saleActive, "Sale is already ended");
          saleActive = false;
          emit SaleStatusChanged(false);
      }
      
      // Function to deposit stablecoins and register the transaction
      function deposit(uint256 _amount, address stablecoin) external payable  saleIsOpen {
          uint256 amount = _amount * 10 ** 6;
          require(stablecoin == USDT_ADDRESS || stablecoin == USDC_ADDRESS , "Unsupported stablecoin");
          require(amount >= minBuyAmount && amount <= maxBuyAmount, "Invalid deposit amount");
          
          // Transfer stablecoins from user to contract
          require(ERC20(stablecoin).transferFrom (msg.sender , address(this), amount), "Transfer failed");
          
          // Calculate tokens based on deposited amount and fixed token price
          uint256 tokensToAllocate = amount / TOKEN_PRICE;
          
          // Update user's deposit and allocated tokens
          deposits[msg.sender] += amount;
          allocations[msg.sender] += tokensToAllocate;
          
          // Emit deposit event
          emit Deposit(msg.sender, amount);
      }
      
      // Function to retrieve allocated tokens for a given user
      function getAllocatedTokens(address user) external view returns (uint256) {
          return allocations[user];
      }
      
      // Function to retrieve all wallet addresses and their respective allocated tokens and deposits
      // function getAllAllocations() external view returns (address[] memory, uint256[] memory, uint256[] memory) {
      //     uint256 totalUsers = 0;
      //     for (uint256 i = 0; i < allocations.length; i++) {
      //         if (allocations[i] > 0) {
      //             totalUsers++;
      //         }
      //     }
          
      //     address[] memory addresses = new address[](totalUsers);
      //     uint256[] memory tokens = new uint256[](totalUsers);
      //     uint256[] memory depositsArr = new uint256[](totalUsers);
          
      //     uint256 index = 0;
      //     for (uint256 i = 0; i < allocations.length; i++) {
      //         if (allocations[i] > 0) {
      //             addresses[index] = i;
      //             tokens[index] = allocations[i];
      //             depositsArr[index] = deposits[i];
      //             index++;
      //         }
      //     }
          
      //     return (addresses, tokens, depositsArr);
      // }
      
      // Function to withdraw tokens after the sale
     function withdrawTokens() external payable  onlyOwner {
          require(!saleActive, "Sale is still active");
          uint USDCToTransfer = ERC20(USDC_ADDRESS).balanceOf(address(this));
          uint USDTToTransfer = ERC20(USDT_ADDRESS).balanceOf(address(this));
         
          
  
          require(ERC20(USDC_ADDRESS).transfer(owner, USDCToTransfer), "Transfer unsuccessful");
          require(ERC20(USDT_ADDRESS).transfer(owner, USDTToTransfer), "Transfer unsuccessful");
        
         
          // uint256 tokensToTransfer = allocations[msg.sender];
        
          
          // // Transfer tokens to user
          // require(ERC20(tokenAddress).transfer(msg.sender, tokensToTransfer), "Transfer failed");
          
          // // Reset user's deposit and allocated tokens to zero after withdrawal
          // deposits[msg.sender] = 0;
          // allocations[msg.sender] = 0;
      }
      
      // Function to set minimum and maximum buying amounts
      function setMinMaxBuyAmount(uint256 _minBuyAmount, uint256 _maxBuyAmount) external onlyOwner {
          require(_minBuyAmount <= _maxBuyAmount, "Invalid amounts");
          minBuyAmount = _minBuyAmount;
          maxBuyAmount = _maxBuyAmount;
      }
      
      // Function to transfer ownership
      function transferOwnership(address _newOwner) external onlyOwner {
          require(_newOwner != address(0), "Invalid address");
          owner = _newOwner;
      }
  }