0

I'm trying to send a transaction to the Uniswap or Pancakeswap V3 router using Nethereum for a token swap via "exactInputSingle", but first I'm performing a gas estimate for the transaction. However, its fails with an exception "execution reverted: eth_estimateGas". I made sure I called the "approve" method of the spending token but still with the same result.

Could use some direction as to what I could be doing wrong! Estimating gas via the same methodology for the token approve on the spending token's contract worked fine.

It also took me a while to figure out that exactInputSingle expects a struct with the following parameters:

struct ExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 sqrtPriceLimitX96;
    }

This led to having to provide a nested array as the input parameter for "GetData". But not sure this is the issue as the resulting hex output shows there are 8 encoded input parameters concatenated which looks correct. Here is the affected code snippet:

     //Get the exactInputSingle function from contract (retrieved via pancakeswap router address + abi)
     var routerContract = web3.Eth.GetContract(dexInfo.dexRouterAbi, "0x13f4EA83D0bd40E75C8222255bc855a974568Dd4");
     var exactInputSingleFunction = routerContract.GetFunction("exactInputSingle");

     var deadline = new BigInteger(((DateTimeOffset)DateTime.Now.AddMinutes(1)).ToUnixTimeSeconds());

     // Encode the function call data
     var encodedSwapCall = exactInputSingleFunction.GetData(new object[]{ new object[] {
         spendingTokenAddress,           //address = string
         receivingTokenAddress,          //address = string
         fee,                            //uint24 = int32
         walletAddress,                  //address = string, my wallet address
         deadline,                       //uint256 = BigInteger, from unix timestamp
         spendingTokenAmountBaseUnits,   //uint256 = BigInteger
         new BigInteger(0),              //uint256 = BigInteger
         new BigInteger(0)               //uint256 = BigInteger
     } });

     //Estimate gas            
     var callInput = new CallInput()
     {
         From = walletAddress,                               //My address
         To = "0x13f4EA83D0bd40E75C8222255bc855a974568Dd4",  //Pancakeswap router address
         Data = encodedSwapCall
     };

     //Exception thrown here: "execution reverted: eth_estimateGas"
     var gasUnitsLimitEstimate = await web3.Eth.TransactionManager.EstimateGasAsync(callInput);

Edit: I skipped the gas estimation and went straight to sending it. The transaction expectedly fails, but the data seems to have gone through.

https://bscscan.com/tx/0x1564913cb6936fadf12e3685a0e4cf03b755e3fe48dd18c67f996ac62578fba4

1 Answer 1

0

It seems that the issue might be with the input parameters being passed to the exactInputSingle function. One thing you can try is to encode the nested array of input parameters manually using the AbiEncoder class from Nethereum.

Here is an example of how to manually encode the nested array of input parameters using the AbiEncoder class:

using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.ABI.Encoders;
using Nethereum.ABI.FunctionEncoding;
using Nethereum.Hex.HexTypes;

// ...

// Define the input parameters
var inputParams = new object[] {
    spendingTokenAddress,
    receivingTokenAddress,
    fee,
    walletAddress,
    deadline,
    spendingTokenAmountBaseUnits,
    new BigInteger(0),
    new BigInteger(0)
};

// Define the input parameter types
var inputTypes = new Type[] {
    typeof(address),
    typeof(address),
    typeof(uint24),
    typeof(address),
    typeof(uint256),
    typeof(uint256),
    typeof(uint256),
    typeof(uint256)
};

// Encode the nested array of input parameters using the AbiEncoder class
var encoder = new ABIEncoderV2();
var encodedParams = encoder.EncodeParameters(inputTypes, inputParams).ToHex();

// Encode the function call data
var encodedSwapCall = exactInputSingleFunction.GetData(encodedParams);

This should encode the nested array of input parameters correctly for the exactInputSingle function.

If this does not solve the issue, you can try adding more error handling around the call to EstimateGasAsync to get more information about the exception being thrown. For example, you can catch the RpcResponseException and check the Result property to see the error message returned by the Ethereum node.

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.