Solidity Crash Course - Part 02
Introduction
Solidity is a high-level programming language used for writing smart contracts on the Ethereum blockchain. It is influenced by JavaScript, Python, and C++. In this part of the crash course, we will cover the basic syntax of Solidity contracts and how to write a smart contract.
Solidity - Contract Syntax
A Solidity contract is a collection of code (functions) and data (state) that resides at a specific address on the Ethereum blockchain. Every Solidity contract starts with a version pragma, which specifies the compiler version.
1. Basic Structure of a Solidity Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyFirstContract {
// State variables
uint public myNumber;
string public myString;
// Constructor - Runs only once when the contract is deployed
constructor(uint _myNumber, string memory _myString) {
myNumber = _myNumber;
myString = _myString;
}
// Function to update number
function setNumber(uint _newNumber) public {
myNumber = _newNumber;
}
// Function to update string
function setString(string memory _newString) public {
myString = _newString;
}
}
Breakdown of Code
- SPDX-License-Identifier - This specifies the license for the contract.
- Pragma directive - It ensures that the contract compiles with Solidity version 0.8.0 or later.
-
Contract declaration - The
contract MyFirstContract {}
defines the contract. -
State Variables -
uint
andstring
variables store persistent data on the blockchain. - Constructor - Initializes the contract's state when deployed.
-
Public Functions - Functions
setNumber
andsetString
modify the contract's state.
Solidity - Smart Contract
A smart contract is a self-executing contract with predefined rules. Once deployed, it interacts with users and other contracts.
2. Writing a Simple Smart Contract
Below is an example of a smart contract that allows users to store and retrieve their favorite number.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract FavoriteNumber {
uint private favoriteNumber;
// Event to notify when a number is changed
event NumberUpdated(uint newNumber);
// Function to set favorite number
function setFavoriteNumber(uint _number) public {
favoriteNumber = _number;
emit NumberUpdated(_number);
}
// Function to get favorite number
function getFavoriteNumber() public view returns (uint) {
return favoriteNumber;
}
}
Explanation
- Events - Used to log contract interactions on the blockchain.
-
Public Function
setFavoriteNumber
- Allows users to store a number. -
View Function
getFavoriteNumber
- Retrieves stored number without modifying the blockchain. - Emit Statement - Logs an event when the number is updated.
Deploying the Contract
To deploy the contract, you can use Remix IDE or Hardhat.
Deploying Using Remix IDE
- Go to Remix IDE.
- Copy and paste the Solidity contract into a new file (
FavoriteNumber.sol
). - Compile the contract using the Solidity compiler.
- Deploy it using the Injected Web3 environment (MetaMask required).
- Interact with the contract using the deployed functions.
If you need any help with deployment, let me know!
Conclusion
In this part, we covered the basic Solidity contract syntax and created a simple smart contract.
Stay tuned for Part 03!
Top comments (0)