Deployment Environments for Managing Ethereum Smart Contracts
Tags: blockchain, ethereum, smartcontracts, solidity
Published: 2019-07-12 18:23:51

<p>Ethereum is a general-purpose blockchain that is  more suited to describing business logic, through advanced scripts, also known  as smart contracts. Ethereum was designed with a broader vision, as a  decentralized or world computer that attempts to marry the power of the  blockchain, as a trust machine, with a Turing-complete contract engine.  Although Ethereum borrows many ideas that were initially introduced by bitcoin,  there are many divergences between the two.<br>
The Ethereum virtual machine and smart contracts  are key elements of Ethereum, and constitute its main attraction. In Ethereum,  smart contracts represent a piece of code written in a high-level language  (Solidity, LLL, Viper) and stored as bytecode in the blockchain, in order to  run reliably in a stack-based virtual machine (Ethereum Virtual Machine), in  each node, once invoked. The interactions with smart contract functions happen  through transactions on the blockchain network, with their payloads being  executed in the Ethereum virtual machine, and the shared blockchain state being  updated accordingly.</p>
<p>For  those who are not familiar with blockchain technology reading <a href="https://myhsts.org/tutorial-history-and-evolution-of-blockchain-technology-from-bitcoin.php">History  and Evolution of Blockchain Technology from Bitcoin</a> article is strongly recommended. Also,  if you wish to learn and practice Hyperledger blockchain development, visit <a href="https://myhsts.org/tutorial-comprehensive-blockchain-hyperledger-developer-guide-for-all-professional-programmers.php">Comprehensive  Hyperledger Training Tutorials</a> page to get the outline of our  Hyperledger tutorial articles. <br>
  We  have written two sets of tutorials to explore Ethereum and Solidity programming  in depth. First set covers the following nine recipes:</p>
<ul>
  <li><a href="https://myhsts.org/tutorial-learn-about-ethereum-blockchain-development-with-dapps-and-ethereum-vm.php">Introduction to Ethereum Blockchain  Development with DApps and Ethereum VM</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-build-auction-dapp-with-ethereum-and-solidity-programming-language.php">Building Auction DApp With Ethereum and  Solidity Programming Language</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-work-with-ethereum-blockchain-applications-through-remix-ide.php">Working with Ethereum Blockchain  Applications through Remix IDE</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-build-bidding-form-in-web3js-for-ethereum-auction-dapp.php">Building Bidding Form in Web3js for  Ethereum Auction DApp</a></li>
  <li><a href="https://developer.ibm.com/recipes/tutorials/working-with-web3js-api-and-json-to-build-ethereum-blockchain-applications/">Working with web3js API and JSON to  Build Ethereum Blockchain Applications</a></li>
  <li>Deployment  Environments for Managing Ethereum Smart Contracts</li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-work-with-ethereum-private-network-with-golang-with-geth.php">Work with Ethereum Private Network with  Golang with Geth</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-compile-and-deploy-ethereum-contracts-using-solidity-compiler.php">Compiling and Deploying Ethereum  Contracts Using Solidity Compiler</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-run-ethereum-auction-dapp-with-some-solidity-tips.php">Running Ethereum Auction DApp and  Solidity Tips</a></li>
</ul>
<p>In  short, you learn about how to set up and configure Ethereum and develop  blockchain applications using Solidity programming language. We explore its key  components, including smart contracts and Web3.JS API via an Auction  Decentralized Application (DApp) step-by-step. <br>
  In  second set, we will discuss more advance topics in Ethereum blockchain  development and solidity while building a Tontine DApp game step-by-step.  Specifically, we cover Truffle and Drizzle. For instance, we show you how a  tool such as Truffle can be an assistant in building, testing, debugging, and  deploying DApps. In summary, we are going to cover four main topics: </p>
<ul>
  <li>Exploring the Truffle suite</li>
  <li>Learning Solidity's advanced features</li>
  <li>Contract testing and debugging </li>
  <li>Building a user interface using Drizzle</li>
</ul>
<p>The 2nd  set consists of 8 recipes as follows:</p>
<ul>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-install-truffle-and-setup-ganache-for-compiling-ethereum-smart-contracts-for-tontine-dapp-game.php">Install Truffle and Setup Ganache for  Compiling Ethereum Smart Contracts for Tontine DApp Game</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-run-tontine-ethereum-dapp-game-contract.php">Run Tontine Ethereum DApp Game Contract</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-design-tontine-ethereum-dapp-game-interfaces.php">Design Tontine Ethereum DApp Game  Interfaces</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-about-blockchain-contract-interactions-between-ethereum-and-solidity-via-tontine-dapp-game.php">Contract Interactions between Ethereum  and Solidity via Tontine DApp Game</a></li>
  <li><a href="https://developer.ibm.com/recipes/tutorials/work-with-ethereum-solidity-and-truffle-unit-testing-in-tontine-dapp-game/">Work with Truffle Unit Testing in  Tontine DApp Game</a></li>
  <li><a href="https://dev.to/weg2g/debugging-with-truffle-and-ethereum-remix-in-tontine-dapp-game-20mi">Debugging  with Truffle and Ethereum Remix in Tontine DApp Game</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-build-frontend-blockchain-application-for-Ethereum-dapp-game-with-drizzle.php">Building Frontend Application for  Tontine DApp Game with Drizzle</a></li>
  <li><a href="https://myhsts.org/tutorial-learn-how-to-run-and-play-tontine-ethereum-dapp-game.php">Running and Playing Tontine Ethereum  DApp Game</a></li>
</ul>
<p><strong><em><u>IMPORTANT: Understanding and completing the first set of recipes are  required prior to working on second set of recipes. </u></em></strong></p>
<p>In Ethereum, we have multiple ways to deploy a  smart contract without spending real ether. In this recipe, we will present how  to set up and deploy your contract in the following testing environments:</p>
<ul>
  <li>Ganache</li>
  <li>Remix Testnet</li>
  <li>Private  network</li>
</ul>
<p>&nbsp;</p>
<h3>Option 1 – Ganache</h3>
<p>If you're looking for a testing blockchain with a graphical
  interface, Ganache (previously TestRpc) is for you.  It's an in-memory blockchain (think of it as a blockchain simulator) that runs locally.</p>
<p>Download and install it from the official Ganache  repository (https://github.com/trufflesuite/ganache/releases) for the appropriate version  for your OS. </p>
<p><br>
  When you run Ganache, you will get a graphical  screen showing some details about the server, the blocks created, transactions,  and a list of created accounts, each loaded with 100 ether:<br>
  <img src="https://coding-bootcamps.com/img/external/15/ethereum-smart-contract-1.gif" alt="Ethereum smart contracts" width="570" height="218"><br clear="ALL">
</p>
<p>To interact with Ganache, you can use Remix, but  this time, you have to specify a new web3 provider with  Ganache's IP and RPC port;  for example, http://localhost:7545  (if you have connection troubles, try to use Remix over HTTP, not HTTPS):<br>
  <img src="https://coding-bootcamps.com/img/external/15/ethereum-smart-contract-2.gif" alt="Ethereum smart contracts" width="570" height="147"><br clear="ALL">
</p>
<br clear="all">

  <p>Once Remix is  connected to Ganache, deploy your smart contract as you did earlier and start  bidding through Remix. In Ganache's interface, you'll be able to visualize the  transactions and the created blocks:<br>
    <img src="https://coding-bootcamps.com/img/external/15/ethereum-smart-contract-3.gif" alt="Ethereum smart contracts" width="570" height="323"><br clear="ALL">
  </p>
  <p>If you think Ganache is enough for you, you can  stop here and jump directly to the Running the auction DApp section, in order  to run the auction DApp using Ganache. However, if you're interested in  learning about other possible deployment environments so you can choose  the suitable one for you, continue reading.</p>
  
  <h3>Option 2 – Testnet</h3>
  <p>Similar to bitcoin, Ethereum's Testnet is a public  network dedicated to testing developers' contracts online without using real  ether. You can join this network without the need to run a full node, by using  a plugin called MetaMask on your browser. Let's see how it's possible.<br>
  </p>


  <h4>Connecting MetaMask to  Testnet</h4>
  <p>MetaMask is a browser plugin that allows your  normal browser to behave as a web3 browser, to interact with DApps, and to send  transactions without running an Ethereum node. MetaMask achieves this by  injecting the web3js API into every web page's JavaScript context.</p>
  <p>The necessary instructions for installing and  setting up MetaMask are well documented on their official website, at: https://metamask.io/. After the process of wallet creation, switch to the Ropsten test network in MetaMask's settings by clicking on  the network selector located on the upper left corner. Once you're connected to Ropsten, you'll need to get some  free worthless ether by choosing to buy some coins as shown in the following screenshot:<br>
    <img src="https://coding-bootcamps.com/img/external/15/ethereum-smart-contract-4.gif" alt="Ethereum smart contracts" width="312" height="299"><br clear="ALL">
</p>
  <p>MetaMask can be connected to any RPC provider locally  (Ganache, Geth) or online (testnet, Mainnet). For a detailed  step-by-step guide to using MetaMask, I point you to the official documentation available at https://github.com/MetaMask/faq/blob/master/USERS.md.</p>

  <p>All set; now, go back to your Remix browser (you  may need to refresh it) and select (in Remix) injected web3 as the environment  option, in order to deploy your contract directly online. MetaMask will  initiate the deployment transaction and give you back the contract address:<br>
    <img src="https://coding-bootcamps.com/img/external/15/ethereum-smart-contract-5.gif" alt="Ethereum smart contracts" width="306" height="440"><br clear="ALL">
  </p>
</div>
<br clear="all">
<p>MetaMask provides an  Ethereum Etherscan link for each transaction, in order to visualize, among  other details, the transaction status and gas cost:<br>
  <img src="https://coding-bootcamps.com/img/external/15/ethereum-smart-contract-6.gif" alt="Ethereum smart contracts" width="492" height="302"><br clear="ALL">
</p>
<p>The contract is easily deployed in the testnet.</p>
<p>Alternatively, you can connect Remix to the testnet  by setting up a light node connected to the testnet network as explained in the  next section. To test your auction web page using MetaMask, you have to add the  following code snippet to the beginning of your auction.js file to detect MetaMask's injected web3 instance:<br>
  if  (typeof web3 !== 'undefined') { App.web3Provider = web3.currentProvider; web3 =  new Web3(web3.currentProvider);<br>
  } else {<br>
  // change to your RPC  provider IP and Port App.web3Provider = new<br>
  web3.providers.HttpProvider('http://127.0.0.1:7545');  web3 = new Web3(App.web3Provider);<br>
  } </p>
<p>Instead using  only web3.setProvider(new web3.providers.HttpProvider(&quot;http://127.0.0.1:8545&quot;));  as we did earlier in section <em>talking to the blockchain.</em></p>
<p>The testnet network  is very helpful as it gives you an accurate idea of the delay processing and  cost of your transactions. However, the implied delay time and limited ether  won't be helpful for developers who regularly change their DApp's code, hence  the following third option or Ganache is more suitable as a starting  environment. You can then you move to this testnet option before going into production on the Mainnet.</p>
<p>&nbsp;</p>
<h3>Option 3 – private network</h3>
<p>Despite the ease of using the previous  environment, which abstracts away all the details, it is still good to know how  things work at a granular level. Besides, you might face the need to deploy  private Ethereum blockchains for private entities to use in production, or  during the software development process.</p>
<p>Let's set up our private Ethereum network (a kind  of localhost), composed of two running nodes on  the same machine which is covered in our next recipe. </p>

<p>
This recipe is written in  collaboration with Brian Wu who is a senior Hyperledger instructor at <a href="https://coding-bootcamps.com/">Coding Bootcamps</a> school in Washington  DC.</p>