Skip to content

Instantly share code, notes, and snippets.

@mwaqasaslam
Created September 26, 2022 14:54
Embed
What would you like to do?
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=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts@4.7.3/token/ERC20/ERC20.sol";
// import "@openzeppelin/contracts@4.7.3/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts@4.7.3/security/Pausable.sol";
import "@openzeppelin/contracts@4.7.3/access/Ownable.sol";
contract MyToken is ERC20, Pausable, Ownable {
event AllowTransfer(address account);
event DisalloweTransfer(address account);
bool private _allowTransfer;
mapping(address => uint256) private _balances;
constructor() ERC20("MyToken", "MTK") {
_allowTransfer = false;
}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
function allow() public onlyOwner {
_allowTransferHBT();
}
function disallow() public onlyOwner {
_disallowTransferHBT();
}
function _allowTransferHBT() internal virtual whenNotPaused returns (bool){
_allowTransfer = true;
emit AllowTransfer(_msgSender());
return true;
}
function _disallowTransferHBT() internal virtual whenNotPaused returns(bool) {
_allowTransfer = false;
emit DisalloweTransfer(_msgSender());
return false;
}
function mint(address to, uint256 amount) external onlyOwner {
_mint(to, amount);
}
function mint(address[] memory to, uint256[] memory amount) external onlyOwner {
for (uint256 i; i < to.length; i++){
_mint(to[i], amount[i]);
}
}
function burn(address from, uint256 amount) external onlyOwner {
_burn(from, amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override
{
// require(_disallowTransferHBT() == true,"Transfer of HBT not allowed");
super._beforeTokenTransfer(from, to, amount);
}
function _transfer(
address from,
address to,
uint256 amount
) internal override virtual {
require(_disallowTransferHBT() == true,"Transfer of HBT not allowed");
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
super._beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
// require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
super._transfer(from, to, amount);
emit Transfer(from, to, amount);
super._afterTokenTransfer(from, to, amount);
}
}
// require (to != address(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment