Lesson 3.2: Writing and Deploying Smart Contracts With Remix IDE 🚀✨

Now that our environment is ready, let’s write and deploy our first smart contract using the Remix IDE.

1. Remix IDE is an online tool for developing smart contracts. It’s super handy for writing, compiling, and deploying contracts.

  • Open Remix: Go to Remix IDE.
  • Create a New File: Click on the file explorer and create a new file called ToDoList.sol.
  • Write Your Contract: Enter the following Solidity code:
    solidity

    pragma solidity ^0.8.0;

    contract ToDoList {

    struct Task {

    uint id;

    string content;

    bool completed;

    }

    mapping(uint => Task) public tasks;

    uint public taskCount;

    function createTask(string memory _content) public {

    taskCount ++;

    tasks[taskCount] = Task(taskCount, _content, false);

    }

     

    function toggleCompleted(uint _id) public {

    Task memory task = tasks[_id];

    task.completed = !task.completed;

    tasks[_id] = task;

    }

    }

     

2. Compiling and deploying contracts on a local blockchain:

  • Compile: Click the Solidity icon on the left sidebar, select your file, and click the “Compile ToDoList.sol” button.
  • Deploy: Click the “Deploy & Run Transactions” button on the sidebar. Ensure you have “Injected Web3” selected, which connects Remix to MetaMask. Click “Deploy” and confirm the transaction in MetaMask.

3. Interacting with your deployed contract through MetaMask:

  • Add Tasks: After deployment, you’ll see your contract under “Deployed Contracts” in Remix. You can call the createTask function to add new tasks by entering a string (the task content) and clicking “transact”.
  • Toggle Completion: Use the toggleCompleted function with the task ID to mark tasks as completed or not.