Introduction to Smart Contracts
A smart contract is a self-executing program deployed at a specific address on the Ethereum blockchain. Comprised of data structures and executable functions, these contracts automatically enforce terms when triggered by transactions. This guide breaks down their core components, best practices, and real-world applications.
Key Components of Smart Contracts
1. Data Storage and Memory
Smart contracts manage data in two primary ways:
Storage: Persistent on-chain data (state variables) that incurs higher gas costs.
Example:contract SimpleStorage { uint256 storedData; // Permanently stored on the blockchain }
- Memory: Temporary data used during function execution (cheaper but ephemeral).
Learn more in the Solidity docs.
👉 Explore Ethereum storage types
Common Data Types:
address
(20-byte Ethereum addresses)bool
,int
,string
- Fixed/dynamic byte arrays
See Vyper types for comparisons.
2. Functions and Execution
Functions define contract behavior. Key classifications:
Type | Scope | Example Use Case |
---|---|---|
public | External calls | Updating contract state |
private | Internal only | Secure internal calculations |
view /pure | Read-only | Retrieving user balances |
function updateName(string memory value) public {
dappName = value; // Modifies state
}
function balanceOf(address _owner) public view returns (uint256) {
return balances[_owner]; // Read-only
}
3. Contract Lifecycle
Constructors: Initialize state during deployment.
constructor() public { owner = msg.sender; // Sets contract creator }
- Built-ins: Native functions like
address.send()
for ETH transfers.
Advanced Concepts
Events and Logging
Smart contracts emit events to communicate with off-chain applications:
event Transfer(address indexed from, address indexed to, uint256 value);
emit Transfer(msg.sender, recipient, amount); // Logs transaction
Security Patterns
- Use
require()
for input validation. - Avoid reentrancy attacks with checks-effects-interactions.
👉 Best practices for secure contracts
Real-World Examples
1. Token Contract (ERC-20)
contract Token {
mapping(address => uint256) public balances;
function transfer(address recipient, uint256 amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
balances[recipient] += amount;
}
}
2. NFT Contract (ERC-721)
Manages unique assets with ownership tracking:
contract CryptoPizza is IERC721 {
mapping(uint256 => address) private _owners;
function ownerOf(uint256 tokenId) public view returns (address) {
return _owners[tokenId];
}
}
FAQs
What’s the difference between Solidity and Vyper?
- Solidity: Feature-rich, similar to JavaScript.
- Vyper: Python-inspired, focuses on security/simplicity.
How do I reduce gas costs?
- Minimize storage operations.
- Use
memory
for temporary data.
Can smart contracts call other contracts?
Yes! Use interface declarations for cross-contract calls.
Further Reading
For hands-on practice, try Remix IDE.
This Markdown output adheres to SEO best practices with:
- Hierarchical headings (`#` to `######`)
- Keyword optimization ("smart contract," "Ethereum," "Solidity," etc.)