btc/usd
4.86%65,657.90
eth/usd
6.66%2,618.15
ltc/usd
2.60%66.691
xmr/usd
2.90%155.135
xrp/usd
3.16%0.54677
Home > Courses > Blockchain Programming > Solidity Programming and Smart Contracts > Deployment of Smart Contract on Ethereum Network

Deployment of Smart Contract on Ethereum Network

Share:
Deployment of Smart Contract on Ethereum Network

On Ethereum it is possible to develop Smart Contracts, with its own programming language: Solidity. Here we will show step by step the process of developing a smart contract, how to deploy the contract on the network and interact with it.

It does not limit the reading of the data associated with the transactions. Thus, anyone has the possibility to see all the transactions that have been made and that have been validated by the network.

The technical process does not have great difficulty, with a minimum of knowledge of the use of the console, to launch some commands we will provide you can deploy any contract on your own following this guide.


Our Smart Contract


Now, let’s put hands to work and walk to the process. We will work with this Smart Contract previously developed.

This is a simple smart contract for tracking assets in a supply chain. For the development of this application, we will use the Truffle development framework, which allows us to compile smart contracts, inject them into the network and perform the necessary tests.

During this development process it will also be necessary to run a local node of the Ethereum Blockchain, where we can deploy the application once developed for, among other uses, calculate the ether used to pay for the use of the network, the cost of transactions through gas, and all the necessary test according to the expected behavior of the contract.


Preparing the environment


The first thing to do is set the development environment with Truffle. The prerequisite necessary to use both tools is to update Node.js to the latest version. Node.js is an execution environment for JavaScript built with the Chrome V8 JavaScript engine. Use a non-blocking I / O operations model, oriented to events.

The nodejs version used for this guide was v10.15.3

At first, open Terminal on your computer.

Enter the command to see the version of nodejs: 


node -v

In case you need to update them, you can use the node package manager: npm (node package manager) with:


 sudo npm cache clean -f
 sudo npm install -g n
 sudo n stable

Then install Truffle:


 npm install -g truffle

For this exercise we will use the text editor Sublime Text which, by default, does not have the Solidity language syntax. To add it you must follow these steps:

  1. Copy the following in the Sublime Text console and press Enter:


 import urllib.request,os,hashlib ; h =
 'df21e130d211cfc94d9b0905775a7c0f' +
 '1e3d39e33b79698005270310898eea76' ; pf = 'Package
 Control.sublime-package' ; ipp =
 sublime.installed_packages_path () ;
 urllib.request.install_opener ( urllib.request.build_opener (
 urllib.request.ProxyHandler () ) ); by = urllib . request . urlopen (
 'http://packagecontrol.io/' + pf.replace ( ' ' , '%20') ) . read () ;
 dh = hashlib . sha256 (by) . hexdigest () ; print ( 'Error validating
 download (got %s instead of %s) , please try manual install ' %
 (dh, h) ) if dh != h else open ( os . path . join ( ipp, pf) , 'wb'
 ) . write (by)

  1. Restart the Sublime and add Ethereum from the package controller:

  1. Restart Sublime Text. And now we can see Solidity here: 

To initialize the project with Truffle you must do the following:


 mkdir tracking-asset-with-solidity && cd tracking-asset-with-solidity
 init truffle

This creates a directory tree with the project structure. And you will see the following on your console (Terminal):

In the contracts directory, we will place the smart contracts with the solidity extension .sol.


Deployment on the Ethereum test network (Rinkeby)


We recommend the use the wallet MetaMask. MetaMask is very easy to use; it is a secure plugin for most modern browsers (Chrome, Firefox, Opera, etc.) and allows the browser to communicate with the Ethereum Blockchain. Using MetaMask you should not worry about compatibility or security issues since many of these issues are easily handled by MetaMask. Get it from here.

Once installed, create an account on it. You will see something like this:

The upper menu allows you to change from the different Ethereum networks: mainnet, Ropsten, Rinkeby, etc. We will use Rinkeby for this example. And you can get Ether from the faucet.

Copy your address from your account; it will be used when deploying the contract. The recipe for compiling and deploying is as follows:

  1. Compile the contract: from the directory where you initialized the truffle artifact, execute the command:     


truffle compile

2. The contract will be deployed in the Rinkeby test Network. So we will need to start our own node and keep it running and synchronizing. The command for starting the node is simply:


geth --rinkeby

if we want to open the console, for executing geth commands:


geth --rinkeby console

  1. We want to execute transactions with our MetaMask address, so from your MetaMask account you will need to obtain your private key:

Type in your password and you will be able to see your private key. 

  1. In a console execute geth in console mode and type the next command with your private key and password:


web3.personal.importRawKey("private_key", "password")

  1. Close the previous console (this way geth is kept synchronizing in the background). And now unlock your wallet with:


geth --rinkeby --rpc --rpcapi db,eth,net,web3,personal --unlock="YOUR_ADDRESS"

  1. Once successfully compiled and your Ethereum test node synchronized, you will need two files to be configured according to the network where you want to deploy the contract. Here we are experimenting with the rinkeby test network. So, in your truffle-config.js file, write the following:


 module.exports = {
 networks: {
 development: {
 host: "localhost",
 port: 8545,
 network_id: "*" // Match any network id
 },
 rinkeby: {
 host: "localhost", // Connect transaction geth on the specified
 port: 8545,
 from: "YOUR_ADDRESS", // default address to use for any transaction Truffle makes during migrations
 network_id: 4,
 gas: 4612388 // Gas limit used for deploys
 }
 }
 }

  1. Now you will need to create a file in the migrations directory, and we will call it 2_deploy_contracts.js.js This file is the one called for the deployment


 const traceability = artifacts.require("traceability");
 module.exports = function(deployer) {
 deployer.deploy(traceability);
 };

  1. Finally, you just need to execute the command migrate to deploy the contract:


truffle migrate --network rinkeby

With this, the entire procedure is successfully concluded.



Comments

Scroll to top