Solidity – Basics of Contracts
Solidity Contracts are like a class in any other object-oriented programming language. They firmly contain data as state variables and functions which can modify these variables. When a function is called on a different instance (contract), the EVM function call happens and the context is switched in such a way that the state variables are inaccessible. A contract or its function needs to be called for anything to happen. Some basic properties of contracts are as follows :
- Constructor: Special method created using the constructor keyword, which is invoked only once when the contract is created.
- State Variables: These are the variables that are used to store the state of the contract.
- Functions: Functions are used to manipulate the state of the contracts by modifying the state variables.
Creating a Contract
Creating contracts programmatically is generally done by using JavaScript API web3.js, which has a built-in function web3.eth.Contract to create the contracts. When a contract is created its constructor is executed, a constructor is an optional special method defined by using the constructor keyword which executes one per contract. Once the constructor is called the final code of the contract is added to the blockchain.
Syntax:
contract <contract_name>{
constructor() <visibility>{
.......
}
// rest code
}
Example: In the below example, the contract Test is created to demonstrate how to create a contract in Solidity.
Solidity
pragma solidity >=0.8.6 <0.9.0;
contract Test {
string str;
constructor(string memory str_in){
str = str_in;
}
function str_out() public view returns(string memory){
return str;
}
}
|
Output :
Visibility Modifiers
Solidity provides four types of visibilities for functions and state variables. Functions have to specified by any of the four visibilities but for state variables external is not allowed.
- External: External functions are can be called by other contracts via transactions. An external function cannot be called internally. For calling an external function within the contract this.function_name() method is used. Sometimes external functions are more efficient when they have large arrays of data.
- Public: Public functions or variables can be called both externally or internally via messages. For public static variables, a getter method is created automatically in solidity.
- Internal: These functions or variables can be accessed only internally i.e. within the contract or the derived contracts.
- Private: These functions or variables can only be visible for the contracts in which they are defined. They are not accessible to derived contracts also.
Example: In the below example, the contract contract_example is created to demonstrate different visibility modifiers discussed above.
Solidity
pragma solidity >=0.8.6 <0.9.0;
abstract contract contract_example {
uint private num1;
uint public num2;
string internal str;
constructor(){
num2 = 10;
}
function increment(
uint data1) private pure returns(
uint) {
return data1 + 1;
}
function updateValue(
uint data1) public {
num1 = data1;
}
function getValue(
) public view returns(
uint) {
return num1;
}
function setStr(
string memory _str) public virtual ;
function getStr(
) public virtual returns (string memory);
}
contract derived_contract is contract_example{
function setStr(
string memory _str) public override {
str = _str;
}
function getStr(
) public view override returns (
string memory){
return str;
}
}
contract D {
function readData(
) public returns(
string memory, uint) {
contract_example c = new derived_contract();
c.setStr( "GeeksForGeeks" );
c.updateValue(16);
return (c.getStr(), c.getValue());
}
}
|
Output :
Last Updated :
02 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...