hello and welcome to red stapler channel in this video we are going
to show you how to add a connect meta mask button to your website
and get the address and wallet balance this example we use the latest version of metamask
api and not using the legacy window.web3 which requires migration ready let's check it out
so here's the page that we are going to work on i already prepared a button
so we can start working on the code first let's add the click event listener to the button
and then when the user clicked the button will use metamask api
ethereum ethereum.request with requestaccounts method
the api return promise so we'll use then
the result is an array of accounts we'll use the first address in the array
and then put it on console
now when we click the button it will try to retrieve the account information
and pop up the menu mask for user to unlock the password
next we work on getting the account balance we'll
use the ethereum request again but this time we'll use getbalance method
and then pass the account address
we also need to pass latest as a second parameter to make sure the
api retrieved the balance from the latest block on blockchain
we'll put the result on the console
as you can see the return balance is in hex
obviously we need to convert this before we can use
we'll use parse int to convert the hex to decimal number
since the return value is in wei unit we'll need to divide it by 10 to power of 18.
then i will put it to the console
finally i will also change the button text content to the account address
after the user connected their wallet
okay let's see the result
now you should have the very basic idea of how to connect to the metamask with javascript
in the next episode we'll use this example to continue to request transaction signing
which can be used for something like accepting tips or donation on website in crypto you can
find a link to the code in the description box below so that's all for this episode if
you like it don't forget to like or subscribe our channel thanks for watching see you next time bye