Week 6
Writing A First Smart Contract
- Blockchain from zero, week 6 of 12
- Write, deploy and call a contract in your browser
Outcomes
What You Will Be Able To Do
- Read and write basic Solidity
- Compile and deploy in Remix
- Call free reads and paid writes
- Guard a function with require
- Explain why deployed code is fixed
Setup
Tools: Solidity And Remix
- Solidity: language for the EVM
- Remix: editor and deployer at remix.ethereum.org
- Remix VM: simulated chain in the browser
- Sepolia: real test network via MetaMask
Syntax
Anatomy Of A Contract
- Licence comment and pragma line first
- contract keyword names the contract
- State variables live in storage
- Functions read or change that state
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Hello {
string public greeting = "hello";
}Vocabulary
Types You Meet First
- uint256: whole numbers from zero upward
- address: a 20-byte account
- bool and string
- mapping(address => uint256): table of balances
| Type | Holds |
|---|
| uint256 | Whole number |
| address | Account address |
| bool | True or false |
| mapping | Key to value |
Vocabulary
Function Words
- external and public set who can call
- view reads state and is free from outside
- payable accepts ether
- msg.sender is the caller, msg.value the ether sent
| Word | Meaning |
|---|
| view | Reads state only |
| pure | Touches no state |
| payable | Can receive ether |
| external | Called from outside |
Contract one
Counter: The Code
- Constructor records the deployer as owner
- increment is open to everyone
- reset is restricted to the owner
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Counter {
uint256 public count;
address public owner;
event Incremented(address indexed by, uint256 newCount);
constructor() { owner = msg.sender; }
function increment() external { count += 1; emit Incremented(msg.sender, count); }
function reset() external { require(msg.sender == owner, "only owner"); count = 0; }
}Remix step 1
Compile
- Compiler tab: choose 0.8.20 or higher
- Press Compile for a green check
- Read errors by line number
- Common first mistake: missing semicolon
Remix step 2
Deploy On The Remix VM
- Environment: Remix VM
- Pick Counter, press Deploy
- Blue buttons read, orange buttons send transactions
- Terminal shows gas used and logs
Safety
Access Control With Require
- require checks a condition before continuing
- A false condition reverts every change
- msg.sender == owner is the simplest guard
- Missing guards cause real losses
function reset() external {
require(msg.sender == owner, "only owner");
count = 0;
}Contract two
TipJar: Handling Ether
- tip is payable and records each giver
- withdraw sends the balance to the owner
- A contract holds its own ether balance
contract TipJar {
address public owner;
mapping(address => uint256) public tips;
constructor() { owner = msg.sender; }
function tip() external payable {
require(msg.value > 0, "send some ETH");
tips[msg.sender] += msg.value; }
function withdraw() external {
require(msg.sender == owner, "only owner");
(bool ok, ) = owner.call{value: address(this).balance}("");
require(ok, "transfer failed"); }
}Practice
Test TipJar In Remix
- Set Value to 1 ether, press tip
- Read tips for your address
- Try withdraw from a second account
- Withdraw as owner and watch balances
Real test network
Deploy To Sepolia
- Environment: Injected Provider with MetaMask
- Confirm the wallet shows Sepolia
- Press Deploy and approve in the wallet
- No recipient: this is a contract creation
Explorer
Read Your Contract On Etherscan
- Paste the contract address into sepolia.etherscan.io
- Find the creation transaction and gas used
- Call increment and spot the Incremented event
- Optionally verify the source code
Immutability
Deployed Code Cannot Change
- A bug means a new contract at a new address
- Test on the Remix VM and testnets first
- Audits matter when contracts hold value
- Private storage is still readable by anyone
Week 6 in five lines
Recap
- Solidity contracts hold state and functions
- Views read free, transactions write and cost gas
- require and msg.sender guard access
- Remix VM first, then Sepolia
- Deployed code is permanent
Week 7
Next Week: Tokens
- What a token is: a contract with a balance table
- The ERC-20 standard and its functions
- Minting, transferring and approving
- Deploying your own test token
Hands-on
Questions And Exercise
- Deploy Counter and TipJar on the Remix VM
- Deploy Counter on Sepolia
- Find your event on Etherscan
- Add and test a decrement function