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!


#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’);
})
})
}


#3

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


#4

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


#5

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


#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.


#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)


#8

Awesome, good job!!!