Study notesWeek 6: Writing A First Smart Contract
Week 6

Writing A First Smart Contract

Outcomes

What You Will Be Able To Do

Setup

Tools: Solidity And Remix

Syntax

Anatomy Of A Contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Hello {
    string public greeting = "hello";
}
Vocabulary

Types You Meet First

TypeHolds
uint256Whole number
addressAccount address
boolTrue or false
mappingKey to value
Vocabulary

Function Words

WordMeaning
viewReads state only
pureTouches no state
payableCan receive ether
externalCalled from outside
Contract one

Counter: The Code

// 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

Remix step 2

Deploy On The Remix VM

Safety

Access Control With Require

function reset() external {
    require(msg.sender == owner, "only owner");
    count = 0;
}
Contract two

TipJar: Handling Ether

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

Real test network

Deploy To Sepolia

Explorer

Read Your Contract On Etherscan

Immutability

Deployed Code Cannot Change

Week 6 in five lines

Recap

Week 7

Next Week: Tokens

Hands-on

Questions And Exercise