<?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: Karthick S</title>
    <description>The latest articles on Forem by Karthick S (@0x24karthick).</description>
    <link>https://forem.com/0x24karthick</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%2F848037%2Fe0e79591-fcb6-480d-bd74-af37d43b8527.jpeg</url>
      <title>Forem: Karthick S</title>
      <link>https://forem.com/0x24karthick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/0x24karthick"/>
    <language>en</language>
    <item>
      <title>How to Switch to Particular Network using Window.Ethereum</title>
      <dc:creator>Karthick S</dc:creator>
      <pubDate>Mon, 23 Jan 2023 16:56:15 +0000</pubDate>
      <link>https://forem.com/0x24karthick/how-to-switch-to-particular-network-using-windowethereum-j0a</link>
      <guid>https://forem.com/0x24karthick/how-to-switch-to-particular-network-using-windowethereum-j0a</guid>
      <description>&lt;p&gt;In Web3, Almost Every Dapps were connected to a Particular Network. For Example, Beginners and Learning based projects are mostly connected to a Test Network.&lt;/p&gt;

&lt;p&gt;When a Transaction triggered by the user, the Metamask will be connected and Perform the Transaction.In this, the user needs to manually Switch to a Particular Network to perform the Transaction.&lt;/p&gt;

&lt;p&gt;Suppose, if the user is new to web3, he doesn't know! What network he need to switch.&lt;/p&gt;

&lt;p&gt;So for that Coming up with the solution. Switch Network/chain in Metamask.&lt;/p&gt;

&lt;p&gt;Here is a JavaScript Program to Switch from one Network or chain to other:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const SwitchNetwork = async (_chainId) =&amp;gt; {
    let chainId = _chainId;
    await window.ethereum
      .request({
        method: "wallet_switchEthereumChain",
        params: [{ chainId: chainId }],
      })
      .then(() =&amp;gt; console.log("Successfully! Connected to the requested Network"))
      .catch((err) =&amp;gt; {
        if (err.message.startsWith("Unrecognized chain ID")) {
          addNewNetwork();
        }
      });
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above in &lt;strong&gt;catch()&lt;/strong&gt; block, you can find &lt;strong&gt;addNetwork()&lt;/strong&gt;. This will help the user to add Automatically connected to the Network,Which doesn't exist in his Metamask.&lt;/p&gt;

&lt;p&gt;To know addNetwork(), Follow this!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/0x24karthick/how-to-add-custom-network-in-metamask-using-windowethereum-3mg"&gt;https://dev.to/0x24karthick/how-to-add-custom-network-in-metamask-using-windowethereum-3mg&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                        Happy Coding!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>snippet</category>
      <category>code</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to add Custom Network in Metamask using Window.Ethereum</title>
      <dc:creator>Karthick S</dc:creator>
      <pubDate>Thu, 19 Jan 2023 18:20:22 +0000</pubDate>
      <link>https://forem.com/0x24karthick/how-to-add-custom-network-in-metamask-using-windowethereum-3mg</link>
      <guid>https://forem.com/0x24karthick/how-to-add-custom-network-in-metamask-using-windowethereum-3mg</guid>
      <description>&lt;p&gt;&lt;strong&gt;What if&lt;/strong&gt;! if the Metamask has no Network, The user wants to works with.He needs to add manually in Metamask.&lt;/p&gt;

&lt;p&gt;It may take sometime. By Copy &amp;amp; Paste! by surfing other website and getting details.&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;ChainLink&lt;/strong&gt; user can add the Network Easily. But Not all time, it won't works!&lt;/p&gt;

&lt;p&gt;The Developer will have a query! Then How to I add Programmatically???&lt;/p&gt;

&lt;p&gt;Here Comes up with a Solution!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A JavaScript code using Window.Ethereum.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Program to Connect Polygon Mainnet Easily&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addNetwork = await window.ethereum.request({
        method: "wallet_addEthereumChain",
        params: [{
          chainId: "0x89",
          rpcUrls: ["https://polygon-rpc.com/"],
          chainName: "Matic Mainnet",
          nativeCurrency: {
            name: "MATIC",
            symbol: "MATIC",
            decimals: 18
          },
          blockExplorerUrls: ["https://polygonscan.com/"]
        }]
      });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope it may useful for Dapp Devs. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Happy Coding!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>blockchain</category>
      <category>web3</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
