Created
March 25, 2022 16:40
-
-
Save noFAYZ/fc5d855cf7b83ee1608d59326bad801c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts@4.5.0/token/ERC20/ERC20.sol"; | |
import "@openzeppelin/contracts@4.5.0/security/Pausable.sol"; | |
import "@openzeppelin/contracts@4.5.0/access/Ownable.sol"; | |
import "@openzeppelin/contracts@4.5.0/token/ERC20/extensions/draft-ERC20Permit.sol"; | |
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | |
contract MyTokenZzz is ERC20, Pausable, Ownable, ERC20Permit, ReentrancyGuard { | |
mapping(address => uint256) private _balances; | |
mapping(address => mapping(address => uint256)) private _allowances; | |
mapping(address => bool) whitelist; | |
struct my_Tokens { | |
string tokenName; | |
uint256 tokenPrice; | |
uint256 tokenPercentage; | |
} | |
uint256 private _totalSupply; | |
string private _symbol; | |
string private _name; | |
uint256 public _price; | |
event AddedToWhitelist(address indexed account); | |
event RemovedFromWhitelist(address indexed account); | |
event NameChanged(string newName, address by); | |
my_Tokens[] private theTokens; | |
constructor() ERC20("MyTokenZZZNew", "MTKZN") ERC20Permit("MyTokenZZZNew") { | |
_mint(msg.sender, 20 * 10 ** 18); | |
} | |
// modifier for checking whether caller address is on whitelist or not | |
modifier onlyWhitelisted() { | |
require(isWhitelisted(msg.sender)); | |
_; | |
} | |
function decimals() public view virtual override returns (uint8) { | |
return 13; | |
} | |
function totalSupply() public view virtual override returns (uint256) { | |
return _totalSupply; | |
} | |
function balanceOf(address account) public view virtual override returns (uint256) { | |
return _balances[account]; | |
} | |
function addOrUpdateToken(string memory _tokenName, uint256 _tokenPrice, uint256 _tokenPercentage) public onlyOwner{ | |
uint256 index = findElementInArray(_tokenName); | |
if (index != 51){ | |
delete theTokens[index]; | |
} | |
theTokens.push(my_Tokens(_tokenName, _tokenPrice, _tokenPercentage)); | |
} | |
function findElementInArray(string memory element) internal view returns(uint256) { | |
for(uint256 i=0;i<=theTokens.length;i++){ | |
if (keccak256(abi.encodePacked(theTokens[0].tokenName)) == keccak256(abi.encodePacked(element))) { | |
return i; | |
} | |
} | |
return 51; | |
} | |
function removeToken(string memory _tokenName) public onlyOwner{ | |
uint256 index = findElementInArray(_tokenName); | |
delete theTokens[index]; | |
} | |
function getTotalTokens() public view returns (uint) { | |
return theTokens.length; | |
} | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
return div(a, b, "SafeMath: division by zero"); | |
} | |
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
// Solidity only automatically asserts when dividing by 0 | |
require(b > 0, errorMessage); | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function priceCalc() internal view returns (uint256){ | |
uint256 divData =0; | |
uint256 sumData =0; | |
uint256 finalPrice =0; | |
for(uint256 i=0;i<theTokens.length;i++){ | |
divData = div(theTokens[i].tokenPrice,theTokens[i].tokenPrice); | |
sumData = sumData + divData; | |
} | |
finalPrice= div(sumData,1000000); | |
return finalPrice; | |
} | |
/** | |
* @dev function to buy token with ether | |
*/ | |
function buy(uint256 amount) public payable nonReentrant onlyWhitelisted returns (bool sucess) { | |
_price=priceCalc(); | |
require(msg.value >= amount * _price, "Insuficient Balance"); | |
require(isWhitelisted(msg.sender),"Not Whitelisted"); | |
_mint(msg.sender,amount); | |
return true; | |
} | |
function transfer(address to, uint256 amount) public virtual override returns (bool) { | |
address owner = _msgSender(); | |
_transfer(owner, to, amount); | |
return true; | |
} | |
function allowance(address owner, address spender) public view virtual override returns (uint256) { | |
return _allowances[owner][spender]; | |
} | |
function transferFrom( | |
address from, | |
address to, | |
uint256 amount | |
) public virtual override returns (bool) { | |
address spender = _msgSender(); | |
_spendAllowance(from, spender, amount); | |
_transfer(from, to, amount); | |
return true; | |
} | |
function increaseAllowance(address spender, uint256 addedValue) public virtual override returns (bool) { | |
address owner = _msgSender(); | |
_approve(owner, spender, _allowances[owner][spender] + addedValue); | |
return true; | |
} | |
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual override returns (bool) { | |
address owner = _msgSender(); | |
uint256 currentAllowance = _allowances[owner][spender]; | |
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero"); | |
unchecked { | |
_approve(owner, spender, currentAllowance - subtractedValue); | |
} | |
return true; | |
} | |
function _transfer( | |
address from, | |
address to, | |
uint256 amount | |
) internal virtual override{ | |
require(from != address(0), "ERC20: transfer from the zero address"); | |
require(to != address(0), "ERC20: transfer to the zero address"); | |
_beforeTokenTransfer(from, to, amount); | |
uint256 fromBalance = _balances[from]; | |
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); | |
unchecked { | |
_balances[from] = fromBalance - amount; | |
} | |
_balances[to] += amount; | |
emit Transfer(from, to, amount); | |
_afterTokenTransfer(from, to, amount); | |
} | |
function _mint(address account, uint256 amount) internal virtual override { | |
require(account != address(0), "ERC20: mint to the zero address"); | |
_beforeTokenTransfer(address(0), account, amount); | |
_totalSupply += amount; | |
_balances[account] += amount; | |
emit Transfer(address(0), account, amount); | |
_afterTokenTransfer(address(0), account, amount); | |
} | |
function mint(uint256 amount) external onlyWhitelisted{ | |
_mint(msg.sender, amount); | |
} | |
function _approve( | |
address owner, | |
address spender, | |
uint256 amount | |
) internal virtual override { | |
require(owner != address(0), "ERC20: approve from the zero address"); | |
require(spender != address(0), "ERC20: approve to the zero address"); | |
_allowances[owner][spender] = amount; | |
emit Approval(owner, spender, amount); | |
} | |
function _spendAllowance( | |
address owner, | |
address spender, | |
uint256 amount | |
) internal virtual override { | |
uint256 currentAllowance = allowance(owner, spender); | |
if (currentAllowance != type(uint256).max) { | |
require(currentAllowance >= amount, "ERC20: insufficient allowance"); | |
unchecked { | |
_approve(owner, spender, currentAllowance - amount); | |
} | |
} | |
} | |
/** | |
* Add particular address onto whitelist | |
*/ | |
function add(address _address) public onlyOwner { | |
whitelist[_address] = true; | |
emit AddedToWhitelist(_address); | |
} | |
/** | |
* Eliminate particular address onto whitelist | |
*/ | |
function remove(address _address) public onlyOwner { | |
whitelist[_address] = false; | |
emit RemovedFromWhitelist(_address); | |
} | |
/** | |
* Check whether particular address is on whitelist or not | |
*/ | |
function isWhitelisted(address _address) public view returns(bool) { | |
return whitelist[_address]; | |
} | |
function pause() public onlyOwner { | |
_pause(); | |
} | |
function unpause() public onlyOwner { | |
_unpause(); | |
} | |
function mintFor(address to, uint256 amount) public onlyOwner { | |
_mint(to, amount); | |
} | |
/** | |
* @param amount (type uint256) amount of ether | |
* @dev function use to withdraw ether from contract | |
*/ | |
function withdraw(uint256 amount) public onlyOwner returns (bool success) { | |
require(amount <= address(this).balance, "ICO: function withdraw invalid input"); | |
payable(_msgSender()).transfer(amount); | |
return true; | |
} | |
function changeName(string memory nname) public onlyOwner{ | |
_name = nname; | |
emit NameChanged(nname, msg.sender); | |
} | |
function name() public view override returns (string memory) { | |
return _name; | |
} | |
function _beforeTokenTransfer(address from, address to, uint256 amount) | |
internal | |
whenNotPaused | |
override | |
{ | |
super._beforeTokenTransfer(from, to, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment