पाठ 4 / 25

Your First Contract

Write, compile and understand the structure of a Solidity contract.

Anatomy of a contract

Solidity is a statically typed, curly-brace language for the EVM, influenced by C++, Python and JavaScript. A source file starts with an SPDX licence identifier comment and a pragma solidity line declaring compatible compiler versions (for example ^0.8.24). A contract is like a class: it has state variables stored permanently on the chain, a constructor that runs once at deployment, and functions. Visibility controls access: public (callable externally and internally; public state variables get automatic getters), external (only from outside), internal (this contract and derived ones) and private (this contract only). Note that private does not hide data: all storage is publicly readable on chain. The special globals msg.sender (the immediate caller), msg.value (ether sent), block.timestamp and block.number give context. Compiling produces bytecode for deployment and an ABI (Application Binary Interface), a JSON description that tools use to encode calls. Reading data through view functions is free when called off-chain; changing state requires a transaction and gas.

From source to deployed contract

Solidity compiles to bytecode and an ABI; a deployment transaction creates the contract account.

A source file icon flowing into a compiler gear, splitting into a binary block and a JSON document, with the binary block going into a chain of blocks.
Figure 2.1 — Compile, deploy and interact.

A simple guestbook contract

State, constructor, events and a view function.

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

contract Guestbook {
    struct Entry {
        address author;
        string message;
        uint64 timestamp;
    }

    address public immutable owner;      // set once in the constructor
    Entry[] private entries;

    event Signed(address indexed author, uint256 indexed index, string message);

    constructor() {
        owner = msg.sender;
    }

    function sign(string calldata message) external {
        require(bytes(message).length > 0 && bytes(message).length <= 280, "invalid length");
        entries.push(Entry(msg.sender, message, uint64(block.timestamp)));
        emit Signed(msg.sender, entries.length - 1, message);
    }

    function count() external view returns (uint256) {
        return entries.length;
    }

    function entry(uint256 index) external view returns (Entry memory) {
        return entries[index];           // reverts if index is out of range
    }
}

private is not secret

Anyone can read any contract's storage directly from a node, whatever its visibility. Never store passwords, unencrypted personal data or secret answers on chain.

त्वरित जाँच: What does the ABI of a contract describe?

  • The gas price
  • The private keys of the deployer
  • The functions, events and types used to encode calls and decode results
  • The block number
Answer

The functions, events and types used to encode calls and decode results — The ABI is the interface description tools use to talk to a contract.