// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
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;
}
}
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
abstract contract Ownable is Context {
using SafeMath for uint256;
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @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() == _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 {
_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);
}
}
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) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
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) {
unchecked {
// 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) {
unchecked {
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) {
unchecked {
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) {
return a + b;
}
/**
* @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) {
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) {
return a * b;
}
/**
* @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.
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
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) {
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) {
unchecked {
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.
*
* 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) {
unchecked {
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) {
unchecked {
require(b > 0, errorMessage);
return a % b;
}
}
}
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external;
function allowance(address owner, address spender) external view returns (uint256);
function decimals() external view returns (uint256);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external;
}
contract TokenLocker is ReentrancyGuard, Ownable {
using SafeMath for uint256;
uint256 private lockId = 0;
uint256 private refferalId = 0;
uint256 private adminFeePercentage = 3;
struct lockStruct {
address user;
uint256 lockAmount;
uint256 amountToReturn;
uint256 startTime;
uint256 endTime;
uint256 multipler;
uint256 divider;
bool isLocked;
address token;
}
struct refferalStruct {
uint256 lockId;
address refferedUser;
uint256 rewardAmount;
address token;
}
struct UserStruct {
address referralAddress;
uint256[] lockIds;
uint256[] refferalIds;
bool isExist;
}
struct TokenDetails {
address token;
uint256 decimal;
uint256 tokenLockedAmount;
uint256 tokenUnlockedAmount;
}
mapping(uint256 => lockStruct) private lockes;
mapping(address => UserStruct) private _userDetails;
mapping(uint256 => refferalStruct) public refferDetails;
mapping(address => bool) public isTokenLocked;
address[] private lockedTokenAddresses;
mapping(address => TokenDetails) private tokenDetails;
event Lock(uint256 lockId, address user, address token, uint256 amount, uint256 endTime);
event Unlock(uint256 lockId, address user, uint256 amount);
function lock(
uint256 amount,
uint256 endTime,
address _token,
address _referralAddress,
uint256 _multipler,
uint256 _divider
) external nonReentrant {
require(amount > 0, "Amount must be greater than zero");
require(msg.sender != _referralAddress, "referral address cannot be same");
uint256 lockAmount = amount;
require(endTime > block.timestamp);
uint256 duration = endTime - block.timestamp;
require(duration >= 7 days && duration <= 20 * 365 days, "Invalid duration");
if (_userDetails[msg.sender].isExist) {
_referralAddress = _userDetails[msg.sender].referralAddress;
} else {
_userDetails[msg.sender].isExist = true;
_userDetails[msg.sender].referralAddress = _referralAddress;
}
IERC20 token = IERC20(_token);
require(token.balanceOf(msg.sender) >= amount, "Insufficient balance");
require(token.allowance(msg.sender, address(this)) >= amount, "Insufficient allowance");
uint256 decimal = token.decimals();
token.transferFrom(msg.sender, address(this), amount);
uint256 adminFee = amount.mul(adminFeePercentage).div(100);
amount = amount.sub(adminFee);
if (_referralAddress != address(0)) {
adminFee = adminFee.div(2);
token.transfer(_referralAddress, adminFee);
_userDetails[_referralAddress].refferalIds.push(refferalId);
refferDetails[refferalId] = refferalStruct({lockId: lockId, refferedUser: msg.sender, rewardAmount: adminFee, token: _token});
refferalId++;
}
token.transfer(owner(), adminFee);
_userDetails[msg.sender].lockIds.push(lockId);
lockes[lockId] = lockStruct({user: msg.sender, amountToReturn: amount, lockAmount: lockAmount, startTime: block.timestamp, endTime: endTime, isLocked: true, token: _token, multipler: _multipler, divider: _divider});
emit Lock(lockId, msg.sender, _token, amount, endTime);
lockId++;
if (!isTokenLocked[_token]) {
isTokenLocked[_token] = true;
lockedTokenAddresses.push(_token);
tokenDetails[_token].token = _token;
tokenDetails[_token].decimal = decimal;
}
tokenDetails[_token].tokenLockedAmount += lockAmount;
}
function getCurrentLockId() public view returns (uint256) {
return lockId;
}
function getUserStakeIds(address _user) public view returns (uint256[] memory) {
return _userDetails[_user].lockIds;
}
function getUserDetails(address _user) public view returns (UserStruct memory) {
return _userDetails[_user];
}
function getLockDetails(uint256 _lockId) public view returns (lockStruct memory lockDetails_, uint256 decimal_) {
lockDetails_ = lockes[_lockId];
decimal_ = tokenDetails[lockDetails_.token].decimal;
return (lockDetails_, decimal_);
}
function getLockedTokensAddresses() public view returns (address[] memory) {
return lockedTokenAddresses;
}
function getLockedTokenDetails() public view returns (TokenDetails[] memory) {
uint256 length = lockedTokenAddresses.length;
TokenDetails[] memory details = new TokenDetails[](length);
for (uint256 i = 0; i < length; i++) {
details[i] = tokenDetails[lockedTokenAddresses[i]];
}
return details;
}
function getAdminFeePercentage() public view returns (uint256) {
return adminFeePercentage;
}
function getCurrentRefferalId() public view returns (uint256) {
return refferalId;
}
function setAdminFeePercentage(uint256 _adminFeePercentage) public onlyOwner {
adminFeePercentage = _adminFeePercentage;
}
function unlock(uint256 _lockId) external nonReentrant {
lockStruct storage lockData = lockes[_lockId];
require(lockData.user == msg.sender, "Not the owner of the lock");
require(lockData.isLocked, "Lock is not active");
uint256 amountToReturn = lockData.amountToReturn;
IERC20 token = IERC20(lockData.token);
if (lockData.endTime > block.timestamp) {
amountToReturn = amountToReturn.div(2);
}
token.transfer(msg.sender, amountToReturn);
lockData.isLocked = false;
emit Unlock(_lockId, msg.sender, amountToReturn);
tokenDetails[lockData.token].tokenUnlockedAmount += lockData.lockAmount;
}
}
{
"compilationTarget": {
"TokenLocker.sol": "TokenLocker"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"lockId","type":"uint256"},{"indexed":false,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"endTime","type":"uint256"}],"name":"Lock","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"lockId","type":"uint256"},{"indexed":false,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Unlock","type":"event"},{"inputs":[],"name":"getAdminFeePercentage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentLockId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentRefferalId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_lockId","type":"uint256"}],"name":"getLockDetails","outputs":[{"components":[{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"lockAmount","type":"uint256"},{"internalType":"uint256","name":"amountToReturn","type":"uint256"},{"internalType":"uint256","name":"startTime","type":"uint256"},{"internalType":"uint256","name":"endTime","type":"uint256"},{"internalType":"uint256","name":"multipler","type":"uint256"},{"internalType":"uint256","name":"divider","type":"uint256"},{"internalType":"bool","name":"isLocked","type":"bool"},{"internalType":"address","name":"token","type":"address"}],"internalType":"struct TokenLocker.lockStruct","name":"lockDetails_","type":"tuple"},{"internalType":"uint256","name":"decimal_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLockedTokenDetails","outputs":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"decimal","type":"uint256"},{"internalType":"uint256","name":"tokenLockedAmount","type":"uint256"},{"internalType":"uint256","name":"tokenUnlockedAmount","type":"uint256"}],"internalType":"struct TokenLocker.TokenDetails[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLockedTokensAddresses","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getUserDetails","outputs":[{"components":[{"internalType":"address","name":"referralAddress","type":"address"},{"internalType":"uint256[]","name":"lockIds","type":"uint256[]"},{"internalType":"uint256[]","name":"refferalIds","type":"uint256[]"},{"internalType":"bool","name":"isExist","type":"bool"}],"internalType":"struct TokenLocker.UserStruct","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getUserStakeIds","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isTokenLocked","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"endTime","type":"uint256"},{"internalType":"address","name":"_token","type":"address"},{"internalType":"address","name":"_referralAddress","type":"address"},{"internalType":"uint256","name":"_multipler","type":"uint256"},{"internalType":"uint256","name":"_divider","type":"uint256"}],"name":"lock","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"refferDetails","outputs":[{"internalType":"uint256","name":"lockId","type":"uint256"},{"internalType":"address","name":"refferedUser","type":"address"},{"internalType":"uint256","name":"rewardAmount","type":"uint256"},{"internalType":"address","name":"token","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_adminFeePercentage","type":"uint256"}],"name":"setAdminFeePercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_lockId","type":"uint256"}],"name":"unlock","outputs":[],"stateMutability":"nonpayable","type":"function"}]