How to Write an Ethereum Smart Contract Using Solidity

·

Overview

This guide is designed for developers new to Ethereum development. We'll explore Solidity and smart contracts—their definitions, roles in Ethereum development, and walk through writing a smart contract using Solidity.

What is Ethereum?

Ethereum is a decentralized, open-source blockchain supporting Turing-complete programming languages like Solidity. Launched in 2015, it enables permissionless access, though every write operation requires payment in ETH (Gas).

What is a Smart Contract?

Coined by Nick Szabo in 1997, smart contracts are self-executing programs stored on the blockchain. They operate within the Ethereum Virtual Machine (EVM), reducing the need for intermediaries when properly audited. Ethereum’s Turing-complete nature allows nearly any operation—with associated Gas costs.

What is Solidity?

Solidity is Ethereum’s primary programming language for smart contracts, inspired by JavaScript, C++, and Python. Alternatives like Vyper exist, but Solidity remains dominant due to its widespread adoption and robust features.

Your First Smart Contract

Below is a simple Solidity contract that stores and retrieves an unsigned integer:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public view returns (uint) {
        return storedData;
    }
}

Code Breakdown:

  1. SPDX License: Mitigates copyright issues (optional).
  2. Pragma Directive: Specifies compiler version.
  3. Contract Declaration: Named SimpleStorage.
  4. Variable: storedData holds the unsigned integer.
  5. Functions:

    • set(): Updates storedData (public).
    • get(): Retrieves storedData (read-only).

👉 Learn more about Solidity syntax

Deploying the Smart Contract

Tools:

Steps:

  1. Compile: Use Remix’s Solidity plugin.
  2. Deploy:

    • Connect MetaMask (Ropsten network).
    • Fund wallet via Ropsten faucet.
    • Deploy via Remix’s "Injected Web3" environment.
  3. Confirm Transaction: Approve via MetaMask; wait for blockchain confirmation.

FAQ Section

1. What’s the cost of deploying a smart contract?

2. Can I update a deployed contract?

3. Why use Solidity over Vyper?

👉 Explore Ethereum development tools

Conclusion

You’ve now written and deployed a basic Solidity smart contract! For deeper dives, refer to the Solidity documentation. Stay updated with Ethereum trends and advanced guides through community forums and newsletters.