Building a TODO Application in Solidity

Building a TODO Application in Solidity

Β·

5 min read

What's up CODERS!! Have you been hearing a lot about Web 3 lately? If you answered yes, I'm sure you've heard of Solidity. If not, here's a quick definition from Wikipedia.

Solidity is an object-oriented programming language used to create smart contracts for use on various blockchain platforms, most notably Ethereum. Christian Reitwiessner, Alex Beregszaszi, and several former Ethereum core contributors created it. Solidity programs run on the Ethereum Virtual Machine.

If that confuses you for now, just imagine Solidity to be a language in which we can write programs(or as we say in blockchain "Smart Contracts") to run on Ethereum Virtual Machine.

πŸ› οΈ What are we building?

Disclaimer: I will not be covering all the nitty-gritty detail of solidity in this article, but I will do my best to explain everything we use.

So, now that we've defined what solidity is, let's move on and try writing some code in this language.

Today we'll be building a Todo Application in solidity.πŸ₯³

The Basic Logic

Our application will have three basic but important functions.

  1. βž• Adding Tasks given by the User
  2. ❌ Deleting Tasks that are completed.
  3. πŸ’» Displaying all the added Tasks

πŸ”— Tech Stack

The best part about coding in Solidity is that we don't have to spend much time getting it set up. We can easily run our code locally as well as online. The latter will be used in this article.

Remix IDE is the online tool we'll be using. It is a powerful open source tool that allows us to create Solidity Contracts directly in the browser.

😎It's Coding Time

Okay enough chit-chat, let's start writing some cool stuff!

  • Go to Remix IDE. You should land on the page below.

image.png

  • Now on the Sidebar, under contracts folder you'll find few .sol files. These are pre-written solidity contracts and we will not be dealing with them in this tutorial.

  • Let's make our ToDo contract. Select contracts folder, click on the new file button ( highlighted in yellow) and name your file Todo.solScreenshot 2022-03-28 142906.png

.sol is the file extension for all solidity files.

Let's start writing code on the blank editor that opened up.

  • The basic structure of any contract including ours will look like this.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
contract Todo{
    // Your contract logic goes here.
}
  • Every solidity contract should begin with a license description comment. For instance, if it is an open source contract, you could use the MIT license. For a non-open source contract you can use UNLICENSED.

We'll be using the MIT License.

// SPDX-License-Identifier: MIT
  • Next we'll define the solidity compiler version we'll be using for our contract. The little ^ before the version number describes that our contract is compatible with compilers equal to and greater than the version number.
pragma solidity ^0.8.1;
  • We begin our contract with the contract keyword, a name and the whole code is enclosed between {...}.
contract Todo{
// Logic Here.....
}
  • As we know in blockchain, a user is identified based on their wallet address. That is why we'll be storing ToDos according to the wallet address they belong to.

For this, we'll me making use of mapping datatype. For anyone coming from Python background, you can imagine mapping similar to dictionaries. In easy language, we can imagine mapping or assigning two data types to each other.

mapping(address => string[]) todos;

As you can see here, we map an address datatype to an array of strings.

Unlike other languages solidity has a specific address datatype to store addresses.

  • Add Function: Let's put the adding function into action. This is a fairly simple function in which we will take a task (string) as a parameter and push it into the array corresponding to the address that is connected.

To make sure we have the address of user or contract that called our contract, we make use of msg.sender. This returns the connected address.

 function addTodo(string memory _todo) public{
        todos[msg.sender].push(_todo);
    }
  • Show Function: This will help us display the tasks that user added. Here we are returning the array of strings that corresponds to the connected address.

    function showTodo() public view returns(string[] memory, uint){
          return(todos[msg.sender]);
      }
    
  • Delete Function: This is a somewhat complicated and costly function. Why? Because deleting from an array is a difficult task, the delete operation is very expensive in terms of gas usage. Furthermore, when deleting, we must consider the empty space left in an array after deletion. This function can be implemented in this way(note: that the rearrangement of the tasks will not be in any order).

function delTodo(uint idx) public{
        require(idx < todos[msg.sender].length);
        delete todos[msg.sender][idx];
        todos[msg.sender][idx] = todos[msg.sender][todos[msg.sender].length - 1];
        todos[msg.sender].pop();

    }
  1. As a first step, we take an index that we want to delete as a parameter.
  2. We'll use require() to check if index is within the array's length.
  3. We'll then use delete to remove the string from that index.
  4. We still need to take care of the empty space after deletion. To accomplish this, we move our values in the array so that the last index is empty.
  5. Finally, we remove the empty space with the pop() function.

Here is the full code:

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

contract Todo{
     mapping(address => string[]) todos;


    function addTodo(string memory _todo) public{
        todos[msg.sender].push(_todo);
    }

    function delTodo(uint idx) public{
        require(idx < todos[msg.sender].length);
        delete todos[msg.sender][idx];
        todos[msg.sender][idx] = todos[msg.sender][todos[msg.sender].length - 1];
        todos[msg.sender].pop();

    }

    function showTodo() public view returns(string[] memory){
        return(todos[msg.sender]);
    }
}

βœ… Deploy and Run

Ah! We are done! πŸ₯³ Now it's time to put our contract to test.

  • Navigate to Solidity Compiler Tab and hit the big blue Compile Todo.sol Button.

image.png

  • Once compiled, deploy it on the chain. Go to Deploy and Run Transactions Tab and press the Deploy button.

image.png

  • After deploying successfully, check deployed contracts in the same Deploy and Run Transactions Tab. Add, delete and see the tasks!

image.png

Conclusion

Hope you guys found this tutorial helpful. Do tag me on twitter @aahiknsv if you try this tutorial out. WAGMI! πŸš€

Β