Contract Source Code:
File 1 of 1 : PreSale
/**
*Submitted for verification at BscScan.com on 2021-05-09
*/
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
/*
* @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 GSN 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 memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
}
pragma solidity 0.8.0;
/**
* @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 () {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == _msgSender(), "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 {
emit OwnershipTransferred(_owner, address(0));
_owner = 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");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
pragma solidity 0.8.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryDiv}.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
//--------------------------CROWD SALE------------------------
pragma solidity 0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract PreSale is Ownable {
using SafeMath for uint256;
IERC20 public token; // Token being sold in the presale.
// Presale parameters
uint256 public _TOTAL_SOLD_TOKEN; // Total amount of tokens sold.
uint256 public _TOTAL_DEPOSIT_BNB; // Total BNB collected.
uint256 public _RATE; // Rate of token per BNB.
uint256 public _MAX_BNB_CONTRIBUTION; // Maximum BNB contribution per wallet.
uint256 public _MIN_BNB_CONTRIBUTION; // Minimum BNB contribution.
// Vesting parameters
uint256 public initialReleasePercentage; // Percentage of tokens to be released initially.
uint256 public fullReleaseTime; // Timestamp when full release completes.
uint256 public _SOFTCAP; // Minimum amount of BNB to be raised for the presale to be considered successful.
uint256 public _HARDCAP; // Maximum amount of BNB that can be raised.
uint256 public _START_TIME; // Start time of the presale.
uint256 public _END_TIME; // End time of the presale.
uint256 public _RELEASE_TOKEN_TIME; // Time when tokens start to become available for withdrawal.
bool public isPreSaleOpen; // Flag indicating if the presale is open.
// Mapping to track blacklisted users and token purchase amounts.
mapping(address => bool) public isBlacklisted;
mapping(address => uint256) public totalPurchased;
mapping(address => uint256) public withdrawnTokens;
mapping(address => bool) public withdrawn; // Tracks whether a user has withdrawn their initial tokens.
constructor() {}
// Allows the owner to set the address of the token being sold.
function setTokenAddress(address _tokenAddress) public onlyOwner {
require(_tokenAddress != address(0), "Invalid token address");
token = IERC20(_tokenAddress);
}
// Allows owner to set the vesting schedule.
function setVestingSchedule(uint256 _initialReleasePercentage, uint256 _releaseTime, uint256 _fullReleaseTime) public onlyOwner {
require(_initialReleasePercentage > 0 && _initialReleasePercentage <= 100, "Invalid release percentage");
require(_releaseTime < _fullReleaseTime, "Release time must be before full release time");
initialReleasePercentage = _initialReleasePercentage;
fullReleaseTime = _fullReleaseTime;
}
// Fallback function to handle BNB deposits.
receive() external payable {
deposite();
}
// Allows the owner to withdraw collected BNB after the presale ends, provided the softcap is reached.
function ownerWithdrawBNB() public onlyOwner {
require(block.timestamp > _END_TIME, "Presale hasn't finished yet");
require(_TOTAL_DEPOSIT_BNB >= _SOFTCAP, "Presale didn't hit softcap");
payable(msg.sender).transfer(address(this).balance);
}
// Allows the owner to withdraw any unsold tokens after the presale.
function ownerWithdrawToken() public onlyOwner {
token.transfer(msg.sender, token.balanceOf(address(this)));
}
// Function to blacklist or unblacklist users.
function addToBlacklist(address user) external onlyOwner { isBlacklisted[user] = true; }
function removeFromBlacklist(address user) external onlyOwner { isBlacklisted[user] = false; }
// Setup function for the presale parameters.
function setupPresale(uint256 start, uint256 end, uint256 release, uint256 rate, uint256 softcap, uint256 hardcap, uint256 maxPerWallet, uint256 minPerWallet) public onlyOwner {
_RATE = rate;
_MAX_BNB_CONTRIBUTION = maxPerWallet;
_MIN_BNB_CONTRIBUTION = minPerWallet;
_SOFTCAP = softcap;
_HARDCAP = hardcap;
_START_TIME = start;
_END_TIME = end;
_RELEASE_TOKEN_TIME = release;
}
function setRate(uint256 rate) public onlyOwner {
_RATE = rate;
}
// Opens or closes the presale.
function openPresale(bool opened) public onlyOwner { isPreSaleOpen = opened; }
// Function for users to deposit BNB and purchase tokens during the presale.
function deposite() public payable {
require(isPreSaleOpen, "Presale is not open");
require(block.timestamp >= _START_TIME && block.timestamp <= _END_TIME, "Not in presale time");
require(msg.value >= _MIN_BNB_CONTRIBUTION && msg.value <= _MAX_BNB_CONTRIBUTION, "Invalid BNB amount");
require(!isBlacklisted[_msgSender()], "You are blacklisted");
require(_TOTAL_DEPOSIT_BNB.add(msg.value) <= _HARDCAP, "Exceed HARD CAP");
totalPurchased[msg.sender]= totalPurchased[msg.sender].add(msg.value);
_TOTAL_DEPOSIT_BNB = _TOTAL_DEPOSIT_BNB.add(msg.value);
}
// Allows users to withdraw their vested tokens.
function withdrawTokens() public {
require(block.timestamp > _RELEASE_TOKEN_TIME, "Vesting period not started");
uint256 totalTokens = totalPurchased[msg.sender].mul(_RATE);
uint256 tokensToRelease = totalTokens.mul(initialReleasePercentage).div(100);
uint256 vestedTokens = 0;
if (block.timestamp >= fullReleaseTime) {
vestedTokens = totalTokens; // All tokens are vested
} else {
uint256 vestingDuration = fullReleaseTime - _RELEASE_TOKEN_TIME;
uint256 timeSinceRelease = block.timestamp - _RELEASE_TOKEN_TIME;
uint256 additionalTokensVested = totalTokens.sub(tokensToRelease).mul(timeSinceRelease).div(vestingDuration);
vestedTokens = tokensToRelease.add(additionalTokensVested);
}
uint256 tokensToWithdraw = vestedTokens.sub(withdrawnTokens[msg.sender]);
require(tokensToWithdraw > 0, "No vested tokens available for withdrawal");
withdrawnTokens[msg.sender] = withdrawnTokens[msg.sender].add(tokensToWithdraw);
withdrawn[msg.sender] = true;
require(token.transfer(msg.sender, tokensToWithdraw), "Token transfer failed");
}
}