- Explain what a contract is and how it compares with procedures and classes.
- List the elements that a contract can contain.
- Create one contract from another with
newand interact with it. - Describe abstract contracts and libraries at an introductory level.
What are Contracts?
All Solidity code is written inside contracts. A contract is a collection of code (its functions) and data (its state) that lives at a specific address on the Ethereum blockchain. It is the fundamental building block of every Ethereum application.
POP vs OOP vs BOP
This course compares three programming styles. “BOP – Block Oriented Programming” is the name used in this course to describe contract-based programming on a blockchain.
| Sr.No. | Title | Short | Language | Unit of code | Contains |
|---|---|---|---|---|---|
| 1 | Procedure Oriented Programming | POP | C | Functions / procedures | Functions operating on separate data |
| 2 | Object Oriented Programming | OOP | C++ / Java / C# | Classes | State & methods, objects in memory |
| 3 | Block Oriented Programming (course term) | BOP | Solidity | Contracts | State & functions, stored permanently on the blockchain |
What Can a Contract Contain?
| Pragma (file level) | Instructions to the compiler. Every file should start with a version pragma so it is not compiled by an incompatible future compiler. |
| contract keyword | Declares a contract and encloses all of its code. |
| State variables | Values stored permanently in contract storage on the blockchain. |
| Functions | Code blocks that read or change state; declared with function, a name, parameters and a body. |
| Function modifiers | Reusable checks applied to functions (Chapter 22). |
| Events | Logs that front ends can listen to (Chapter 20). |
| Custom errors | Named errors used with revert (Chapter 21). |
| Struct and enum types | User-defined data types (Chapters 11 and 13). |
| Constructor | Runs once at deployment (Chapter 17). |
Kinds of Contract-like Units
- contract – a normal deployable contract.
- abstract contract – has at least one function without implementation (or is marked
abstract); it cannot be deployed and is used as a base for inheritance (Chapter 18). - interface – only function declarations, no implementation or state (Chapter 19).
- library – reusable code with no state of its own, e.g. maths helpers; its functions are called by contracts.
Example : One Contract Creating Another
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Wallet {
address public owner;
string public label;
constructor(address _owner, string memory _label) {
owner = _owner;
label = _label;
}
}
contract WalletFactory {
Wallet[] public wallets; // contract type as a variable type
function createWallet(string calldata label) external returns (address) {
Wallet w = new Wallet(msg.sender, label); // deploys a new contract
wallets.push(w);
return address(w); // address of the new contract
}
function ownerOf(uint index) external view returns (address) {
return wallets[index].owner(); // call a getter of another contract
}
function totalWallets() external view returns (uint) {
return wallets.length;
}
}
Key Takeaways
- A contract = state + functions at a blockchain address, similar to a class whose objects live on-chain.
- Contracts can contain state variables, functions, modifiers, events, errors, structs, enums and a constructor.
newdeploys a contract from another contract; contract types can be stored and called.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
// SPDX-License-Identifier: MIT declares the file’s licence.Wallet w = new Wallet(msg.sender, label);. This deploys a new instance and returns a reference to it.address(w). Inside a contract, address(this) gives its own address.wallets[index].owner(); this performs an external message call.Wallet[] wallets), and they can be converted to and from addresses.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Contracts (creating contracts, visibility and getters, function modifiers, constant and immutable, functions, events, custom errors, inheritance, abstract contracts, interfaces, libraries). Solidity Documentation. https://docs.soliditylang.org/en/latest/contracts.html
- Solidity Team. (n.d.). Structure of a Contract. Solidity Documentation. https://docs.soliditylang.org/en/latest/structure-of-a-contract.html
- Solidity Team. (n.d.). Layout of a Solidity Source File (SPDX, pragmas, imports, comments). Solidity Documentation. https://docs.soliditylang.org/en/latest/layout-of-source-files.html
- The Linux Foundation. (n.d.). About SPDX (ISO/IEC 5962:2021). https://spdx.dev/about/
- OpenZeppelin. (n.d.). OpenZeppelin Contracts Documentation. https://docs.openzeppelin.com/contracts/
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.