DEV Community

Cover image for Deployment Environments for Managing Ethereum Smart Contracts
Matt
Matt

Posted on

Deployment Environments for Managing Ethereum Smart Contracts

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.
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.

For those who are not familiar with blockchain technology reading History and Evolution of Blockchain Technology from Bitcoin article is strongly recommended. Also, if you wish to learn and practice Hyperledger blockchain development, visit Comprehensive Hyperledger Training Tutorials page to get the outline of our Hyperledger tutorial articles.
We have written two sets of tutorials to explore Ethereum and Solidity programming in depth. First set covers the following nine recipes:

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.
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:

  • Exploring the Truffle suite
  • Learning Solidity's advanced features
  • Contract testing and debugging
  • Building a user interface using Drizzle

The 2nd set consists of 8 recipes as follows:

IMPORTANT: Understanding and completing the first set of recipes are required prior to working on second set of recipes.

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:

  • Ganache
  • Remix Testnet
  • Private network

 

Option 1 – Ganache

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.

Download and install it from the official Ganache repository (https://github.com/trufflesuite/ganache/releases) for the appropriate version for your OS.


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:
Ethereum smart contracts

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):
Ethereum smart contracts

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:

Ethereum smart contracts


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.

Option 2 – Testnet


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.

Connecting MetaMask to Testnet


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.


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:

Ethereum smart contracts


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.

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:

Ethereum smart contracts




MetaMask provides an Ethereum Etherscan link for each transaction, in order to visualize, among other details, the transaction status and gas cost:
Ethereum smart contracts

The contract is easily deployed in the testnet.

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:
if (typeof web3 !== 'undefined') { App.web3Provider = web3.currentProvider; web3 = new Web3(web3.currentProvider);
} else {
// change to your RPC provider IP and Port App.web3Provider = new
web3.providers.HttpProvider('http://127.0.0.1:7545'); web3 = new Web3(App.web3Provider);
}

Instead using only web3.setProvider(new web3.providers.HttpProvider("http://127.0.0.1:8545")); as we did earlier in section talking to the blockchain.

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.

 

Option 3 – private network

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.

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.

This recipe is written in collaboration with Brian Wu who is a senior Hyperledger instructor at Coding Bootcamps school in Washington DC.

Top comments (0)