0

I'm trying to developp my first smart contract to make some arbitrage between DEX.

I started by using the code from : https://github.com/pugzly/token_swap/blob/main/token_swap.sol

But when I execute the swap function with :
_tokenIn : WBNB address
_tokenOut : BUSD address
_amountIn: 0x038d7ea4c68000 //0.001 WBNB
_amountOutMin : from getAmountMin function
_to : My address

I have an error with gas estimation but impossible for me to understand why : enter image description here

Here is my code :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

interface IERC20 {
    function totalSupply() external view returns (uint);
    function balanceOf(address account) external view returns (uint);
    function transfer(address recipient, uint amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint);
    function approve(address spender, uint amount) external returns (bool);
    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

interface IUniswapV2Router {
  function getAmountsOut(uint256 amountIn, address[] memory path)
    external
    view
    returns (uint256[] memory amounts);
  
  function swapExactTokensForTokens(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] calldata path,
    address to,
    uint256 deadline
  ) external returns (uint256[] memory amounts);
}

interface IUniswapV2Pair {
  function token0() external view returns (address);
  function token1() external view returns (address);
  function swap(
    uint256 amount0Out,
    uint256 amount1Out,
    address to,
    bytes calldata data
  ) external;
}


contract ArbitrageV1{
    event approve(uint256 buy1,uint256 _montant);
    event estimate(uint256 montan);
    address private constant routerPCS = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
    address private constant wbnb1=0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;

    
    function swap(address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _amountOutMin, address _to) external {
        IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn);
    
        IERC20(_tokenIn).approve(routerPCS, _amountIn);

        address[] memory path;
        if (_tokenIn == wbnb1 || _tokenOut == wbnb1) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
        } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = wbnb1;
            path[2] = _tokenOut;
        }
        IUniswapV2Router(routerPCS).swapExactTokensForTokens(_amountIn, _amountOutMin, path, _to, block.timestamp);
    }receive() payable external {}
    
    function getAmountOutMin(address _tokenIn, address _tokenOut, uint256 _amountIn) external view returns (uint256) {
        address[] memory path;
        if (_tokenIn == wbnb1 || _tokenOut == wbnb1) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
        } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = wbnb1;
            path[2] = _tokenOut;
        }
        uint256[] memory amountOutMins = IUniswapV2Router(routerPCS).getAmountsOut(_amountIn, path);
        return amountOutMins[path.length -1];
    } 
    }

I would be very greatful if you can help me,

UPDATE : Thanks for your answer Rihe, I just changed my gas settings but it still doesn't work. enter image description here

UPDATE 2 : Thanks again, I just removed the transferFrom line as you recommended me.

I still have the Gas Estimation failed error, but it's more detailed : enter image description here

I know that this error is generally linked to approve problem. Do you have any idea to solve it ?

2
  • What are the gas price settings?
    – rihekopo
    May 30, 2022 at 16:04
  • There is no gas settings, I don't find where to put it...
    – Flavien
    May 30, 2022 at 16:17

1 Answer 1

1

I believe your gas settings are incorrect. In the Remix IDE you can fix it easily. Please find the below image. I think something like 300000 for gaslimit and 5 gwei for value should be fineenter image description here.

UPDATE

After checking your code I observed that your swap function may be incorrect. When you call the swapExactTokensForTokens, you don't have to manually make a transfer. The router will handle it. You just need to approve it.

function swap(address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _amountOutMin, address _to) external {    
    IERC20(_tokenIn).approve(routerPCS, _amountIn);
address[] memory path;
if (_tokenIn == wbnb1 || _tokenOut == wbnb1) {
    path = new address[](2);
    path[0] = _tokenIn;
    path[1] = _tokenOut;
} else {
    path = new address[](3);
    path[0] = _tokenIn;
    path[1] = wbnb1;
    path[2] = _tokenOut;
}
IUniswapV2Router(routerPCS).swapExactTokensForTokens(_amountIn, _amountOutMin, path, _to, block.timestamp);
}
7
  • Thanks, I just updated my question with your suggestion but that didn't solve the problem
    – Flavien
    May 30, 2022 at 16:41
  • It is not easy without the full code. BTW other thing I see is that you make a transfer before approving the token in your swap function. That will cause an error too. @Flavien
    – rihekopo
    May 30, 2022 at 16:49
  • This is my full code, only using this function swap. Yes, I saw that when I only ask approve function, there is no problem, but when I use TransferFrom there is the gas issue. So do you recommend me to put my Approve before my TransferFrom ? Thx
    – Flavien
    May 30, 2022 at 16:52
  • The approve is to allow the contract to spend my tokens on PCS router. But I don't understand if I need to approve the contract for my wallet before transferFrom, if yes, idk where to do it
    – Flavien
    May 30, 2022 at 16:55
  • @Flavien I added a modified version from your code. The point is you don't have to make the transfer in this case.
    – rihekopo
    May 30, 2022 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.