{"id":1733,"date":"2020-03-03T09:21:39","date_gmt":"2020-03-03T09:21:39","guid":{"rendered":"\/blog\/?p=1733"},"modified":"2020-03-03T10:06:16","modified_gmt":"2020-03-03T10:06:16","slug":"coding-smart-contracts-tutorial-infura","status":"publish","type":"post","link":"\/blog\/2020\/03\/coding-smart-contracts-tutorial-infura\/","title":{"rendered":"Coding Smart Contracts -Tutorial Part III"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Why Infura<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait.png\" alt=\"Photo of Natallia Martchouk, co-founder of trimplement\" class=\"wp-image-337\" width=\"293\" height=\"293\" srcset=\"\/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait.png 556w, \/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait-150x150.png 150w, \/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait-300x300.png 300w, \/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait-60x60.png 60w, \/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait-24x24.png 24w, \/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait-48x48.png 48w, \/blog\/wp-content\/uploads\/2018\/12\/Natallia-Martchouk-trimplement-portrait-96x96.png 96w\" sizes=\"auto, (max-width: 293px) 100vw, 293px\" \/><figcaption>Natallia Martchouk, co-founder of trimplement, explains how to develop Ethereum smart contracts.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Last year I published<a href=\"https:\/\/trimplement.com\/blog\/2019\/08\/coding-smart-contracts-tutorial-part-ii\/\"> a tutorial<\/a> helping you get started with the development of smart contracts and using them from java applications. In those tutorials, I\u2019ve described how to use <em>parity<\/em> as the Ethereum client. Exactly this setup we\u2019ve used also in our own project <a href=\"https:\/\/trimplement.com\/blog\/2019\/10\/art-technology-value-manifesto\/\">\u201cValue Manifesto\u201d<\/a>. But a couple of weeks ago we\u2019ve decided to switch the Ethereum client from <em>parity<\/em> to <em>Infura<\/em>. This gives me an opportunity to extend my tutorial with new information.<br><\/p>\n\n\n\n<p>One of the reasons for changing the Ethereum client is as follows: maintaining your own Ethereum node can give you headaches. First of all, you need to keep your <em>parity<\/em> version up to date as it\u2019s being continuously improved and adapted to the changes happening on the Ethereum blockchain.&nbsp;<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Also, you need to pay for a hard disk with enough space (in our case a volume in the AWS environment) where the Ethereum database is stored. Actually this was not a huge issue, because we believe in decentralization and would like to maintain our own node, contributing to the growing power of the Ethereum network. But unfortunately due to some political and commercial reasons the development team of <em>parity<\/em> <a href=\"https:\/\/www.parity.io\/parity-ethereum-openethereum-dao\/\">decided to source out <em>parity<\/em> to the community.<\/a>&nbsp;<br><\/p>\n\n\n\n<p>Of course, you never know, probably it will be a positive change! But when your live system with active customers is based on a component where future development and support are not clear, you start to think about alternatives. And thus we\u2019ve decided to switch to <em>Infura<\/em>.&nbsp;<br><\/p>\n\n\n\n<p><a href=\"https:\/\/infura.io\/\"><em>Infura<\/em><\/a> is a cloud-based solution offered by the <a href=\"https:\/\/consensys.net\/\">global blockchain company ConsenSys<\/a>. You don\u2019t need to start your own Ethereum node, you can access the blockchain via Infura APIs. They have flexible pricing with a free tier that is perfectly fine for our needs.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Migration from parity to Infura<\/h2>\n\n\n\n<p>It\u2019s not rocket science to switch from <em>parity<\/em> to <em>Infura<\/em> if you are using <em>web3j<\/em> library as we do. The main differences between using both clients are:<br><br>&#8211; <em>Infura<\/em> is maintaining Ethereum nodes for you in their cloud. You call them via remote APIs. So no need to install anything locally and to wait until your local node is synchronized with Ethereum\u2019s testnet or mainnet database<\/p>\n\n\n\n<p>&#8211; the only thing you need to store locally are key files of your accounts, either for your own smart contract owner account or for your users. E.g. if you are creating Ethereum accounts on behalf of your users and keep their private keys, you will need to take care of this. But that\u2019s&nbsp; not the topic of this tutorial, so let\u2019s continue with the steps needed to setup and use <em>Infura<\/em> for calling your smart contract.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1. Register on Infura<\/h3>\n\n\n\n<p>Register an account at <a href=\"https:\/\/infura.io\/\">https:\/\/infura.io\/<\/a> and create a project there following the instructions.&nbsp;<br><\/p>\n\n\n\n<p>In your <em>Infura<\/em> project dashboard, you will see your <em>Project ID<\/em> and <em>Project Secret<\/em>.&nbsp;<br><\/p>\n\n\n\n<p><em>Project ID<\/em> will be the part of URL when calling Infura, e.g. <em>mainnet.infura.io\/v3\/&lt;YOUR-PROJECT-ID&gt;&nbsp;<\/em> for mainnet or <em>rinkeby.infura.io\/v3\/&lt;YOUR-PROJECT-ID&gt;<\/em> for the rinkeby testnet<br><\/p>\n\n\n\n<p>And <em>Project Secret<\/em> is your API key which needs to be kept secret and non-readable by humans. <em>Project Secret<\/em> is not mandatory for API calls by default. It\u2019s a very good idea to make it mandatory. You can enable it in your project\u2019s settings under Security. Please check also, whether whitelisting of origins, contract addresses and user agents is applicable for you. There cannot be \u201ctoo much\u201d security, especially important are proper security settings when you are not only playing with test apps but going to launch a production system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2. Implement Infura client<\/h3>\n\n\n\n<p>In the java service class where we use the <em>parity<\/em> client to call our smart contract (see the <a href=\"https:\/\/trimplement.com\/blog\/2019\/08\/coding-smart-contracts-tutorial-part-ii\/\">part II of my tutorial<\/a>) we just need to switch to the Infura <em>client<\/em> instead.<br><\/p>\n\n\n\n<p>As we would like to use our <em>Project Secret<\/em> for a proper authentication the creation of the <em>Infura<\/em> client is a bit more verbose.&nbsp;<br><\/p>\n\n\n\n<p>First we create a <em>HttpService<\/em> and add the authorization header with our <em>Project Secret<\/em> to it. If you\u2019ve enabled mandatory <em>Project Secret <\/em>and don\u2019t provide it, you will get an error saying:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">org.web3j.protocol.exceptions.ClientConnectionException:&nbsp;Invalid response received: 403; {\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32002,\"message\":\"rejected due to project ID settings\"}}<br><\/pre>\n\n\n\n<p>We will be using rinkeby as tesnet:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">final String url = \"https:\/\/rinkeby.infura.io\/v3\/<strong>&lt;YOUR-PROJECT-ID&gt;<\/strong>\";<br>final HttpService httpService = new HttpService(url);<br>final String auth = StringUtils.EMPTY + \":\" + <strong>&lt;YOUR-PROJECT-SECRET&gt;)<\/strong>;<br>final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));<br>final String authHeader = \"Basic \" + new String(encodedAuth);<br>httpService.addHeader(HttpHeaders.AUTHORIZATION, authHeader);<br><\/pre>\n\n\n\n<p>Now we can create an <em>Infura<\/em> client using this HttpService:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">final Web3j infuraClient = Web3j.<em>build<\/em>(httpService);<\/pre>\n\n\n\n<p>The remaining steps for loading <em>Credentials<\/em> from the keyfile, loading and calling your smart contract stays actually the same as described in <a href=\"https:\/\/trimplement.com\/blog\/2019\/08\/coding-smart-contracts-tutorial-part-ii\/\">part II of the tutorial<\/a>. However, we could make a small improvement when loading the smart contract. Instead of providing fixed values for gas price and gas limit we can provide our own implementation of a <em>GasProvider<\/em>.&nbsp;<\/p>\n\n\n\n<p>The beauty of this version lies in that you can either use the default implementation <em>StaticGasProvider<\/em> with fixed values for gas price and gas limit like before. Or you can implement your own dynamic <em>GasProvider<\/em> that calculates gas price and gas limit per smart contract function. For the <em>StaticGasProvider<\/em> it would look as follows:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">final BigInteger gasPrice = BigInteger.valueOf(2200000);<br>final BigInteger gasLimit = BigInteger.valueOf(4300000);<br>final ContractGasProvider gasProvider = <strong>new<\/strong> StaticGasProvider(gasPrice, gasLimit);<br>final Ownable contract = Ownable.load(smartContractAddress, infuraClient, credentials, gasProvider);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3. Remove <em>parity<\/em><\/h3>\n\n\n\n<p>When you\u2019ve implemented and tested your <em>Infura<\/em> client, you can stop and remove your <em>parity<\/em> instance and free some disk space removing unneeded <em>parity<\/em> files and the local Ethereum database.<br><\/p>\n\n\n\n<p>That\u2019s it!<br><\/p>\n\n\n\n<p>Hope this was helpful. Happy coding!<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why Infura Last year I published a tutorial helping you get started with the development of smart contracts and using them from java applications. In those tutorials, I\u2019ve described how to use parity as the Ethereum client. Exactly this setup we\u2019ve used also in our own project \u201cValue Manifesto\u201d. But a couple of weeks ago [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":1734,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[162],"tags":[171,191,172,164,163],"class_list":["post-1733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development-tutorials","tag-coding","tag-parity","tag-programming","tag-smart-contracts","tag-tutorial"],"_links":{"self":[{"href":"\/blog\/wp-json\/wp\/v2\/posts\/1733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/comments?post=1733"}],"version-history":[{"count":10,"href":"\/blog\/wp-json\/wp\/v2\/posts\/1733\/revisions"}],"predecessor-version":[{"id":1865,"href":"\/blog\/wp-json\/wp\/v2\/posts\/1733\/revisions\/1865"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/media\/1734"}],"wp:attachment":[{"href":"\/blog\/wp-json\/wp\/v2\/media?parent=1733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/categories?post=1733"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/tags?post=1733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}