Programming Project - Phase 1

I have a question, why deposit first is needed ? Just asking. Is this for security reasons ? I made that when a bet function is called then draw happens and if there a win then in the same time money is send back to player. Do you think this solution will work ? or its better to have deposit funds first.

1 Like

Hey @Spartak

That means that the function you are trying to call is not in your ABI file.
Take a look and let me know.

Happy learning

That solution will still work. However, @filip mentions in one of the videos, maybe in the Phase 2 section, that you shouldn’t send the winning bet at the same time for security. He mentioned that one function should only push or pull but not both.

I believe you can start the contract with an existing deposit if you include that information in your migration files as a {value: }. (not 100% sure as I did not do it though)

For me, the deposit function is needed if someone wins all of the money.

I did it that way first also then changed it later for security. Additionally, I am also doing it in this way to make this a liquidity pool operation later.

But, Yes. To finish the Phase 1 project, paying out at the same time will work.

Good Luck with your project! :+1:

Thank you for your answer. I see now. Will also make a deposit button. I have two other questions, maybe someone can help me. @filip

  1. How to mock the random function to test win or lose case ? I’m trying to create a new contract that inherit from my base contract that has a random function hardcoded as 1 for example. https://dappsdev.org/hands-on/testing/solidity-mocks/ Do think its a good solution ?
  2. I understand that its better to have less code then more code, to have less gas fees. I saw that people use uint as head or tail option, I have used a string ‘head’ or ‘tail’ so its more descriptive, but then I needed to write string compare functions like.
    function compareStrings(string memory a, string memory b) private view returns (bool) {
        return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
    }

    function isHead(string memory side) private view returns(bool) {
        return compareStrings(side, "head");
    }

    function isTail(string memory side) private view returns(bool) {
        return compareStrings(side, "tail");
    }

which solution is better here ? using uint(less readable code) or string(more readable code)

Hey @dan-i i have migrate the files again and made new ABI.

Now i am getting this error "Returned values aren’t valid, did it run Out of Gas? "

Tried with different versions of web3, downloaded the new web3.min.js file, and still doesnt work. Do you know any other fixes?

Thanks in advance

Here is my submission for phase 1.

I had to convert to gif for upload. Also windows screen capture doesnt pick up the metamask window unfortunately. However you still can see the contract balance changing when I confirm transactions.

working in action: coinflip dapp demo

1 Like

Hey there. Here’s a short video of my pseudo-randomness coin flip dapp:

https://youtu.be/8bGn3b8KkKk

Not sure if it is helpful for anyone else, but I was stuck for a while trying to capture the event I had set up to say whether the person won or lost. I found that using web3 with an http provider and Ganache that the event was not firing at all. Gananche was showing no events and my transaction receipt was not showing my event parameters.

So I published to Kovan and suddenly I was receiving the events back. Hopefully this is helpful for anyone who ran into this.

Hi there, I also had trouble implementing this. In the end I just gave up - you can tell whether your bet won or lost by your balance changing but it would have been nice to capture the event then display the result. I might try it on kovan testnet too.

Hey I feel you, It was many days trying to resolve this! What I did was use remix to publish my contract, then used that contract address in my web3 code. It also took me a bit to work out how to capture the event data and then parse it to get my return variable and use that for the user’s response. Let me know if you get stuck on it.

There seems to be some issues with Ganache or Truffle. This thing with the events is one problem. I also had an issue when I was running my tests. Even though Ganache and Truffle both shows my contract’s address, when the actual TESTS were being run, they would use a different (newly generated) address every time the test file was run! It was weird. I posted in several forums but nobody could explain it. I think it is an issue on my local system because others were saying this was not happening for them. But not sure how to fix it.

Here is my work in progress:

https://gist.github.com/chrisdbarnett/9e08587dbf1f21c60d722a920e47122a

There are two images of the coin - heads and tails with the user being invited to click on one of them.

The smart contract is geared up to accept an argument for which side the use clicked on but at the moment a zero is passed to it and it will just “randomly” decide whether you won or lost.

It’s hardcoded to 1 Ether a bet at the moment and uses party.js to sprinkle the page with confetti if the user wins.

The smart contract does everything in one transaction in that the user gets their payout right back at them.

The smart contract assumes the funds where there in the first place and it’s down the creator to deposit enough money to cover any bets.

I had considered doing an IOU system and having the user withdraw the funds, but I need to push on through the course.

That said, I don’t want to skimp. One thing I’m not happy about, is that I seem to be losing more than winning, so I do wonder if the smart contract has been done right.

Metamask sometimes bombs out with an error - something to do with the transaction nonce.

Please let me know if mine is a complete disaster. I’ll probably get the whole lot and upload to Github. When I have time, I will improve it further.

Chris.

2 Likes

Hi, I’m stuck and could use some help. I have problem that popup from metamask is not showing when I send a transaction.

Here is the deposit function that send transaction

https://github.com/dominno/coinflip/blob/main/frontend/main2.js#L62

Here video from problem I have, not sure what is wrong. I can read from my contract, but cant send a transaction.
https://www.dropbox.com/s/v50cr5islzqnv87/2020-11-10%2011-31-53.mov

1 Like

Hey @Dominik_Szopa

I deployed your contract and followed the error given by the console, give it a check:

 var config = {
        value: web3.utils.toWei(valueEther, "ether"),
        from: web3.currentProvider.selectedAddress
    };

valueEther should be a string. value: web3.utils.toWei(valueEther.toString(), "ether"),

Also there is not such a function deposit in your contract abi.
Migrate and recreate the abi file :slight_smile:

cheers,
Dani

1 Like

@dan-i you know what is causeing this error?

“Couldn’t decode uint256 from ABI: 0x”

I am on a right network, contracts deployed, even changed web3 version also not working.

thanks in advance

Hello…my coinToss Dapp phase1:

2 Likes

Hi @filip / @dan-i I have a question and am a bit stuck.
Never created a web app before so will have to take your “Javascript Programming for Blockchain Developers” after this.
However, I have a maxCost function that only gets the available contract funds but cant get it to display on my page.
The js code is

function displayMaxBet(){
    contractInstance.methods.maxCost().call().then(function(res){
      $("#maxcost_output").text(res);
      console.log(res);
    })
}

solidity code:

  function maxCost()
  public
  view
  returns(uint)
  {
    // used division so there will always be money in the contract
    return contractFunds.availableBalance / 4;
  }

html:

<p type="text" id="maxcost_output"></p>

The betting part, deposit and withdraw works but cant get an output for this text.
Can you see what I am doing wrong? it doesnt output to console either in my console.log(res). :slight_smile:

/J

1 Like

Thank you, all works now. Will post the final code soon.

1 Like

Nice @Dominik_Szopa :slight_smile: let me know once done I will take a look!

Hey @jbo

return contractFunds.availableBalance / 4;

Why are you using a dot notation? Can you please post the whole smart contract?

Thanks!
Dani

Here are screenshots of my first dapp :slight_smile: for the phase 1.

I will continue improving and developing my dapp with oracles… and I am thinking now that the “React web development” course would be also interesting at this point… :thinking:

Easy use - place your bet, select head or tail and flip!


All numbers (# of win / loss, balance) are being updated real-time (after each flip and withdrawal).

Winnings can be withdrawn.

And of course there are some checks (the bet is a positive number, only owner, withdrawal to the correct account etc.)

1 Like

@dan-i

Finished working on the project.

https://www.dropbox.com/s/9o30br9382xz4hr/2020-11-11%2022-16-04.mov

source is here

https://github.com/dominno/coinflip

Please review and let me know what to improve.
In the movie I also have a question why when I restart the genache then I need to remove accounts in metamask and import them again, can you tell why ?