Programming Project - Phase 1

@filip I get this error in the console:

jquery-3.4.1.min.js:2 Uncaught TypeError: Cannot read property ‘enable’ of undefined
at HTMLDocument. (main.js:4)
at e (jquery-3.4.1.min.js:2)
at t (jquery-3.4.1.min.js:2)

Hi @0xinu sharing your code will be helpful, i guess this is when you are doing window.thereum.enable() ?
is the web3 object initialized ?

var web3 = new Web3(Web3.givenProvider);

var contractInstance;

$(document).ready(function() {

    window.ethereum.enable().then( (accounts) => {

        contractInstance = new web3.eth.Contract(abi, "0x473e2392889af7E65c7443219d181e9d1BDB7c10",{from: accounts[0]});

        console.log(contractInstance);

    }); // brings out metamask pop-up window asking permission

    $("#bet_button").click(setBetSizeAndBet)

});

also getting the same error now on the old page, from the People contract in the previous lesson

Hey @saoirse
I pulled the last version of your project but it seems that your abi.js is not up to date.
If you call this functions in your $(document).ready function they will be called:

$(document).ready(async function() {
    await window.ethereum.enable().then(function(accounts){
        playerAccount = accounts[0];
        contractInstance = new web3.eth.Contract(window.abi, "0x12357A57f2670aC1b65634e6007c7fC796A968DB", {from:playerAccount}); //abi is found in the People.json file
        console.log(contractInstance);
    });

    $("#result-output").html("Nearly there, just deposit some Ether.");
    $("#deposit-button").click(deposit);
    $("#flip-coin-heads").click(flipCoinHeads);
    $("#flip-coin-tails").click(flipCoinTails);
    $("#withdraw").click(getWinnings);

    getUserBalance();
    getContractBalance();
    //click handler, executes the function in bracets
    
});
1 Like

Can you type
window.ethereum in your browser console ?
It seems that you have an issue with the metamask object not injected in the page. Your code seems ok

window.ethereum
undefined

closed all browser windows and restarted, it workde!

Your funding function is working


What kind of issues do you have @Toby_K ?

Edit: But your withdraw function is not, the method in your smart contract is called withdraw not withdrawFunds

function withdrawFunds(){
  contractInstance.methods.withdrawFunds().send().then(function(result){
    result = web3.utils.fromWei(result, "ether");
    alert("Withdrawal successful!");
  });
}
1 Like

Thanks a mil, this maybe a silly question but how do you deploy a contract with an initial balance to payout? At the start of my game, contract balance is zero.
Been looking at different githubs but cannot find an answer.

You can make your constructor payable and do it during the migration

module.exports = function(deployer) {
  deployer.deploy(CoinFlip, {value: web3.utils.toWei("1", "ether")});
};
1 Like

@Gaba Thank you for testing my contract! It appears my issues are with Metamask. Should I reinstall?
image

Hum it’s weird can you share your transaction id to see the error on etherscan ?
I re deployed the contract on my side so maybe the contract you have deployed have an issue.
Where did you deployed your contract ? If it’s locally try

truffle migrate --reset

i tried your contract on the ropsten testnet

I don’t understand. The tx didn’t go through on Ganache. How can it be on Etherscan?

i was just asking because i tried on the testnet i didn’t try locally with ganache. If it’s a local error try to restart ganache and to logout login from metamask.

2 Likes

Hi @franky
If you have a github repository do no hesitate to link it here we can have a look at your code :slight_smile:
Well done for the first project

1 Like

Got this working quite nicely. Definitely going to work on the UI before completing the final project.

2 Likes

Thanks again Gabba! I have seen a few withdrawAll() functions for the contract owner to withdraw the contract balance.

I am trying to create a function for the application user to be able to withdraw their balance from the mapping. Should I be using msg.sender below?

function withdrawUserBalance() public returns(uint) {
uint transfer1 = availableBalances[msg.sender];
availableBalances[msg.sender] = 0;
msg.sender.transfer(transfer1);
return transfer1;
}

Before bet is placed ::

1 Like