<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sashrika Waidyarathna</title>
    <description>The latest articles on Forem by Sashrika Waidyarathna (@sashrika).</description>
    <link>https://forem.com/sashrika</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F287131%2Fd4007b2d-30c2-4d37-8e16-b304e6023b65.jpg</url>
      <title>Forem: Sashrika Waidyarathna</title>
      <link>https://forem.com/sashrika</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sashrika"/>
    <language>en</language>
    <item>
      <title>Metamask: when does actually `accountChanged` event emits?</title>
      <dc:creator>Sashrika Waidyarathna</dc:creator>
      <pubDate>Tue, 15 Jun 2021 15:35:17 +0000</pubDate>
      <link>https://forem.com/sashrika/metamask-when-does-actually-accountchanged-event-emits-34i8</link>
      <guid>https://forem.com/sashrika/metamask-when-does-actually-accountchanged-event-emits-34i8</guid>
      <description>&lt;p&gt;If you are an Ethereum application developer and are looking for information about &lt;code&gt;accountChanged&lt;/code&gt; event, you will find that there is no much useful information available out there. I will also share some useful tips about building a dApp intergrating with Metamask. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;accountsChanged will be emitted whenever the user's exposed account address changes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;☝️ This is how Metamask define the behavior of accountsChanged event. Let's start exploring when actually user's exposed account address changes. &lt;/p&gt;

&lt;p&gt;First thing to know is either &lt;code&gt;eth_accounts&lt;/code&gt; or &lt;code&gt;eth_requestAccounts&lt;/code&gt; will always return an array with utmost one element. Yes, even though if you have linked two or more accounts, Metamask will only return the currently selected account in Metamask. Metamask may return multiple accounts in future and they have mentioned this in their docs. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs1.im.ge%2F2021%2F06%2F15%2FQefvW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs1.im.ge%2F2021%2F06%2F15%2FQefvW.png" alt="Metamask connect multiple accounts"&gt;&lt;/a&gt;&lt;br&gt;
☝️ If you have more than one Ethereum accounts linked in your Metamask wallet, you will prompt to select accounts which you need to connect with the dApp. &lt;/p&gt;

&lt;p&gt;Still &lt;code&gt;eth_accounts&lt;/code&gt; or &lt;code&gt;eth_requestAccounts&lt;/code&gt; will provide you only one account. I will share two scenarios where this event will emit. &lt;/p&gt;
&lt;h5&gt;
  
  
  1) Switch between accounts which are connected with the dApp.
&lt;/h5&gt;

&lt;p&gt;Let's assume that you have three accounts A,B,C in Metamask. Only A and B is connected with your dApp. If you switch from A to B, &lt;code&gt;accountsChanged&lt;/code&gt; event will be emitted with account B in the array. The reason is Metamask has to change the exposed account to the dApp in this case. Important thing to note here is if you switch from B to C in Metamask, &lt;code&gt;accountsChanged&lt;/code&gt; event will not be emitted as C is not exposed to your dApp. &lt;/p&gt;
&lt;h5&gt;
  
  
  2) Disconnect the currently selected account.
&lt;/h5&gt;

&lt;p&gt;When you disconnect the currently selected account, obviously Metamask has to change the account address that it can expose to the dApp. So the &lt;code&gt;accountsChanged&lt;/code&gt; event will be called with the next Ethereum account linked with the dApp or will be called with an empty array. &lt;/p&gt;
&lt;h6&gt;
  
  
  Sample code which you can inspired from
&lt;/h6&gt;

&lt;p&gt;You can try this React code snippet and modify it according to your requirements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    if (window.ethereum) {
      window.ethereum.on("accountsChanged", (accounts) =&amp;gt; {
        if (accounts.length &amp;gt; 0) {
          setAdress(accounts[0]);
        } else {
          // setWallet("");
          // setStatus("🦊 Connect to Metamask using the top right button.");
        }
      });
    }
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;⭐ Feel free to have a look into this repo. I have implemented most frequently used Metamask events and methods here. &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/sashrika" rel="noopener noreferrer"&gt;
        sashrika
      &lt;/a&gt; / &lt;a href="https://github.com/sashrika/nft-metamask-marketplace" rel="noopener noreferrer"&gt;
        nft-metamask-marketplace
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A complete project that handles buying NFT assets via Metamask. You will be able to buy assets on your own site after listing them on OpenSea. 
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>blockchain</category>
      <category>metamask</category>
      <category>ethereum</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
