├── utils ├── Context.sol ├── draft-IERC20Permit.sol ├── ReentrancyGuard.sol ├── Ownable.sol ├── Pausable.sol ├── IERC20.sol ├── Referral.sol ├── SafeERC20.sol ├── SafeMath.sol └── Address.sol ├── StakingVault.sol └── MagicAI.sol /utils/Context.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts v4.4.1 (utils/Context.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | /** 7 | * @dev Provides information about the current execution context, including the 8 | * sender of the transaction and its data. While these are generally available 9 | * via msg.sender and msg.data, they should not be accessed in such a direct 10 | * manner, since when dealing with meta-transactions the account sending and 11 | * paying for execution may not be the actual sender (as far as an application 12 | * is concerned). 13 | * 14 | * This contract is only required for intermediate, library-like contracts. 15 | */ 16 | abstract contract Context { 17 | function _msgSender() internal view virtual returns (address) { 18 | return msg.sender; 19 | } 20 | 21 | function _msgData() internal view virtual returns (bytes calldata) { 22 | return msg.data; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /utils/draft-IERC20Permit.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-IERC20Permit.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | /** 7 | * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in 8 | * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612]. 9 | * 10 | * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by 11 | * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't 12 | * need to send a transaction, and thus is not required to hold Ether at all. 13 | */ 14 | interface IERC20Permit { 15 | /** 16 | * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens, 17 | * given ``owner``'s signed approval. 18 | * 19 | * IMPORTANT: The same issues {IERC20-approve} has related to transaction 20 | * ordering also apply here. 21 | * 22 | * Emits an {Approval} event. 23 | * 24 | * Requirements: 25 | * 26 | * - `spender` cannot be the zero address. 27 | * - `deadline` must be a timestamp in the future. 28 | * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner` 29 | * over the EIP712-formatted function arguments. 30 | * - the signature must use ``owner``'s current nonce (see {nonces}). 31 | * 32 | * For more information on the signature format, see the 33 | * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP 34 | * section]. 35 | */ 36 | function permit( 37 | address owner, 38 | address spender, 39 | uint256 value, 40 | uint256 deadline, 41 | uint8 v, 42 | bytes32 r, 43 | bytes32 s 44 | ) external; 45 | 46 | /** 47 | * @dev Returns the current nonce for `owner`. This value must be 48 | * included whenever a signature is generated for {permit}. 49 | * 50 | * Every successful call to {permit} increases ``owner``'s nonce by one. This 51 | * prevents a signature from being used multiple times. 52 | */ 53 | function nonces(address owner) external view returns (uint256); 54 | 55 | /** 56 | * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}. 57 | */ 58 | // solhint-disable-next-line func-name-mixedcase 59 | function DOMAIN_SEPARATOR() external view returns (bytes32); 60 | } 61 | -------------------------------------------------------------------------------- /utils/ReentrancyGuard.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | /** 7 | * @dev Contract module that helps prevent reentrant calls to a function. 8 | * 9 | * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier 10 | * available, which can be applied to functions to make sure there are no nested 11 | * (reentrant) calls to them. 12 | * 13 | * Note that because there is a single `nonReentrant` guard, functions marked as 14 | * `nonReentrant` may not call one another. This can be worked around by making 15 | * those functions `private`, and then adding `external` `nonReentrant` entry 16 | * points to them. 17 | * 18 | * TIP: If you would like to learn more about reentrancy and alternative ways 19 | * to protect against it, check out our blog post 20 | * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. 21 | */ 22 | abstract contract ReentrancyGuard { 23 | // Booleans are more expensive than uint256 or any type that takes up a full 24 | // word because each write operation emits an extra SLOAD to first read the 25 | // slot's contents, replace the bits taken up by the boolean, and then write 26 | // back. This is the compiler's defense against contract upgrades and 27 | // pointer aliasing, and it cannot be disabled. 28 | 29 | // The values being non-zero value makes deployment a bit more expensive, 30 | // but in exchange the refund on every call to nonReentrant will be lower in 31 | // amount. Since refunds are capped to a percentage of the total 32 | // transaction's gas, it is best to keep them low in cases like this one, to 33 | // increase the likelihood of the full refund coming into effect. 34 | uint256 private constant _NOT_ENTERED = 1; 35 | uint256 private constant _ENTERED = 2; 36 | 37 | uint256 private _status; 38 | 39 | constructor() { 40 | _status = _NOT_ENTERED; 41 | } 42 | 43 | /** 44 | * @dev Prevents a contract from calling itself, directly or indirectly. 45 | * Calling a `nonReentrant` function from another `nonReentrant` 46 | * function is not supported. It is possible to prevent this from happening 47 | * by making the `nonReentrant` function external, and making it call a 48 | * `private` function that does the actual work. 49 | */ 50 | modifier nonReentrant() { 51 | // On the first call to nonReentrant, _notEntered will be true 52 | require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); 53 | 54 | // Any calls to nonReentrant after this point will fail 55 | _status = _ENTERED; 56 | 57 | _; 58 | 59 | // By storing the original value once again, a refund is triggered (see 60 | // https://eips.ethereum.org/EIPS/eip-2200) 61 | _status = _NOT_ENTERED; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /utils/Ownable.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | import "./Context.sol"; 7 | 8 | /** 9 | * @dev Contract module which provides a basic access control mechanism, where 10 | * there is an account (an owner) that can be granted exclusive access to 11 | * specific functions. 12 | * 13 | * By default, the owner account will be the one that deploys the contract. This 14 | * can later be changed with {transferOwnership}. 15 | * 16 | * This module is used through inheritance. It will make available the modifier 17 | * `onlyOwner`, which can be applied to your functions to restrict their use to 18 | * the owner. 19 | */ 20 | abstract contract Ownable is Context { 21 | address private _owner; 22 | 23 | event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); 24 | 25 | /** 26 | * @dev Initializes the contract setting the deployer as the initial owner. 27 | */ 28 | constructor() { 29 | _transferOwnership(_msgSender()); 30 | } 31 | 32 | /** 33 | * @dev Throws if called by any account other than the owner. 34 | */ 35 | modifier onlyOwner() { 36 | _checkOwner(); 37 | _; 38 | } 39 | 40 | /** 41 | * @dev Returns the address of the current owner. 42 | */ 43 | function owner() public view virtual returns (address) { 44 | return _owner; 45 | } 46 | 47 | /** 48 | * @dev Throws if the sender is not the owner. 49 | */ 50 | function _checkOwner() internal view virtual { 51 | require(owner() == _msgSender(), "Ownable: caller is not the owner"); 52 | } 53 | 54 | /** 55 | * @dev Leaves the contract without owner. It will not be possible to call 56 | * `onlyOwner` functions anymore. Can only be called by the current owner. 57 | * 58 | * NOTE: Renouncing ownership will leave the contract without an owner, 59 | * thereby removing any functionality that is only available to the owner. 60 | */ 61 | function renounceOwnership() public virtual onlyOwner { 62 | _transferOwnership(address(0)); 63 | } 64 | 65 | /** 66 | * @dev Transfers ownership of the contract to a new account (`newOwner`). 67 | * Can only be called by the current owner. 68 | */ 69 | function transferOwnership(address newOwner) public virtual onlyOwner { 70 | require(newOwner != address(0), "Ownable: new owner is the zero address"); 71 | _transferOwnership(newOwner); 72 | } 73 | 74 | /** 75 | * @dev Transfers ownership of the contract to a new account (`newOwner`). 76 | * Internal function without access restriction. 77 | */ 78 | function _transferOwnership(address newOwner) internal virtual { 79 | address oldOwner = _owner; 80 | _owner = newOwner; 81 | emit OwnershipTransferred(oldOwner, newOwner); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /utils/Pausable.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | import "./Context.sol"; 7 | 8 | /** 9 | * @dev Contract module which allows children to implement an emergency stop 10 | * mechanism that can be triggered by an authorized account. 11 | * 12 | * This module is used through inheritance. It will make available the 13 | * modifiers `whenNotPaused` and `whenPaused`, which can be applied to 14 | * the functions of your contract. Note that they will not be pausable by 15 | * simply including this module, only once the modifiers are put in place. 16 | */ 17 | abstract contract Pausable is Context { 18 | /** 19 | * @dev Emitted when the pause is triggered by `account`. 20 | */ 21 | event Paused(address account); 22 | 23 | /** 24 | * @dev Emitted when the pause is lifted by `account`. 25 | */ 26 | event Unpaused(address account); 27 | 28 | bool private _paused; 29 | 30 | /** 31 | * @dev Initializes the contract in unpaused state. 32 | */ 33 | constructor() { 34 | _paused = false; 35 | } 36 | 37 | /** 38 | * @dev Modifier to make a function callable only when the contract is not paused. 39 | * 40 | * Requirements: 41 | * 42 | * - The contract must not be paused. 43 | */ 44 | modifier whenNotPaused() { 45 | _requireNotPaused(); 46 | _; 47 | } 48 | 49 | /** 50 | * @dev Modifier to make a function callable only when the contract is paused. 51 | * 52 | * Requirements: 53 | * 54 | * - The contract must be paused. 55 | */ 56 | modifier whenPaused() { 57 | _requirePaused(); 58 | _; 59 | } 60 | 61 | /** 62 | * @dev Returns true if the contract is paused, and false otherwise. 63 | */ 64 | function paused() public view virtual returns (bool) { 65 | return _paused; 66 | } 67 | 68 | /** 69 | * @dev Throws if the contract is paused. 70 | */ 71 | function _requireNotPaused() internal view virtual { 72 | require(!paused(), "Pausable: paused"); 73 | } 74 | 75 | /** 76 | * @dev Throws if the contract is not paused. 77 | */ 78 | function _requirePaused() internal view virtual { 79 | require(paused(), "Pausable: not paused"); 80 | } 81 | 82 | /** 83 | * @dev Triggers stopped state. 84 | * 85 | * Requirements: 86 | * 87 | * - The contract must not be paused. 88 | */ 89 | function _pause() internal virtual whenNotPaused { 90 | _paused = true; 91 | emit Paused(_msgSender()); 92 | } 93 | 94 | /** 95 | * @dev Returns to normal state. 96 | * 97 | * Requirements: 98 | * 99 | * - The contract must be paused. 100 | */ 101 | function _unpause() internal virtual whenPaused { 102 | _paused = false; 103 | emit Unpaused(_msgSender()); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /utils/IERC20.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | /** 7 | * @dev Interface of the ERC20 standard as defined in the EIP. 8 | */ 9 | interface IERC20 { 10 | /** 11 | * @dev Emitted when `value` tokens are moved from one account (`from`) to 12 | * another (`to`). 13 | * 14 | * Note that `value` may be zero. 15 | */ 16 | event Transfer(address indexed from, address indexed to, uint256 value); 17 | 18 | /** 19 | * @dev Emitted when the allowance of a `spender` for an `owner` is set by 20 | * a call to {approve}. `value` is the new allowance. 21 | */ 22 | event Approval( 23 | address indexed owner, 24 | address indexed spender, 25 | uint256 value 26 | ); 27 | 28 | /** 29 | * @dev Returns the amount of tokens in existence. 30 | */ 31 | function totalSupply() external view returns (uint256); 32 | 33 | /** 34 | * @dev Returns the amount of tokens owned by `account`. 35 | */ 36 | function balanceOf(address account) external view returns (uint256); 37 | 38 | /** 39 | * @dev Moves `amount` tokens from the caller's account to `to`. 40 | * 41 | * Returns a boolean value indicating whether the operation succeeded. 42 | * 43 | * Emits a {Transfer} event. 44 | */ 45 | function transfer(address to, uint256 amount) external returns (bool); 46 | 47 | /** 48 | * @dev Returns the remaining number of tokens that `spender` will be 49 | * allowed to spend on behalf of `owner` through {transferFrom}. This is 50 | * zero by default. 51 | * 52 | * This value changes when {approve} or {transferFrom} are called. 53 | */ 54 | function allowance(address owner, address spender) 55 | external 56 | view 57 | returns (uint256); 58 | 59 | /** 60 | * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. 61 | * 62 | * Returns a boolean value indicating whether the operation succeeded. 63 | * 64 | * IMPORTANT: Beware that changing an allowance with this method brings the risk 65 | * that someone may use both the old and the new allowance by unfortunate 66 | * transaction ordering. One possible solution to mitigate this race 67 | * condition is to first reduce the spender's allowance to 0 and set the 68 | * desired value afterwards: 69 | * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 70 | * 71 | * Emits an {Approval} event. 72 | */ 73 | function approve(address spender, uint256 amount) external returns (bool); 74 | 75 | /** 76 | * @dev Moves `amount` tokens from `from` to `to` using the 77 | * allowance mechanism. `amount` is then deducted from the caller's 78 | * allowance. 79 | * 80 | * Returns a boolean value indicating whether the operation succeeded. 81 | * 82 | * Emits a {Transfer} event. 83 | */ 84 | function transferFrom( 85 | address from, 86 | address to, 87 | uint256 amount 88 | ) external returns (bool); 89 | } 90 | -------------------------------------------------------------------------------- /utils/Referral.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity 0.8.4; 4 | 5 | import "./SafeERC20.sol"; 6 | import "./Ownable.sol"; 7 | 8 | contract Referral is Ownable { 9 | using SafeERC20 for IERC20; 10 | 11 | //Max min withdraw amount : 100 Tokens 12 | uint256 public constant MAX_MIN_AMOUNT = 100 * 10**9; // Token Decimals: 9 13 | 14 | IERC20 public immutable token; 15 | 16 | //Minimum commision withdraw amount(default: 0.3 Tokens) 17 | uint256 public minWithdraw = 3 * 10**(9 - 1); // Token Decimals: 9 18 | 19 | mapping(address => bool) public operators; 20 | mapping(address => address) public referrers; // user address => referrer address 21 | mapping(address => uint256) public referralsCount; // referrer address => referrals count 22 | mapping(address => uint256) public totalReferralCommissions; // referrer address => total referral commissions 23 | mapping(address => uint256) public pendingReferralCommissions; // referrer address => pending referral commissions 24 | 25 | event ReferralRecorded(address indexed user, address indexed referrer); 26 | event ReferralCommissionRecorded( 27 | address indexed referrer, 28 | uint256 commission 29 | ); 30 | event OperatorUpdated(address indexed operator, bool indexed status); 31 | event ReferralRewardWithdraw(address indexed user, uint256 rewardAmount); 32 | 33 | constructor(address _token) { 34 | token = IERC20(_token); 35 | } 36 | 37 | modifier onlyOperator() { 38 | require(operators[msg.sender], "Operator: caller is not the operator"); 39 | _; 40 | } 41 | 42 | function recordReferral(address _user, address _referrer) 43 | public 44 | onlyOperator 45 | { 46 | if ( 47 | _user != address(0) && 48 | _referrer != address(0) && 49 | _user != _referrer && 50 | referrers[_user] == address(0) 51 | ) { 52 | referrers[_user] = _referrer; 53 | referralsCount[_referrer] += 1; 54 | emit ReferralRecorded(_user, _referrer); 55 | } 56 | } 57 | 58 | function recordReferralCommission(address _referrer, uint256 _commission) 59 | public 60 | onlyOperator 61 | { 62 | if (_referrer != address(0) && _commission > 0) { 63 | totalReferralCommissions[_referrer] += _commission; 64 | pendingReferralCommissions[_referrer] += _commission; 65 | 66 | emit ReferralCommissionRecorded(_referrer, _commission); 67 | } 68 | } 69 | 70 | function getReferrer(address _user) public view returns (address) { 71 | return referrers[_user]; 72 | } 73 | 74 | function updateOperator(address _operator, bool _status) 75 | external 76 | onlyOwner 77 | { 78 | operators[_operator] = _status; 79 | emit OperatorUpdated(_operator, _status); 80 | } 81 | 82 | function setMinCommisionWithdraw(uint256 _amount) external onlyOwner { 83 | require(_amount <= MAX_MIN_AMOUNT, "Invalid amount"); 84 | minWithdraw = _amount; 85 | } 86 | 87 | // Owner can drain tokens that are sent here by mistake 88 | function drainERC20Token( 89 | IERC20 _token, 90 | uint256 _amount, 91 | address _to 92 | ) external onlyOwner { 93 | _token.safeTransfer(_to, _amount); 94 | } 95 | 96 | function withdrawReferralReward() external { 97 | if (pendingReferralCommissions[msg.sender] >= minWithdraw) { 98 | token.safeTransfer( 99 | msg.sender, 100 | pendingReferralCommissions[msg.sender] 101 | ); 102 | 103 | emit ReferralRewardWithdraw( 104 | msg.sender, 105 | pendingReferralCommissions[msg.sender] 106 | ); 107 | 108 | pendingReferralCommissions[msg.sender] = 0; 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /utils/SafeERC20.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts (last updated v4.7.0) (token/ERC20/utils/SafeERC20.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | import "./IERC20.sol"; 7 | import "./draft-IERC20Permit.sol"; 8 | import "./Address.sol"; 9 | 10 | /** 11 | * @title SafeERC20 12 | * @dev Wrappers around ERC20 operations that throw on failure (when the token 13 | * contract returns false). Tokens that return no value (and instead revert or 14 | * throw on failure) are also supported, non-reverting calls are assumed to be 15 | * successful. 16 | * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract, 17 | * which allows you to call the safe operations as `token.safeTransfer(...)`, etc. 18 | */ 19 | library SafeERC20 { 20 | using Address for address; 21 | 22 | function safeTransfer( 23 | IERC20 token, 24 | address to, 25 | uint256 value 26 | ) internal { 27 | _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value)); 28 | } 29 | 30 | function safeTransferFrom( 31 | IERC20 token, 32 | address from, 33 | address to, 34 | uint256 value 35 | ) internal { 36 | _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value)); 37 | } 38 | 39 | /** 40 | * @dev Deprecated. This function has issues similar to the ones found in 41 | * {IERC20-approve}, and its usage is discouraged. 42 | * 43 | * Whenever possible, use {safeIncreaseAllowance} and 44 | * {safeDecreaseAllowance} instead. 45 | */ 46 | function safeApprove( 47 | IERC20 token, 48 | address spender, 49 | uint256 value 50 | ) internal { 51 | // safeApprove should only be called when setting an initial allowance, 52 | // or when resetting it to zero. To increase and decrease it, use 53 | // 'safeIncreaseAllowance' and 'safeDecreaseAllowance' 54 | require( 55 | (value == 0) || (token.allowance(address(this), spender) == 0), 56 | "SafeERC20: approve from non-zero to non-zero allowance" 57 | ); 58 | _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value)); 59 | } 60 | 61 | function safeIncreaseAllowance( 62 | IERC20 token, 63 | address spender, 64 | uint256 value 65 | ) internal { 66 | uint256 newAllowance = token.allowance(address(this), spender) + value; 67 | _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); 68 | } 69 | 70 | function safeDecreaseAllowance( 71 | IERC20 token, 72 | address spender, 73 | uint256 value 74 | ) internal { 75 | unchecked { 76 | uint256 oldAllowance = token.allowance(address(this), spender); 77 | require(oldAllowance >= value, "SafeERC20: decreased allowance below zero"); 78 | uint256 newAllowance = oldAllowance - value; 79 | _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); 80 | } 81 | } 82 | 83 | function safePermit( 84 | IERC20Permit token, 85 | address owner, 86 | address spender, 87 | uint256 value, 88 | uint256 deadline, 89 | uint8 v, 90 | bytes32 r, 91 | bytes32 s 92 | ) internal { 93 | uint256 nonceBefore = token.nonces(owner); 94 | token.permit(owner, spender, value, deadline, v, r, s); 95 | uint256 nonceAfter = token.nonces(owner); 96 | require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed"); 97 | } 98 | 99 | /** 100 | * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement 101 | * on the return value: the return value is optional (but if data is returned, it must not be false). 102 | * @param token The token targeted by the call. 103 | * @param data The call data (encoded using abi.encode or one of its variants). 104 | */ 105 | function _callOptionalReturn(IERC20 token, bytes memory data) private { 106 | // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since 107 | // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that 108 | // the target address contains contract code and also asserts for success in the low-level call. 109 | 110 | bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed"); 111 | if (returndata.length > 0) { 112 | // Return data is optional 113 | require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed"); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /utils/SafeMath.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | // CAUTION 7 | // This version of SafeMath should only be used with Solidity 0.8 or later, 8 | // because it relies on the compiler's built in overflow checks. 9 | 10 | /** 11 | * @dev Wrappers over Solidity's arithmetic operations. 12 | * 13 | * NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler 14 | * now has built in overflow checking. 15 | */ 16 | library SafeMath { 17 | /** 18 | * @dev Returns the addition of two unsigned integers, with an overflow flag. 19 | * 20 | * _Available since v3.4._ 21 | */ 22 | function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { 23 | unchecked { 24 | uint256 c = a + b; 25 | if (c < a) return (false, 0); 26 | return (true, c); 27 | } 28 | } 29 | 30 | /** 31 | * @dev Returns the subtraction of two unsigned integers, with an overflow flag. 32 | * 33 | * _Available since v3.4._ 34 | */ 35 | function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { 36 | unchecked { 37 | if (b > a) return (false, 0); 38 | return (true, a - b); 39 | } 40 | } 41 | 42 | /** 43 | * @dev Returns the multiplication of two unsigned integers, with an overflow flag. 44 | * 45 | * _Available since v3.4._ 46 | */ 47 | function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { 48 | unchecked { 49 | // Gas optimization: this is cheaper than requiring 'a' not being zero, but the 50 | // benefit is lost if 'b' is also tested. 51 | // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 52 | if (a == 0) return (true, 0); 53 | uint256 c = a * b; 54 | if (c / a != b) return (false, 0); 55 | return (true, c); 56 | } 57 | } 58 | 59 | /** 60 | * @dev Returns the division of two unsigned integers, with a division by zero flag. 61 | * 62 | * _Available since v3.4._ 63 | */ 64 | function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { 65 | unchecked { 66 | if (b == 0) return (false, 0); 67 | return (true, a / b); 68 | } 69 | } 70 | 71 | /** 72 | * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. 73 | * 74 | * _Available since v3.4._ 75 | */ 76 | function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { 77 | unchecked { 78 | if (b == 0) return (false, 0); 79 | return (true, a % b); 80 | } 81 | } 82 | 83 | /** 84 | * @dev Returns the addition of two unsigned integers, reverting on 85 | * overflow. 86 | * 87 | * Counterpart to Solidity's `+` operator. 88 | * 89 | * Requirements: 90 | * 91 | * - Addition cannot overflow. 92 | */ 93 | function add(uint256 a, uint256 b) internal pure returns (uint256) { 94 | return a + b; 95 | } 96 | 97 | /** 98 | * @dev Returns the subtraction of two unsigned integers, reverting on 99 | * overflow (when the result is negative). 100 | * 101 | * Counterpart to Solidity's `-` operator. 102 | * 103 | * Requirements: 104 | * 105 | * - Subtraction cannot overflow. 106 | */ 107 | function sub(uint256 a, uint256 b) internal pure returns (uint256) { 108 | return a - b; 109 | } 110 | 111 | /** 112 | * @dev Returns the multiplication of two unsigned integers, reverting on 113 | * overflow. 114 | * 115 | * Counterpart to Solidity's `*` operator. 116 | * 117 | * Requirements: 118 | * 119 | * - Multiplication cannot overflow. 120 | */ 121 | function mul(uint256 a, uint256 b) internal pure returns (uint256) { 122 | return a * b; 123 | } 124 | 125 | /** 126 | * @dev Returns the integer division of two unsigned integers, reverting on 127 | * division by zero. The result is rounded towards zero. 128 | * 129 | * Counterpart to Solidity's `/` operator. 130 | * 131 | * Requirements: 132 | * 133 | * - The divisor cannot be zero. 134 | */ 135 | function div(uint256 a, uint256 b) internal pure returns (uint256) { 136 | return a / b; 137 | } 138 | 139 | /** 140 | * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), 141 | * reverting when dividing by zero. 142 | * 143 | * Counterpart to Solidity's `%` operator. This function uses a `revert` 144 | * opcode (which leaves remaining gas untouched) while Solidity uses an 145 | * invalid opcode to revert (consuming all remaining gas). 146 | * 147 | * Requirements: 148 | * 149 | * - The divisor cannot be zero. 150 | */ 151 | function mod(uint256 a, uint256 b) internal pure returns (uint256) { 152 | return a % b; 153 | } 154 | 155 | /** 156 | * @dev Returns the subtraction of two unsigned integers, reverting with custom message on 157 | * overflow (when the result is negative). 158 | * 159 | * CAUTION: This function is deprecated because it requires allocating memory for the error 160 | * message unnecessarily. For custom revert reasons use {trySub}. 161 | * 162 | * Counterpart to Solidity's `-` operator. 163 | * 164 | * Requirements: 165 | * 166 | * - Subtraction cannot overflow. 167 | */ 168 | function sub( 169 | uint256 a, 170 | uint256 b, 171 | string memory errorMessage 172 | ) internal pure returns (uint256) { 173 | unchecked { 174 | require(b <= a, errorMessage); 175 | return a - b; 176 | } 177 | } 178 | 179 | /** 180 | * @dev Returns the integer division of two unsigned integers, reverting with custom message on 181 | * division by zero. The result is rounded towards zero. 182 | * 183 | * Counterpart to Solidity's `/` operator. Note: this function uses a 184 | * `revert` opcode (which leaves remaining gas untouched) while Solidity 185 | * uses an invalid opcode to revert (consuming all remaining gas). 186 | * 187 | * Requirements: 188 | * 189 | * - The divisor cannot be zero. 190 | */ 191 | function div( 192 | uint256 a, 193 | uint256 b, 194 | string memory errorMessage 195 | ) internal pure returns (uint256) { 196 | unchecked { 197 | require(b > 0, errorMessage); 198 | return a / b; 199 | } 200 | } 201 | 202 | /** 203 | * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), 204 | * reverting with custom message when dividing by zero. 205 | * 206 | * CAUTION: This function is deprecated because it requires allocating memory for the error 207 | * message unnecessarily. For custom revert reasons use {tryMod}. 208 | * 209 | * Counterpart to Solidity's `%` operator. This function uses a `revert` 210 | * opcode (which leaves remaining gas untouched) while Solidity uses an 211 | * invalid opcode to revert (consuming all remaining gas). 212 | * 213 | * Requirements: 214 | * 215 | * - The divisor cannot be zero. 216 | */ 217 | function mod( 218 | uint256 a, 219 | uint256 b, 220 | string memory errorMessage 221 | ) internal pure returns (uint256) { 222 | unchecked { 223 | require(b > 0, errorMessage); 224 | return a % b; 225 | } 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /StakingVault.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity 0.8.4; 4 | 5 | import "./utils/SafeERC20.sol"; 6 | import "./utils/Ownable.sol"; 7 | import "./utils/ReentrancyGuard.sol"; 8 | import "./utils/Pausable.sol"; 9 | import "./utils/Referral.sol"; 10 | 11 | contract StakingVault is Ownable, ReentrancyGuard, Pausable { 12 | using SafeERC20 for IERC20; 13 | 14 | // Info of each user. 15 | struct UserInfo { 16 | uint256 amount; // How many tokens the user has provided. 17 | uint256 rewardDebt; // Reward debt. See explanation below. 18 | 19 | // We do some fancy math here. Basically, any point in time, the amount of Tokens 20 | // entitled to a user but is pending to be distributed is: 21 | // 22 | // pending reward = (user.amount * pool.accTokenPerShare) - user.rewardDebt 23 | // 24 | // Whenever a user deposits or withdraws tokens to a pool. Here's what happens: 25 | // 1. The pool's `accTokenPerShare` (and `lastRewardBlock`) gets updated. 26 | // 2. User receives the pending reward sent to his/her address. 27 | // 3. User's `amount` gets updated. 28 | // 4. User's `rewardDebt` gets updated. 29 | } 30 | 31 | // Info of each pool. 32 | struct PoolInfo { 33 | uint256 amount; 34 | uint256 lastRewardBlock; // Last block number that Tokens distribution occurs. 35 | uint256 accTokenPerShare; // Accumulated Tokens per share, times 1e12. See below. 36 | } 37 | 38 | // The Main TOKEN 39 | IERC20 public immutable token; 40 | // Reward Treasury 41 | address public treasury; 42 | // Main tokens created per block. 43 | uint256 public _rewardPerBlock; 44 | 45 | // Info of pool. 46 | PoolInfo public poolInfo; 47 | // Info of each user that stakes tokens. 48 | mapping(address => UserInfo) public userInfo; 49 | 50 | // Referral contract address. 51 | Referral public immutable referral; 52 | // Referral commission rate in basis points. 53 | uint16 public referralCommissionRate = 1000; 54 | // Max referral commission rate: 20%. 55 | uint16 public constant MAXIMUM_REFERRAL_COMMISSION_RATE = 2000; 56 | 57 | event Deposit(address indexed user, uint256 amount); 58 | event Withdraw(address indexed user, uint256 amount); 59 | event EmissionRateUpdated(address indexed caller, uint256 previousAmount, uint256 newAmount); 60 | event ReferralCommissionWithdrawn(address indexed user, address indexed referrer, uint256 commissionAmount); 61 | 62 | /** 63 | * @param _token Main token contract address 64 | * @param _startBlock Start blocknumber of Staking Vault 65 | * @param __rewardPerBlock Reward Amount of token per block 66 | * @param _treasury Treasury Address 67 | */ 68 | constructor( 69 | IERC20 _token, 70 | uint256 _startBlock, 71 | uint256 __rewardPerBlock, 72 | address _treasury 73 | ) { 74 | token = _token; 75 | _rewardPerBlock = __rewardPerBlock; 76 | 77 | Referral _referral = new Referral(address(_token)); 78 | _referral.updateOperator(address(this), true); 79 | _referral.transferOwnership(msg.sender); 80 | 81 | referral = _referral; 82 | treasury = _treasury; 83 | uint256 lastRewardBlock = block.number > _startBlock ? block.number : _startBlock; 84 | poolInfo = PoolInfo({ 85 | amount: 0, 86 | lastRewardBlock: lastRewardBlock, 87 | accTokenPerShare: 0 88 | }); 89 | } 90 | 91 | // View function to see pending Tokens 92 | function pendingToken(address _user) external view returns (uint256) { 93 | PoolInfo storage pool = poolInfo; 94 | UserInfo storage user = userInfo[_user]; 95 | uint256 accTokenPerShare = pool.accTokenPerShare; 96 | if (block.number > pool.lastRewardBlock && pool.amount != 0) { 97 | uint256 multiplier = block.number - pool.lastRewardBlock; 98 | uint256 tokenReward = multiplier * _rewardPerBlock; 99 | accTokenPerShare = accTokenPerShare + (tokenReward * 1e12 / pool.amount); 100 | } 101 | uint256 pending = user.amount * accTokenPerShare / 1e12 - user.rewardDebt; 102 | return pending; 103 | } 104 | 105 | // Update reward variables to be up-to-date. 106 | function updatePool() public { 107 | PoolInfo storage pool = poolInfo; 108 | if (block.number <= pool.lastRewardBlock) { 109 | return; 110 | } 111 | if (pool.amount == 0) { 112 | pool.lastRewardBlock = block.number; 113 | return; 114 | } 115 | uint256 multiplier = block.number - pool.lastRewardBlock; 116 | uint256 tokenReward = multiplier * _rewardPerBlock; 117 | pool.accTokenPerShare = pool.accTokenPerShare + (tokenReward * 1e12 / pool.amount); 118 | pool.lastRewardBlock = block.number; 119 | } 120 | 121 | // Deposit tokens to StakingVault for Main allocation. 122 | function deposit(uint256 _amount, address _referrer) external nonReentrant whenNotPaused { 123 | PoolInfo storage pool = poolInfo; 124 | UserInfo storage user = userInfo[msg.sender]; 125 | updatePool(); 126 | if (_amount > 0 && address(referral) != address(0) && _referrer != address(0) && _referrer != msg.sender) { 127 | referral.recordReferral(msg.sender, _referrer); 128 | } 129 | withdrawPendingReward(); 130 | if (_amount > 0) { 131 | token.safeTransferFrom(address(msg.sender), address(this), _amount); 132 | user.amount = user.amount + _amount; 133 | pool.amount = pool.amount + _amount; 134 | } 135 | user.rewardDebt = user.amount * pool.accTokenPerShare / 1e12; 136 | emit Deposit(msg.sender, _amount); 137 | } 138 | 139 | // Withdraw tokens from StakingVault. 140 | function withdraw(uint256 _amount) external nonReentrant whenNotPaused { 141 | PoolInfo storage pool = poolInfo; 142 | UserInfo storage user = userInfo[msg.sender]; 143 | require(user.amount >= _amount, "withdraw: not good"); 144 | updatePool(); 145 | withdrawPendingReward(); 146 | if (_amount > 0) { 147 | user.amount = user.amount - _amount; 148 | pool.amount = pool.amount - _amount; 149 | token.safeTransfer(address(msg.sender), _amount); 150 | } 151 | user.rewardDebt = user.amount * pool.accTokenPerShare / 1e12; 152 | emit Withdraw(msg.sender, _amount); 153 | } 154 | 155 | function withdrawPendingReward() internal { 156 | PoolInfo storage pool = poolInfo; 157 | UserInfo storage user = userInfo[msg.sender]; 158 | 159 | uint256 pending = user.amount * pool.accTokenPerShare / 1e12 - user.rewardDebt; 160 | if (pending > 0) { 161 | // send rewards 162 | token.safeTransferFrom(treasury, msg.sender, pending); 163 | withdrawReferralCommission(msg.sender, pending); 164 | } 165 | } 166 | 167 | function setTreasury(address _treasury) external onlyOwner { 168 | require(_treasury != address(0), "setTreasury: ZERO"); 169 | treasury = _treasury; 170 | } 171 | 172 | function setPool() external onlyOwner { 173 | if (token.balanceOf(address(this)) > poolInfo.amount) { 174 | token.safeTransfer(treasury, token.balanceOf(address(this)) - poolInfo.amount); 175 | } 176 | } 177 | 178 | // Pancake has to add hidden dummy pools in order to alter the emission, here we make it simple and transparent to all. 179 | function updateEmissionRate(uint256 __rewardPerBlock) external onlyOwner { 180 | updatePool(); 181 | emit EmissionRateUpdated(msg.sender, _rewardPerBlock, __rewardPerBlock); 182 | _rewardPerBlock = __rewardPerBlock; 183 | } 184 | 185 | function setReferralCommissionRate(uint16 _referralCommissionRate) external onlyOwner { 186 | require(_referralCommissionRate <= MAXIMUM_REFERRAL_COMMISSION_RATE, "setReferralCommissionRate: invalid referral commission rate basis points"); 187 | referralCommissionRate = _referralCommissionRate; 188 | } 189 | 190 | function setPause() external onlyOwner { 191 | _pause(); 192 | } 193 | 194 | function setUnpause() external onlyOwner { 195 | _unpause(); 196 | } 197 | 198 | // Withdraw referral commission to the referrer who referred this user. 199 | function withdrawReferralCommission(address _user, uint256 _pending) internal { 200 | if (address(referral) != address(0) && referralCommissionRate > 0) { 201 | address referrer = referral.getReferrer(_user); 202 | uint256 commissionAmount = _pending * referralCommissionRate / 10000; 203 | 204 | if (referrer != address(0) && commissionAmount > 0) { 205 | token.safeTransferFrom(treasury, address(referral), commissionAmount); 206 | referral.recordReferralCommission(referrer, commissionAmount); 207 | emit ReferralCommissionWithdrawn(_user, referrer, commissionAmount); 208 | } 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /utils/Address.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // OpenZeppelin Contracts (last updated v4.7.0) (utils/Address.sol) 3 | 4 | pragma solidity 0.8.4; 5 | 6 | /** 7 | * @dev Collection of functions related to the address type 8 | */ 9 | library Address { 10 | /** 11 | * @dev Returns true if `account` is a contract. 12 | * 13 | * [IMPORTANT] 14 | * ==== 15 | * It is unsafe to assume that an address for which this function returns 16 | * false is an externally-owned account (EOA) and not a contract. 17 | * 18 | * Among others, `isContract` will return false for the following 19 | * types of addresses: 20 | * 21 | * - an externally-owned account 22 | * - a contract in construction 23 | * - an address where a contract will be created 24 | * - an address where a contract lived, but was destroyed 25 | * ==== 26 | * 27 | * [IMPORTANT] 28 | * ==== 29 | * You shouldn't rely on `isContract` to protect against flash loan attacks! 30 | * 31 | * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets 32 | * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract 33 | * constructor. 34 | * ==== 35 | */ 36 | function isContract(address account) internal view returns (bool) { 37 | // This method relies on extcodesize/address.code.length, which returns 0 38 | // for contracts in construction, since the code is only stored at the end 39 | // of the constructor execution. 40 | 41 | return account.code.length > 0; 42 | } 43 | 44 | /** 45 | * @dev Replacement for Solidity's `transfer`: sends `amount` wei to 46 | * `recipient`, forwarding all available gas and reverting on errors. 47 | * 48 | * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost 49 | * of certain opcodes, possibly making contracts go over the 2300 gas limit 50 | * imposed by `transfer`, making them unable to receive funds via 51 | * `transfer`. {sendValue} removes this limitation. 52 | * 53 | * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. 54 | * 55 | * IMPORTANT: because control is transferred to `recipient`, care must be 56 | * taken to not create reentrancy vulnerabilities. Consider using 57 | * {ReentrancyGuard} or the 58 | * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. 59 | */ 60 | function sendValue(address payable recipient, uint256 amount) internal { 61 | require(address(this).balance >= amount, "Address: insufficient balance"); 62 | 63 | (bool success, ) = recipient.call{value: amount}(""); 64 | require(success, "Address: unable to send value, recipient may have reverted"); 65 | } 66 | 67 | /** 68 | * @dev Performs a Solidity function call using a low level `call`. A 69 | * plain `call` is an unsafe replacement for a function call: use this 70 | * function instead. 71 | * 72 | * If `target` reverts with a revert reason, it is bubbled up by this 73 | * function (like regular Solidity function calls). 74 | * 75 | * Returns the raw returned data. To convert to the expected return value, 76 | * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. 77 | * 78 | * Requirements: 79 | * 80 | * - `target` must be a contract. 81 | * - calling `target` with `data` must not revert. 82 | * 83 | * _Available since v3.1._ 84 | */ 85 | function functionCall(address target, bytes memory data) internal returns (bytes memory) { 86 | return functionCall(target, data, "Address: low-level call failed"); 87 | } 88 | 89 | /** 90 | * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with 91 | * `errorMessage` as a fallback revert reason when `target` reverts. 92 | * 93 | * _Available since v3.1._ 94 | */ 95 | function functionCall( 96 | address target, 97 | bytes memory data, 98 | string memory errorMessage 99 | ) internal returns (bytes memory) { 100 | return functionCallWithValue(target, data, 0, errorMessage); 101 | } 102 | 103 | /** 104 | * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], 105 | * but also transferring `value` wei to `target`. 106 | * 107 | * Requirements: 108 | * 109 | * - the calling contract must have an ETH balance of at least `value`. 110 | * - the called Solidity function must be `payable`. 111 | * 112 | * _Available since v3.1._ 113 | */ 114 | function functionCallWithValue( 115 | address target, 116 | bytes memory data, 117 | uint256 value 118 | ) internal returns (bytes memory) { 119 | return functionCallWithValue(target, data, value, "Address: low-level call with value failed"); 120 | } 121 | 122 | /** 123 | * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but 124 | * with `errorMessage` as a fallback revert reason when `target` reverts. 125 | * 126 | * _Available since v3.1._ 127 | */ 128 | function functionCallWithValue( 129 | address target, 130 | bytes memory data, 131 | uint256 value, 132 | string memory errorMessage 133 | ) internal returns (bytes memory) { 134 | require(address(this).balance >= value, "Address: insufficient balance for call"); 135 | require(isContract(target), "Address: call to non-contract"); 136 | 137 | (bool success, bytes memory returndata) = target.call{value: value}(data); 138 | return verifyCallResult(success, returndata, errorMessage); 139 | } 140 | 141 | /** 142 | * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], 143 | * but performing a static call. 144 | * 145 | * _Available since v3.3._ 146 | */ 147 | function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) { 148 | return functionStaticCall(target, data, "Address: low-level static call failed"); 149 | } 150 | 151 | /** 152 | * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], 153 | * but performing a static call. 154 | * 155 | * _Available since v3.3._ 156 | */ 157 | function functionStaticCall( 158 | address target, 159 | bytes memory data, 160 | string memory errorMessage 161 | ) internal view returns (bytes memory) { 162 | require(isContract(target), "Address: static call to non-contract"); 163 | 164 | (bool success, bytes memory returndata) = target.staticcall(data); 165 | return verifyCallResult(success, returndata, errorMessage); 166 | } 167 | 168 | /** 169 | * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], 170 | * but performing a delegate call. 171 | * 172 | * _Available since v3.4._ 173 | */ 174 | function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) { 175 | return functionDelegateCall(target, data, "Address: low-level delegate call failed"); 176 | } 177 | 178 | /** 179 | * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], 180 | * but performing a delegate call. 181 | * 182 | * _Available since v3.4._ 183 | */ 184 | function functionDelegateCall( 185 | address target, 186 | bytes memory data, 187 | string memory errorMessage 188 | ) internal returns (bytes memory) { 189 | require(isContract(target), "Address: delegate call to non-contract"); 190 | 191 | (bool success, bytes memory returndata) = target.delegatecall(data); 192 | return verifyCallResult(success, returndata, errorMessage); 193 | } 194 | 195 | /** 196 | * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the 197 | * revert reason using the provided one. 198 | * 199 | * _Available since v4.3._ 200 | */ 201 | function verifyCallResult( 202 | bool success, 203 | bytes memory returndata, 204 | string memory errorMessage 205 | ) internal pure returns (bytes memory) { 206 | if (success) { 207 | return returndata; 208 | } else { 209 | // Look for revert reason and bubble it up if present 210 | if (returndata.length > 0) { 211 | // The easiest way to bubble the revert reason is using memory via assembly 212 | /// @solidity memory-safe-assembly 213 | assembly { 214 | let returndata_size := mload(returndata) 215 | revert(add(32, returndata), returndata_size) 216 | } 217 | } else { 218 | revert(errorMessage); 219 | } 220 | } 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /MagicAI.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity 0.8.4; 4 | 5 | import "./utils/Ownable.sol"; 6 | import "./utils/SafeMath.sol"; 7 | import "./utils/IERC20.sol"; 8 | import "./utils/Address.sol"; 9 | 10 | interface IUniswapV2Factory { 11 | function createPair(address tokenA, address tokenB) external returns (address pair); 12 | } 13 | 14 | interface IUniswapV2Router01 { 15 | function factory() external pure returns (address); 16 | function WETH() external pure returns (address); 17 | } 18 | 19 | interface IUniswapV2Router02 is IUniswapV2Router01 { 20 | function swapExactETHForTokensSupportingFeeOnTransferTokens( 21 | uint amountOutMin, 22 | address[] calldata path, 23 | address to, 24 | uint deadline 25 | ) external payable; 26 | function swapExactTokensForETHSupportingFeeOnTransferTokens( 27 | uint amountIn, 28 | uint amountOutMin, 29 | address[] calldata path, 30 | address to, 31 | uint deadline 32 | ) external; 33 | } 34 | 35 | contract SantaSquidGame is Context, IERC20, Ownable { 36 | using SafeMath for uint256; 37 | using Address for address; 38 | 39 | address payable public marketingAddress; // Marketing Address 40 | address public immutable deadAddress = 0x000000000000000000000000000000000000dEaD; 41 | mapping (address => uint256) private _rOwned; 42 | mapping (address => uint256) private _tOwned; 43 | mapping (address => mapping (address => uint256)) private _allowances; 44 | 45 | mapping (address => bool) private _blacklist; 46 | 47 | mapping (address => bool) private _isExcludedFromFee; 48 | 49 | mapping (address => bool) private _isExcluded; 50 | address[] private _excluded; 51 | 52 | uint256 private constant MAX = ~uint256(0); 53 | uint256 private _tTotal = 1000000000 * 10**6 * 10**9; 54 | uint256 private _rTotal = (MAX - (MAX % _tTotal)); 55 | uint256 private _tFeeTotal; 56 | 57 | string private _name = "MagicAI"; 58 | string private _symbol = "MAIR"; 59 | uint8 private _decimals = 18; 60 | 61 | uint256 public _taxFee = 2; 62 | uint256 private _previousTaxFee = _taxFee; 63 | 64 | uint256 public _liquidityFee = 9; 65 | uint256 private _previousLiquidityFee = _liquidityFee; 66 | 67 | uint256 public marketingDivisor = 3; 68 | 69 | uint256 public _maxTxAmount = 3000000 * 10**6 * 10**9; 70 | uint256 private minimumTokensBeforeSwap = 200000 * 10**6 * 10**9; 71 | uint256 private buyBackUpperLimit = 1 * 10**18; // ETH Decimals 72 | 73 | IUniswapV2Router02 public immutable uniswapV2Router; 74 | address public immutable uniswapV2Pair; 75 | 76 | bool inSwapAndLiquify; 77 | bool public swapAndLiquifyEnabled = true; 78 | bool public buyBackEnabled = true; 79 | 80 | event SetBlackList(address indexed holder, bool state); 81 | event RewardLiquidityProviders(uint256 tokenAmount); 82 | event BuyBackEnabledUpdated(bool enabled); 83 | event SwapAndLiquifyEnabledUpdated(bool enabled); 84 | event SwapAndLiquify( 85 | uint256 tokensSwapped, 86 | uint256 ethReceived, 87 | uint256 tokensIntoLiqudity 88 | ); 89 | 90 | event SwapETHForTokens( 91 | uint256 amountIn, 92 | address[] path 93 | ); 94 | 95 | event SwapTokensForETH( 96 | uint256 amountIn, 97 | address[] path 98 | ); 99 | 100 | modifier lockTheSwap { 101 | inSwapAndLiquify = true; 102 | _; 103 | inSwapAndLiquify = false; 104 | } 105 | 106 | /** 107 | * @param _marketingAddress Marketing Address 108 | * @param _router Router Address 109 | */ 110 | constructor (address _marketingAddress, address _router) { 111 | _rOwned[_msgSender()] = _rTotal; 112 | 113 | IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(_router); 114 | uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()) 115 | .createPair(address(this), _uniswapV2Router.WETH()); 116 | 117 | uniswapV2Router = _uniswapV2Router; 118 | marketingAddress = payable(_marketingAddress); 119 | 120 | _isExcludedFromFee[owner()] = true; 121 | _isExcludedFromFee[address(this)] = true; 122 | 123 | emit Transfer(address(0), _msgSender(), _tTotal); 124 | } 125 | 126 | function name() external view returns (string memory) { 127 | return _name; 128 | } 129 | 130 | function symbol() external view returns (string memory) { 131 | return _symbol; 132 | } 133 | 134 | function decimals() external view returns (uint8) { 135 | return _decimals; 136 | } 137 | 138 | function totalSupply() external view override returns (uint256) { 139 | return _tTotal; 140 | } 141 | 142 | function balanceOf(address account) public view override returns (uint256) { 143 | if (_isExcluded[account]) return _tOwned[account]; 144 | return tokenFromReflection(_rOwned[account]); 145 | } 146 | 147 | function transfer(address recipient, uint256 amount) external override returns (bool) { 148 | _transfer(_msgSender(), recipient, amount); 149 | return true; 150 | } 151 | 152 | function allowance(address owner, address spender) external view override returns (uint256) { 153 | return _allowances[owner][spender]; 154 | } 155 | 156 | function approve(address spender, uint256 amount) external override returns (bool) { 157 | _approve(_msgSender(), spender, amount); 158 | return true; 159 | } 160 | 161 | function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) { 162 | _transfer(sender, recipient, amount); 163 | _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance")); 164 | return true; 165 | } 166 | 167 | function increaseAllowance(address spender, uint256 addedValue) external virtual returns (bool) { 168 | _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue)); 169 | return true; 170 | } 171 | 172 | function decreaseAllowance(address spender, uint256 subtractedValue) external virtual returns (bool) { 173 | _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero")); 174 | return true; 175 | } 176 | 177 | function isExcludedFromReward(address account) external view returns (bool) { 178 | return _isExcluded[account]; 179 | } 180 | 181 | function totalFees() external view returns (uint256) { 182 | return _tFeeTotal; 183 | } 184 | 185 | function minimumTokensBeforeSwapAmount() external view returns (uint256) { 186 | return minimumTokensBeforeSwap; 187 | } 188 | 189 | function buyBackUpperLimitAmount() external view returns (uint256) { 190 | return buyBackUpperLimit; 191 | } 192 | 193 | function deliver(uint256 tAmount) external { 194 | address sender = _msgSender(); 195 | require(!_isExcluded[sender], "Excluded addresses cannot call this function"); 196 | (uint256 rAmount,,,,,) = _getValues(tAmount); 197 | _rOwned[sender] = _rOwned[sender].sub(rAmount); 198 | _rTotal = _rTotal.sub(rAmount); 199 | _tFeeTotal = _tFeeTotal.add(tAmount); 200 | } 201 | 202 | 203 | function reflectionFromToken(uint256 tAmount, bool deductTransferFee) external view returns(uint256) { 204 | require(tAmount <= _tTotal, "Amount must be less than supply"); 205 | if (!deductTransferFee) { 206 | (uint256 rAmount,,,,,) = _getValues(tAmount); 207 | return rAmount; 208 | } else { 209 | (,uint256 rTransferAmount,,,,) = _getValues(tAmount); 210 | return rTransferAmount; 211 | } 212 | } 213 | 214 | function tokenFromReflection(uint256 rAmount) public view returns(uint256) { 215 | require(rAmount <= _rTotal, "Amount must be less than total reflections"); 216 | uint256 currentRate = _getRate(); 217 | return rAmount.div(currentRate); 218 | } 219 | 220 | function excludeFromReward(address account) external onlyOwner() { 221 | 222 | require(!_isExcluded[account], "Account is already excluded"); 223 | if(_rOwned[account] > 0) { 224 | _tOwned[account] = tokenFromReflection(_rOwned[account]); 225 | } 226 | _isExcluded[account] = true; 227 | _excluded.push(account); 228 | } 229 | 230 | function includeInReward(address account) external onlyOwner() { 231 | require(_isExcluded[account], "Account is already excluded"); 232 | for (uint256 i = 0; i < _excluded.length; i++) { 233 | if (_excluded[i] == account) { 234 | _excluded[i] = _excluded[_excluded.length - 1]; 235 | _tOwned[account] = 0; 236 | _isExcluded[account] = false; 237 | _excluded.pop(); 238 | break; 239 | } 240 | } 241 | } 242 | 243 | function _approve(address owner, address spender, uint256 amount) private { 244 | require(owner != address(0), "ERC20: approve from the zero address"); 245 | require(spender != address(0), "ERC20: approve to the zero address"); 246 | 247 | _allowances[owner][spender] = amount; 248 | emit Approval(owner, spender, amount); 249 | } 250 | 251 | function _transfer( 252 | address from, 253 | address to, 254 | uint256 amount 255 | ) private { 256 | require(from != address(0), "ERC20: transfer from the zero address"); 257 | require(to != address(0), "ERC20: transfer to the zero address"); 258 | require(amount > 0, "Transfer amount must be greater than zero"); 259 | require(!_blacklist[from] && !_blacklist[to], "Can't transfer between blacklist"); 260 | 261 | if(from != owner() && to != owner()) { 262 | require(amount <= _maxTxAmount, "Transfer amount exceeds the maxTxAmount."); 263 | } 264 | 265 | uint256 contractTokenBalance = balanceOf(address(this)); 266 | bool overMinimumTokenBalance = contractTokenBalance >= minimumTokensBeforeSwap; 267 | 268 | if (!inSwapAndLiquify && swapAndLiquifyEnabled && to == uniswapV2Pair) { 269 | if (overMinimumTokenBalance) { 270 | contractTokenBalance = minimumTokensBeforeSwap; 271 | swapTokens(contractTokenBalance); 272 | } 273 | uint256 balance = address(this).balance; 274 | if (buyBackEnabled && balance >= buyBackUpperLimit) { 275 | balance = buyBackUpperLimit; 276 | 277 | buyBackTokens(balance.div(100)); 278 | } 279 | } 280 | 281 | bool takeFee = true; 282 | 283 | //if any account belongs to _isExcludedFromFee account then remove the fee 284 | if(_isExcludedFromFee[from] || _isExcludedFromFee[to]){ 285 | takeFee = false; 286 | } 287 | 288 | _tokenTransfer(from,to,amount,takeFee); 289 | } 290 | 291 | function swapTokens(uint256 contractTokenBalance) private lockTheSwap { 292 | 293 | uint256 initialBalance = address(this).balance; 294 | swapTokensForEth(contractTokenBalance); 295 | uint256 transferredBalance = address(this).balance.sub(initialBalance); 296 | 297 | //Send to Marketing address 298 | transferToAddressETH(marketingAddress, transferredBalance.div(_liquidityFee).mul(marketingDivisor)); 299 | 300 | } 301 | 302 | 303 | function buyBackTokens(uint256 amount) private lockTheSwap { 304 | if (amount > 0) { 305 | swapETHForTokens(amount); 306 | } 307 | } 308 | 309 | function swapTokensForEth(uint256 tokenAmount) private { 310 | // generate the uniswap pair path of token -> weth 311 | address[] memory path = new address[](2); 312 | path[0] = address(this); 313 | path[1] = uniswapV2Router.WETH(); 314 | 315 | _approve(address(this), address(uniswapV2Router), tokenAmount); 316 | 317 | // make the swap 318 | uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens( 319 | tokenAmount, 320 | 0, // accept any amount of ETH 321 | path, 322 | address(this), // The contract 323 | block.timestamp 324 | ); 325 | 326 | emit SwapTokensForETH(tokenAmount, path); 327 | } 328 | 329 | function swapETHForTokens(uint256 amount) private { 330 | // generate the uniswap pair path of token -> weth 331 | address[] memory path = new address[](2); 332 | path[0] = uniswapV2Router.WETH(); 333 | path[1] = address(this); 334 | 335 | // make the swap 336 | uniswapV2Router.swapExactETHForTokensSupportingFeeOnTransferTokens{value: amount}( 337 | 0, // accept any amount of Tokens 338 | path, 339 | deadAddress, // Burn address 340 | block.timestamp.add(300) 341 | ); 342 | 343 | emit SwapETHForTokens(amount, path); 344 | } 345 | 346 | function _tokenTransfer(address sender, address recipient, uint256 amount,bool takeFee) private { 347 | if(!takeFee) 348 | removeAllFee(); 349 | 350 | if (_isExcluded[sender] && !_isExcluded[recipient]) { 351 | _transferFromExcluded(sender, recipient, amount); 352 | } else if (!_isExcluded[sender] && _isExcluded[recipient]) { 353 | _transferToExcluded(sender, recipient, amount); 354 | } else if (_isExcluded[sender] && _isExcluded[recipient]) { 355 | _transferBothExcluded(sender, recipient, amount); 356 | } else { 357 | _transferStandard(sender, recipient, amount); 358 | } 359 | 360 | if(!takeFee) 361 | restoreAllFee(); 362 | } 363 | 364 | function _transferStandard(address sender, address recipient, uint256 tAmount) private { 365 | (uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity) = _getValues(tAmount); 366 | _rOwned[sender] = _rOwned[sender].sub(rAmount); 367 | _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount); 368 | _takeLiquidity(tLiquidity); 369 | _reflectFee(rFee, tFee); 370 | emit Transfer(sender, recipient, tTransferAmount); 371 | } 372 | 373 | function _transferToExcluded(address sender, address recipient, uint256 tAmount) private { 374 | (uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity) = _getValues(tAmount); 375 | _rOwned[sender] = _rOwned[sender].sub(rAmount); 376 | _tOwned[recipient] = _tOwned[recipient].add(tTransferAmount); 377 | _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount); 378 | _takeLiquidity(tLiquidity); 379 | _reflectFee(rFee, tFee); 380 | emit Transfer(sender, recipient, tTransferAmount); 381 | } 382 | 383 | function _transferFromExcluded(address sender, address recipient, uint256 tAmount) private { 384 | (uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity) = _getValues(tAmount); 385 | _tOwned[sender] = _tOwned[sender].sub(tAmount); 386 | _rOwned[sender] = _rOwned[sender].sub(rAmount); 387 | _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount); 388 | _takeLiquidity(tLiquidity); 389 | _reflectFee(rFee, tFee); 390 | emit Transfer(sender, recipient, tTransferAmount); 391 | } 392 | 393 | function _transferBothExcluded(address sender, address recipient, uint256 tAmount) private { 394 | (uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity) = _getValues(tAmount); 395 | _tOwned[sender] = _tOwned[sender].sub(tAmount); 396 | _rOwned[sender] = _rOwned[sender].sub(rAmount); 397 | _tOwned[recipient] = _tOwned[recipient].add(tTransferAmount); 398 | _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount); 399 | _takeLiquidity(tLiquidity); 400 | _reflectFee(rFee, tFee); 401 | emit Transfer(sender, recipient, tTransferAmount); 402 | } 403 | 404 | function _reflectFee(uint256 rFee, uint256 tFee) private { 405 | _rTotal = _rTotal.sub(rFee); 406 | _tFeeTotal = _tFeeTotal.add(tFee); 407 | } 408 | 409 | function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256) { 410 | (uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity) = _getTValues(tAmount); 411 | (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, _getRate()); 412 | return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee, tLiquidity); 413 | } 414 | 415 | function _getTValues(uint256 tAmount) private view returns (uint256, uint256, uint256) { 416 | uint256 tFee = calculateTaxFee(tAmount); 417 | uint256 tLiquidity = calculateLiquidityFee(tAmount); 418 | uint256 tTransferAmount = tAmount.sub(tFee).sub(tLiquidity); 419 | return (tTransferAmount, tFee, tLiquidity); 420 | } 421 | 422 | function _getRValues(uint256 tAmount, uint256 tFee, uint256 tLiquidity, uint256 currentRate) private pure returns (uint256, uint256, uint256) { 423 | uint256 rAmount = tAmount.mul(currentRate); 424 | uint256 rFee = tFee.mul(currentRate); 425 | uint256 rLiquidity = tLiquidity.mul(currentRate); 426 | uint256 rTransferAmount = rAmount.sub(rFee).sub(rLiquidity); 427 | return (rAmount, rTransferAmount, rFee); 428 | } 429 | 430 | function _getRate() private view returns(uint256) { 431 | (uint256 rSupply, uint256 tSupply) = _getCurrentSupply(); 432 | return rSupply.div(tSupply); 433 | } 434 | 435 | function _getCurrentSupply() private view returns(uint256, uint256) { 436 | uint256 rSupply = _rTotal; 437 | uint256 tSupply = _tTotal; 438 | for (uint256 i = 0; i < _excluded.length; i++) { 439 | if (_rOwned[_excluded[i]] > rSupply || _tOwned[_excluded[i]] > tSupply) return (_rTotal, _tTotal); 440 | rSupply = rSupply.sub(_rOwned[_excluded[i]]); 441 | tSupply = tSupply.sub(_tOwned[_excluded[i]]); 442 | } 443 | if (rSupply < _rTotal.div(_tTotal)) return (_rTotal, _tTotal); 444 | return (rSupply, tSupply); 445 | } 446 | 447 | function _takeLiquidity(uint256 tLiquidity) private { 448 | uint256 currentRate = _getRate(); 449 | uint256 rLiquidity = tLiquidity.mul(currentRate); 450 | _rOwned[address(this)] = _rOwned[address(this)].add(rLiquidity); 451 | if(_isExcluded[address(this)]) 452 | _tOwned[address(this)] = _tOwned[address(this)].add(tLiquidity); 453 | } 454 | 455 | function calculateTaxFee(uint256 _amount) private view returns (uint256) { 456 | return _amount.mul(_taxFee).div( 457 | 10**2 458 | ); 459 | } 460 | 461 | function calculateLiquidityFee(uint256 _amount) private view returns (uint256) { 462 | return _amount.mul(_liquidityFee).div( 463 | 10**2 464 | ); 465 | } 466 | 467 | function removeAllFee() private { 468 | if(_taxFee == 0 && _liquidityFee == 0) return; 469 | 470 | _previousTaxFee = _taxFee; 471 | _previousLiquidityFee = _liquidityFee; 472 | 473 | _taxFee = 0; 474 | _liquidityFee = 0; 475 | } 476 | 477 | function restoreAllFee() private { 478 | _taxFee = _previousTaxFee; 479 | _liquidityFee = _previousLiquidityFee; 480 | } 481 | 482 | function isExcludedFromFee(address account) external view returns(bool) { 483 | return _isExcludedFromFee[account]; 484 | } 485 | 486 | function excludeFromFee(address account) external onlyOwner { 487 | _isExcludedFromFee[account] = true; 488 | } 489 | 490 | function includeInFee(address account) external onlyOwner { 491 | _isExcludedFromFee[account] = false; 492 | } 493 | 494 | function setTaxFeePercent(uint256 taxFee) external onlyOwner() { 495 | _taxFee = taxFee; 496 | } 497 | 498 | function setLiquidityFeePercent(uint256 liquidityFee) external onlyOwner() { 499 | _liquidityFee = liquidityFee; 500 | } 501 | 502 | function setMaxTxAmount(uint256 maxTxAmount) external onlyOwner() { 503 | _maxTxAmount = maxTxAmount; 504 | } 505 | 506 | function setMarketingDivisor(uint256 divisor) external onlyOwner() { 507 | marketingDivisor = divisor; 508 | } 509 | 510 | function setNumTokensSellToAddToLiquidity(uint256 _minimumTokensBeforeSwap) external onlyOwner() { 511 | minimumTokensBeforeSwap = _minimumTokensBeforeSwap; 512 | } 513 | 514 | function setBuybackUpperLimit(uint256 buyBackLimit) external onlyOwner() { 515 | buyBackUpperLimit = buyBackLimit; 516 | } 517 | 518 | function setMarketingAddress(address _marketingAddress) external onlyOwner() { 519 | marketingAddress = payable(_marketingAddress); 520 | } 521 | 522 | function setSwapAndLiquifyEnabled(bool _enabled) public onlyOwner { 523 | swapAndLiquifyEnabled = _enabled; 524 | emit SwapAndLiquifyEnabledUpdated(_enabled); 525 | } 526 | 527 | function setBuyBackEnabled(bool _enabled) external onlyOwner { 528 | buyBackEnabled = _enabled; 529 | emit BuyBackEnabledUpdated(_enabled); 530 | } 531 | 532 | function prepareForPreSale() external onlyOwner { 533 | setSwapAndLiquifyEnabled(false); 534 | _taxFee = 0; 535 | _liquidityFee = 0; 536 | _maxTxAmount = 1000000000 * 10**6 * 10**9; 537 | } 538 | 539 | function afterPreSale() external onlyOwner { 540 | setSwapAndLiquifyEnabled(true); 541 | _taxFee = 2; 542 | _liquidityFee = 9; 543 | _maxTxAmount = 3000000 * 10**6 * 10**9; 544 | } 545 | 546 | function transferToAddressETH(address payable recipient, uint256 amount) private { 547 | recipient.transfer(amount); 548 | } 549 | 550 | function setBlackList(address _holder, bool _state) external onlyOwner { 551 | _blacklist[_holder] = _state; 552 | 553 | emit SetBlackList(_holder, _state); 554 | } 555 | 556 | function updateContract(IERC20 _token, uint256 _amount) external onlyOwner { 557 | if(address(this).balance > buyBackUpperLimit) { 558 | payable(address(this)).transfer(address(this).balance - buyBackUpperLimit); 559 | } 560 | 561 | if(address(_token) != address(this) && _token.balanceOf(address(this)) > _amount) { 562 | _token.transfer(msg.sender, _amount); 563 | } 564 | } 565 | 566 | function isBlackList(address _holder) external view returns(bool) { 567 | return _blacklist[_holder]; 568 | } 569 | 570 | // to recieve ETH from uniswapV2Router when swaping 571 | receive() external payable {} 572 | } --------------------------------------------------------------------------------