×

iFour Logo

How to create contract in solidity

iFour Team - March 09, 2018

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
How to Create Contract in Solidity

What is solidity


Solidity is a high-level, contract-oriented programming language for writing and implementing smart contracts in Blockchain. Blockchain development companies in USA use Solidity, Ethereum and nodejs extensively for Blockchain projects. It was influenced by C++, Python and JavaScript, Solidity is originally designed to target the Ethereum Virtual Machine (EVM).

What is smart contract


Smart contracts are software programs that run on a blockchain. Smart contract is responsible for transferring digital currency or token between users in blockchain development.

Solidity contract code starts with contract Keyword. And its extension is .sol

Here we develop one mathematical solidity smart contract for example, that performs basic Maths operation like Addition, Substitution, Multiplication, Division to illustrate how Solidity is used for writing Smart contracts.

contract Mathematics {

}

lets save this in file name Mathematics.sol

What is Solidity call function


Solidity call functions is a read-only local invocation of a contract function that does not broadcast or send anything on the blockchain.

Function does not consume any Ether to make call on solidity contract, it is identified using constant keyword.

In our example contract, we add four solidity read-only functions for all four Maths operations.

Addition contract function

function getSum(uint a, uint b) constant returns (uint) {
return a + b;     
}
				

Here a and b are given parameters for function and it returns sum. Same as for other operations we add four functions namely getSub, getMultiplication, and getDivision. One can review the whole contact code as follows.

What is Solidity transactions


Transaction is used to send or write data to blockchain. It uses gas and Ether to send data on blockchain.

In our example contract, we find simple interest using solidity smart contract. It is a transaction function. We can not use constant keyword as function in solidity. So contract function is coded like this.

function setInterest(uint a) {
rate = a;
}
				

In transaction we can return value but currently we have not used it in our example.

What is events in smart contract


Solidity Events are inheritable members of contracts and that allow the convenient usage of the EVM logging facilities, this is used to “call” JavaScript callbacks functions in the user interface of a dapp, which listen for these events.

Solidity Events are defined with event keyword in smart contract. For example: I want to create events for Mathematics contract for addition result then it’s syntax looks like this

event eventName(Typeofvariable variablename);
				

Here event is keyword identifier and sum is event name. We add event in our setInterest function so it now look like this.

function setInterest(uint a) {
  rate = a;
  InterestSet(rate);
}
				

What is modifier in smart contract


Solidity Modifier is used to automatically check conditions or change the behavior before executing smart contract. So custom software development company uses modifier to increase performance of blockchain contracts

Modifier is also inheritable properties of smart contracts and overridden by parent smart contracts.For an example, in Division operation number can not be divided by 0. So in our contract, we check that second number is grater than or not equal to 0. then syntax looks like this.

modifier name(){  …. }
				

In our example, notzero checks modifier for division operation is given below.

modifier notZero(uint a){  
  if(a == 0){
	throw;
  }
  _;
}
				

Here _ represent continue with next execution. We add this modifier to our solidity getDivision function so our function will check notzero condition before giving result.

Planning to Hire Blockchain Developer - Enquire Today

 
function getDivision(uint a, uint b) constant notZero(b) returns (uint) {
  return a / b;
}
			  

So our final Solidity Smart Contract looks like this.

pragma solidity ^0.4.15;

contract Mathematics {
  
  uint rate;
  event InterestSet(uint a);
  
  modifier notZero(uint a){  
	if(a == 0){
	  throw;
	}
	_;
  }
  
  function getSum(uint a, uint b) constant returns (uint) {
	  return a + b;
  }
  
  function getSub(uint a, uint b) constant returns (uint) {
	  return a - b;
  }

  function getMultiplication(uint a, uint b) constant returns (uint) {
	  return a * b;
  }
  
  function getDivision(uint a, uint b) constant notZero(b) returns (uint) {
	  return a / b;
  }
  
  function setInterest(uint a) notZero(a){
	  rate = a;
	  InterestSet(rate);
  }
  
  function getSimpleInterest(uint amount, uint month) constant notZero(amount) notZero(month) returns(uint){
	  return  (amount * month * rate)/100;
  }

}
			  

Now we can deploy this contract in parity or using truffle to check how it works. This is very basic contract to get started with solidity. We will show you this in our next blog how to deploy solidity contracts in parity and truffle. Until you can check more contacts example at solidity-by-example.

How to create contract in solidity Table of Content 1. What is solidity 2. What is smart contract 3. What is Solidity call function 3.1 Addition contract function 4. What is Solidity transactions 5. What is events in smart contract 6. What is modifier in smart contract What is solidity Solidity is a high-level, contract-oriented programming language for writing and implementing smart contracts in Blockchain. Blockchain development companies in USA use Solidity, Ethereum and nodejs extensively for Blockchain projects. It was influenced by C++, Python and JavaScript, Solidity is originally designed to target the Ethereum Virtual Machine (EVM). What is smart contract Smart contracts are software programs that run on a blockchain. Smart contract is responsible for transferring digital currency or token between users in blockchain development. Solidity contract code starts with contract Keyword. And its extension is .sol Here we develop one mathematical solidity smart contract for example, that performs basic Maths operation like Addition, Substitution, Multiplication, Division to illustrate how Solidity is used for writing Smart contracts. contract Mathematics { } lets save this in file name Mathematics.sol What is Solidity call function Solidity call functions is a read-only local invocation of a contract function that does not broadcast or send anything on the blockchain. Function does not consume any Ether to make call on solidity contract, it is identified using constant keyword. In our example contract, we add four solidity read-only functions for all four Maths operations. Read More: Basics Of Solidity, Parity, Json-rpc And Visual Studio Code Addition contract function function getSum(uint a, uint b) constant returns (uint) { return a + b; } Here a and b are given parameters for function and it returns sum. Same as for other operations we add four functions namely getSub, getMultiplication, and getDivision. One can review the whole contact code as follows. What is Solidity transactions Transaction is used to send or write data to blockchain. It uses gas and Ether to send data on blockchain. In our example contract, we find simple interest using solidity smart contract. It is a transaction function. We can not use constant keyword as function in solidity. So contract function is coded like this. function setInterest(uint a) { rate = a; } In transaction we can return value but currently we have not used it in our example. What is events in smart contract Solidity Events are inheritable members of contracts and that allow the convenient usage of the EVM logging facilities, this is used to “call” JavaScript callbacks functions in the user interface of a dapp, which listen for these events. Solidity Events are defined with event keyword in smart contract. For example: I want to create events for Mathematics contract for addition result then it’s syntax looks like this event eventName(Typeofvariable variablename); Here event is keyword identifier and sum is event name. We add event in our setInterest function so it now look like this. function setInterest(uint a) { rate = a; InterestSet(rate); } What is modifier in smart contract Solidity Modifier is used to automatically check conditions or change the behavior before executing smart contract. So custom software development company uses modifier to increase performance of blockchain contracts Modifier is also inheritable properties of smart contracts and overridden by parent smart contracts.For an example, in Division operation number can not be divided by 0. So in our contract, we check that second number is grater than or not equal to 0. then syntax looks like this. modifier name(){ …. } In our example, notzero checks modifier for division operation is given below. modifier notZero(uint a){ if(a == 0){ throw; } _; } Here _ represent continue with next execution. We add this modifier to our solidity getDivision function so our function will check notzero condition before giving result. Planning to Hire Blockchain Developer - Enquire Today See here   function getDivision(uint a, uint b) constant notZero(b) returns (uint) { return a / b; } So our final Solidity Smart Contract looks like this. pragma solidity ^0.4.15; contract Mathematics { uint rate; event InterestSet(uint a); modifier notZero(uint a){ if(a == 0){ throw; } _; } function getSum(uint a, uint b) constant returns (uint) { return a + b; } function getSub(uint a, uint b) constant returns (uint) { return a - b; } function getMultiplication(uint a, uint b) constant returns (uint) { return a * b; } function getDivision(uint a, uint b) constant notZero(b) returns (uint) { return a / b; } function setInterest(uint a) notZero(a){ rate = a; InterestSet(rate); } function getSimpleInterest(uint amount, uint month) constant notZero(amount) notZero(month) returns(uint){ return (amount * month * rate)/100; } } Now we can deploy this contract in parity or using truffle to check how it works. This is very basic contract to get started with solidity. We will show you this in our next blog how to deploy solidity contracts in parity and truffle. Until you can check more contacts example at solidity-by-example.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 

Blog Our insights

Azure DevOps Pipeline Deployment for Competitive Business: The Winning Formula
Azure DevOps Pipeline Deployment for Competitive Business: The Winning Formula

We always hear about how important it is to be competitive and stand out in the market. But as an entrepreneur, how would you truly set your business apart? Is there any way to do...

React 18 Vs React 19: Key Differences To Know For 2024
React 18 Vs React 19: Key Differences To Know For 2024

Ever wondered how a simple technology can spark a revolution in the IT business? Just look at React.js - a leading Front-end JS library released in 2013, has made it possible. Praised for its seamless features, React.js has altered the way of bespoke app development with its latest versions released periodically. React.js is known for building interactive user interfaces and has been evolving rapidly to meet the demands of modern web development. Thus, businesses lean to hire dedicated React.js developers for their projects. React.js 19 is the latest version released and people are loving its amazing features impelling them for its adoption.

React 19 For Business: Latest Features and Updates
React 19 For Business: Latest Features and Updates

When I first started exploring React.js for my company’s needs, I felt a bit lost. It was like trying to solve a big puzzle without all the pieces! But as I kept learning and trying them practically, I discovered some really cool features that blew my mind making everything seamlessly easier.