Metamask Address Exercise

#1

In this exercise I want to you create a piece of code by yourself. Watch the lecture video to see me explaining the assignment. You probably need to use the web3.js documentation (https://web3js.readthedocs.io/en/1.0/) in order to solve this exercise. The most important thing is that you TRY, not that you get it 100% correct the first time. Good luck!

1 Like

#2

function mintAfterGame(noOfTokens) {
web3.eth.getAccounts(accounts).then(accounts => {
contract.methods.mint(accounts[1], noOfTokens)
.send({from: accounts[1]}).on(‘reciept’, reciept => {
alert(‘Transaction Completed’);
})
})
}

0 Likes

#3

Looks good. Maybe you need to change to accounts[0], but I’m not sure.

0 Likes

#4

You are right @filip this do not work with accounts[1]. But I don’t understand why is it happening.

0 Likes

#5

The array is zero-indexed. So the first (and only) address in the array is id nr 0.

1 Like

#6

This took a bit for me to figure out on my own but super happy to have figured it out without watching the solution video ;o)

            if(gameOver){
                if(!coinsSent){
                    var address;
                    web3.eth.getAccounts().then(acct => {
                        address = acct[0];
                        console.log("address: " + address);

                        if (address == null || address == "") {
                        console.log("No ETH Address Entered");
                        alert("No ETH Address Entered");
                        }
                        else {
                        console.log("Mint the winning tokens");
                        mintAfterGame(address, score);
                    }
                    coinsSent = true;
                    });
                }
                return;
                }

Found some interesting code I’m going to implement now that I got this working… it will check that metamask is installed, unlocked and if there is enough balance to process.

1 Like

#7

OK after watching the solutions video I see that even though my code works, it of course makes sense that this function go in the eth.js file etc… cleaning up my code to reflect this ;o)

2 Likes

#8

Awesome, good job!!!

0 Likes

#9

I’m sorry, I already peeked to the solution. So no point to paste the code :see_no_evil: time–;
But I think I understood everything :v:

1 Like

#10

After some trial and error and googling, this is the solution I came up with:

In the index.html file I made these changes:

  function updateTimeLeft(){
          if(gameOver) {
            if(!coinsSent){
            //  var address = prompt("Please enter your ETH address", "");
            //  if(address == null || address == ""){
            //    alert("User cancelled the prompt");
            //  }
            //  else{
                mintAfterGame(score);
              }
              coinsSent = true;
          //  }
            return;
          };

And in the eth.js file I made these changes:

  var address = 0;

  web3.eth.getAccounts().then(e => {
    address = e[0];
  //  alert("Address: " + address);
  })

//my mintAfter game function is: 

function mintAfterGame(nrOfTokens){
  contract.methods.mint(address, nrOfTokens).send({from: address})
  .on('receipt', receipt => {
    alert("Transaction Complete");
  })
1 Like