{"id":1442,"date":"2019-08-05T10:03:07","date_gmt":"2019-08-05T10:03:07","guid":{"rendered":"\/blog\/?p=1442"},"modified":"2024-04-25T14:55:59","modified_gmt":"2024-04-25T14:55:59","slug":"coding-smart-contracts-tutorial-part-ii","status":"publish","type":"post","link":"\/blog\/2019\/08\/coding-smart-contracts-tutorial-part-ii\/","title":{"rendered":"Coding Smart Contracts \u2013 Tutorial Part II"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">How to use a smart contract from a java application<\/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><a href=\"https:\/\/trimplement.com\/blog\/2019\/07\/coding-smart-contracts-tutorial-part-i\/\">In Part I <\/a>of my tutorial I\u2019ve explained how to develop and deploy a simple smart contract. Today we will see how this deployed contract can be used in java applications.&nbsp;<br><\/p>\n\n\n\n<p>We are going to use <a href=\"https:\/\/www.parity.io\/ethereum\/\">Parity<\/a> as Ethereum client and <a href=\"https:\/\/web3j.readthedocs.io\/\">Web3j<\/a> java library for interaction with Parity. I\u2019m assuming that you already have installed <em>Web3j<\/em>, <em>solc<\/em> and <em>Parity<\/em> following \u201cPrepare\u201d instructions in <a href=\"https:\/\/trimplement.com\/blog\/2019\/07\/coding-smart-contracts-tutorial-part-i\/\">Part I<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Get Parity Synced<\/strong><\/h3>\n\n\n\n<p>First of all your <em>Parity<\/em> needs to get synchronized with the <em>Ethereum<\/em> testnet <em>Rinkeby<\/em>, meaning it needs to download the current database status to your local machine. Start your local parity with&nbsp;<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$parity --chain rinkeby --rpcapi \"eth,net,web3,personal\"<br><\/pre>\n\n\n\n<p>See also this <a href=\"https:\/\/wiki.parity.io\/Getting-Synced\">documentation of Parity about getting synced<\/a>.<br><\/p>\n\n\n\n<p>In the meantime, we can prepare everything that we need to call our smart contract from a java application.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">6. Create A Smart Contract Wrapper<\/h3>\n\n\n\n<p>Generate a smart contract wrapper in java, go to the folder where we stored your smart contract and compile the contract using <em>solc<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd myproject\/contracts\/<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mkdir target<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">$ solc Ownable.sol --bin --abi --optimize -o target\/<br><\/pre>\n\n\n\n<p>Now generate the java wrapper class for your token, replacing <em>`\/yourpath\/to\/src\/main\/java`<\/em> and <em>`com.your.organisation.name`<\/em> with the correct path to the target output dir and java package name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ web3j solidity generate target\/Ownable.bin target\/Ownable.abi -o \/yourpath\/to\/src\/main\/java -p your.package.name&nbsp;<br><\/pre>\n\n\n\n<p>Type&nbsp;<em>web3j -h<\/em>&nbsp;to see more options.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Import Your Ethereum Account to Parity<\/h3>\n\n\n\n<p>Do you remember from our <a href=\"https:\/\/trimplement.com\/blog\/2019\/07\/coding-smart-contracts-tutorial-part-i\/\">Part I<\/a> that we used <em>MetaMask<\/em> and <em>Remix<\/em> to test our smart contract directly? Now it\u2019s time to test it via the java application. To do so we need to import the private key of the account that was created via <em>MetaMask<\/em> to our local <em>Parity<\/em>. <em>Parity<\/em> stores credentials in so-called keyfiles. To import your private key from <em>MetaMask<\/em> into <em>Parity<\/em> take the following steps (at the time of writing this article, you need to take an indirect way over <em>Geth<\/em> \u2013 probably <em>Parity<\/em> will provide a direct way later):<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>From MetaMask select<em> Export private key<\/em><\/li><li>Copy the private key and paste it into a file called <em>pass.txt<\/em><\/li><li>Import the account\u2019s private key into <em>Geth<\/em><br><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">$geth --testnet account import ~\/pass.txt<br><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Geth will create a keystore file in <em>~\/.ethereum\/testnet\/keystore\/<\/em> or<br><em>.\/Library\/Ethereum\/testnet\/keystore<\/em> (MacOS)<\/li><li>Now import the keystore file into <em>Parity<\/em>:<br><\/li><\/ul>\n\n\n\n<p>MacOS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$parity --chain=rinkeby account import&nbsp;.\/Library\/Ethereum\/testnet\/keystore<br><\/pre>\n\n\n\n<p>Others:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$parity --chain=rinkeby account import ~\/.ethereum\/testnet\/keystore\/<br><\/pre>\n\n\n\n<p>Thanks to Filip Bielejec for providing <a href=\"https:\/\/www.blog.nodrama.io\/parity-private-key-import\/\">good instructions for importing a private key into Parity<\/a>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Call the Smart Contract from Java<\/strong><br><\/h3>\n\n\n\n<p>Now we have an account in <em>Parity<\/em>, java wrapper is generated&nbsp; for the contract and our <em>Parity<\/em> client is synced with <em>Rinkeby<\/em>. (You can check this looking into the <em>Parity<\/em> logs. The number of the last imported block should be the same as the number of the last block shown in <a href=\"https:\/\/www.rinkeby.io\/#explorer\">Rinkeby Block Explorer<\/a>).<br><\/p>\n\n\n\n<p>Here is a simplified code excerpt on how to call smart contract with <em>web3j<\/em>, <em>Parity<\/em> and our account:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Don't forget to import org.web3j.* and of course our contract <br>\/\/ wrapper class Ownable<br>\/\/ With our contract deployed at address<br>\/\/ 0xc1c29f4ccd7c22e62e7f3aba83ae48e05295acbb<br>\/\/ and the deployer address<br>\/\/ 0x4cc685fC4903dD267F0aE2C27232A81bBFCED751 <br>\/\/ we can do the following:<br>final String keystorefile = \"\/path\/to\/your\/parity\/keyfile\";<br>final String smartContractAddress = \"0xc1c29f4ccd7c22e62e7f3aba83ae48e05295acbb\";<br>final Parity parityClient = Parity.build(new HttpService(\"http:\/\/localhost:8545\/\"));<br>final BigInteger gasPrice = BigInteger.valueOf(2200000);<br>final BigInteger gasLimit = BigInteger.valueOf(4300000);<br>\/\/ Load credentials from the keystore file created in step<br>\/\/ \"Import your Ethereum account to parity\" <br>final Credentials credentials = WalletUtils.loadCredentials(adminPassword, new File(keystorefile));<br>\/\/ Load the smart contract using this credentials<br>final Ownable contract = Ownable.load(smartContractAddress, parityClient, credentials, gasPrice, gasLimit);<br>\/\/ Call getContractOwner(). This should return<br>\/\/ \"0x4cc685fC4903dD267F0aE2C27232A81bBFCED751\"<br>final String contractOwner = contract.getContractOwner().send();<br>\/\/ Call isContractOwner<br>\/\/ This should return true<br>final boolean isContractOwner = contract.isContractOwner(\"0x4cc685fC4903dD267F0aE2C27232A81bBFCED751\").send();<br>\/\/ This should return false<br>final boolean isContractOwner2 = contract.isContractOwner(\"0xc2f16d5040deDa48Fe9292c183c5D76321e83467\").send();<br><\/pre>\n\n\n\n<p>That&#8217;s it!&nbsp;<br><\/p>\n\n\n\n<p>We are done, we created a smart contract, tested it, deployed it and called it&nbsp; from a java application.<br><\/p>\n\n\n\n<p>When you will develop smart contracts for production, you will need to consider some security questions and probably a role-based access to your smart contract. Please let me know if it would be an interesting topic to be described in a separate article.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use a smart contract from a java application In Part I of my tutorial I\u2019ve explained how to develop and deploy a simple smart contract. Today we will see how this deployed contract can be used in java applications.&nbsp; We are going to use Parity as Ethereum client and Web3j java library for [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":1443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[162],"tags":[171,165,192,191,164,163],"class_list":["post-1442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development-tutorials","tag-coding","tag-ethereum","tag-java","tag-parity","tag-smart-contracts","tag-tutorial"],"_links":{"self":[{"href":"\/blog\/wp-json\/wp\/v2\/posts\/1442","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=1442"}],"version-history":[{"count":6,"href":"\/blog\/wp-json\/wp\/v2\/posts\/1442\/revisions"}],"predecessor-version":[{"id":1473,"href":"\/blog\/wp-json\/wp\/v2\/posts\/1442\/revisions\/1473"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/media\/1443"}],"wp:attachment":[{"href":"\/blog\/wp-json\/wp\/v2\/media?parent=1442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/categories?post=1442"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/tags?post=1442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}