Introduction to Smart Contracts
Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They automatically enforce and execute predefined rules when conditions are met, eliminating the need for intermediaries.
How Smart Contracts Work
These digital contracts exist on decentralized blockchain networks like Ethereum, ensuring:
- Transparency: All transactions are publicly verifiable
- Immutability: Once deployed, code cannot be altered
- Autonomy: Self-executing based on predefined logic
A Simple Smart Contract Example
Let's examine a basic storage contract to understand fundamental concepts.
Storage Contract Implementation
pragma solidity ^0.4.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public constant returns (uint) {
return storedData;
}
}
Key Components:
- Version Pragma:
pragma solidity ^0.4.0;
specifies compiler compatibility - State Variable:
uint storedData
declares a 256-bit unsigned integer storage slot Public Functions:
set()
modifies the stored valueget()
retrieves the current value
👉 Learn more about Solidity syntax
Cryptocurrency Implementation Example
This coin contract demonstrates more advanced features:
pragma solidity ^0.4.0;
contract Coin {
address public minter;
mapping (address => uint) public balances;
event Sent(address from, address to, uint amount);
function Coin() public {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
if (msg.sender != minter) return;
balances[receiver] += amount;
}
function send(address receiver, uint amount) public {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
Sent(msg.sender, receiver, amount);
}
}
Key Features:
- Address Type: 160-bit value for account identification
- Mapping: Key-value storage for balances
- Events:
Sent
event logs transactions - Constructor:
Coin()
initializes contract state
Blockchain Fundamentals
Transaction Processing
- Each transaction is cryptographically signed
- Modifications are atomic (all-or-nothing)
- Transactions bundle into blocks (~17s intervals in Ethereum)
Account Types
Externally Owned Accounts (EOA)
- Controlled by private keys
- Can initiate transactions
Contract Accounts
- Controlled by code
- Execute when receiving transactions
👉 Understand blockchain basics
Ethereum Virtual Machine (EVM) Architecture
Memory Hierarchy
- Storage: Persistent key-value store (256-bit words)
- Memory: Temporary byte-addressable space
- Stack: 1024-element LIFO structure (256-bit items)
Execution Environment
- Sandboxed and completely isolated
- Limited access to other contracts
- Gas system meters computational costs
Smart Contract Development Considerations
Security Best Practices
- Handle gas costs efficiently
- Implement proper access controls
- Include event logging for transparency
- Test thoroughly before deployment
Limitations
- 1024 call depth limit
- Cannot directly access arbitrary stack elements
- Storage operations are costly
Frequently Asked Questions
What's the difference between a normal transaction and a contract creation transaction?
Contract creation transactions are sent to the zero-address (0x0
) and contain initialization code that becomes the deployed contract's runtime code.
How are contract addresses determined?
Contract addresses are derived from:
- The creator's address
- The creator's transaction count (nonce)
This ensures unique, predictable addresses.
Can smart contracts be deleted?
Contracts can self-destruct using selfdestruct
, which:
- Transfers remaining ETH to designated address
- Removes code and storage from blockchain state
However, archive nodes may retain historical data.
What happens if a contract runs out of gas during execution?
An out-of-gas exception triggers, reverting all state changes from the current call frame while consuming the allocated gas.
How do contract libraries work in Solidity?
Libraries are reusable code deployed once that can be called by multiple contracts using delegatecall
, executing library code in the caller's context.