Ethereum smart contracts are self-executing programs that run on the blockchain, enabling decentralized applications (dApps) to function without intermediaries. Whether you're building a decentralized finance (DeFi) platform, a token system, or an NFT marketplace, understanding how to deploy and execute Ethereum smart contracts is fundamental. This guide walks you through setting up a private Ethereum network, managing accounts, deploying contracts via Geth console and Ethereum Wallet, and interacting with them securely and efficiently.
Setting Up an Ethereum Client
To begin working with smart contracts, you need an Ethereum client. The most widely used is Geth (Go Ethereum), an official implementation of the Ethereum protocol.
Running a Private Blockchain
For development and testing, it's recommended to run a private blockchain instead of using the mainnet. This avoids transaction fees and allows full control over the environment.
Start by creating a custom genesis block configuration in a file named custom_genesis.json:
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0",
"gasLimit": "0x800000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}Initialize the blockchain with:
geth --datadir "your_data_directory" init custom_genesis.jsonThen launch the node:
geth --datadir "your_data_directory" --rpc --rpcport 8545 --port 30303 \
--nodiscover --rpcapi "db,eth,net,web3" --autodag --networkid 1234 console👉 Learn how to securely manage Ethereum accounts and deploy your first contract today.
Managing Accounts and Balances
Use the personal module in Geth to manage Ethereum accounts.
Create and Unlock Accounts
personal.newAccount() // Creates a new account
personal.unlockAccount(eth.accounts[0]) // Unlocks account for transactionsCheck Balance and Send Transactions
All values in Ethereum are in Wei (1 ETH = 1e18 Wei).
eth.getBalance(eth.accounts[1]) // Returns balance in Wei
web3.fromWei(eth.getBalance(eth.accounts[1]), "ether") // Convert to Ether
// Send ETH
eth.sendTransaction({
from: eth.accounts[1],
to: eth.accounts[2],
value: web3.toWei(1, "ether"),
gas: 21000,
gasPrice: web3.toWei(1, "gwei")
})You can also retrieve transaction details:
eth.getTransaction("transaction_hash")
eth.getTransactionReceipt("transaction_hash")Deploying Smart Contracts via Geth Console
Smart contracts are typically written in Solidity and compiled using tools like Remix IDE. Once compiled, you can deploy them directly from the Geth JavaScript console.
After compilation, copy the Web3 deploy code from Remix:
var contract = eth.contract([ABI]);
var deployed = contract.new(
/* constructor args */,
{
from: eth.accounts[1],
data: 'compiled_bytecode',
gas: 4712388
}
);This returns a transaction hash. Once mined, use eth.getTransactionReceipt() to get the contract address.
👉 Discover how to streamline contract deployment using modern blockchain tools.
Interacting with Deployed Contracts
Once deployed, interact with your contract using its ABI and address.
Access Contract Functions
If you saved the instance during deployment:
deployed.owner() // Call constant function
deployed.rent.sendTransaction({from: eth.accounts[1], value: web3.toWei(3, "ether")})If you didn’t save the instance:
var contractInstance = eth.contract([ABI]).at("contract_address");
contractInstance.owner();Note: Use.call()to simulate execution locally (read-only), and.sendTransaction()to broadcast state-changing actions.
Handling Events and Logs
Events allow contracts to communicate with frontends or monitoring systems.
Define and Listen to Events
In Solidity:
event RentingRecord(address renter, uint rentTime, uint unitsOfTime);In Web3:
var event = contractInstance.RentingRecord({fromBlock: 12345, toBlock: 'latest'});
event.watch(function(error, result) {
if (!error) console.log("Renter:", result.args.renter);
});
event.get(function(error, logs) { /* retrieve past logs */ });
event.stopWatching();Using Ethereum Wallet (Mist)
The Ethereum Wallet (Mist) provides a graphical interface for deploying and interacting with contracts.
Deploying a Contract
- Paste Solidity code or bytecode.
- Specify constructor parameters.
- Choose sender account and gas fee.
- Confirm deployment.
The wallet shows:
- Transaction hash
- Function selector (first 4 bytes of function signature)
- Parameter encoding (each parameter uses 32 bytes)
Executing Contract Functions
Public variables appear automatically. To call a function:
- Select the function
- Input parameters
- Choose sender account and attached Ether
- Confirm transaction
Limitation: The wallet doesn’t show execution results until the transaction is mined. Use eth.getTransactionReceipt() in console for real-time verification.Mining and Network Management
Mining generates new blocks and confirms transactions.
Start/Stop Mining
miner.start() // Begin mining
miner.stop() // Stop miningYou can also start Geth with mining enabled:
geth --mine --datadir "your_data_directory" ...Attach to a running instance:
geth attach http://localhost:8545Check peer connections:
admin.peers // List connected nodes
admin.addPeer("enode://...@ip:port") // Connect to another nodeView your node info:
admin.nodeInfoKey Tools and APIs
Web3.js
The web3.js library enables interaction with Ethereum nodes via RPC.
Common uses:
- Deploying contracts programmatically
- Building dApp frontends
- Listening to events
Explore the full JavaScript API documentation for advanced functionality.
👉 Explore powerful Web3 integrations that simplify smart contract execution.
Frequently Asked Questions (FAQ)
Q: What is the difference between .call() and .sendTransaction()?
A: .call() reads data without changing state (free), while .sendTransaction() modifies state and requires gas.
Q: Why does my transaction show as null in getTransactionReceipt?
A: The transaction hasn’t been mined yet. Wait for confirmation before checking again.
Q: How do I convert between Ether and Wei?
A: Use web3.toWei(value, 'ether') and web3.fromWei(valueInWei, 'ether').
Q: Can I deploy a contract without writing Solidity code?
A: Yes—via bytecode—but this is error-prone. Always prefer high-level languages like Solidity.
Q: Is it safe to expose RPC ports publicly?
A: No—avoid --rpccorsdomain "*" in production. Restrict access to trusted domains only.
Q: How do I connect Ethereum Wallet to Geth?
A: Use IPC path:
- macOS:
~/Library/Ethereum/geth.ipc - Linux:
~/.ethereum/geth.ipc - Windows:
%APPDATA%\Ethereum\geth.ipc
Core Keywords
- Ethereum smart contracts
- Deploy smart contract
- Execute smart contract
- Geth console
- Private blockchain
- Web3.js
- Contract interaction
- Mining Ethereum
By mastering these fundamentals, you’ll be well-equipped to develop, test, and deploy secure and efficient smart contracts on Ethereum. Always test thoroughly in isolated environments before going live.