Programming Project - Phase 2

Thanks for that, I deployed my contract on the testnet (with a selfdestruct feature). Only problem now is it’s quite expensive to use the testnet, and I can’t get enough ETH from the Metamask faucet! Might just have to get started on unit testing and/or web design while I stock up on testnet ETH.

I believe I’m done, but would appreciate if someone can approve it.
contract events
github

2 Likes

Is there something wrong with web3? I keep on getting an error saying that ethereum.enable() has been deprecated and to use eth_sendTransaction, anybody have any idea? Thanks

beginners question:
does this alert (exception in contract code) mean that transaction run out of gas?
I haven’t changed anything in my contract, so it shouldn’t be “revert” issue… :thinking:

When in doubt reset…select account => Settings => Advanced => Reset Account

1 Like

done this many times, always in doubt :see_no_evil: but now its working, I dont understand why… :face_with_raised_eyebrow:

Yeah can be frustrating - especially when you are sure you finally understand something and are certain it is correct but it doesn’t work - then you end up spending hours diagnosing and solving to realise that you had it right the whole time but there was something else going on behind the scenes that beginners like ourselves just haven’t learned about yet…we’ll get there, good luck with it

couldn’t explain it better… I really admire knowledge people in this academy have:)

2 Likes

Here is my git for Phase 2… need a little bit of cleaning but I’m at least like 95% finished :slight_smile:

1 Like

Can someone here send me test ether?
I keep getting error “Too Many Requests” . I do it once every 2 or 3 days. But this is stopping my testing.
I am trying on :
https://faucet.metamask.io/

faucet.metamask.io works for me after midnight european time…

Ok so it this a timing thing?

I have trouble with event listeners, tried many options, here is one of them:

.on("confirmation", function(confirmationNr){
        $("#action").text("Transaction confirmed! Wait for result of your game");
        if (confirmationNr == 1) console.log("transaction confirmed");  
      
        let queryId = contractInstance.events.LogQueryId.returnValues.queryId;
        contractInstance.events.coinFlipResult({filter: {queryId: queryId}
        , fromBlock: 0, toBlock: 'latest'}, function (error, event) {
            if (event.returnValues.win == true) {
                let prize = web3.utils.fromWei(event.returnValues.prize, "ether")
                $("#action").text("Congratulations! You won! Your prize is: " + prize);
            }
            else {
                $("#action").text("Ahhh... You lost a bet");
            }
        } 
    )
    })   

here is what I get:


Does anyone have any advice on that?
here is my repo: https://github.com/Ulakal/Coin-Flip-phase-2

Can someone verify that I have the correct migration file for provableAPI?

const provableAPI = artifacts.require("provableAPI");

module.exports = function(deployer) {
  deployer.deploy(provableAPI);
};

or

const usingProvable= artifacts.require("usingProvable");

module.exports = function(deployer) {
  deployer.deploy(usingProvable);
};

How much are you guys sending as gas to get the transactions approved on ropsten?

Depends on your contract and what each transaction is doing - if you are changing state variables a lot and chewing up lots of memory, it will be high. When I did my testing in remix, I adjusted the gas amount a few times for each function and called them mutiple times to figure out how much I needed. Then, once I narrowed it down and got a fairly good idea of how much gas each transaction would cost me, I rounded up a little in my javascript function calls to be safe.
For example, if the transaction took say 90,000 gas, when sending a contract transaction from my javascript file - I’d round it up to 100,000 to be safe.
You can estimate gas needed this way too -
https://web3js.readthedocs.io/en/v1.3.0/web3-eth-contract.html?highlight=events#methods-mymethod-estimategas
…but I found testing and verifying in remix to be a good way to do it as the amount of gas may vary depending on the state of the contract when the function is called - e.g. flip coin function may take say 80,000 gas the first time you call it, but the second flip will probably be higher as you are going to be overwriting data in the contract.

You could make a few changes in your contract so that you don’t have to send a full eth for each flip if your chewing through it. I believe the metamask faucet only allows 5 ethereum a day or something. As a side note, the test net is free to use, so miners that maintain it are spending their own resources to achieve this and test eth are not easy to come by apaprently - that is why they cap it and also why it could be a good idea to use them sparingly by sending less per transaction call maybe. I have changed all amount to finny (0.001 eth) and only ever send like 5 or 10 finney per transaction generally. Make sure you withdraw all your funds before destroying the contract too so that the eth are not being sent to the nether.

Good luck

I didn’t actually create a migration file for provable API - my understanding is that the child contract will inherit everything it needs from this contract so no need to migrate it as it will just be another transaction that you have to wait for on the blockchain. My contract inheritance structure goes Ownable => Destroyable => usingProvable => Accountable => Randomised => FlipOut but I have only migrated the Migrations Contract and then the FlipOut Contract and everything is working for me. I have set it up this way so that I can deploy other games (for example a dice roll game and high card draw game) and in my situation I would create migrations for these contracts as they would not be children of my FlipOut contract and therefore would need to be deployed separately on the blockchain. I hope this helps, cheers

1 Like

Your Coin Flip Phase 2 repo is empty - I can only see your phase 1 project. Regarding your event listener - can you see the event being logged in your console? Or is it the entire transaction that is not being mined? Assuming the transaction is working, all of your events should show in your console if they are tracking through your dapp correctly. If you can see it on the console but its just the listener that isn’t catching anything for 50 or more blocks, then I would say you probably need to fiddle with the filters you have in place for the event listener. If it is the entire transaction that is failing then you probably need to rewrite that function until you start seeing things in your console, then work on the event listener once you know everything else is working alright.

Also, this part could be causing drama for you, I’d change variable name or the case of the letters or something so that there is clarity, not just for you, but also for your program (I’m not really sure, but this could be causing your filter to filter for itself, not the variable you have set earlier). I’d change the case or add an underscore or something…e.g

let query_Id = ... 
/*then*/  {queryId: query_Id}  //for your coin flip event listener filter
or let queryID = ...
/*then*/  {queryId: queryID}  //for your coin flip event listener filter

Also, just on a side note, how do you know that the queryId for the LogQueryId event is the correct queryId for the current dapp user? i.e. if you have multiple users playing all at once, the LogQueryId event will be emitted for everyone, so you need to make sure that you are catching the right person’s queryId in order for the user to receive the right results.

Anyways, I hope you get it working and hopefully something here helps, good luck

Thank you @cryptocrom for your time!
my events are logged in console and tx is mined, I get confirmation, but then nothing happens until this error appears, also console.log("confirmation") doesnt appear in console…although this part $("#action").text.... shows up on the screen…

contractInstance.methods.flip(coinside).send(config)
    .on("transactionHash", function(hash){
        console.log(hash);
        $("#action").text("Wait for your transaction to be confirmed");
    })
    .on("confirmation", function(confirmationNr){
        $("#action").text("Transaction confirmed! Wait for result of your game");
        console.log("transaction confirmed"); 

I’ve tried your suggestions, but I started to get “Alert: exception in contract code” from my metamask, so I can’t do anything anymore…
here is my repo (hope it will work now): https://github.com/Ulakal/Coin-Flip-phase-2

Also I deployed the first Coin flip project (without oracle) to ropsten and same thing happens:


no confirmation in console, neither receipt and after some time same error in console appears, alerts are showing up normally, here is part of the code:

contractInstance.methods.setAbet(coinside).send(config)
    .on("transactionHash", function(hash){
        console.log(hash);
    })
    .on("confirmation", function(confirmationNr){
        console.log(confirmation);
    })
    .on("receipt", function(receipt){
        console.log(receipt);
    })   
}

function flipAcoin() {

    contractInstance.methods.flip().send().then(function(res){
        contractInstance.methods.showResult().call().then(function(result){
            if (result==true) {
                alert("You won!");
            }
            else alert("You lost a bet.");
        })
    })
}

Does anyone have any ideas what is happening? Please help :pray:

I’d start with putting your event listeners in this part of your js file. I was pulling my hair out wonderingwhy mine weren’t working until I realised this.

$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
        contractInstance = new web3.eth.Contract(abi, "0x82c6549E28045abE84D29c479C1e8eE832A6A323", {from: accounts[0]});
        console.log(contractInstance);
//put event listeners in here
    });

Also, I wasn’t sure earlier as I am still learning myself, but I had a little play e=with my event listeners to see if the way you have structured your event listener to get the var query_Id and I could not replicate anything successfully this way. I would say that you will probably need to listen for the event, then get the events results before instantiating the variable query_Id…I could be wrong, but I get results by structuring my event listener and instantiating/defining variables as illustrated below…
Instead of this:

Try something like this:

var query_Id;
contractInstance.events.LogQueryId
    function(error, event){console.log(event);})
    .on('data', function(event){
            query_Id = event.returnValues.queryId;
    });

Again, I am also just learning but I’d do things step by step and console.log everything as you go to make sure it is working before going to the next step. With my front end, I have added one event listener at a time, tested it thoroughly and played with it to ensure I am getting the results I am looking for each time, if I don’t get the results I expect to get, I try writing the event listener in a different way until I get what I am looking for.

Regarding you redeploying the original contract and getting the same issue - assuming that it was working originally, you would have to assume that the new additions to your js file are the issue. I’d break each function down and build it back up bit by bit, working from the things you know worked in the past. Start with what works and work on one function or event listener at a time, verifying as you go. As stated in an earlier post, it sometimes pays to change the file name of your js file so that the server is forced to reread the file as it sometimes doesn’t pick up on changes that you make.

Start with event listeners in the right place, try writing them differently and hopefully you have a little bit of success. Good luck