I get this error when trying to write data. This error only occurs with some function calls.
Error : Uncaught (in promise) TypeError: Cannot read property 'indexOf' of undefined
Sample Code of where error is encountered:
let contract = new ethers.Contract(process.env.CONTRACT_ADDRESS, process.env.CONTRACT_ABI,
this.state.web3);
let privateKey = process.env.PRIVATE_KEY;
let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, this.state.web3);
let contractWithSigner = contract.connect(wallet);
let tx = contractWithSigner.addUpdatedFishData(this.state.tagid, this.state.newTagId, this.state.processType).then((result) => {
console.log(result);
});
Is it something to deal with tagid as it is my index in solidity mapping?
My guess is that this.state.web3 is a Web3 Provider? You need an ethers.js Provider. The providers.Web3Provider wraps a Web3 Provider. It's a bit confusing. :)
Try:
let provider = new ethers.providers.Web3Provider(this.state.web3);
// Change this to the provider
let contract = new ethers.Contract(process.env.CONTRACT_ADDRESS, process.env.CONTRACT_ABI, provider);
let privateKey = process.env.PRIVATE_KEY;
// Change this to the provider
let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
let contractWithSigner = contract.connect(wallet);
let tx = contractWithSigner.addUpdatedFishData(this.state.tagid, this.state.newTagId, this.state.processType).then((result) => {
console.log(result);
});
If this.state.web3 is actually a Web3 object, instead you will need to use:
let provider = new ethers.providers.Web3Provider(this.state.web3.currentProvider);
Let me know if that helps. :)
Thanks for the response :).
Actually, the provider is ether.js provider. Since the code below was in a helper file, I forgot to mention it in the question.
let abi = process.env.CONTRACT_ABI;
var provider = new Web3.providers.HttpProvider(process.env.LOCAL_PROVIDER) //http://127.0.0.1:7545 is the local provider
let web3Provider = new ethers.providers.Web3Provider(provider);
So this.state.web3 is web3Provider. Since web3 name was already used, I did not change it but rather changed its contents.
The same code works when calling other functions. Also there was another function for which if the tag id was around the value 1000, it worked but with higher values e.g 8000, I got the same error.
I hope my question is clear :)
Hmmm... I think I need more context then. :s
Can you include the ABI signature for that call? And can you maybe try running a bunch of values against the tag to see if there is a pattern to which ones work and which do not?
This is the ABI for that function.
{
"constant": false,
"inputs": [
{
"name": "_oldTag",
"type": "uint256"
},
{
"name": "_newTag",
"type": "uint256"
},
{
"name": "_processType",
"type": "uint256"
}
],
"name": "addUpdatedFishData",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
Currently this one doesn't work for any values. I tried.
The error is mapped to this place in the ethers.js file (The first if)
case 'sendTransaction':
return this.send('eth_sendRawTransaction', [params.signedTransaction]).catch(function (error) {
// "insufficient funds for gas * price + value"
if (error.responseText.indexOf('insufficient funds') > 0) {
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, {});
}
// "nonce too low"
if (error.responseText.indexOf('nonce too low') > 0) {
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, {});
}
// "replacement transaction underpriced"
if (error.responseText.indexOf('replacement transaction underpriced') > 0) {
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, {});
}
throw error;
});
I see, I see. So there was an error getting a response, but for some reason responseText wasn't populated in the error. I should certainly add a check in that block. I will add that tomorrow.
In the mean time, to help you debug, you should be able to just comment out all those if statements. At least you'll be able to march forward. :)
And I'll update this ticket once the new code is live.
Ok nice. Thanks for the help :)
In my case, the actual error was this:
Error: VM Exception while processing transaction: out of gas
How can I set gas limit while calling the contract function to ensure gas doesn't run out while the function is being executed?
You can pass in 1 additional parameter, which is a dictionary of overrides, so if foo takes in 2 parameters: contract.foo(param1, param2, { gasLimit: 23000 }). You can override nonce and such as well.
But if you are getting that error, it probably means tour contract is throwing an error, which has the same effect as basically requiring more gas than is allowed.
Ok.. Thanks again. Will check the contract :)
Also, if you are using truffle, try blowing away the build. Sometimes it updates the ABI but not the bytecode or address, so it lets you call functions that don鈥檛 actually exist. :)
Yeah I am using truffle. Ok will try that :)
This should be fixed now, at least as far as nested internal errors shadowing the actual errors. :)
Let me know if you still have problems, or feel free to open another issue to track other problems.
Thanks! :)
Thanks :)