How to Build Your First Smart Contract Using Solidity

Now, it’s time to write the Solidity code for the smart contract. 

In the file created, paste the following code:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract SimpleStorage {

    uint256 private data;

    function set(uint256 _data) public {

        data = _data;

    }

    function get() public view returns (uint256) {

        return data;

    }

}

Let’s break down the code:

a. The contract is named SimpleStorage.

b. It contains a single state variable data, which is of type uint256 (an unsigned integer).

c. The set function allows users to store a value in data.

d. The get function is a view function, meaning it only reads the data and doesn’t modify the blockchain state. It returns the value stored in data.

This is a very simple smart contract, but it lays the foundation for more complex contracts.

Read more here: Source link