interfaceIUniswapV2Router02{
function swapExactTokensForTokens(
uint256 amountIn, //输入代币数量(用户卖出数量
uint256 amountOutMin, //用户希望输出的代币数量
address[] calldata path, //交易路径,例如weth到usdc
address to, // 接受代币的地址
uint256 deadline // 截止交易时间
)externalreturns(uint256[] memory amounts);
}//实际返回的交易数量
interfaceIWETH{
function deposit()externalpayable;
function approve(address guy, uint256 wad)externalreturns(bool);
function withdraw(uint256 wad)external;
}
//设置一个uniswap地址池,交易币种为IWETH和USDT
contract ContractTest is Test {
address UNISWAP_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; // Uniswap Router address on Ethereum Mainnet
IWETH WETH = IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
address USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
//它创建了一个以太坊主网的分叉,从区块号17568400开始
function setUp()public{
vm.createSelectFork("mainnet", 17568400);
}
functiont estswapTokensWithMaxDeadline()externalpayable{
WETH.approve(address(UNISWAP_ROUTER), type(uint256).max);
WETH.deposit{value: 1 ether}();
uint256 amountIn = 1 ether;
//定义了最小接收数量,这里设置为0
uint256 amountOutMin = 0;
//uint256 amountOutMin = 1867363899; //1867363899 INSUFFICIENT_OUTPUT_AMOUNT
// Path for swapping ETH to USDT
address[] memory path = new address[](2);
path[0] = address(WETH); // WETH (Wrapped Ether)
path[1] = USDT; // USDT (Tether)
// No Effective Expiration Deadline
// The function sets the deadline to the maximum uint256 value, which means the transaction can be executed at any time,
// possibly under unfavorable market conditions.
IUniswapV2Router02(UNISWAP_ROUTER).swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
address(this),
type(uint256).max // Setting deadline to max value
);
console.log("USDT", IERC20(USDT).balanceOf(address(this)));
}
receive() external payable {}
}
functiont estswapTokensWithMaxDeadline()externalpayable{
WETH.approve(address(UNISWAP_ROUTER), type(uint256).max);
WETH.deposit{value: 1 ether}();
uint256 amountIn = 1 ether;
//定义了最小接收数量,这里设置为0
uint256 amountOutMin = 0;
//uint256 amountOutMin = 1867363899; //1867363899 INSUFFICIENT_OUTPUT_AMOUNT
// Path for swapping ETH to USDT
address[] memory path = new address[](2);
path[0] = address(WETH); // WETH (Wrapped Ether)
path[1] = USDT; // USDT (Tether)
// No Effective Expiration Deadline
// The function sets the deadline to the maximum uint256 value, which means the transaction can be executed at any time,
// possibly under unfavorable market conditions.
IUniswapV2Router02(UNISWAP_ROUTER).swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
address(this),
type(uint256).max // Setting deadline to max value
);
uint256 amountOutMin = amountIn * 99 / 100;
uint256 amountOutMin = amountIn * 995 / 1000;
原文始发于微信公众号(Ice ThirdSpace):DeFiVulnLabs靶场全系列详解(三十六)没有设置滑点保护,允许最小代币接收数量为0,导致代币价值遭受损失
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论