Programming Project - Phase 1

1 Like

Hey @enrico

The 1st issue reported is related to metamask.
If you already reset the account just temporary use another browser where you will install MetaMask.

In order to access a public variable using web3 you can just let res = contractInstance.varName() console.log(res)

Should be enough, let me know,
Dani

Hey @Lucus

Next step is phase two!

Post your code once done,
Dani

1 Like

Hey @rou369

Passing a param uint256 _amount when depositing is not correct.
The only thing you care is msg.value.
If the user calls the deposit function giving 1 ether as parameter but then the msg.value is 0 (because he does not send ether to the contract), then his balance would be 0, because what you care is the amount of ether you receive (msg.value) and not the parameter _value

I am proud to see that you finished the exercise, now jump on phase two !

Cheers,
Dani

1 Like

Hi @dan-i

I thought I had to make a front-end? If I can’t - should I rewatch previous videos and try to make one, or should I just move on to the second phase?

Kind Regards,
Lucus.

Hi Dani, i manage to fix the problem few days ago , didnt even fill up the contract before flip up. But now few days later i have a another problem that when i try to deploy contracts to see if they still work.

“ExtendableError: Unknown network “ganache”. See your Truffle configuration file for available networks.”

I tried changeing the truffle-config.js file and tried few fixes that i found online but it doesn’t work. Do you know maybe what is the problem?

1 Like

Hey @Lucus

I would go for phase two, create the smart contract, test it and then add a front end.

happy coding,
Dani

Hey @Spartak

Something wrong with your truffle config.
I do usually suggest to use ganache-cli as this is the version you will use once you get experience.

Proceed as follow if you’re testing on local blockchain.

truffle develop
truffle migrate --reset 

have fun,
Dani

2 Likes

I have made the following items: 1.webpage for coinflipping (frontend) 2. smartcontract (backend) 3.tests.

But Im having some troubles with compiling my smartcontract. I could use some feedback, geeting some code blindness I guess… I would like to fix this before I continue with testing and running some simulations.
This is the link to all my files in Github: https://github.com/Knickend/Coinflip

import "./SafeMath.sol";
import "./ContractOwner.sol";
pragma solidity 0.5.8;

contract Coinflip is ContractOwner{

  using SafeMath for uint256;

  uint private balance;




  //This creates a map for storing addresses and coinflib ETH bets
  mapping (address => uint256) private CoinflipStorage;
  address[] private coinflipper;

  // Events
  event EthDepositComplete(uint256 EtherInput);

  //This function creates the input (minimum 1 ETH) for the coinflip
  //The input is stored in the map CoinflipStorage
  function Coinflip_input(uint256 EtherInput) public payable{
    //CHECK
    require (msg.value >= 1 ether);
    balance += msg.value;
    //EFFECTS
    uint256 EtherInput = uint256;
    //TRANSACTION
    coinflipper.push(msg.sender);
    //CHECK
    assert(EtherInput>100);
    //emit Event
    emit EthDepositComplete(EtherInput);
  }

  //This function returns  profit(ETH deposit*2) to the player
  function CoinflipWin(CoinflipStorage address[]) external payable {
      //CHECK
      require(address[1].val() > 0);
      address = msg.sender;
      var amount = address.val();
    //  var profit = mul(2,amount);
      var profit = 2*amount;

      //EFFECTS
      address.val() = 0;
      balance -= profit;
      //TRANSACTION
      address.transfer(profit);
      //CHECK
      assert (address.val() == 0);
    }

    // This function transfers the players ETH to contract balance
    function CoinflipLose(coinflipStorage address[]) private payable {
        //CHECK
        require(address.msg.sender.val() > 0);
        address = address(1);
        var amount = address.val();
        //EFFECTS
        address.msg.sender.val() = 0;
        balance += amount;
        //TRANSACTION
        address.transfer(amount);
        //CHECK
        assert (address.msg.sender.val() == 0);
      }

      // This function allows the owner to withdraw the contract balance
      function withdrawAll() private onlyOwner returns(uint) {
          // CHECK
          require(balance > 0);
          uint256 toTransfer = balance;
          // EFFECTS
          balance = 0;
          // TRANSACTION
          msg.sender.transfer(toTransfer);
          return toTransfer;
          //CHECK
          assert(balance == 0 );
      }
      //This function checks the coinflip balance (only visible for owner)
      function getBalance() private returns(uint){
        return balance;
      }
}
1 Like

Wow that was quite the learning experience!!! Still working on the CSS but IT’S WORKING!!!

ezgif.com-gif-maker (1)

Here’s the code: https://github.com/dylankress/CashFlip

1 Like

Okay, thank you very much!

Quick question - how would i send a message whether the user won or lose. Also, I don’t think my contract sends 2 ether when you win - not sure how to fix this ;-;

// SPDX-License-Identifier: UNLICENSED

contract CoinToss {
    uint public betChoice = 0;
    uint public outcome;

    event BetOutcome(address userAddress, uint amount, bool winOrLose);

    function setBet(uint _setBet) external payable {
        require(msg.value == 1 ether || msg.value > 1 ether, "please send minimum 1 ether to play");
        require(_setBet == 1 || _setBet == 2, "please choose either 1 or 2");
        betChoice = _setBet;
        outcome = 0;
    }


    function tossCoin() external payable {
        require(address(this).balance == 1 ether || address(this).balance > 1 ether , "you must select 0 or 1 prior to tossing");
        require(betChoice == 1 || betChoice == 2,  "send minimum 1 ether to choice between 1 or 2");
        
        uint _outcome = block.timestamp % 2;
        
        if(_outcome == betChoice) {
            msg.sender.transfer(msg.value*2);
            betChoice = 0;
            outcome = _outcome;
            emit BetOutcome(msg.sender, msg.value, true);
        }
        
        else {
            betChoice = 0;
            outcome = _outcome;
            emit BetOutcome(msg.sender, msg.value, false);
        }
    }
    
}

https://github.com/CodeNateP/CoinFlip.git

I was having trouble running the code in main.js.
It keep asking me for a from address, assuming there is something wrong with my connection to meta mask. so i tried adding a {from: address} with no luck. Not sure if this has to do with meta mask’s new update to get rid of injected web3

the error looks like

I manage to deploy contracts. Now i have "Trying to call a function on a non-contract address " error.

I had it before also few times and manage to fix it, but now it doesnt work. Checked metamask , reset it, migrate few times, changed the address when i migrate, everything. Even download a newer version of ganache. Do you know any other fixes?

Sry to bother you again…

Hi,

thanks again for your help.

  1. about the public variable i tried to wrote this in main.js in order to display the balance on the website (I tried also with console.log like you show but i get the same error):
    $("#get_balance_button").click(function(){
      let res = contractInstance.balance();
      $("#balance_output").text(res);
    });

I get an error saying:

Uncaught TypeError: contractInstance.balance is not a function
    <anonymous> http://localhost:8000/main.js:20
    jQuery 9
    <anonymous> http://localhost:8000/main.js:19
    jQuery 13

  1. I tried metamask in another browser (chrome and firefox) and it worked for a few times but after a while it gives always the RPC Error: Error: [ethjs-rpc] rpc error with payload. Is there a way to overcome this issue?

  2. besides when I try my contract if the player win I see on ganache that its balance increase. But if the player loose its balance doesn’t decrease (it stays 100). But this was not happening in remix where everything was working fine. maybe is metamask issue?

@enrico try let res = await contractInstance.methods.balance().call()

Here’s a little video of my dapp. Enjoy, lol.
https://youtu.be/8F8ojEzoc_c

@Pigott

you used accounts[1] in main.js. Are you sure you have more than 1 account configured in MetaMask? Perhaps you meant accounts[0]?

hi! thanks, unfortunately I get an error saying await it is only for asyncronous function

@enrico 2 things to think about: if you change your contract, your ABI might change, so may need to regrab the ABI from the json file and replace what’s in abi.js. Also, if you redeployed, did the contract address change? You may need to replace the contract address at the top of main.js?

1 Like