First project in Solidity
Create the contract that recieve the data and retrive the data. - Be able to do following 1. Receive the information, 2. Store the inforamtion, 3. Show the information
pragma solidity >=0.8.2 <0.9.0;
// create the contract that recieve the data and retrive the data.
// be able to do following
//1. receive the information, 2. store the inforamtion, 3. show the information
contract simpleStorage{ // our contract name is simpleStorage
uint storevalue; // variables are the reserved memory location to store value.
// Function is a group of resueable code that can be used anywhere in our application. They perform a specific task.
// For this contract we need two function. One is to set the value and another is to get the value.
// to make a function, "Funtion" keyword is used.
function set(uint _storedvalue) public { // here public enables the visibility so that function can be call from outside the function.
storevalue= _storedvalue;
}
function get() public view returns(uint){// view is modifier which prevent the function to change the state.
return storevalue; // reuturns specifies the return type of the function.
}
}
pragma solidity ...;:- The
pragmakeyword is a directive that provides additional information to the compiler. In this case, it’s used to specify the version of Solidity that should be used to compile the contract. solidityindicates that this pragma is specifically for Solidity, the programming language used to write smart contracts on the Ethereum blockchain.
- The
>=0.8.2:>=means “greater than or equal to.”0.8.2refers to version 0.8.2 of the Solidity compiler.- This part of the pragma specifies that the contract can be compiled with Solidity version 0.8.2 or any newer version.
<0.9.0:<means “less than.”0.9.0refers to version 0.9.0 of the Solidity compiler.- This part of the pragma specifies that the contract cannot be compiled with Solidity version 0.9.0 or any later version.
Detailed Explanation:
- Contract Declaration:
contract simpleStorage { ... }:- This declares a new contract named
simpleStorage. A contract in Solidity is similar to a class in object-oriented programming and contains state variables, functions, and other types of data.
- This declares a new contract named
- State Variable:
uint storevalue;:storevalueis a state variable of typeuint(unsigned integer), which is stored on the blockchain. This variable holds the value that is set by thesetfunction and retrieved by thegetfunction.- State variables are persistent, meaning their values are stored on the blockchain and persist between function calls.
- Function Definition:
setfunction:function set(uint _storedvalue) public { ... }:functionis the keyword used to define a function.setis the name of the function.- The function takes one argument,
_storedvalue, of typeuint. - The
publickeyword makes the function accessible from outside the contract. - Inside the function, the value passed to
_storedvalueis assigned to the state variablestorevalue.
getfunction:function get() public view returns (uint) { ... }:functionis the keyword used to define a function.getis the name of the function.- The function does not take any arguments.
- The
publickeyword makes the function accessible from outside the contract. - The
viewmodifier indicates that the function will not modify the contract’s state (i.e., it will not change any state variables). returns (uint)specifies that the function returns a value of typeuint.- The function returns the value stored in
storevalue.
Summary:
- State Variable (
storevalue): Stores an unsigned integer value that persists on the blockchain. setFunction: Allows the user to set the value ofstorevalue.getFunction: Allows the user to retrieve the value ofstorevaluewithout modifying the blockchain state.