Blockchian Development Basics - Learning Solidity : Step By Step Tutorial : 1

in #blockchain7 years ago

 

A Simple Smart Contract

Let us begin with the most basic example. It is fine if you do not understand everything right now, we will go into more detail later.

pragma solidity ^0.4.0;

contract SimpleStorage {
   uint storedData;

   function set(uint x) public {
       storedData = x;
   }

   function get() public constant returns (uint) {
       return storedData;
   }
}
  1. Keyword pragma solidity ^0.4.0; means Declare the source file compiler version.the source code is written for Solidity version 0.4.0 or anything newer that does not break functionality (up to, but not including, version 0.5.0). This is to ensure that the contract does not suddenly behave differently with a new compiler version.
  2. 'contract' has similarities to 'class' in other languages (class variables, inheritance, etc. SimpleStorage is a class name with the first name in Capital Letter.
  3. The line uint storedData;declares a state variable called storedData of type uint (unsigned integer of 256 bits).
  4. function is a keyword.It is declared each time a new function is created.
  5. set and get can be used to modify or retrieve the value of the variable.
  6. 'public' makes externally readable (not writeable) by users or contracts. "private" means that other contracts can't directly query balances but data is still viewable to other parties on blockchain
  7. constant return (uint)-  The function get() results in the constant  (of type uint256

Steps to execute the above contract in Solidity Remix

  1. Launch http://remix.ethereum.org
  2.  Create a file SimpleStorage.sol. This filename is same as Contract name.
  3. On the right-hand side select run tab.
  4. Below run tab Select Environment as JavaScript VM.
  5. Click Create.Leave other fields populated.
  6. Contract gets executed with a new contract address. Enter an integer value in the text box beside setter method set label .
  7. Click Set label
  8. click getter method get label.
  9. You can observe the details of the executed contract on the transaction debugger as shown below.Click on the Details button to expand.
  10. Transactions SimpleStorage.set method. 
  11. Transactions SimpleStorage.get method
Sort:  

Congratulations @akashgoyal086! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!