Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to fetch gas estimate when trying to test contract method #326

Closed
Gabets opened this issue Jun 9, 2021 · 7 comments
Closed

Failed to fetch gas estimate when trying to test contract method #326

Gabets opened this issue Jun 9, 2021 · 7 comments
Labels
help wanted Extra attention is needed

Comments

@Gabets
Copy link

Gabets commented Jun 9, 2021

Hi all!
I'm tried to create simple iOS app and test contract on Kovan network and when I send transaction I get error:
processingError(desc: "Failed to fetch gas estimate")
My code:

main function:

private func addLiquidity() {        
        let token0: web3swift.EthereumAddress = EthereumAddress("0xd0a1e359811322d97991e03f863a0c30c2cf029c")! //  (WETH)
        let token1: web3swift.EthereumAddress = EthereumAddress("0x1484a6020a0f08400f6f56715016d2c80e26cdc1")! // (USDT)
        let ethAddress: web3swift.EthereumAddress = EthereumAddress("0xA394bE3499bece634fcBF4d89F2DFe9dABD72B54")! // wallet

        let amount0 = Web3.Utils.parseToBigUInt("0.5", units: .eth) ?? 0
        let amount1 = Web3.Utils.parseToBigUInt("1400.0", units: .eth) ?? 0
        
        let timeInterval = Date().timeIntervalSince1970
        let deadline = Int(timeInterval) + 300 
        
        var params = [Any]()
        params.append(token0)
        params.append(token1)
        params.append(ethAddress)
        params.append(amount0)
        params.append(amount1)
        params.append(BigUInt(0))
        params.append(BigUInt(0))
        params.append(BigUInt(deadline))
        
        let parameters = [params as AnyObject]        
        let contract = getContract()
        
        var options = TransactionOptions.defaultOptions
        options.value = amount0
        options.from = ethAddress
        options.gasPrice = .automatic
        options.gasLimit = .automatic
        
        let method: String = "addLiquidity"
        let tx: WriteTransaction? = contract.write(
            method,
            parameters: parameters as [AnyObject],
            extraData: Data(),
            transactionOptions: options)
        
        guard let transaction = tx else {
            print(" LOG transaction is nil")
            return
        }

        do {
            let result = try transaction.send(password: "") //yes, empty password string :) with transactionOptions: options got same error
            print(" LOG output \n", result.transaction.description as Any)
        } catch {
            print(error) // got processingError(desc: "Failed to fetch gas estimate")
        }
    }

getContract function:

private func getContract() -> web3.web3contract {        
        let contractABI: String = """
        [{"inputs":[{"internalType":"address","name":"router_","type":"address"},{"internalType":"address","name":"factory_","type":"address"},{"internalType":"address","name":"pocket_","type":"address"},{"internalType":"address","name":"WETH9_","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH9","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount0Desired","type":"uint256"},{"internalType":"uint256","name":"amount1Desired","type":"uint256"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"internalType":"struct IPocketSwapLiquidityRouter.AddLiquidityParams","name":"params","type":"tuple"}],"name":"addLiquidity","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pocket","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"rewards","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"internalType":"struct IPocketSwapLiquidityRouter.RemoveLiquidityParams","name":"params","type":"tuple"}],"name":"removeLiquidity","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"router","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMinimum","type":"uint256"}],"internalType":"struct IPocketSwapRouter.SwapParams","name":"params","type":"tuple"}],"name":"swap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"sweepToken","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"unwrapWETH9","outputs":[],"stateMutability":"payable","type":"function"}]
        """

        let web3 = infuraKovanWeb3()        
        let contractAddress: web3swift.EthereumAddress = EthereumAddress("0x812809F2BeAE43ba3fC9719fAb171E2A20F9Fb50")!
        let contract = web3.contract(contractABI, at: contractAddress)

        return contract!
    }

and infuraKovanWeb3 function:

private func infuraKovanWeb3(accessToken: String? = nil) -> web3 {
        let infura = InfuraProvider(Networks.Kovan, accessToken: accessToken)!
        return web3(provider: infura)
    }

could somebody help me with this issue?

можно отвечать и на русском)

@Iysbaera Iysbaera added the help wanted Extra attention is needed label Jun 9, 2021
@Gabets
Copy link
Author

Gabets commented Jun 12, 2021

As I think, main problem - correcttly pass parameters in to the contract method. In this exaple
photo_2021-06-11_22-01-49
inputs is an object that contains several parameters

My question is: "How do I pass the oblect correctly"?

I tryed pass as object, as dictionary, as tuple, as array of tuples but all time I got problem in this place:
photo_2021-06-11_22-01-49 (2)
Object values are treated as one value, not as array that can be encoded.

For example, contract method input argument is object named params and below his representation:

struct InputParams {
    address token; // Address of the token
    uint256 amountDesired; // Amount of the token
    uint256 deadline; // Reverts in case of transaction confirmed too late 
}

How I can pass it to contract method? Any ideas?

@Gabets
Copy link
Author

Gabets commented Jun 14, 2021

little update:
if I try

let result1 = try transaction.call()

I get nodeError(desc: "VM execution error.")

if I try

let estimate = try transaction.estimateGas(transactionOptions: options)

I got nodeError(desc: "The execution failed due to an exception.")

still need help :(

@Iysbaera
Copy link
Collaborator

@Gabets Hey, please check #324, it might be the solution for your issue,

@Gabets
Copy link
Author

Gabets commented Jun 23, 2021

@Iysbaera Hello, I will look, thank you.

@Gabets
Copy link
Author

Gabets commented Aug 9, 2021

@AlleniCode Hello!
Thank you , but not the same I think. For example https://etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#writeContract
look at addLiquidity method parameters and same method the contract I work with: https://kovan.etherscan.io/address/0x812809F2BeAE43ba3fC9719fAb171E2A20F9Fb50#writeContract
problem was - how to pass object, I hadn't problem with the simple values

@anonymouz4
Copy link

Hi, removing the value fixed it for me, see #147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants