Hey Guys!  In this blog post I am going to explain in detail about  blockchain technology,  truffle framework, solidity, deploying smart contracts on blockchain network.

 

What is blockchain ?

Blockchain is a list of blocks that are linked using hashing. It is a kind of digital ledger that is incorruptible and is ideal for recording financial transactions or anything of value. It is based on Merkel tree data structure.  Some major applications of blockchain technology are bitcoin,ether which are some of the widely used cryptocurrencies. Bitcoins and other cryptocurrencies are earned by validating the transaction/block on the network which is also known as mining.

 

To understand the whole process of blockchain we need to first go through the following terminologies:

Smart Contract: A contract comprises of code(business logic) and data that is stored at a specific address on the Ethereum blockchain.

Solidity: Solidity is a contract-oriented, high-level language for implementing smart contracts.

Ether: Cryptocurrency that will be used for transactions on blockchain. We can buy these digital currencies like bitcoin,ether from any exchange. The value of each unit varies from day to day depending on demand and supply.

Wallet:  Wallet is identified by a private key.  We can create our wallet on main network. We have to retain the private key of wallet which will be used to receive and send Ether/bitcoin.  It is like a bank account in a decentralised network. No government agencies  like CIA, FBI etc. can track the owner of the wallet because it is stored in a decentralised network and does not have any owner information associated with it.   When we deploy contracts and perform transactions on public blockchain it always consumes some gas(cost) which we have to pay in the form of Ether.  

 

Writing contract in solidity

 I am going to demonstrate a very simple example of a contract for the purpose of concept understanding. Usually the contracts written for blockchain projects are far more complex. The contract below contains the basic business logic and the fields that we want to store against this contract. 

In the above example we have created a struct for File which resembles an object for storing related info. We will store list of file struct where each “file struct” will contain file hash including other details. We will invoke the setter method for storing the file details and getter method for accessing the file record stored at a particular index of a list.

More complex example of a contract would be following(Source Link) :

 

Once our contract is ready, the next step is to deploy our contract on the blockchain network. To deploy contract we can use truffle framework that allows us to write,manage and deploy contracts. In truffle.js file we will make the following configuration:

Let’s focus on the “production” block. Host will contain the URL of the Ethereum blockchain RPC endpoint.  By default port is 8545. The gas attributes contains the value of the gas that we are willing to spend for a particular transaction. If the gas value is higher, transaction will be faster. We must decide on an optimum value of gas. The gas value that we spend goes to the miner that mines the block. Now, to deploy the contract we will use the following command:


truffle migrate --network production

The above command deploys a contract and returns a contract address:

 

The contract address returned is 0xb1d72c7f78692cc2b239264e705367382908d2ff.

We will use the above contract address to interact with our blockchain using Web3.js. The interact with the contract we need the ABI of the contract. In truffle framework, when the contract is compiled the ABI of the contract can be found in ./build/contracts folder.

 

We need to copy the whole ABI json and use it when writing the web3.js call.

 

Web3.js code to interact with the blockchain is given below:

I will demonstrate the functioning of the contract calls(setter and getter) using ethereum remix tool. 

In the above example we are calling the set method of the contract and passing the file name and the file hash. We will click on transact button to initiate this transaction. Once the transaction is complete we will get a transaction hash and we can verify the transaction on the blockchain portal. It shows the amount of gas expended on this transaction.

In a similar way we can call the get method to retrieve the values stored at a particular index of a list.

That was all folks! If you have any questions/doubts related to this topic you may comment below. I will be more than happy to help.

Thanks and keep learning! 😉