current position:Home>Flashswap encountered execution reversed: uni:_ Transfertokens: transfer amount exceeded balance error.

Flashswap encountered execution reversed: uni:_ Transfertokens: transfer amount exceeded balance error.

2022-02-04 00:24:06 Q & A of Denglian community

I'm learning today uni Of flashswap, The code is as follows :

contract Flash_swap_arb is Ownable,IUniswapV2Callee{    using SafeMath for uint256;    address public my_to_address = 0x194A025C90D384539D05094eC73F21EC5b81b277;    address public factory;    address payable public uni_router_add = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;    IUniswapV2Router02 public uni_router;    //bytes public data;    address public lastTokenBorrow;    uint public lastAmount;    IUniswapV2Router02 public sushiRouter;    constructor() public{        factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;        uni_router = IUniswapV2Router02(uni_router_add);        sushiRouter = IUniswapV2Router02(0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506);    }    function flashswap(uint amount0,uint amount1) public  {        address token0 = 0xc778417E063141139Fce010982780140Aa0cD5Ab;//weth address on Rinkeby        address token1 = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984;//uni address on Rinkeby       // address token1 = 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735;//dai         address pairAddress = IUniswapV2Factory(factory).getPair(token0,token1);        require(pairAddress != address(0),'There is no such pool');        IUniswapV2Pair(pairAddress).swap(            amount0,amount1,address(this),            bytes('not empty')            );    }    function uniswapV2Call(address sender,uint amount0,uint amount1, bytes calldata _data)external override{        address[] memory path = new address[](2);        uint amountToken = amount0 == 0? amount1:amount0;        address token0 = IUniswapV2Pair(msg.sender).token0();        address token1 = IUniswapV2Pair(msg.sender).token1();        address pairAddress = IUniswapV2Factory(factory).getPair(token0,token1);         require(msg.sender == UniswapV2Library.pairFor(factory,token0,token1),'Unauthorized');        // require(sender == address(this),'only this contract can arb');        path[0] = amount0 == 0? token1:token0;        path[1] = amount0 == 0? token0:token1;        IERC20 token = IERC20(amount0 == 0 ? token1 : token0);        token.approve(address(sushiRouter), amountToken);        lastTokenBorrow = token0;        lastAmount = amount0;        uint deadline = block.timestamp + 300;        uint amountRequired = UniswapV2Library.getAmountsIn(factory,amount1,path)[0];        uint amountReceived = sushiRouter.swapExactTokensForTokens(      amountToken,       amountRequired/10,       path,       msg.sender,       deadline        )[1];    require(amountReceived > amountRequired,'No profit to earn');        IERC20(token0).transfer(pairAddress,amountRequired);        IERC20(token0).transfer(tx.origin, amountReceived - amountRequired);       // IERC20(token1).transfer(pairAddress,amountToken);    }

Then I call flashswap function , The parameters passed are amount0=0 ,amount1=10000000000000000, perform , Just report a mistake :

Gas estimation errored with the following message (see below). Thetransaction execution will likely fail. Do you want to force sending?executionreverted: Uni::_transferTokens: transfer amount exceeds balance {"originalError": { "code": 3, "data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000035556e693a3a5f7472616e73666572546f6b656e733a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000","message": "execution reverted: Uni::_transferTokens: transfer amountexceeds balance" }

What's the matter ? According to the tip , The number of transfers exceeds the balance , Which balance does this balance refer to ?






Other answers 1:

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202040024048990.html

Random recommended