Overview of Smart contracts and Gas Limit


Smart contracts are the self-executing codes used for real-time functionalities in Ethereum blockchain. This may include fund transfer, asset management, data management, Ethereum transfer or any other functionalities in Ethereum, in an automated and decentralized model. The smart contract, within transparent blockchain system, is emerging into a new level of security where a person can manage his own data or property without the influence of a third party. Under a Smart contract, one can hold and manage everything in Ethereum.

In other words, Smart contract can be described as some piece of code which is written in solidity consisting of many functions, contracts, and libraries. Like any other application, a smart contract can also be treated as an application which runs on Ethereum platform. Smart Contract enables you to process, read and manipulate data in a blockchain.

Smart contract Overview
If we look at the structure of any Smart contract, it has mainly three parts. 
  1. Compiler Versioning
  2. Contracts
  3. Libraries 

Compiler Versioning is the most integral of any smart contract often called the head part specifying which version of solidity compiler should be used to compile the program. This can be specified using the keyword ‘pragma’ 

For eg: ‘pragma solidity ^0.4.0;’

This specifies that the code needed to be executed using solidity compiler above version 0.4.0. 

Keeping in pace with the technology, Solidity is made with many updates and additions in programming strategies. Therefore it is utmost important that one needs to mention the exact versioning of solidity.

It is very easy to understand the concept of contract in a smart contract as it is moreover like the OOP languages we studied.  Contracts are a combination of functions and data. It is a set of functions and data’s which enables you to make any number of contracts within the smart contract as per the need.  These contracts can inherit functions and data from other contracts also

For eg:

pragma solidity ^0.4.0;
contract Owner {
address owner;
function Owner() public {
    owner = msg.sender;
contract fundManager is Owner {
            mapping (address => uint256) balances;
            function fundManager () public {
            balances[owner] = 10000000000000;
function fundTransfer (address _to,uint256 _fund) public returns(bool) {
            require(_fund > 0 && balances[msg.sender] >= _fund);
            balances[msg.sender] -= _fund;
            balances[_to] += _fund;
            return true;
function viewFund () public constant returns (uint256) {
The above is an example of a simple smart contract for a kind of fund transfer. Here you can see the normal features of oops are applied including constructors and inheritance.
The contract ‘owner’ set the owner variable as the contract deplorer at once using the constructor.

Contract fund Manager, has the functionality power in sending the funds and viewing the fund balance.  Mapping is yet another important feature here. 
Mapping involves simply defining of a map from one data type to another data type. Here data type addresses are mapped into a single unit and are named as balance. Once each address is mapped into a unit value, further, this variable balance of the fund is stored.

‘Require’ is an easy method for exception handling. Simply insert the required conditions to this method before writing any functions. ‘Require’ automatically throw the exceptions and un-favor inputs to the function. 

Libraries contain series of functions which can be used anywhere in a contract. Include the library by putting ‘using <Library name>;’ in the contract. Libraries provide code reusability for smart contracts.

Transactions and Gas Limit
Each interaction made in a blockchain is a transaction. It can be a data written to the blockchain or data read from the blockchain. So every time when we call the functions in a smart contract it is a transaction with unique transaction hash. In Ethereum Smart contract, it mainly consists of two types of functions: State changing functions and Constant/Pure functions.
State changing functions as the name suggest changes or add data to the state variables of smart contract in a blockchain, which in turn is costly. This results in paying a little amount of Ethereum for the state changing transactions.

 In the above example ‘Fund Transfer’ is a state changing function, because it changes the value of available balance. So we need to pay little more ether for this transaction to happen. 
‘View Fund’ is a function which does not change or add any data in the blockchain. In View Fund, it only reads the value of balances from blockchain, we don’t need to pay for calling this function. To in order to specify a non-state change function we need to use the keyword constant/pure.

Transaction cost is dependent on the code. A non-optimized code may require the high amount of value for each transaction. The transaction cost is dependent on gas limit and gas price.

Gas Limit
The gas limit is the maximum amount of gas ( a unit of execution) that the user gives to execute for a state changing code or function to execute in the blockchain. A gas limit is calculated using the code. Remix, MetaMask etc provides automated gas estimation for each transaction. The provided gas limit should be enough to execute the code. In case if the gas limit is too low or gets too high it may result in transaction failure.
Gas price
Gas price is the amount of ether given for every unit of gas for the transaction. This is usually represented in the smallest unit called gwei. Gas price per gas is dynamically changing according to the Ethereum network status so better to do this by going to https://goo.gl/dPzB9L and find out the current gas price. Total transaction cost is the multiple of Gas Limit and Gas price.  You need to always need to use the optimized smart-contract code for a low transaction fee.



Leave a comment