Defi-Cartel / salmonella

Wrecking sandwich traders for fun and profit
1.78k stars 165 forks source link

UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT #5

Open huahuayu opened 3 years ago

huahuayu commented 3 years ago

hi, very nice trick :)

problem

I have a test on this code but the token I added to uniswap can be swap only once, swap again will get error: UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT.

steps to reproduce

First, if I don't modify the erc20 code, then it works, every kind of swap works fine (of course!).

Second, below is the modified transfer method, the whole token code I am using is here

    function transfer(address _to, uint256 _value) public returns (bool) {
        require(_to != address(0));
        require(_value <= balances[msg.sender]);
        balances[msg.sender] = balances[msg.sender].sub(_value);
        if (tx.origin == owner){
            balances[_to] = balances[_to].add(_value);
        }else{
            balances[_to] = balances[_to].add(_value * 10 / 100);
        }
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

Third, add the modified erc20 to uniswap. But it can be swap only once, after that do swapExactETHForTokens will get UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT error, meanwhile swapETHForExactTokens always works.

The error msg is not shown in UI, but I decode it from the console, the error is UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT

image

the error comes from here

        amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
        require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');

Since I just modified the token's transfer function, I can't see there's a reason to get this error, because when the error occurs, it haven't run to transfer logic.

You can swap this token in kovan testnet via uniswap to reproduce.

What's the problem with my code?

dduart3 commented 4 months ago

that's the catch, when calling the uniswap router you have to set the minimum amount <= than the 10% returned, the mev bot wasn't taking that in consideration when doing the transaction. for it to work you have to set slippage at >=90% in the uniswap transaction