Solidity, the programming language for Ethereum smart contracts, offers a robust framework for creating decentralized applications. Central to this framework is the concept of contracts. In this guide, we delve deep into Solidity contracts, elucidating their properties, visibility quantifiers, and providing illustrative examples.
classDiagram
C --|> D: External Contract
C --|> E: Derived Contract
class C {
+info: uint
-data: uint
+updateData(uint): void
+getData(): uint
-increment(uint): uint
+compute(uint, uint): uint
}
class D {
+readData(): uint
}
class E {
-result: uint
-c: C
+getComputedResult(): void
+getResult(): uint
+getData(): uint
}
Understanding Solidity Contracts
A Solidity contract is analogous to a Class in languages like C++. Each contract encapsulates:
- Constructor: A unique function, designated by the
constructor
keyword. It runs once per contract and is triggered during the contract's creation. - State Variables: These are variables specific to each contract, holding the contract's state. They play a pivotal role in determining the contract's behavior and interactions.
- Functions: Contracts in Solidity possess functions that can modify state variables, thereby altering the contract's state.
Visibility Quantifiers in Solidity
Visibility quantifiers determine the accessibility of functions and state variables within a contract. Here's a breakdown:
- External: Functions with this quantifier are designed for calls from other contracts. They aren't suited for internal calls. To invoke an external function within the contract, use
this.function_name()
. State variables, however, can't be labeled as external. - Public: Functions and variables with this quantifier can be accessed both externally and internally. For public state variables, Solidity automatically crafts a getter function.
- Internal: Functions and variables with this designation are accessible only internally or by contracts that inherit from them.
- Private: These functions and variables are strictly for internal use, inaccessible even by inheriting contracts.
Solidity Contract Example
Solidity
pragma solidity ^0.5.0;
contract C {
// Private state variable
uint private data;
// Public state variable
uint public info;
// Constructor
constructor() public {
info = 10;
}
// Private function
function increment(uint a) private pure returns(uint) { return a + 1; }
// Public function
function updateData(uint a) public { data = a; }
function getData() public view returns(uint) { return data; }
function compute(uint a, uint b) internal pure returns (uint) { return a + b; }
}
// External Contract
contract D {
function readData() public returns(uint) {
C c = new C();
c.updateData(7);
return c.getData();
}
}
// Derived Contract
contract E is C {
uint private result;
C private c;
constructor() public {
c = new C();
}
function getComputedResult() public {
result = compute(3, 5);
}
function getResult() public view returns(uint) { return result; }
function getData() public view returns(uint) { return c.info(); }
}
FAQs
- What is a Solidity contract?
- A Solidity contract is akin to a Class in C++, encapsulating constructors, state variables, and functions.
- How do visibility quantifiers work in Solidity?
- Visibility quantifiers, like external, public, internal, and private, determine the accessibility of functions and state variables within a contract.
- Can state variables be marked as external?
- No, state variables cannot be labeled as external in Solidity.
- What is the role of a constructor in a Solidity contract?
- A constructor is a unique function that runs once per contract and is invoked during the contract's creation.
- How does Solidity handle public state variables?
- For public state variables, Solidity automatically creates a getter function.