Creating a Simple App Using Motoko

Motoko is a programming language designed for the Internet Computer (ICP). It allows developers to build decentralized applications (dApps) that run directly on the blockchain. In this article, we’ll go through the process of setting up your development environment and creating a simple app using Motoko.

Setting Up Your Environment

Installing the Internet Computer SDK (dfx)

The Internet Computer SDK (dfx) is required to develop and deploy Motoko applications.

For macOS & Linux Users:

Run the following command in your terminal:

sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"

This will install dfx and set up the necessary components for development.

For Windows Users:

Since dfx is not natively supported on Windows, you need to use Windows Subsystem for Linux (WSL) to install it.

Steps to Install dfx on Windows:

  1. Install WSL: Open a PowerShell window as Administrator and run:

     wsl --install -d Ubuntu
    
  2. Open the Ubuntu Environment: After installation, launch Ubuntu from the Start menu.

  3. Install Node.js (inside WSL): Before installing dfx, ensure Node.js is installed:

     curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
     source ~/.bashrc
     nvm install --lts
    
  4. Install the Internet Computer SDK (dfx) (inside WSL):

     sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
    
  5. Verify Installation: Run dfx --version to check if it's installed correctly.

Creating Your First Motoko App

Once your environment is set up, follow these steps to create a basic Motoko app.

Step 1: Create a New Project

Run the following command to create a new Internet Computer project:

dfx new my_first_motoko_app
cd my_first_motoko_app

This command creates a new directory with all the necessary files.

Step 2: Modify the Default Code

Open main.mo inside the src/my_first_motoko_app folder and replace its content with the following:

actor SimpleCounter {
  var count : Nat = 0;

  public func increment() : async Nat {
    count += 1;
    return count;
  }

  public query func getCount() : async Nat {
    return count;
  }
}

This defines a simple counter that can be incremented and queried.

Step 3: Start the Local Internet Computer

Before deploying your app, start a local Internet Computer instance:

dfx start --background

Step 4: Deploy Your App

Deploy your app using:

dfx deploy

This compiles and deploys your Motoko smart contract.

Step 5: Interact With Your App

Now, test your app by running:

dfx canister call my_first_motoko_app increment

This should return 1. You can call increment multiple times and check the count using:

dfx canister call my_first_motoko_app getCount

Congratulations! You’ve successfully created a simple Motoko app. By following these steps, you now have a basic understanding of how to set up your environment, write Motoko code, and deploy it on the Internet Computer.

For more advanced features, check out the official Motoko documentation and start experimenting with more complex applications. Happy coding!

0
Subscribe to my newsletter

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

Written by

Jerome Stephanie
Jerome Stephanie