Connect a Metamask Account to a Svelte Application

Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

The first step in this part of the journey, will be to setup a simple UI that allow you to connect the current user to the blockchain through it's wallet.

To do this, let's write some code.

Sveltekit applications works by using a file system routing method, meaning that you need to create files under the src/routes/ folder and those will be then reflected as routes (urls) in your app. Let's keep ti simple and just use the default route here, open src/routes/index.svelte and delete the content of it.

You're first step will be create a button that allow the user to "sing-in" to your new dapp and allow the UI to react to that showing a different content if the authorization process was successful.

Instructor: [0:00] Now that your smart contract is done, it's time to work on the front-end application for it. In this case, you'll work with SvelteKit. The first step in this part of your journey is to implement a simple UI that allows the application to connect the current user to the blockchain using the wallet. [0:23] Let's write some code. SvelteKit applications work by using a file system routing strategy. It means that you need to create files under the routes folder, and those will be then reflected as URLs in your application. Let's keep it simple and just use the default route here and delete its contents.

[0:47] Let's create first a button that will allow the user to sign in into your application and enable the UI to react to that change, showing a different content. If the authorization process was successful, this little snippet of code will render a button if the user's address state variable is null or a message otherwise.

[1:14] Let's add now a function to react to the click event. This function will be named as connectWallet() and will be associated with the button element using the onclick action. Now, it's time to start working with the wallet.

[1:31] MetaMask injects an object named Ethereum into the global window object. The first step is to check that the window.ethereum is available. If that's true, it's time to request access to the Ethereum account. For that, you can use the method provided by the window.ethereum object.

[1:55] To retrieve accounts available, you'll use the request method, parsing the string eth.request(accounts) as action. This will return an array of accounts. Choose the first one, since that is the one selected in MetaMask, and update the state to reflect that the user is connected.

[2:17] Don't forget that in Svelte, the state update is done just by assigning a new value to a certain variable. Save it. You can test this out by checking your application. Don't forget to set the onClick action at the bottom and pass the connectWallet function.

[2:37] Remember, you have to keep this running in a terminal with npm run dev. Click on the button. You'll see that MetaMask pop-up shows up asking to allow the connection to be accepted. You can see that the application UI reflects that change by rendering the user account.

[2:58] In this lesson, you learned how to request access to the user's wallet and to add this interaction to the Svelte application.

egghead
egghead
~ 17 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today