web3swift-team / web3swift

Elegant Web3js functionality in Swift. Native ABI parsing and smart contract interactions.
Apache License 2.0
818 stars 441 forks source link

gas required exceeds allowance or always failing transaction #860

Open sonukumar35 opened 5 months ago

sonukumar35 commented 5 months ago

What happened?

I am trying to send vote using ABI string and contract address, but getting error message always , i have tried changing options, args , parameters several time but getting same response

What are the steps to reproduce?

func voteSmartContract(voteCount: Double) {

    guard let urlStr = URL(string: rpcURL) else {
        print(" error in creating URL die rpc \(#file)  ===> \(#function)")
        return
    }

    guard let kManager = wethioKeystoreManager else { return }

    web3ProvideInstance = Web3HttpProvider(urlStr, keystoreManager: kManager)
    guard let wProvier = self.web3ProvideInstance else {return}

    self.web3Instance = Web3(provider: wProvier)
    guard let wInstance = self.web3Instance else {return}

    DispatchQueue.global(qos: .background).async {
        do {

            let validatorContractEthAddress = EthereumAddress(self.contractAddress)

            let toaddress = EthereumAddress(self.candidateMasternode!.candidate!)

            let senderEthAddress = EthereumAddress(self.etheriumAccountAddress ?? "")

            let estimateGasPrice = try wInstance.eth.getGasPrice()
            print(estimateGasPrice)

            let amountDouble = BigInt((voteCount)*pow(10, 18))

            let amount = BigUInt.init(amountDouble)

            var options = Web3Options.defaultOptions()

            options.from = senderEthAddress
            options.gasLimit = 37162 //gas estimated price
            options.value = amount
            options.gasPrice = 250000000

            let tokenTransactionIntermediate = try wInstance.contract(Web3.Utils.validatorABI2, at: validatorContractEthAddress).method("vote", args: toaddress, options: options)

            let mainTransaction = try? tokenTransactionIntermediate.send(options: options, onBlock: "latest")

            print(mainTransaction)
            print(mainTransaction?.hash)

            DispatchQueue.main.async {
                self.loaderInstance.hideIndicator()

            }

        } catch {
            print(error)
            DispatchQueue.main.async {
                self.loaderInstance.hideIndicator()
                self.showAlertMessage(message: "Unable to send Token")
            }
        }
    }
}

What is the expected behavior?

I expect that this function should return a valid hash after completing smart contract to the vote function

What is the error thrown?

web3swift.JsonRpcResponse.ErrorMessage(code: -32000, message: "gas required exceeds allowance or always failing transaction")

What's the stack trace said?

func voteSmartContract(voteCount: Double) {

    guard let urlStr = URL(string: rpcURL) else {
        print(" error in creating URL die rpc \(#file)  ===> \(#function)")
        return
    }

    guard let kManager = wethioKeystoreManager else { return }

    web3ProvideInstance = Web3HttpProvider(urlStr, keystoreManager: kManager)
    guard let wProvier = self.web3ProvideInstance else {return}

    self.web3Instance = Web3(provider: wProvier)
    guard let wInstance = self.web3Instance else {return}

    DispatchQueue.global(qos: .background).async {
        do {

            let validatorContractEthAddress = EthereumAddress(self.contractAddress)

            let toaddress = EthereumAddress(self.candidateMasternode!.candidate!)

            let senderEthAddress = EthereumAddress(self.etheriumAccountAddress ?? "")

            let estimateGasPrice = try wInstance.eth.getGasPrice()
            print(estimateGasPrice)

            let amountDouble = BigInt((voteCount)*pow(10, 18))

            let amount = BigUInt.init(amountDouble)

            var options = Web3Options.defaultOptions()

            options.from = senderEthAddress
            options.gasLimit = 37162 //gas estimated price
            options.value = amount
            options.gasPrice = 250000000

            let tokenTransactionIntermediate = try wInstance.contract(Web3.Utils.validatorABI2, at: validatorContractEthAddress).method("vote", args: toaddress, options: options)

            let mainTransaction = try? tokenTransactionIntermediate.send(options: options, onBlock: "latest")

            print(mainTransaction)
            print(mainTransaction?.hash)

            DispatchQueue.main.async {
                self.loaderInstance.hideIndicator()

            }

        } catch {
            print(error)
            DispatchQueue.main.async {
                self.loaderInstance.hideIndicator()
                self.showAlertMessage(message: "Unable to send Token")
            }
        }
    }
}

OS version

mac OS 12.6.2

Library version

web3swift.pod (2.1.10) xcode Version 14.1

WazirxDax commented 1 month ago

@sonukumar35 Have you found the solution? I am facing same issue.