Ethereum coinflip dapp discussion

I am indeed using the gui. OK I will give it a go. Thanks :slight_smile:

@gabba I tried to get ganache-cli working. it kept throwing this error

I have tried reinstalling it without any joy. I looked for the error and can’t see anything on it.

open for error
PS C:\WINDOWS\system32> ganache-cli
Ganache CLI v6.9.1 (ganache-core: 2.10.2)
Error: Callback was already called.
    at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:17:276
    at s.<anonymous> (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:17:2238)
    at s.emit (events.js:315:20)
    at s.destroy (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:37:712589)
    at finish (_stream_writable.js:659:14)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

I thought it was to do with the GUI being open possibly, but it isn’t open. I restarted the computer too, just incase there were any lingering background tasks.

The install threw some errors too, but I think they are related to hooking up with visual studio and well, I am not using visual studio, so that doesn’t shock me. There are some things in there that indicate issues with keccak which worry me a little more. I have already reinstalled npm once. :confused:

output from installer
PS C:\WINDOWS\system32> npm install -g ganache-cli
C:\Users\kira.summers\AppData\Roaming\npm\ganache-cli -> C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\cli.js

> [email protected] install C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\keccak
> npm run rebuild || echo "Keccak bindings compilation fail. Pure JS implementation will be used."


> [email protected] rebuild C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\keccak
> node-gyp rebuild


C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\keccak>if not defined npm_config_node_gyp (node "C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
gyp ERR! find VS
gyp ERR! find VS msvs_version not set from command line or npm config
gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer
gyp ERR! find VS looking for Visual Studio 2015
gyp ERR! find VS - not found
gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
gyp ERR! find VS
gyp ERR! find VS **************************************************************
gyp ERR! find VS You need to install the latest version of Visual Studio
gyp ERR! find VS including the "Desktop development with C++" workload.
gyp ERR! find VS For more information consult the documentation at:
gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
gyp ERR! find VS **************************************************************
gyp ERR! find VS
gyp ERR! configure error
gyp ERR! stack Error: Could not find any Visual Studio installation to use
gyp ERR! stack     at VisualStudioFinder.fail (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\n
ode-gyp\lib\find-visualstudio.js:121:47)
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualst
udio.js:74:16
gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:70:14
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:372:16
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:310:5)
gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
gyp ERR! stack     at maybeClose (internal/child_process.js:1051:16)
gyp ERR! System Windows_NT 10.0.17763
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\kira.summers\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\keccak
gyp ERR! node -v v14.2.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] rebuild: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] rebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\kira.summers\AppData\Roaming\npm-cache\_logs\2020-06-03T09_57_43_662Z-debug.log
"Keccak bindings compilation fail. Pure JS implementation will be used."

> [email protected] install C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\secp256k1
> npm run rebuild || echo "Secp256k1 bindings compilation fail. Pure JS implementation will be used."


> [email protected] rebuild C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\secp256k1
> node-gyp rebuild


C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\secp256k1>if not defined npm_config_node_gyp (node "C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
gyp ERR! find VS
gyp ERR! find VS msvs_version not set from command line or npm config
gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer
gyp ERR! find VS looking for Visual Studio 2015
gyp ERR! find VS - not found
gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
gyp ERR! find VS
gyp ERR! find VS **************************************************************
gyp ERR! find VS You need to install the latest version of Visual Studio
gyp ERR! find VS including the "Desktop development with C++" workload.
gyp ERR! find VS For more information consult the documentation at:
gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
gyp ERR! find VS **************************************************************
gyp ERR! find VS
gyp ERR! configure error
gyp ERR! stack Error: Could not find any Visual Studio installation to use
gyp ERR! stack     at VisualStudioFinder.fail (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:121:47)
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:74:16
gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:70:14
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:372:16
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
gyp ERR! stack     at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:310:5)
gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
gyp ERR! stack     at maybeClose (internal/child_process.js:1051:16)
gyp ERR! System Windows_NT 10.0.17763
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\kira.summers\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\node_modules\secp256k1
gyp ERR! node -v v14.2.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] rebuild: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] rebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\kira.summers\AppData\Roaming\npm-cache\_logs\2020-06-03T09_57_46_520Z-debug.log
"Secp256k1 bindings compilation fail. Pure JS implementation will be used."
+ [email protected]
added 88 packages from 85 contributors in 13.132s

What do you think? Should I throw the laptop out of the window? :-p

@Kiki

ahaahhah
Not yet did you look at my post about how to install everything on windows ?
https://forumtest.ivanontech.com/t/windows-installation-git-vscode-nvm-nodes-ganache-truffle/15582

I think i did this before installing ganache-cli

npm install -g -production windows-build-tools
1 Like

Hi @gabba
Frustratingly, I went through the how to, but I still have the same error.

error message
Ganache CLI v6.9.1 (ganache-core: 2.10.2)
Error: Callback was already called.
    at C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:17:276
    at s.<anonymous> (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:17:2238)
    at s.emit (events.js:315:20)
    at s.destroy (C:\Users\kira.summers\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:37:712589)
    at finish (_stream_writable.js:658:14)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
PS C:\WINDOWS\system32>

Do you have any thoughts? (besides 'aaarrrrggghghhhh &^(&(*"

I’m stumped.

Hi @Kiki

When this issue is raised ?
What command are you using to start ganache-cli ?
Can you share your version ? (node npm )

1 Like

Hi guys,

Currently in the Coinflip Dapp section of the Old smart contract programming course. I am having a problem in the Superblocks studio. I’m trying to change the environment of the external account from default to “test”. I click the save button but it never saves. I go from another tab and return to the external account but then the environment shows that it’s still in default. Any ideas why this is happening? See screenshot below.

@newavel
Thanks for reaching out!
I suggest you try another browser.
If it still does’t work then do it in REMIX. By the way, we are in the space of open-source development, why shall we restrict ourself to one IDE when we have options :slight_smile:

1 Like

My dapp is generally working in the sense that it takes and pays out bets in the expected manner, however I can’t get events to emit properly. Here are some snippets from the contract I’ve written. I’m trying to emit an event when the coin is flipped in order to notify the user of the resulting flip.

Definition:

event coinFlipped(uint result);

Call:

function flipCoin() private returns(uint) {
        uint result = now % 2;
        emit coinFlipped(result);
        return result;
    }

This doesn’t appear to be working for me because ganache is not showing anything in the events log.

Additionally, my queries return no events data on the front end. Here is the function call where I’m querying for events:

function placeBet(){
    var prediction = $("#prediction").val();
    var bet = $("#bet_input").val();

    contractInstance.methods.settleBet(prediction).send({value: bet})
        .on("receipt", function(receipt) {
            console.log(contractInstance);
            console.log(receipt);
            contractInstance.getPastEvents('coinFlipped').then((events)=>console.log(events));
        });
}

This calls the settleBet function which then calls the flipCoin function which should be emitting an event. However, neither the contractInstance, the receipt, or the getPastEvents function show any events being logged.

Here is the settleBet function in the contract as well in case you see any issues here:

function settleBet(uint prediction) public payable validateBet {
        require(prediction == 0 || prediction == 1);
        uint outcome = flipCoin();
        if(outcome == prediction) {
            balance -= msg.value;
            msg.sender.transfer(msg.value * 2);
        } else {
            balance += msg.value;     
        }
    }

I’m using solidity 0.5.12 and connected metamask to a local blockchain using Ganache. What’s especially frustrating to me is that when I run this on remix I can see the event being logged so I’m wondering if there is a different way to emit events using a local blockchain vs. the remix JS VM?

Please let me know if there is any other information I can provide and thanks in advance for any advice you may have.

Hey @Charlie_Pontrelli

Check this out: Programming Project - Phase 2
Look at my code and try to implement it, if you struggle please create a repo on github with your project and give me the link.
I will have a look.

Cheers,
Dani

2 Likes

Thank you for the reply. It was very helpful to see how you are querying for events.

I also just noticed that I had redeployed my contract and forgot to change the contract address in my main.js file which wasn’t helping my issue. I think I can figure it out from here.

1 Like

Is there any way to test for time requirements by unit testing? If I need a certain amount of time to pass before a function can be executed, how do I test this? Thanks

Hey @JeffE

You can surely do that.
This is a really nice article that will tell you how to proceed:

Look at the function setTimeout(), it allows you to execute a function when an x amount of time is passed. This is the example provided in the link above:

console.log("Hello");
setTimeout(() => { console.log("World!"); }, 2000);
console.log("Goodbye!");

Happy learning,
Dani

1 Like

Hi Filip,
just a question regarding function flip() msg. I have followed all your instruction and at the end, I had to copy and paste the code you provided in Coinflip Dapp Part 2. Whatever I had done I still not getting any message (neither error nor success). I have checked my pop-ups and still nothing.
Does anyone else have the same problem? Do you think it is because Superblocks are no longer operative?
Cheers
M

1 Like

Hey @MartinH

If you still have the code that is not working, I might take a look verify the issue.

Happy learning,
Dani

1 Like

Hi @dan-i
that would be awesome if you could look and verify the issue.
This code is from Coinflip Dapp Part 3 and pretty much getting the same result. When I hit submit button on the view page nothing happened. No error or confirmation button.
Please find code attach in two separate screenshots (can’t figure out how else I could post the code here)

cheers
M

Hey @MartinH

Can you please paste your code so that I can copy it? :slight_smile:

Thanks
Dani

1 Like

You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

Carlos Z.

1 Like

Here is my code:

(function (Contract) {
    var web3;
    var instance;

    function init(cb) {
        web3 = new Web3(
            (window.web3 && window.web3.currentProvider) ||
            new Web3.providers.HttpProvider(Contract.endpoint));

        var contract_interface = web3.eth.contract(Contract.abi);
        instance = contract_interface.at(Contract.address);
        cb();
    }

    function getBalance() {
        instance.getBalance(function (error, result) {
            if(error){
              alert(error);
            }
            else{
              $("#balance").html(result.toString());
            }
        });
    }

    function waitforReceipt(txHash, cb){
        web3.eth.getTransationReceipt(txHash, function(error, receipt){
            if(error){
                alert(error);
            }
            else if(receipt !==null){
                cb(receipt);
            }
            else{
                window.setTimeout(function(){
                    waitForReceipt(txHash, cb);
                }, 5000);
            }
        });
    }

    function flip(){
      let val = parseInt($("#bet").val());
      instance.flip.sendTransaction({from: web3.eth.accounts[0], gas:10000, value: val}, function(error, txHash){
        if(error){
          alert(error);
        }
        else{
            waitForReceipt(txHash, function(receipt){
                if(receipt.status === "0x1"){
                    alert(JASON.stringify(receipt));
                }
                else{
                    alert("Receipt status fail");
                }
            });
        }
      })
    }

    $(document).ready(function () {
          init(function () {
              getBalance();
          });
          $("#submit").click(function(){
            flip();
          })
      });
})(Contracts['Coinflip']);

Thanks @dan-i and @thecil. Learing new stuff everyday lol

Hey @MartinH

You are sending the tx with web3.sendTx, try this instead

function flip(){
      let val = parseInt($("#bet").val());
      instance.methods.flip(@params).send({from:accountAddress,value: ethYouWantToSend});

let me know :slight_smile:

1 Like

Hey @dan-i,
thank you for the effort. Still nothing. There must be something else. I can’t even connect to the external address and deploy my contract. But thank you for your help, muchly appreciated.
Btw, where did you learn to be such a prodigy in programing?