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

Getting error while trying to send transaction "Failed to fetch nonce" #531

Closed
k1ran-ak opened this issue Apr 19, 2022 · 8 comments
Closed

Comments

@k1ran-ak
Copy link

k1ran-ak commented Apr 19, 2022

Previously I was getting error mentioned in #254 after that when I tried to run I'm getting -> processingError(desc: "Failed to fetch nonce")

    let value: String = value
    let walletAddress = EthereumAddress(walletAddress)!
    let toAddress = EthereumAddress(toAddress)!
    let endpoint = URL(string: "https://ropsten.infura.io/v3/fa38f7a943ae4c02a407220a4d442f5f")!
    do {
    let web3 = try Web3.new(endpoint)
    web3.addKeystoreManager(KeystoreManager([globalKeystore]))
    let contract = web3.contract(Web3.Utils.coldWalletABI, at: toAddress, abiVersion: 2)!
    let amount = Web3.Utils.parseToBigUInt(value, units: .eth)
    var options = TransactionOptions.defaultOptions
    options.value = amount
    options.from = walletAddress
    options.gasPrice = .automatic
    options.gasLimit = .automatic
    let tx = contract.write(
    "fallback",
    parameters: [AnyObject](),
    extraData: Data(),
    transactionOptions: options)!
    let mainTransaction = try tx.send(password: password)
    print(mainTransaction.hash, "hash of transaction")
    } catch {
        print(error)
    }

// Any help would be appreciated as I'm new to this repo.

@mloit
Copy link
Contributor

mloit commented Apr 29, 2022

is this still an issue as of 2.6.3? Is the sending address new, or has it made previous transactions?

@k1ran-ak
Copy link
Author

k1ran-ak commented Apr 29, 2022

Yes its an issue as of 2.6.3. The sending account has only one transaction in which it was received some ETH.

@mloit
Copy link
Contributor

mloit commented Apr 29, 2022

have you tried with the built-in Infura provider?
let web3 = InfuraRopstenWeb3()
Also have you validated that walletAddress and your other input parameters are what you expect. Try adding print(tx.transactionOptions) before the line where you call tx.send() to make sure.

@k1ran-ak
Copy link
Author

k1ran-ak commented Apr 29, 2022

Yes I have validated my walletAddress and other input parameters.
Thanks for the input, I have made changed to my code based on your inputs but now I get

inputError(desc: "Failed to locally sign a transaction")

as error.

@mloit
Copy link
Contributor

mloit commented Apr 29, 2022

I suspect your keystore/wallet is not set up correctly then. If you saw a change in behavior between the built-in provider and your original URL, there must be something wrong with your key, and it was resulting in the nonce error, because the node rejected your request.

@k1ran-ak
Copy link
Author

k1ran-ak commented May 2, 2022

I followed as per #285 and
let walletAddress = manager.addresses?.first?.address
I used this walletAddress to get ETH balance using https://api-ropsten.etherscan.io/api?module=account&action=txlist&address=walletAddress... which works completely fine. This is the same walletAddress I'm sending from. so I don't think the problem is with wallet address or Keystore. The url I used from infura is the same as they provided. Even when I used built-in provider as let web3 = Web3.InfuraRopstenWeb3() it throws the same error.

inputError(desc: "Failed to locally sign a transaction")

@mloit
Copy link
Contributor

mloit commented May 2, 2022

"Failed to locally sign a transaction" is an indicator that the public/private key pair don't match up. (hence my earlier comment about the wallet not being set-up correctly)

To elaborate: when web3swiftsigns a transaction, it attempts to recover the public address from the signature to verify. The error you see is what you get when that verification fails.

@k1ran-ak
Copy link
Author

k1ran-ak commented May 5, 2022

I tried with different wallet address and private key. Still the issue exists. Can you share a sample code on how to get the private key ? I used the following

func importWalletWith(privateKey: String){
       let formattedKey = privateKey.trimmingCharacters(in: .whitespacesAndNewlines)
       guard let dataKey = Data.fromHex(formattedKey) else {
           self.showAlertMessage(title: "Error", message: "Please enter a valid Private key ", actionName: "Ok")
           return
       }
       do {
           let keystore =  try EthereumKeystoreV3(privateKey: dataKey)
         
           if let myWeb3KeyStore = keystore {
               let manager = KeystoreManager([myWeb3KeyStore])
               globalKeystore = myWeb3KeyStore
               let address = keystore?.addresses?.first
#if DEBUG
               print("Address :::>>>>> ", address as Any)
               print("Address :::>>>>> ", manager.addresses as Any)
#endif
               let walletAddress = manager.addresses?.first?.address
//                self.walletAddressTF.text = walletAddress ?? "0x"
               let vc = instantiateVC(identifier: "WalletDetailsVC") as! WalletDetailsVC
               vc.walletAddress = walletAddress ?? "0x"
               self.navigationController?.pushViewController(vc, animated: true)
               print(walletAddress as Any)
           } else {
               print("error")
           }
       } catch {
#if DEBUG
           print("error creating keyStrore")
           print("Private key error.")
#endif
           let alert = UIAlertController(title: "Error", message: "Please enter correct Private key", preferredStyle: .alert)
           let okAction = UIAlertAction(title: "OK", style: .destructive)
           alert.addAction(okAction)
           self.present(alert, animated: true)
       }

@Kristenlike1234 Kristenlike1234 added the FAQ common questions tend to recur about web3 infra label Oct 4, 2022
@yaroslavyaroslav yaroslavyaroslav removed the FAQ common questions tend to recur about web3 infra label Oct 23, 2022
@yaroslavyaroslav yaroslavyaroslav closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants