New course launching soon Join the waitlist!

Learn Solidity for free

Kickstart your blockchain journey with our free, hands-on Solidity course.

Blockchain

How to Build a Simple Smart Contract on Ethereum

How to Build a Simple Smart Contract on Ethereum

Ethereum smart contracts are at the heart of many blockchain innovations. Whether you're new or seasoned in programming, this guide will help you understand how to build a simple smart contract. Let's dive in.

What are Smart Contracts?

Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They automatically enforce and trigger the agreement actions when preset conditions are met, eliminating the need for intermediaries.

Setting Up Your Environment

Before we start coding, you need to set up your development environment. Follow these steps:

  1. Install Node.js: Make sure you have Node.js installed. Check by running: bash node -v

  2. Install Truffle: Truffle is a development framework for Ethereum. bash npm install -g truffle

  3. Install Ganache: Ganache is a personal Ethereum blockchain used for testing. bash npm install -g ganache-cli

Creating Your First Smart Contract

Now, let's write a simple smart contract that stores a number and allows you to update it.

Setting Up the Project

Create a new Truffle project:

truffle init

Navigate to the contracts directory and create a new file named SimpleStorage.sol.

Writing the Smart Contract

Here’s a simple contract in Solidity:

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

contract SimpleStorage {
    uint256 storedData;

    function set(uint256 x) public {
        storedData = x;
    }

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

Understanding the Code

  • storedData: A uint256 variable to store the data.
  • set Function: Takes an unsigned integer x and updates storedData.
  • get Function: Returns the current value of storedData.

Deploying the Smart Contract

Now that your contract is ready, let's deploy it using Truffle.

  1. Start Ganache: Run Ganache CLI for a local blockchain. bash ganache-cli

  2. Compile the Contract: bash truffle compile

  3. Migrate the Contract: Deploy your contract on the local blockchain. bash truffle migrate

Interacting with the Contract

Once deployed, you can interact with your contract using Truffle's console.

  1. Open Truffle Console: bash truffle console

  2. Interact with the Contract: javascript const instance = await SimpleStorage.deployed(); await instance.set(89); const value = await instance.get(); console.log(value.toNumber()); // Outputs: 89

Conclusion

Congratulations! You've just built and interacted with a simple smart contract on Ethereum. This foundational understanding will empower you to explore more complex blockchain projects. Remember, the blockchain world is full of possibilities, and smart contracts are key to unlocking them.

Create and deploy a simple Ethereum smart contract. Understand smart contracts and get hands-on with Truffle and Ganache to start your blockchain journey.