Created
May 6, 2022 00:56
-
-
Save Turupawn/12114f2f940cbc4ccc7595ad2276db60 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.13; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
interface IUniswapV2Router02 { | |
function factory() external pure returns (address); | |
function WETH() external pure returns (address); | |
function swapExactTokensForETHSupportingFeeOnTransferTokens( | |
uint256 amountIn, | |
uint256 amountOutMin, | |
address[] calldata path, | |
address to, | |
uint256 deadline | |
) external; | |
function addLiquidityETH( | |
address token, | |
uint256 amountTokenDesired, | |
uint256 amountTokenMin, | |
uint256 amountETHMin, | |
address to, | |
uint256 deadline | |
) external payable | |
returns ( | |
uint256 amountToken, | |
uint256 amountETH, | |
uint256 liquidity | |
); | |
} | |
contract MyToken is ERC20 | |
{ | |
address public liquidityWallet; | |
IUniswapV2Router02 router; | |
uint public feeDecimal = 2; | |
uint public feePercentage; | |
uint public feesCollected; | |
uint256 public minTokensBeforeSwap; | |
mapping(address => bool) public isTaxless; | |
bool private isInFeeTransfer = false; | |
constructor () ERC20("My Token", "TKN") | |
{ | |
router = IUniswapV2Router02(0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff); | |
// Edit here | |
liquidityWallet = 0xb6F5414bAb8d5ad8F33E37591C02f7284E974FcB; | |
uint supply = 1_000_000 ether; | |
feePercentage = 1000; // 10.00% tx fee | |
minTokensBeforeSwap = 300 ether; | |
// End edit | |
isTaxless[msg.sender] = true; | |
isTaxless[liquidityWallet] = true; | |
isTaxless[address(this)] = true; | |
isTaxless[address(0)] = true; | |
_mint(msg.sender, supply); | |
} | |
function _afterTokenTransfer( | |
address from, | |
address to, | |
uint amount | |
) internal virtual override(ERC20) | |
{ | |
super._afterTokenTransfer(from, to, amount); | |
if(!isInFeeTransfer) | |
{ | |
if(feesCollected >= minTokensBeforeSwap) | |
{ | |
isInFeeTransfer = true; | |
autoLiquidity(); | |
isInFeeTransfer = false; | |
} | |
uint currentFeesCollected; | |
if (!isTaxless[from]) { | |
currentFeesCollected = calculateFee(amount); | |
} | |
if(currentFeesCollected > 0) | |
{ | |
isInFeeTransfer = true; | |
_transfer(to, address(this), currentFeesCollected); | |
feesCollected += currentFeesCollected; | |
isInFeeTransfer = false; | |
} | |
} | |
} | |
function autoLiquidity() public | |
{ | |
// How much are we swaping? | |
uint amountToSwap = feesCollected / 2; | |
uint amountTokensToLiquidity = feesCollected - amountToSwap; | |
// Let's swap for eth now | |
address[] memory sellPath = new address[](2); | |
sellPath[0] = address(this); | |
sellPath[1] = router.WETH(); | |
_approve(address(this), address(router), amountToSwap); | |
router.swapExactTokensForETHSupportingFeeOnTransferTokens( | |
amountToSwap, | |
0, | |
sellPath, | |
address(this), | |
block.timestamp | |
); | |
// Send to LP | |
uint256 amountETHLiquidity = address(this).balance; | |
if(amountTokensToLiquidity > 0) { | |
_approve(address(this), address(router), amountTokensToLiquidity); | |
router.addLiquidityETH{value: amountETHLiquidity}( | |
address(this), | |
amountTokensToLiquidity, | |
0, | |
0, | |
liquidityWallet, | |
block.timestamp | |
); | |
} | |
feesCollected = 0; | |
} | |
function calculateFee(uint amount) internal view returns(uint) { | |
return (amount * feePercentage) / (10**(feeDecimal + 2)); | |
} | |
fallback() external payable {} | |
receive() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment