A Developer Guide to Building your First Smart Contract

Posted By : Jagveer

Jan 29, 2021

Developers code smart contracts in solidity language. It gets stored on the blockchain and executes automatically in terms of the agreement between a seller and a buyer. In a smart contract solution, certain conditions have to be fulfilled to execute the order successfully.

 

Prerequisites

 

  • Remix Online IDE

 

Structure of a Smart Contract

 

Solidity smart contract is the collection of following

 

  • Function:- In function, apply the logic to transition the state of the contract.
  • Data:- Maintain the current state of the contract.

 

Pragma

 

Pragma is used to enable checks and certain compiler futures. The below statement defines that the smart contract will not compile earlier than 0.5.0 and compiler version after 0.7.0

 

pragma solidity >=0.5.0 <=0.7.0

 

Contract Declaration

 

To declare a contract we use the "contract" keyword. This declares an empty contract which is identified by the name “BuyOrder”.

 

contract BuyOrder {

}

 

Introducing Variables

 

In Solidity language, variables are of two types

 

  • Reference Type:- These types of variables passed by reference and do not fit into 256 bit.
  • Value Type:- These types of variables are passed by value and used as function arguments or in assignments.

 

Adding Data into the Smart Contract

 

Add variables:- Let us add a variable quantity which will be of the data type of integer. Unsigned integer variables are represented by uint256, here 256 is signifying the 256 bits storage. 

 

contract BuyOrder {

uint256 quantity;

}

 

Defining the constructor:- Constructor is always called when the contract is deployed on any node. the constructor initializes the contract with some values. In this scenario, we are initializing the quantity value to 50 when the contract is deployed.

 

constructor() public {

quantity = 50;

}

 

Adding getter functions:- Getter methods are used to read the stored value and the function declaration looks like function <function name> <access modifier> <state> <return value>.

 

function getQuantity() public view returns(uint256) {

return quantity;

}

 

Adding setter functions:- Setter methods are used to write/update the stored value.

 

function setQuantity(uint256 _newQuanity) public {

quantity = _newQuanity;

}

 

After Plugging all this together, Overall contract should look like

 

pragma solidity >=0.5.0 <=0.7.0;

contract BuyOrder {

uint256 quantity;

constructor() public {

         quantity = 50;

}

function getQuantity() public view returns(uint256) {

          return quantity;

}

function setQuantity(uint256 _newQuanity) public {

          quantity = _newQuanity;

}

}

 

Deploying Contract:- 

 

We can use Remix Online IDE to test and deploy the smart contract. Remix Ide completely browser-based where you can write and deploy your smart contract. it also provides you an online solidity compiler to compile your smart contract.

1:- Create a new file and name the file BuyOrder.sol.

2:- Copy and paste the whole contract in the buy order.sol file.

3:- Click on the second icon on the left side of IDE and select the compile to compile the code.

4:- Click on compile BuyOrder.sol.

5:- Once the smart contract compiles successfully, click on the third icon Deploy and Run Transaction to deploy the contract.

6:- Click on the deploy button to deploy the contract. 

 

Conclusion

 

This is an example to build your first smart contract on Remix Online IDE and this can only be checked on the local environment.

 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

October 8, 2024 at 03:30 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text