Skip to content

Instantly share code, notes, and snippets.

@gradam26
Created October 18, 2021 01:22
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gradam26/df6a659c25a455eafaccd3e66629150e to your computer and use it in GitHub Desktop.
/*
_ _ _ _ _ _ _ _ _____ _____
| | | | | |_(_)_ __ ___ __ _| |_ ___ | \ | | ___|_ _|
| | | | | __| | '_ ` _ \ / _` | __/ _ \ | \| | |_ | |
| |_| | | |_| | | | | | | (_| | || __/ | |\ | _| | |
\___/|_|\__|_|_| |_| |_|\__,_|\__\___| |_| \_|_| |_|
A ERC-20 designed with improved security for sensitive functions and liquidity fees/burns.
Author: Grant Fields
*/
pragma solidity 0.8.9;
// SPDX-License-Identifier: MIT
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
interface IUniswapV2Factory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
function createPair(address tokenA, address tokenB) external returns (address pair);
function setFeeTo(address) external;
function setFeeToSetter(address) external;
}
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;
function initialize(address, address) external;
}
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}
interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
contract Ultimate is IERC20, Context {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
mapping(address => bool) private feeExcluded;
uint256 private _totalSupply;
address private contractOwner;
address payable public feeAddress = payable(0xbcED8d1e27deBA6473792063e886816E634492a1);
address public immutable deadAddress = 0x000000000000000000000000000000000000dEaD;
address private secureSignerOne;
uint256 private signerOneTime;
address private secureSignerTwo;
uint256 private signerTwoTime;
string private _name;
string private _symbol;
uint256 public feePercent = 5;
uint256 private previousFee;
uint256 public feeDivisor = 3;
IUniswapV2Router02 public immutable uniswapV2Router;
address public immutable uniswapV2Pair;
bool inSwapAndLiquify;
bool public liquiditySwapEnabled = true;
uint256 public nativeSwapThresh = 2500000 * 10**18;
constructor(address _secureSignerOne, address _secureSignerTwo){
require(_secureSignerOne != address(0) && _secureSignerOne != msg.sender, "Signer #1 is zero address or owner");
require(_secureSignerTwo != address(0) && _secureSignerTwo != msg.sender, "Signer #2 is zero address or owner");
require(_secureSignerOne != _secureSignerTwo, "Signers are identical");
_name = "Ultimate NFT";
_symbol = "UNFT";
_totalSupply = 1000000000 * 10**18;
contractOwner = msg.sender;
secureSignerOne = _secureSignerOne;
secureSignerTwo = _secureSignerTwo;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
uniswapV2Router = _uniswapV2Router;
feeExcluded[address(this)] = true;
feeExcluded[contractOwner] = true;
_balances[contractOwner] = _totalSupply;
emit Transfer(address(0), contractOwner, _totalSupply);
}
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event SignerTransferred(address indexed previousSigner, address indexed newSigner);
event changeFeePercent(uint256 oldValue, uint256 newValue);
event changeFeeDivisor(uint256 oldValue, uint256 newValue);
event setTokenSwapThreshold(uint256 oldValue, uint256 newValue);
event SetLiquiditySwap(bool status);
event feeOn(bool status);
event SwapTokensForETH(uint256 amountIn, address[] path);
modifier lockSwap {
inSwapAndLiquify = true;
_;
inSwapAndLiquify = false;
}
/*
GF: In order for a contractOwner to make a significant call,
it is required that both secureSignerOne & secureSignerTwo
have authenticated access for the contractOwner. They will
both individually call signerAuthenticator(), which will
open a window of ~1 hour during which a contractOwner
can sucessfully make calls. Both signers are required to
authenticate before contractOwner can make sucessful calls.
*/
modifier secureAuth() {
require(contractOwner == _msgSender(), "Caller is not contractOwner");
require(signerOneTime > block.timestamp, "Signer #1 not authenticated.");
require(signerTwoTime > block.timestamp, "Signer #2 not authenticated.");
_;
}
modifier signerVerifed() {
require(secureSignerOne == _msgSender() || secureSignerTwo == _msgSender(), "Caller is not a signer");
_;
}
modifier signerOrOwner() {
require(contractOwner == _msgSender() || secureSignerOne == _msgSender() || secureSignerTwo == _msgSender(), "Caller not owner or signer");
_;
}
function signerAuthenticator() public signerVerifed returns (uint256) {
if (secureSignerOne == _msgSender()){
signerOneTime = block.timestamp + 1 hours;
return signerOneTime;
} else if (secureSignerTwo == _msgSender()){
signerTwoTime = block.timestamp + 1 hours;
return signerTwoTime;
} else {
return 0;
}
}
function getSignerTime() public view signerVerifed returns (uint256) {
if (secureSignerOne == _msgSender()){
return signerOneTime;
} else if (secureSignerTwo == _msgSender()){
return signerTwoTime;
} else {
return 0;
}
}
function signerAuth() public view signerOrOwner returns (bool){
if (signerOneTime > block.timestamp && signerTwoTime > block.timestamp) {
return true;
} else {
return false;
}
}
function transferSigner(address _newSigner) public virtual signerVerifed {
if (secureSignerOne == _msgSender()){
require(_newSigner != address(0) && _newSigner != _msgSender(), "Input is zero address or self");
require(_newSigner != contractOwner && _newSigner != secureSignerTwo, "Input is contractOwner or other signer");
secureSignerOne = _newSigner;
emit SignerTransferred(_msgSender(), _newSigner);
} else if (secureSignerTwo == _msgSender()){
require(_newSigner != address(0) && _newSigner != _msgSender(), "Input is zero address or self");
require(_newSigner != contractOwner && _newSigner != secureSignerOne, "Input is contractOwner or other signer");
secureSignerTwo = _newSigner;
emit SignerTransferred(_msgSender(), _newSigner);
}
}
function isSigner() public view returns (bool){
if (secureSignerOne == _msgSender()){
return true;
} else if (secureSignerTwo == _msgSender()){
return true;
} else{
return false;
}
}
function getContractOwner() public view returns (address) {
return contractOwner;
}
function setFeeOff() public secureAuth {
previousFee = feePercent;
feePercent = 0;
liquiditySwapEnabled = false;
emit feeOn(false);
}
function setFeeOn() public secureAuth {
feePercent = previousFee;
liquiditySwapEnabled = true;
emit feeOn(true);
}
function setLiquiditySwap(bool status) public secureAuth {
liquiditySwapEnabled = status;
emit SetLiquiditySwap(status);
}
function setFeePercent(uint256 _feePercent) public secureAuth {
require (_feePercent > feeDivisor);
emit changeFeePercent(feePercent, _feePercent);
feePercent = _feePercent;
}
function setDivisor(uint256 _divisor) public secureAuth {
require (_divisor < feePercent);
emit changeFeeDivisor(feeDivisor, _divisor);
feeDivisor = _divisor;
}
function setFeeAddress(address payable _feeAddress) public secureAuth {
feeAddress = _feeAddress;
}
function setSwapThreshold(uint256 amount) public secureAuth {
require(amount > 0 && amount < _totalSupply);
emit setTokenSwapThreshold(nativeSwapThresh, amount);
nativeSwapThresh = amount;
}
function excludeFromFee(address user) public secureAuth {
feeExcluded[user] = true;
}
function includeInFee(address user) public secureAuth {
feeExcluded[user] = false;
}
function transferOwnership(address _newOwner) public virtual secureAuth {
require(_newOwner != address(0) && _newOwner != secureSignerOne && _newOwner != secureSignerTwo, "Input is zero address or a signer");
emit OwnershipTransferred(contractOwner, _newOwner);
contractOwner = _newOwner;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public pure returns (uint8) {
return 18;
}
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/*
Approve & Allowance
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
}
return true;
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
/*
Transfers
*/
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_transfer(sender, recipient, amount);
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
}
return true;
}
function _transfer(address sender, address recipient, uint256 amount) private {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
require(amount > 0, "Transfer amount must be greater than zero");
uint256 contractTokenBalance = balanceOf(address(this));
uint256 leftover;
if (!inSwapAndLiquify && liquiditySwapEnabled && recipient == uniswapV2Pair) {
if (contractTokenBalance >= nativeSwapThresh) {
contractTokenBalance = ((nativeSwapThresh) * (feeDivisor)) / (feePercent);
leftover = nativeSwapThresh - contractTokenBalance;
feeTokenSwap(contractTokenBalance);
_transfer(address(this), deadAddress, leftover);
}
}
bool takeFee = true;
if(feeExcluded[sender] || feeExcluded[recipient]){
takeFee = false;
}
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[sender] = senderBalance - amount;
}
if (takeFee && feePercent > 0) {
uint256 feeAmount = getFeeAmount(amount, takeFee);
uint256 transferAmount = amount - feeAmount;
_balances[recipient] = _balances[recipient] + transferAmount;
_balances[address(this)] = _balances[address(this)] + feeAmount;
} else {
_balances[recipient] += amount;
}
emit Transfer(sender, recipient, amount);
}
function feeTokenSwap(uint256 amount) private lockSwap {
swapTokensForEth(amount);
}
function swapTokensForEth(uint256 tokenAmount) private {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
feeAddress,
block.timestamp
);
emit SwapTokensForETH(tokenAmount, path);
}
function _approve(address owner,address spender,uint256 amount) private {
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 getFeeAmount(uint256 amount, bool takeFee) private view returns(uint256) {
if (!takeFee){
return 0;
} else {
return amount*feePercent/(10**2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment