Exploring Solidity: Creating a Simple Identity Smart Contract

ljb630ljb630
3 min read

As part of my journey into blockchain development, I've been working on writing smart contracts using Solidity. Today, I'd like to share a simple smart contract I've created, called Identity. This contract allows you to store and retrieve a person's name and age, as well as update the age.

The Smart Contract

Here's the complete code for the Identity contract:

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;

contract Identity {
    string private name;
    uint private age;

    constructor() public {
        name = "Laljan";
        age = 24;
    }

    function getName() public view returns (string memory) {
        return name;
    }

    function getAge() public view returns (uint) {
        return age;
    }

    function setAge(uint _age) public {
        age = _age;
    }
}

Understanding the Code

Let's break down the different parts of the contract:

  1. License and Version Pragma:

     // SPDX-License-Identifier: MIT
     pragma solidity >=0.5.0 <0.9.0;
    

    The SPDX license identifier ensures that the license type is clear. The pragma directive specifies that the code is written for Solidity versions between 0.5.0 (inclusive) and 0.9.0 (exclusive).

  2. Contract Definition:

     contract Identity {
    

    This line declares a new contract named Identity.

  3. State Variables:

     string private name;
     uint private age;
    

    These variables store the name and age of the person. They are marked as private to restrict direct access from outside the contract.

  4. Constructor:

     constructor() public {
         name = "Laljan";
         age = 24;
     }
    

    The constructor initializes the state variables with default values. It is executed only once when the contract is deployed.

  5. Getters:

     function getName() public view returns (string memory) {
         return name;
     }
    
     function getAge() public view returns (uint) {
         return age;
     }
    

    These functions allow external users to read the values of name and age.

  6. Setter:

     function setAge(uint _age) public {
         age = _age;
     }
    

    This function allows the age to be updated. It takes a new age as an input and updates the state variable age.

Deploying and Interacting with the Contract

To deploy and interact with this contract, you can use Remix, an online Solidity IDE. Here are the steps:

  1. Open Remix: Go to Remix.

  2. Create a New File: Create a new file and name it Identity.sol. Copy and paste the contract code into this file.

  3. Compile the Contract: Click on the "Solidity Compiler" tab and compile the contract. Ensure that the compiler version matches the pragma statement in your contract.

  4. Deploy the Contract: Go to the "Deploy & Run Transactions" tab, select Identity from the dropdown menu, and deploy the contract.

  5. Interact with the Contract: Once deployed, you can interact with the contract using the provided interface. You can call getName and getAge to retrieve the current values and use setAge to update the age.

Conclusion

This simple Identity contract is a great starting point for learning Solidity and smart contract development. It covers basic concepts like state variables, functions, visibility, and contract deployment. As I continue my journey, I'll be exploring more complex contracts and sharing my progress. Stay tuned for more updates!


Written by ljb630 | Trying to find my place in the web3 community! | Technical Writer & Researcher | Team Lead

0
Subscribe to my newsletter

Read articles from ljb630 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ljb630
ljb630

Trying to find my place in the web3 community! | Technical Writer & Researcher | Team Lead