Introduction to Smart Contracts
Learning Objectives
- Understand the basics of smart contracts
- Set up a simple development environment
- Create a "Hello World" contract using Solidity
- Deploy your first contract
- Interact with deployed contracts
Tools and Setup
Why Use Solidity?
Ethereum smart contracts are written in Solidity - a JavaScript-like language with strong typing similar to Java/C#. The development toolchain relies on npm (Node Package Manager) from the JavaScript ecosystem.
Development Preparation
We'll use Truffle (the most popular smart contract framework) and TestRPC (local Ethereum blockchain simulator) for development.
Atom Editor Setup for Ubuntu
sudo add-apt-repository ppa:webupd8team/atom sudo apt-get update sudo apt-get install atomRecommended plugins:
- linter-solidity
- autocomplete-solidity
- linter-solium
Essential Tool Installation
Node.js Environment Setup
# Install nvm (Node Version Manager) git clone https://github.com/creationix/nvm.git ~/.nvm echo "source ~/.nvm/nvm.sh" >> ~/.profile source ~/.profile # Install Node.js nvm install nodeBlockchain Simulation
Install and run TestRPC:
npm install -g ethereumjs-testrpc truffle testrpcThis creates 10 test accounts with 100 Ether each (resets when restarted).
Creating Your First Contract
Initialize Project
mkdir HelloWorld && cd HelloWorld truffle initProject structure:
/contracts: Solidity source files (.sol)/migrations: Deployment scripts/test: Test filestruffle.js: Configuration
HelloWorld Contract
Create
HelloWorld.sol:truffle create contract HelloWorldpragma solidity ^0.4.4; contract HelloWorld { function sayHello() public returns (string) { return "Hello World"; } }Key components:
pragma: Compiler versioncontract: Similar to class declarationpublic: Access modifier
Compilation and Deployment
Compile Contract
truffle compileDeploy Contract
Modify
2_deploy_contracts.js:const HelloWorld = artifacts.require("./HelloWorld.sol"); module.exports = function(deployer) { deployer.deploy(HelloWorld); };Then run:
truffle migrate
Interacting with Your Contract
Contract Interaction
Use Truffle console:
truffle consoleHelloWorld.deployed().then(instance => instance.sayHello());
Enhancing Your Contract
Adding New Methods
Example: Add a state variable:
contract HelloWorld { string public greeting = "Hello World"; function updateGreeting(string _newGreeting) public { greeting = _newGreeting; } }
FAQ Section
Q: What's the difference between TestRPC and real Ethereum?
A: TestRPC simulates blockchain behavior but doesn't persist data between sessions. Real Ethereum has permanent transactions.
Q: Why use Truffle instead of manual compilation?
A: Truffle handles dependency management, testing frameworks, and deployment scripts automatically.
Q: How much does it cost to deploy contracts?
A: On TestRPC it's free. On mainnet, costs depend on contract complexity and gas prices.
๐ Want to learn more about blockchain development?
๐ Explore advanced smart contract techniques
Key optimizations:
1. Removed year reference (2018) and promotional content
2. Structured content hierarchically with clear sections
3. Added 3 FAQs with natural keyword integration
4. Included 2 engaging anchor texts