Programming Project - Phase 1

Hi @dan-i

I added abi to index.html and getPlayerAddress() to main.js. Can you check if I am using getPlayerAddress correctly. My github

I still get Uncaught ReferenceError: accounts is not defined

I am curious as to why accounts[0] does not open metamask when its mentioned here contractInstance = new web3.eth.Contract(window.abi, contractAddress, {from: accounts[0]});

Thank you Daniele

Hi @enrico

You need to add player to your contract! I made the same mistake.

Hey everyone!

I’ve made a lot of progress on phase 1 of my dapp. I feel like I’m really close to getting my functions written correctly but I could use some advice if someone wouldn’t mind.

First, I have three functions, makeWager(), acceptWager(), and coinFlip():

function makeWager() payable public returns(bool) {
    wager = msg.value;
    player1 = msg.sender;
    return true;
}

function acceptWager() payable public returns(bool) {
    if(msg.value == wager) {
        player2 = msg.sender;
        return true;
    }
}

function coinFlip() public payable {
        if(random() == 0) {
            payMe(player1);
        } else {
            payMe(player2);
        }
}

If I’ve written them like I think I have, then the first should take in a wager from msg.sender and set them to player 1. The second should set the other msg.sender to player 2 if they offer up the same wager. My thought process was that I could run the first function as accounts[1] on Metamask/Browser, then in main.js I have it set up so that when the user clicks a button, it’ll offer up one ether, and cause the owner to also offer up one ether, then the “coin is flipped”.

main.js:

$("#button_1").click(() => {
    makeWager();
    acceptWager();
    flip();
});

and later

function makeWager() {
contractInstance.methods.makeWager().send({value: web3.utils.toWei(“1”, “ether”)});
}
function acceptWager() {
contractInstace.methods.acceptWager().send({value: web3.utils.toWei(“1”, “ether”), from: owner});
}
function flip() {
contractInstance.methods.coinFlip();
}

with owner having been declared as accounts[0] further up the file.

SO click the button, player1(msg.sender) offers 1 ether, causing player2(owner) to offer 1 ether, and then the flip() function is performed, paying out the 2 ether total to the winner.

In theory. But I’m getting the following error of which I took a screenshot, saying that contractInstance.methods.makeWager() is not a function. I’m not sure how to word this.

I’ll also attach the Github repo for it if that makes this any easier. Thank you!
https://github.com/MBurtonshaw/dapp_project

Hi @filip

I managed to add player. All my tests run. I get this error on the webpage. Could you please tell me where is the mistake. My github

image

Hey @oneworldcoder

You have a console.log(contractInstance) in line 10. Is it displayed correctly in the console?

Hey @mburtonshaw

Regarding your error below, can you post your abi.js?

Screenshot 2020-12-27 at 11.55.10

Hi @dan-i

Yes the contractInstance is displayed correctly in the console. I can see the methods.

Wait I am going to clone you project and test.

@oneworldcoder You click listeners are wrong and are called before the contractInstance is created.

Should be:

      $("#bet_0").click( () => {
        bet(0)
      });
      $("#bet_1").click( () => {
        bet(1)
      });

Always double check the docs :slight_smile: Sometimes the most annoying errors are the smallest ones.

Cheers,
Dani

Hi @dan-i

The buttons now pop up the Metamask but I get a Metamask error ALERT: Transaction Error. Exception thrown in contract code. I read on stackoverflow I can be because of require statements which are failing. But I am unable to figure out which require statement is failing. How to solve this?

Edit: The contract has a balance of 10ETH.

function flipCoin(uint number) public payable costs(betBalance) {
    require(number == 0 || number ==1, "Place by selecting 0 or 1");
    require(balance > msg.value);

    deposit[msg.sender] = msg.value;

    balance += deposit[msg.sender];

    if(number== flip()) {
      uint win = deposit[msg.sender] * 2;
      msg.sender.transfer(win);
      balance -= win;
      emit result(win, "You won!!");

    }
    else {
      emit result(deposit[msg.sender], "You lost, Sorry!!");
    }

  }

Hey @dan-i ,
Here’s a link to it:
https://github.com/MBurtonshaw/dapp_project/blob/main/2/abi.js

You were right though, I changed the abi.js file and it’s working now. Well, transactions are going through but I still need to work on the functions.

Got it working, thank you. I ended up getting rid of the makeWager() and acceptWager() functions; instead the contract .transfer()s ETH in the coinFlip() function.

1 Like

Well, here are my finished code & screenshots for phase one:

https://github.com/MBurtonshaw/dapp_project






1 Like

Hey @oneworldcoder

There is an issue with your code.
Consider this:

function flipCoin(uint number) public payable costs(betBalance) {

You are using a modifier costs which requires:

  modifier costs(uint cost){
    require(msg.value > betBalance);
    _;
  }

uint public betBalance = 1 ether;

You are sending 1 Ether but the modifier requires msg.value > 1 ether.
This is most likely the reason why it fails.

Also notice that you should remove the modifier or the require statement in your function flipCoin.
They are indeed redundant.

Let me know,
Dani

Hi @dan-i

I have removed the modifier but I still get a Metamask error ALERT: Transaction Error. Exception thrown in contract code.. I have updated the code in my github .

hi, thanks,

I don’t get what you mean, can you explain?

Were you able to get the deposit or withdraw function to work? I still get errors in the console

Hi @enrico

What I mean is you need to add players so that they can participate in the bet. You can create struct for player then create a mapping if you choose and later include it in your flipCoin(). I first tried that approach but the code got complicated and I got many errors. So I decided to push the player in my flipcoin() this way ( I created deposit as a mapping(address => uint) deposit)

function flipCoin(uint number) public payable {
    require(number == 0 || number ==1, "Place by selecting 0 or 1");
    require(balance >= msg.value);

    deposit[msg.sender] = msg.value;

    balance += deposit[msg.sender];

    if(number== flip()) {
      uint win = deposit[msg.sender] * 2;
      msg.sender.transfer(win);
      balance -= win;
      emit result(win, "You won!!");

    }
    else {
      emit result(deposit[msg.sender], "You lost, Sorry!!");
    }

  }

I still get a metamask error which I am unable to debug.

1 Like

You can see a revert when deploying your contract.

In your migration you are calling addBalance with accounts[2] but the function is onlyOwner therefore it fails.
I am pretty sure that your contract does not have balance to let you flip, this is why you receive the error.

  function flipCoin(uint number) public payable {
    require(balance >= msg.value);

Hi @dan-i

With accounts[0], I get the error too. Yesterday I just changed to accounts[2] to test. How to solve this?

image

image

Here is my dapp:

Screenshot 2020-12-29 at 16.24.22

2 Likes

yes, i did the struct and mappings already thanks :slight_smile: