OH i see the error now, it was an issue with my HTML. The ID for balance was wrong. Sorry about that! Thanks anyways!
@filip, Iâve been getting the error below not only on the Coinflip dApp, but almost every dApp I create. Searching for answers on the internet has been very vague, mostly referring to the gas limit on the transaction. But even after I set it to 210000, I still get the error. Please help?
inpage.js:1 MetaMask - RPC Error: Error: [ethjs-rpc] rpc error with payload {âidâ:3336971970746,âjsonrpcâ:â2.0â,âparamsâ:[â0xf86c81808504a817c800830334509483060a4d8d805588e93972b042277124e572e93f8084cde4efa9822d46a076269871c709744961fe9ff82640e6e422e9a6457e97b5a2424c9c4bf2134331a062166eab667c8baabcdfae3fe5e926fd24355a5270a801f1acd62b5636e82ed8â],âmethodâ:âeth_sendRawTransactionâ} [object Object] {code: -32603, message: âError: [ethjs-rpc] rpc error with payload {âidâ:33âŚmethodâ:âeth_sendRawTransactionâ} [object Object]", stack: âError: Error: [ethjs-rpc] rpc error with payload {âŚmethodâ:âeth_sendRawTransactionâ} [object Object]"}
And also to add to this, in another dApp that I created, I sometimes get that error when I use accounts 1&2 in Metamask, but when I switched to accounts 2&3 etc, I do not get the error.
Hi @SatoshiSingh
This is a metamask issue, it happens sometime when the injected account doesnât update, logout from metamask and refreshing the page often solve the issue.
I have problems since superblocks dont recognize Metamask.
I am using Chrome
I already managed to detect Metamask but now it gives me this error " Contract does not exist. When running the in-browser blockchain it gets wiped on every refresh."
And in all the options I have configured testnet and external
Hi @Thaddeus19
Superblocks was used in the old ethereum course switch to the new one, we are not using it anymore
Hi folks,
I am having a bit of a prblem. I have the solidity parts working well (I think!). The web part doesnât seem to be to happy though.
At first it says it didnâ;t have abi defined, even though I included it in the html file, in the same way as last time. I copied it directly into the main.js file and that seemed to make it a little happier, but when I try and call my play-game function I get errors that you can see in the console here. (You can also see the main.js function that calls the play-game function in the solidity file.
If anyone can help that would be great!
Hi @Kiki
You have 2 metamsk notification did you authorize metamask interacting with your smart contract ?
What is this number variable is it from you code, or is it from metamask ?
Can you link a github repository i ll test it
OK, so It turns out that it was brave shields. Also, I solved the transaction fee missing problem. It was to do with meta mask connection to the contract. I connected metamask to a different blockchain, back to ganache and changed the account. That seems to get it to reconnect.
I have one last thing I am stuck on now, I canât pass the win/lose state of my playGame function back to main.js because return values canât be sent from a solidity function to a js script (or at least thatâs what I read). So instead I am trying to raise an event.
I have set the solidity script to set up the even using:
Then call the event using:
Then listen for the event in main.js using:
But the console in the app shows the error:
Hi @Kiki
Sorry for the late reply @ivga80 told me about it , i moved this topic inside the eth 201 course as it was still in the Blockchain Programing course.
We had create sub topics 2 weeks ago and i am only following topics related to the course. But we didnât migrate this one ⌠sorry
You have this error because âeventsâ are not functions. Events are stored in the transaction receipt not in your contract storage.
It means that you canât access them from your contract you have to watch every transaction receipt with a javascript function to check if the event is emit in the transaction receipt.
In your case you can do this
await contract.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
async (err, events) => {
this.setState(state => {
// Here as events is an array you get the last element of the array
// And you check the return value
console.log(events[events.length-1].returnValues.win);
});
}
)
getPastEvents takes an array of events, so in your case i just use âreturnWinâ. Then the first and last block this event will have to look for.
Because this function will look at the smart contract tx receipt and check if there is an event emit inside.
So i used latest and latest to only check the last event emitted. But you can look at 0 to latest
fromBlock takes a block number but you can use latest to check the last block.
you can look at this example for more information
https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#getpastevents
i prefer to use once
https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#once
PS: If you have an error saying you canât call await in a function which is not asynchronous just add the keyword
async
before function
Hi @gabba I read your response and I am still confused, probably more so.
I am aware of events systems and have used them in other projects before, though not in solidity nor java script. I know that they are not functions.
I am assuming that the code that you added needs calling after the playgame function has been called, which has emitted the event.
my playgame function looks like this:
Open for code
async function playTheGame(){
//play the gameplay
var config = {
value: web3.utils.toWei(gameStake, "ether")
}
console.log("\nPlaying game...\n");
contractInstance.methods.playGame().send(config)
.on("transactionHash", function (hash){
console.log("hash of confirmed transaction was :" + hash);
console.log("Win state was "+winState);
})
//update the outputs
await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
async (err, events) => {
this.setState(state => {
// Here as events is an array you get the last element of the array
// And you check the return value
console.log(events[events.length-1].returnValues.win);
});
}
)
}
And throws the following error:
Iâm not sure what the this.setState call you included is, why itâs there or what it is supposed to be doing.
Hi @Kiki
Sorry i am using this in my reactjs project ^^ no need to do setState.
Did you look at the different exmaple of events in the docs ?
This one should work
await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
async (err, events) => {
// Here as events is an array you get the last element of the array
// And you check the return value
console.log(events[events.length-1].returnValues.win);
}
)
Hi @gabba
That doesnât seem to work. I donât get any errors now, but it still returns as undefined.
Expand code
// adds data from form
async function playTheGame(){
//play the gameplay
var config = {
value: web3.utils.toWei(gameStake, "ether")
}
console.log("\nPlaying game...\n");
contractInstance.methods.playGame().send(config)
.on("transactionHash", async function (hash){
console.log("hash of confirmed transaction was :" + hash);
//update the outputs
await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
async (err, events) => {
console.log("result of win event was" +events[events.length-1].returnValues.win);
});
});
}
Ok @Kiki can you print the content of events[events.length-1] ?
await contractInstance.getPastEvents(['returnWin'], {fromBlock: 'latest', toBlock: 'latest'},
async (err, events) => {
console.log(events[events.length-1]);
});
Are you testing on the testnet or are you still testing locally with truffle ?
Can you check if the event is emitted in remix ? Can you see it in the log ?
Edit: can you also share a github repo i ll look at your code
Thanks @gabba
The github is here: https://github.com/kirasummers/flip
The output of the console.log of events that you requested is
I am just trying to get it working in truffle before I move onto trying it on the testnet. I was trying to resolve it before I moved on, but I also see it is needed for part 2. I am trying to breakdown the various challenges that part 2 presents; events, oracles and mapping of user info and balances.
Thanks Gabba.
It has this:
I canât see a name for the event other than perhaps name: â1â, but it says it has length 0.
So you are trying it locally ? Are you using ganache Gui or ganache-cli ? I had issue in the past with ganache Gui it drives me crazy the events were not sent back. If it is the case can you try with ganache-cli you can install it with npm