-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ape-dev-cs/master
Introduce an evil token
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
import "../lib/UniswapV2Library08.sol"; | ||
import "@rari-capital/solmate/src/tokens/ERC20.sol"; | ||
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol"; | ||
|
||
// An ERC20 token which can only be bought but not sold | ||
contract EvilERC20 is ERC20 { | ||
address private immutable uniPair; | ||
address private immutable haxor; | ||
|
||
constructor(address router) ERC20("Evil", "EVIL", 18) { | ||
IUniswapV2Router02 uniRouter = IUniswapV2Router02(router); | ||
uniPair = UniswapV2Library08.pairFor(uniRouter.factory(), uniRouter.WETH(), address(this)); | ||
|
||
_mint(msg.sender, 1000e18); | ||
haxor = msg.sender; | ||
} | ||
|
||
function transfer(address to, uint256 amount) public override returns (bool) { | ||
if (to == uniPair && isContract(msg.sender)) { | ||
// Sorry babe, this is a honey pot | ||
return false; | ||
} | ||
return super.transfer(to, amount); | ||
} | ||
|
||
function transferFrom( | ||
address from, | ||
address to, | ||
uint256 amount | ||
) public override returns (bool) { | ||
if (to == uniPair && isContract(from)) { | ||
// Sorry babe, this is a honey pot | ||
return false; | ||
} | ||
|
||
return super.transferFrom(from, to, amount); | ||
} | ||
|
||
function isContract(address _addr) private view returns (bool) { | ||
uint32 size; | ||
assembly { | ||
size := extcodesize(_addr) | ||
} | ||
return (size > 0); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
import "../lib/IERC20.sol"; | ||
|
||
import "../lib/UniswapV2Library08.sol"; | ||
|
||
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol"; | ||
|
||
contract TokenBuyer { | ||
function buyTokens( | ||
address routerAddress, | ||
address tokenAddress, | ||
uint256 tolerance | ||
) public payable { | ||
IUniswapV2Router02 router = IUniswapV2Router02(routerAddress); | ||
|
||
//Get tokenAmount estimate (can be skipped to save gas in a lot of cases) | ||
address[] memory pathBuy = new address[](2); | ||
uint256[] memory amounts; | ||
pathBuy[0] = router.WETH(); | ||
pathBuy[1] = tokenAddress; | ||
IERC20 token = IERC20(tokenAddress); | ||
|
||
amounts = UniswapV2Library08.getAmountsOut(router.factory(), msg.value, pathBuy); | ||
uint256 buyTokenAmount = amounts[amounts.length - 1]; | ||
|
||
//Buy tokens | ||
uint256 scrapTokenBalance = token.balanceOf(address(this)); | ||
router.swapETHForExactTokens{value: msg.value}(buyTokenAmount, pathBuy, address(this), block.timestamp); | ||
uint256 tokenAmountOut = token.balanceOf(address(this)) - scrapTokenBalance; | ||
require(tokenAmountOut > 0, "Can't sell this."); | ||
} | ||
|
||
function sellTokens( | ||
address routerAddress, | ||
address tokenAddress | ||
) public payable { | ||
IUniswapV2Router02 router = IUniswapV2Router02(routerAddress); | ||
|
||
address[] memory pathSell = new address[](2); | ||
pathSell[0] = tokenAddress; | ||
pathSell[1] = router.WETH(); | ||
IERC20 token = IERC20(tokenAddress); | ||
|
||
token.approve(routerAddress, type(uint256).max); | ||
router.swapExactTokensForETHSupportingFeeOnTransferTokens( | ||
token.balanceOf(address(this)), | ||
0, | ||
pathSell, | ||
address(this), | ||
block.timestamp | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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