<?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: Espanicon</title>
    <description>The latest articles on Forem by Espanicon (@espanicon).</description>
    <link>https://forem.com/espanicon</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%2Forganization%2Fprofile_image%2F5285%2F19506f60-e0f1-4798-853e-f446b5909226.jpg</url>
      <title>Forem: Espanicon</title>
      <link>https://forem.com/espanicon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/espanicon"/>
    <language>en</language>
    <item>
      <title>Interacting with ICON Bridge contracts (proxy contracts) on EVM chains.</title>
      <dc:creator>FidelVe</dc:creator>
      <pubDate>Sat, 22 Oct 2022 17:59:10 +0000</pubDate>
      <link>https://forem.com/espanicon/interacting-with-icon-bridge-contracts-proxy-contracts-on-evm-chains-19fd</link>
      <guid>https://forem.com/espanicon/interacting-with-icon-bridge-contracts-proxy-contracts-on-evm-chains-19fd</guid>
      <description>&lt;p&gt;The ICON Network is a decentralized blockchain project from South Korea with the goal of becoming a hub for interconnecting many other blockchains with its &lt;a href="https://medium.com/helloiconworld/blockchain-transmission-protocol-btp-explained-c4d9927ad398"&gt;BTP technology&lt;/a&gt;, it currently hosts many Dapps (Decentralized Apps) ranging from NFT games and platforms ( like &lt;a href="https://gangstabet.io/"&gt;gangstabet&lt;/a&gt;, &lt;a href="https://craft.network/"&gt;Craft Network&lt;/a&gt;, &lt;a href="https://projectnebula.app/"&gt;Project Nebula&lt;/a&gt;, etc) to DeFi platforms (like &lt;a href="https://balanced.network/"&gt;Balanced Network&lt;/a&gt;, &lt;a href="https://omm.finance/"&gt;OMM Finance&lt;/a&gt; and &lt;a href="https://optimus.finance/"&gt;Optimus Finance&lt;/a&gt;) and even a fully decentralized casino running entirely on smart contracts (&lt;a href="https://iconbet.io/"&gt;ICONBet&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In this tutorial we will learn to interact with &lt;a href="https://medium.com/helloiconworld/introducing-icon-bridge-f8d3f2d93bf8"&gt;ICON Bridge&lt;/a&gt; a chain-agnostic bridge that is able to interconnect any blockchain that supports smart contracts.&lt;/p&gt;

&lt;h2&gt;
  
  
  ICON Bridge on EVM Chains
&lt;/h2&gt;

&lt;p&gt;Proxy contracts on EVM chains &lt;strong&gt;stores the address of the logic contract and delegates all function calls to the logic contract&lt;/strong&gt; (which holds the business logic) using the &lt;em&gt;delegatecall&lt;/em&gt; function. After the call is forwarded to the logic contract, the returned data from the logic contract is retrieved and returned to the user.&lt;/p&gt;

&lt;p&gt;As a practical example lets interact with the &lt;a href="https://github.com/icon-project/icon-bridge/blob/main/docs/testnet_deployment.json"&gt;BTS contract on the BSC testnet&lt;/a&gt; using javascript (web3.js).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BTS proxy contract: 0x1a2aDf985D6c2700fdAf72A9c1e2b39e3B647F7e&lt;/li&gt;
&lt;li&gt;BTS logic contract: 0xE020d4aD483C7eC90a24d9db502e66564eF9c236&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To interact with the BTS contract is necessary to &lt;strong&gt;call the proxy contract directly and the proxy contract will pass the call to the logic contract&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Using web3.js we can do this with the following method call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Web3&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;web3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bscTestnetWeb3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Web3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://data-seed-prebsc-1-s1.binance.org:8545&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BTSLogicContractAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0xE020d4aD483C7eC90a24d9db502e66564eF9c236&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BTSLogicContractABI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// at this point this data is unkown&lt;/span&gt;

&lt;span class="c1"&gt;// This next line will return in an error because we dont have the ABI&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contractObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;bscTestnetWeb3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;BTSLogicContractABI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;BTSLogicContractAddress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the previous code example we still need one variable, the &lt;code&gt;BTSLogicContractABI&lt;/code&gt; in order to create the contract object that we are going to use to interact with the ICON Bridge using web3.js.&lt;/p&gt;

&lt;p&gt;On EVM chains the ABI data is not available on chain, there are 2 ways to obtain that data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile the contract yourself and obtain the ABI.&lt;/li&gt;
&lt;li&gt;Fetch the ABI directly from somewhere else. This is generally done using trackers in the specific chain you are trying to interact.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this example you can fetch the ABI data directly from the bscscan tracker either using the &lt;code&gt;https&lt;/code&gt; module on &lt;code&gt;nodejs&lt;/code&gt; or a &lt;code&gt;fetch&lt;/code&gt; call with js in the browser. The following is an example of getting the ABI with &lt;code&gt;curl&lt;/code&gt; from a shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="s2"&gt;"https://api-testnet.bscscan.com/api?module=contract&amp;amp;action=getabi&amp;amp;address=0xE020d4aD483C7eC90a24d9db502e66564eF9c236&amp;amp;format=raw"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is important to notice that we are getting the ABI of the &lt;code&gt;BTSLogicContract&lt;/code&gt; and not the ABI of the &lt;code&gt;BTSProxyContract&lt;/code&gt;, as explained previously, &lt;strong&gt;the correct ABI with the methods to interact with the ICON Bridge (or any evm proxy contract pattern) are on the logic contract not on the proxy contract&lt;/strong&gt;, but you have to make the query to the proxy contract because this is the one that will forward this call to the logic contract and handle the proper reply back.&lt;/p&gt;

&lt;p&gt;Once we have the proper ABI data we can continue creating the contract object and making the method calls that we wish to make to the BTS proxy contract.&lt;/p&gt;

&lt;p&gt;The following is a working example using &lt;code&gt;nodejs&lt;/code&gt; (is necessary to previously install the &lt;code&gt;web3&lt;/code&gt; library)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                                                    
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Web3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;web3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                                                      

&lt;span class="c1"&gt;// wrapping everything in an async function to handle the data request             &lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;asyncRun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                                                        
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bscTestnetWeb3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Web3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;                                                 
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://data-seed-prebsc-1-s1.binance.org:8545&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;                               
  &lt;span class="p"&gt;);&lt;/span&gt;                                                                               
  &lt;span class="c1"&gt;// ABI and address of both proxy and logic contract                              &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BTSProxyContractAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0x1a2aDf985D6c2700fdAf72A9c1e2b39e3B647F7e&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BTSLogicContractAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0xE020d4aD483C7eC90a24d9db502e66564eF9c236&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BTSProxyContractABI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;asyncABIRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;BTSProxyContractAddress&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BTSLogicContractABI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;asyncABIRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;BTSLogicContractAddress&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      

  &lt;span class="c1"&gt;// Web3 Contract Object of the BTS logic contract                                &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contractObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;bscTestnetWeb3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;                          
    &lt;span class="nx"&gt;BTSLogicContractABI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                                                           
    &lt;span class="nx"&gt;BTSLogicContractAddress&lt;/span&gt;                                                        
  &lt;span class="p"&gt;);&lt;/span&gt;                                                                               

  &lt;span class="c1"&gt;// encoding a call to a readonly method named 'getNativeCoinName'                &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getNativeCoinName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;contractObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getNativeCoinName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;            
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encodedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getNativeCoinName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;encodeABI&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;                               

  &lt;span class="c1"&gt;// making a readonly call   &lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nativeCoinNameCallResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bscTestnetWeb3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;53&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BTSProxyContractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                                                   
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;encodedData&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// parsing the hex response into utf8&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parsedResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;bscTestnetWeb3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUtf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;nativeCoinNameCallResponse&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parsedResponse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Running the async example&lt;/span&gt;
&lt;span class="nx"&gt;asyncRun&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;



&lt;span class="c1"&gt;// the following is a function to handle the request asynchronously created only&lt;/span&gt;
&lt;span class="c1"&gt;// to illustrate the example, it works "as is" but doesnt handle many things&lt;/span&gt;
&lt;span class="c1"&gt;// that you should handle when writing production code in an app (timeout, proper&lt;/span&gt;
&lt;span class="c1"&gt;// data on error, etc)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;asyncABIRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;asyncQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ABIRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;hostname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;api-testnet.bscscan.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`/api?module=contract&amp;amp;action=getabi&amp;amp;address=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;format
=raw`&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// process chunked data&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rawData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;rawData&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;end&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parsedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parsedData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`error making async query: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;ABIRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Error on request: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;ABIRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;asyncQuery&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>crypto</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>web3</category>
    </item>
    <item>
      <title>ICON Network and JavaScript: How to interact with the ICON Blockchain with JS (node.js)</title>
      <dc:creator>FidelVe</dc:creator>
      <pubDate>Thu, 03 Feb 2022 11:31:40 +0000</pubDate>
      <link>https://forem.com/espanicon/icon-network-and-javascript-how-to-interact-with-the-icon-blockchain-with-js-nodejs-547c</link>
      <guid>https://forem.com/espanicon/icon-network-and-javascript-how-to-interact-with-the-icon-blockchain-with-js-nodejs-547c</guid>
      <description>&lt;p&gt;The ICON Network is a decentralized blockchain project from South Korea with the goal of becoming a hub for interconnecting many other blockchains with its &lt;a href="https://medium.com/helloiconworld/blockchain-transmission-protocol-btp-explained-c4d9927ad398"&gt;BTP technology&lt;/a&gt;, it currently hosts many Dapps (Decentralized Apps) ranging from NFT games and platforms ( like &lt;a href="https://gangstabet.io/"&gt;gangstabet&lt;/a&gt;, &lt;a href="https://craft.network/"&gt;Craft Network&lt;/a&gt;, &lt;a href="https://projectnebula.app/"&gt;Project Nebula&lt;/a&gt;, etc) to DeFi platforms (like &lt;a href="https://balanced.network/"&gt;Balanced Network&lt;/a&gt;, &lt;a href="https://omm.finance/"&gt;OMM Finance&lt;/a&gt; and &lt;a href="https://optimus.finance/"&gt;Optimus Finance&lt;/a&gt;) and even a fully decentralized casino running entirely on smart contracts (&lt;a href="https://iconbet.io/"&gt;ICONBet&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;This is the first article in a series dedicated to show how to interact with the ICON Network using &lt;a href="https://nodejs.org/en/"&gt;nodejs&lt;/a&gt;. While the article is directed to people that have no prior knowledge of how to interact with smarts contracts on ICON or any other blockchain project, it is necessary to have a prior knowledge of coding with JavaScript (specially asynchronous programming with JavaScript) and nodejs (knowing how to use modules is necessary).&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the JSON RPC API
&lt;/h2&gt;

&lt;p&gt;All your interactions with the ICON Network and the different smart contracts in the chain are going to be done via a &lt;em&gt;JSON RPC API&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The JSON RPC API is a &lt;a href="https://en.wikipedia.org/wiki/Remote_procedure_call"&gt;remote procedure call&lt;/a&gt; encoded in &lt;a href="https://en.wikipedia.org/wiki/JSON"&gt;JSON&lt;/a&gt;, to put it in simple terms, you are going to be sending data in a predefined schema or format to a smart contract in the ICON Network and if all goes well you'll be receiving a reply with the data you asked (if things don't go well you'll still receive an error reply too).&lt;/p&gt;

&lt;p&gt;This is the description of the &lt;a href="https://www.icondev.io/references/reference-manuals/icon-json-rpc-api-v3-specification"&gt;ICON JSON RPC API V3&lt;/a&gt; and this is how a common call and response would look like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Request
{
    "jsonrpc": "2.0",
    "method": "$STRING1",
    "id": $INT,
    "params": {
        "$KEY1": "$VALUE1",
        "$KEY2": {
            "method": "$STRING2",
            "params": {
                "$KEY3": "$VALUE3"
            }
        }
    }
}

// Response - success
{
    "jsonrpc": "2.0",
    "id": $INT,
    "result": "$STRING"
    // or
    "result": {
      "$KEY1": "$VALUE1",
      "$KEY2": "$VALUE2"
    }
}

// Response - error
{
    "jsonrpc": "2.0",
    "id": $INT1,
    "error": {
        "code": $INT2,
        "message": "$STRING"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The communication to the ICON Network is done from your code (any script or Dapp you build) to a citizen node (which is basically a full node with no governance participation in the network, a silent listener of the chain) an then to an active governance node (called Public Representatives or P-Reps) that is in charge to act up or process the interactions and/or transactions you are going to be making and then it propagates those changes to the entire network. The following diagram shows these interaction.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hgk4t5PD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khla470mg1923r1xoq8r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hgk4t5PD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khla470mg1923r1xoq8r.png" alt="Talking with the ICON Network via the JSON RPC API" width="774" height="545"&gt;&lt;/a&gt; &lt;sup&gt;&lt;a href="https://medium.com/helloiconworld/icon-deconstructed-5eb7f99eeeb1"&gt;Image taken from the "ICON Deconstructed Part III" article on Medium&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to know before talking to the chain, the https module and JS promises
&lt;/h2&gt;

&lt;p&gt;As shown in the previous image there are several SDK in different languages that you can use but for the purpose of this article and because we are going to start with simple things we will be doing the communication directly via HTTPS requests.&lt;/p&gt;

&lt;p&gt;Now that we know what is the language that we will be using to communicate to the ICON Network (JSON RPC API), there is one important piece of code that we need to write which will become our main module to talk to the ICON Network and that is an HTTPS request wrapped in a promise to allow for asynchronous interactions with the network.&lt;/p&gt;

&lt;p&gt;Assuming that you already have &lt;a href="https://nodejs.org/en/download/"&gt;nodejs&lt;/a&gt; installed in your computer, go and create a folder and then run &lt;code&gt;npm init&lt;/code&gt; to create a new project. The folder structure in the project is going to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dev.to-article/
├── httpsRequest.js
├── index.js
└── package.json

0 directories, 3 files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to start working on the &lt;code&gt;httpsRequest.js&lt;/code&gt; file. The content is pretty straightforward we import the &lt;code&gt;https&lt;/code&gt; module, create an &lt;code&gt;async function&lt;/code&gt; wrap the &lt;code&gt;https request&lt;/code&gt; inside this function and execute it using &lt;code&gt;await&lt;/code&gt; to get the response from the network and then at the end we export the &lt;code&gt;httpsRequest&lt;/code&gt; async function as a module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// httpsRequest.js
// This module is a https request wrapped &lt;span class="k"&gt;in &lt;/span&gt;a promise design to be used
// to interact with the ICON Blockchain
//
// Imports
const https &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

/&lt;span class="k"&gt;**&lt;/span&gt;
 &lt;span class="k"&gt;*&lt;/span&gt; async https request wrapped &lt;span class="k"&gt;in &lt;/span&gt;a promise
 &lt;span class="k"&gt;*&lt;/span&gt; @param &lt;span class="o"&gt;{&lt;/span&gt;Object&lt;span class="o"&gt;}&lt;/span&gt; param - params &lt;span class="k"&gt;for &lt;/span&gt;the http request
 &lt;span class="k"&gt;*&lt;/span&gt; @param &lt;span class="o"&gt;{&lt;/span&gt;string&lt;span class="o"&gt;}&lt;/span&gt; param.hostname
 &lt;span class="k"&gt;*&lt;/span&gt; @param &lt;span class="o"&gt;{&lt;/span&gt;string&lt;span class="o"&gt;}&lt;/span&gt; param.ip
 &lt;span class="k"&gt;*&lt;/span&gt; @param &lt;span class="o"&gt;{&lt;/span&gt;number&lt;span class="o"&gt;}&lt;/span&gt; param.port
 &lt;span class="k"&gt;*&lt;/span&gt; @param &lt;span class="o"&gt;{&lt;/span&gt;number&lt;span class="o"&gt;}&lt;/span&gt; param.timeout
 &lt;span class="k"&gt;*&lt;/span&gt; @param &lt;span class="o"&gt;{&lt;/span&gt;string&lt;span class="o"&gt;}&lt;/span&gt; param.path
 &lt;span class="k"&gt;*&lt;/span&gt;/
async &lt;span class="k"&gt;function &lt;/span&gt;httpsRequest&lt;span class="o"&gt;(&lt;/span&gt;params, data &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  const promisifiedQuery &lt;span class="o"&gt;=&lt;/span&gt; new Promise&lt;span class="o"&gt;((&lt;/span&gt;resolve, reject&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    const query &lt;span class="o"&gt;=&lt;/span&gt; https.request&lt;span class="o"&gt;(&lt;/span&gt;params, res &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      // Print status code on console
      console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Status Code: "&lt;/span&gt; + res.statusCode&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      // Process chunked data
      &lt;span class="nb"&gt;let &lt;/span&gt;rawData &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      res.on&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"data"&lt;/span&gt;, chunk &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        rawData +&lt;span class="o"&gt;=&lt;/span&gt; chunk&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      // when request completed, pass the data to the &lt;span class="s1"&gt;'resolve'&lt;/span&gt; callback
      res.on&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"end"&lt;/span&gt;, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;let &lt;/span&gt;data &lt;span class="o"&gt;=&lt;/span&gt; JSON.parse&lt;span class="o"&gt;(&lt;/span&gt;rawData&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        resolve&lt;span class="o"&gt;(&lt;/span&gt;data&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      // &lt;span class="k"&gt;if &lt;/span&gt;error, print on console
      res.on&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;, err &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Got error: "&lt;/span&gt;, +err.message&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    // If request &lt;span class="nb"&gt;timeout &lt;/span&gt;destroy request
    query.on&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"timeout"&lt;/span&gt;, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"timeout. destroying query"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      query.destroy&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    // Handle query error
    query.on&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;, err &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error running query, passing error to callback reject"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      reject&lt;span class="o"&gt;(&lt;/span&gt;err&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;data &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      // If data param is passed into &lt;span class="k"&gt;function &lt;/span&gt;we write the data
      query.write&lt;span class="o"&gt;(&lt;/span&gt;data&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    // end request
    query.end&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  // &lt;span class="nb"&gt;wait &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;the response and &lt;span class="k"&gt;return &lt;/span&gt;it
  try &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;await promisifiedQuery&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;err&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error while running promisifiedQuery"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    console.log&lt;span class="o"&gt;(&lt;/span&gt;err&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
module.exports &lt;span class="o"&gt;=&lt;/span&gt; httpsRequest&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Talking to the governance smart contract on the ICON Network
&lt;/h2&gt;

&lt;p&gt;On the &lt;code&gt;index.js&lt;/code&gt; file we are going to start by importing the &lt;code&gt;httpsRequest.js&lt;/code&gt; module and then start creating the JSON RPC formatted request we will be sending to the ICON network.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// index.js
const httpsRequest &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'./httpsRequest.js'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// These are the params of the https request
const PARAMS &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;hostname&lt;/span&gt;: &lt;span class="s1"&gt;'api.icon.geometry.io'&lt;/span&gt;, // This is a citizen node &lt;span class="k"&gt;in &lt;/span&gt;the network
  path: &lt;span class="s1"&gt;'/api/v3'&lt;/span&gt;,
  method: &lt;span class="s1"&gt;'POST'&lt;/span&gt; // all https request to the network are POST requests
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// This is the JSON RPC formatted call we are sending to the network
const DATA &lt;span class="o"&gt;=&lt;/span&gt; JSON.stringify&lt;span class="o"&gt;({&lt;/span&gt;
  jsonrpc: &lt;span class="s1"&gt;'2.0'&lt;/span&gt;,
  method: &lt;span class="s1"&gt;'icx_call'&lt;/span&gt;,
  &lt;span class="nb"&gt;id&lt;/span&gt;: 1,
  params: &lt;span class="o"&gt;{&lt;/span&gt;
    to: &lt;span class="s1"&gt;'cx0000000000000000000000000000000000000000'&lt;/span&gt;,
    dataType: &lt;span class="s1"&gt;'call'&lt;/span&gt;,
    data: &lt;span class="o"&gt;{&lt;/span&gt;
      method: &lt;span class="s1"&gt;'getPReps'&lt;/span&gt;,
      params: &lt;span class="o"&gt;{&lt;/span&gt;
        startRanking: &lt;span class="s1"&gt;'0x1'&lt;/span&gt;,
        endRanking: &lt;span class="s1"&gt;'0x16'&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this specific example we are interacting with the governance smart contract in the ICON Network (&lt;code&gt;cx000...&lt;/code&gt;) and calling the method &lt;code&gt;getPReps&lt;/code&gt;, this will allow us to get a list of the P-Reps (node validators) in the network, also since there are more than 100 P-Reps in the network we are passing a &lt;code&gt;startRanking&lt;/code&gt; and &lt;code&gt;endRanking&lt;/code&gt; values to get only the top 22 P-Reps by delegation in the network.&lt;/p&gt;

&lt;p&gt;The final code in the &lt;code&gt;index.js&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// index.js
const httpsRequest &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"./httpsRequest.js"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// These are the params of the https request
const PARAMS &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;hostname&lt;/span&gt;: &lt;span class="s2"&gt;"api.icon.geometry.io"&lt;/span&gt;, // This is a citizen node &lt;span class="k"&gt;in &lt;/span&gt;the network
  path: &lt;span class="s2"&gt;"/api/v3"&lt;/span&gt;,
  method: &lt;span class="s2"&gt;"POST"&lt;/span&gt; // all https request to the network are POST requests
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// This is the JSON RPC formatted call we are sending to the network
const DATA &lt;span class="o"&gt;=&lt;/span&gt; JSON.stringify&lt;span class="o"&gt;({&lt;/span&gt;
  jsonrpc: &lt;span class="s2"&gt;"2.0"&lt;/span&gt;,
  method: &lt;span class="s2"&gt;"icx_call"&lt;/span&gt;,
  &lt;span class="nb"&gt;id&lt;/span&gt;: 1,
  params: &lt;span class="o"&gt;{&lt;/span&gt;
    to: &lt;span class="s2"&gt;"cx0000000000000000000000000000000000000000"&lt;/span&gt;,
    dataType: &lt;span class="s2"&gt;"call"&lt;/span&gt;,
    data: &lt;span class="o"&gt;{&lt;/span&gt;
      method: &lt;span class="s2"&gt;"getPReps"&lt;/span&gt;,
      params: &lt;span class="o"&gt;{&lt;/span&gt;
        startRanking: &lt;span class="s2"&gt;"0x1"&lt;/span&gt;,
        endRanking: &lt;span class="s2"&gt;"0x16"&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Run async httpsRequest and print response on console
&lt;span class="o"&gt;(&lt;/span&gt;async &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  try &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;let &lt;/span&gt;response &lt;span class="o"&gt;=&lt;/span&gt; await httpsRequest&lt;span class="o"&gt;(&lt;/span&gt;PARAMS, DATA&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    console.log&lt;span class="o"&gt;(&lt;/span&gt;response&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;err&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Error running httpsRequest"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    console.log&lt;span class="o"&gt;(&lt;/span&gt;err&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running &lt;code&gt;node index.js&lt;/code&gt; we are going to get a reply like this from the smart contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dev.to-article&lt;span class="nv"&gt;$ &lt;/span&gt;node index.js 
Status Code: 200
&lt;span class="o"&gt;{&lt;/span&gt;
  jsonrpc: &lt;span class="s1"&gt;'2.0'&lt;/span&gt;,
  result: &lt;span class="o"&gt;{&lt;/span&gt;
    blockHeight: &lt;span class="s1"&gt;'0x2b924c6'&lt;/span&gt;,
    preps: &lt;span class="o"&gt;[&lt;/span&gt;
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object], &lt;span class="o"&gt;[&lt;/span&gt;Object],
      &lt;span class="o"&gt;[&lt;/span&gt;Object]
    &lt;span class="o"&gt;]&lt;/span&gt;,
    startRanking: &lt;span class="s1"&gt;'0x1'&lt;/span&gt;,
    totalDelegated: &lt;span class="s1"&gt;'0x1205ef103fffce01d3dd1ff'&lt;/span&gt;,
    totalStake: &lt;span class="s1"&gt;'0x13f0b214efa073bac2d0d53'&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="nb"&gt;id&lt;/span&gt;: 1
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! now you know how to interact with smart contracts on the ICON Network!, I invite you to join the community by visiting the &lt;a href="https://forum.icon.community/"&gt;forum&lt;/a&gt; and &lt;a href="https://t.me/icondevs"&gt;telegram channel for developers&lt;/a&gt; and keep learning so that you can also build amazing projects in the ICON Ecosystem, we have a &lt;a href="https://medium.com/@helloiconworld/introducing-the-contribution-proposal-system-b7ad5d877dc5"&gt;decentralized funding program called CPS (Contribution Proposal System)&lt;/a&gt; that anyone can use to finance and build projects on the ICON Network so if you have a great idea please come and build with us!.&lt;/p&gt;

&lt;p&gt;If you like this article please leave a comment and share it on social media, more articles will be coming explaining things about interacting with smart contracts on the ICON Network.&lt;/p&gt;

&lt;p&gt;Thanks for reading!.&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
