Writing a basic Smart Contract with Solidity

Loading

Solidity is a high-level programming language used for writing smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts with the terms of the agreement directly written into lines of code. Solidity provides a way to define the rules and behavior of these contracts, which automatically execute when certain conditions are met.

In this article, we’ll write a basic smart contract using Solidity. The contract will have simple functionalities: a variable to store a value, functions to set and get that value, and a constructor for initial setup.


1. Setting Up Your Environment

Before writing Solidity code, you need a development environment where you can write and deploy smart contracts. There are several options available, but the most common are:

  • Remix IDE: An online Solidity editor with built-in Ethereum virtual machine (EVM) support, making it ideal for quickly writing, testing, and deploying contracts.
  • Truffle: A framework for building decentralized applications (DApps), which includes features for managing contracts, compiling them, and interacting with them on the Ethereum network.
  • Hardhat: A development environment for building DApps, offering debugging, testing, and deployment capabilities.

For this tutorial, we will use Remix IDE because it is easy to use and doesn’t require any setup.

Visit Remix IDE to start coding directly in your browser.


2. Basic Smart Contract Example

Let’s write a simple contract to store and retrieve a number. The contract will have:

  • A state variable to store the value.
  • A function to set the value.
  • A function to retrieve the value.
  • A constructor to initialize the value at the time of contract deployment.

Here’s the code for a basic Solidity smart contract:

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

contract SimpleStorage {
    // State variable to store a number
    uint256 public storedNumber;

    // Constructor to initialize the contract with an initial value
    constructor(uint256 _initialValue) {
        storedNumber = _initialValue;
    }

    // Function to set the value of the storedNumber
    function set(uint256 _newNumber) public {
        storedNumber = _newNumber;
    }

    // Function to retrieve the current value of storedNumber
    function get() public view returns (uint256) {
        return storedNumber;
    }
}

3. Explanation of the Code

a) Version Declaration:

pragma solidity ^0.8.0;

This line specifies the version of Solidity that the contract is written for. The ^0.8.0 means that the contract will work with version 0.8.0 or higher, but not with a major version above 0.8. This ensures that you have compatibility with the correct version of Solidity.

b) State Variable:

uint256 public storedNumber;

Here, we define a state variable called storedNumber of type uint256. This variable is used to store the number within the contract. The public keyword automatically generates a getter function, allowing anyone to access this variable from outside the contract.

c) Constructor:

constructor(uint256 _initialValue) {
    storedNumber = _initialValue;
}

The constructor is a special function that is executed only once, when the contract is deployed. It initializes the contract with an initial value. In this case, it takes an argument _initialValue, which is used to set the value of storedNumber at the time of deployment.

d) Set Function:

function set(uint256 _newNumber) public {
    storedNumber = _newNumber;
}

The set function allows users to update the value of storedNumber. It takes a uint256 parameter called _newNumber, which is used to modify the state variable. The public visibility means that the function can be called from anywhere, either internally (within the contract) or externally (by other accounts interacting with the contract).

e) Get Function:

function get() public view returns (uint256) {
    return storedNumber;
}

The get function allows users to retrieve the current value of storedNumber. It is marked as view, which means it does not modify the state of the blockchain and does not incur any gas costs when called (if used externally). It simply returns the current value of storedNumber.


4. Deploying the Smart Contract

Once your contract is written, the next step is to deploy it to the blockchain. In Remix IDE, you can follow these steps:

  1. Compile the Contract:
    • Go to the Solidity Compiler tab in Remix (on the left side).
    • Click the Compile button to compile the contract. If there are no errors, the contract is ready for deployment.
  2. Deploy the Contract:
    • Go to the Deploy & Run Transactions tab.
    • Under the Environment, select JavaScript VM (for a local blockchain simulation) or Injected Web3 (to deploy on a testnet or mainnet using MetaMask).
    • In the Contract section, select SimpleStorage (your contract).
    • Provide the initial value for storedNumber (e.g., 10) in the input box next to the Deploy button.
    • Click the Deploy button. You should now see the contract listed in the Deployed Contracts section.

5. Interacting with the Contract

After deployment, you can interact with your contract using the following functions:

  • Get the current value:
    • Click on the get function next to your deployed contract.
    • It will display the current value of storedNumber.
  • Set a new value:
    • In the set function, enter a new number (e.g., 25) in the input box.
    • Click the set button. This will trigger a transaction to update the value of storedNumber on the blockchain.
    • After the transaction is confirmed, you can call the get function again to see the updated value.

6. Gas Fees

Every time you execute a function that modifies the blockchain’s state (e.g., calling the set function), it will cost gas. Gas is a unit used to measure the computational work required to execute operations on the Ethereum network. The gas price varies depending on network congestion, and you will need to pay a fee in Ether (ETH) to execute transactions.

You can see the gas estimation in Remix under the Deploy & Run Transactions tab when interacting with the contract.


Leave a Reply

Your email address will not be published. Required fields are marked *