I am trying to test 1inch swap function from the contract deployed via Remix on a Ganache fork of the Ethereum Mainnet. When calling this function I always get the "Gas estimation failed" error and "VM Exception while processing transaction: revert" error in Remix. My contract has enough sold off token (USDC) and Ether.

The interface of the swap function looks as follows:

function swap(
            IERC20 fromToken,
            IERC20 toToken,
            uint256 amount,
            uint256 minReturn,
            uint256[] memory distribution,
            uint256 featureFlags
        ) public payable;

The contract code:

contract TradeBot {
// OneSplit Config
    address ONE_SPLIT_ADDRESS = 0xC586BeF4a0992C495Cf22e1aeEE4E446CECDee0E;
    uint256 PARTS = uint256(10);
    uint256 FLAGS = uint256(0);

   function oneSplitSwap(address _from, address _to, uint256 _amount, uint256 _minReturn, 
                        uint256[] memory _distribution) public payable {
        _oneSplitSwap(_from, _to, _amount, _minReturn, _distribution);

   function _oneSplitSwap(address _from, address _to, uint256 _amount, uint256 _minReturn, 
                          uint256[] memory _distribution) internal {
        // Setup contracts
        IERC20 _fromIERC20 = IERC20(_from);
        IERC20 _toIERC20 = IERC20(_to);
        IOneSplit _oneSplitContract = IOneSplit(ONE_SPLIT_ADDRESS);
        // Approve tokens
        _fromIERC20.approve(ONE_SPLIT_ADDRESS, _amount);
        // Swap tokens: give _from, get _to
        _oneSplitContract.swap(_fromIERC20, _toIERC20, _amount, _minReturn, _distribution, 
        // Reset approval
        _fromIERC20.approve(ONE_SPLIT_ADDRESS, 0);

I test the function with the following parameters:

fromToken: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 // USDC
toToken:   0x6B175474E89094C44Da98b954EedeAC495271d0F // DAI
amount:    100000000
minReturn: 10 
distribution: [0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0]

Can't figure out why this does not work. All parameters seem to be correct and the contract has enough USDC. Any ideas?


After checking some successful transactions I used the FLAGS parameter set to "4406099890176" instead of default 0 and it worked. However, it does not work with the default 0 flag value. Also, had to use addresses instead of IERC20 types for "from" and "to" parameters.

  • To begin with, I'd try with passing minReturn as 0. Or better yet (if you don't wanna lose what you have), find out what this value should be by calling the getExpectedReturn function (or some function with a similar name) on the same contract. – goodvibration Nov 24 '20 at 13:14
  • @goodvibration thanks for your suggestion. I tried both setting to 0 and returnValue but get the same result: "Gas estimation failed" and "VM Exception while processing transaction: revert". I also tried setting minReturn to something above the expected return value to trigger revert with a valuable message but it seems that the revert causing the trouble happens before the actual returnAmount - minAmount comparison. Do you have any other ideas? I don't get how this simple function could not possibly work. Might be something wrong with the contract code. I'll appreciate your help. – Mttank Nov 24 '20 at 14:29
  • It's not that simple though. The function in _oneSplitContract calls a function in yet another contract (a proxy of some sort), which then calls the actual function via delegate call, which, if reverts, then reverts without an error-message. It's all on Etherscan, so you can follow the code just like I did, and you might come up with some other insights. – goodvibration Nov 24 '20 at 14:35
  • @goodvibration did you find it in the 1inch contract source code on Etherscan? – Mttank Nov 24 '20 at 14:47
  • 1
    @goodvibration thank you, updated the question with my finding. It has something to do with flags and the way we pass token addresses but I am not sure. I made it work by changing the FLAGS value and passing tokens as addresses. – Mttank Nov 24 '20 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.