Programming Project - Phase 1

Thank you for the help :slight_smile:

Hi @gabba,
Do you mean by

/ Time to reload your interface with accounts[0]!

is something like this? :

// Time to reload your interface with accounts[0]!
let accounts = await window.web3.eth.getAccounts();
    playerAddress = accounts[0];

Hi @gabba, @filip,
I combined all possible actions , including above recommendations from @gabba, and
I got rid of the " Payload error…"

I implemented carefull test on Remix and Truffle testing… everything is OK.

But still on browser i got this …[object object]… error – Dont know what it is?

How can I log out values from Solidity code out? to Browser console or to truffle UI ?
Can I see events on Truffle UI from events emited from Solidity code?

New update: I solved this… Just a simple mistake when I typed “from:contractAddress” instead of “from: playerAddress” inside Call() :frowning: (

New question:
I really really stuck here… :face_with_symbols_over_mouth: :face_with_symbols_over_mouth: :face_with_symbols_over_mouth:
I changed to solidity Pragma 6.
I got the Error in Remix: " Expected Primary expression…" for the line: if (betChoice == 1) {activeBet[msg.sender].for1 += betAmount;};

the full funstion as follow…

    function createMyBet(uint betChoice, uint betAmount_eth) public validPlayerBalance returns (uint, uint){
        updateMyBalance();
        uint betAmount = eth2Wei(betAmount_eth);

        if (betChoice == 1) {activeBet[msg.sender].for1 += betAmount;};
       //ParserError: Expected primary expression. ---here---^
        if (betChoice == 0) {activeBet[msg.sender].for0 += betAmount;};
        
        return (activeBet[msg.sender].for0, activeBet[msg.sender].for1);
    }

definetly that is a kind of bug(?) … I deleted those lines and copy paste some similar code from other place then rearrange line… and that stupid error disappears… It is just crazy :innocent:

 if (1 == betChoice) {
            activeBet[msg.sender].for1 += betAmount;
            }
        else {
            activeBet[msg.sender].for0 += betAmount;
        }

What is the recommended project folder/repo structure for a dapp (web app + smart contract)?

Let’s say I want to use create-react-app to build the front-end, and of course, truffle to build the backend (smart contract) – do you recommend keeping the two projects in their isolated directories each with their own .git, package.json, and node_modules? Or do you recommend combining both into a parent folder?

Very curious what best practices are around this!

Thanks!
David

PS – I’m loving this so far :slight_smile:

@gabba @filip
Hi guys,

Having this weird error when clicking my buttons. Here is the error:

main.js:42 Uncaught TypeError: contractInstance.methods.fundContract is not a function
at HTMLButtonElement.fundContract (main.js:42)
at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2)
at HTMLButtonElement.v.handle (jquery-3.4.1.min.js:2)

Seems to be surrounding the methods command, I think.
This is probably something simple. :slight_smile:

Learning a lot and thank you in advance,
Dustin

Project Link:

I have problem getting return from the COntract:

This code:

  function playerTossCoin() public validPlayerBalance returns(uint) {
        uint result = random();
        uint spentAmount = 0;
        uint winAmount = 0;

        require(    result == 1 || result==0);
        require(    activeBet[msg.sender].for1 > 0 || activeBet[msg.sender].for0 > 0,
                    "No bet placed, plase place the bet!");

        if (1 == result) {
            winAmount += activeBet[msg.sender].for1 * 2;
            }
        else {
            winAmount += activeBet[msg.sender].for0 * 2;
        }

        resetPlayerBetList();
        myWinBalance(winAmount);

        emit betFinished(spentAmount, winAmount, result);
        return (winAmount);
    }

When I run test in Truffle console and log out return, It became like this:

What did I do wrong?

I finaly finished Phase 1 of Coin FLip.
It was a little bit confusing with debugging proccess…
to sum up, I recommend followings for those after me:
To avoid Metamask Payload error: Control carefully which Metamask account is for what:

  • account 0 from truffle is the owner account which should not be use as player account
  • account 1…9 can use to play bets. You should switch to these account before placing bets…
  • to control accounts injection, use following code; each time update the ActiveAccountAddress = accounts[0] when ever account change is detected.
window.ethereum.on('accountsChanged', function (accounts) {
  // Time to reload your interface with accounts[0]!
})
1 Like

@gabba @filip
I am still getting the error when clicking the button to call the methods commands. I’ve looked over my code and compared it to many others. Also, I’ve deployed some of the other student submissions and I am getting the same thing! I wonder if there is something wrong with the jquery-3.4.1.min.js file?

I do not have the same issues when testing out the people dapp using the Get and Add data commands.

Dustin

Hi @DeCryptolorian

The fundContract is missing into your abi file (json Interface for your function), copy again you’re new function into the abi.js file from your build directory.

1 Like

@jsd
Thank you!! This is the missing piece!!!

1 Like

@jsd
Thanks AGAIN. Everything is fully working. I knew it had to be something easy. Ugh! Learned a lot from this project hah.

Dustin

1 Like

Hey @filip, can you or someone reply to my question when you get a chance. Thanks :slight_smile:

Small logic question: Is the first require unnecessary? Will the 2nd require revert the function if the _address is empty?

    modifier mustHaveRequiredFunds(address _address, uint256 _value) {
        require(_address != address(0), 'Must be a valid address');
        require(_address.balance >= _value, 'Must have sufficient funds');
        _;
    }

Hi @Daveed

Sorry for the late reply, i m really lateeee answering in this topics, we ll improve in the coming weeks.
The best way to start if you are using react is to use the truffle box.
It will setup your project with a great architecture

I love to use this boxes , and to answer you it s better to add your frontend in a client folder inside your backend folder. Splitting the project is just to make it simpler to understand during the course.

Something like this

$ls
build  client  contracts  img  LICENSE  migrations  node_modules  README.md  test  truffle-config.js

$ ls client/
node_modules  package.json  package-lock.json  public  README.md  src

1 Like

Hi @Edward_Valencia

True but the error message will be ‘Must have sufficent funds’ but the issue will be that the contract address is not valid :man_shrugging:

1 Like

Thx @jsd for helping :slight_smile:

2 Likes

@gabba Oh okay so it’s better to keep things like that to increase efficient debugging?

Why is it that when I try to create function random() by returning a uint8 to be gas efficient, like so:

contract PseudoRandom {
    function random() public view returns(uint8) {
        return now % 2;
    }
}

remix gives me error:
‘TypeError: Return argument type uint256 is not implicitly convertible to expected type (type of first return variable) uint8.
return now % 2;
^-----^’

Is it because the block number itself is larger than 8 bits?

Hi @Edward_Valencia,

now is uint256 (now variable missing from you’re PseudoRandom contract) and you have to explicitly down cast to uint8 by doing uint8(now) or just use the same return type (uint256).

Ahh I see. Thanks for the help. @jsd