The life cycle of a Motoko program

The life cycle of a Motoko program

Introduction

The process of writing a program and getting it to compile and run correctly can be complex and require several stages. Writing a program involves developing a set of instructions or code that performs a specific task or set of tasks. Once the code has been written, the next step is to compile it into a form that can be executed by a computer.

In this article, we will explore the stages that a Motoko program passes through from when it is created until it is live on the internet computer to be accessed worldwide.

What is the Internet Computer?

Internet Computer is a decentralized network that aims to provide a platform for hosting decentralized apps while offering speed, scalability, and efficiency. It is built on top of the Internet Computer Protocol consensus mechanism and has its native coin called ICP. ICP offers a platform to run both the smart contracts and Dapps on-chain and this eliminates the need for a separate platform or network

What is Motoko?

Motoko is a programming language specifically built for the internet computer. It is a statically typed language that compiles to WebAssembly, the binary format used by the Internet Computer.

Stage 1: Writing the program

Just like all other programs, the process starts by combining different parts of the Motoko language to come up with a specific logic. This is usually done in code editors like Vscode, Sublime text and many others. Some code editors offer more than just a platform to write programs. They have extra features like highlighting keywords and showing errors.

Once the program is done, it is tested locally to see if it works properly as desired. At this time, the program is running on our machine. So the machine provides the environment in which the program runs and also provides the memory where the program stores its data.

One thing to note here is that every program in Motoko is wrapped in something called an actor. Think of it as a temporary virtual house. Just like you wrap the beef in aluminum foil when putting it in an oven for heating. You wrap the program in an actor to be able to perform the next step of compiling smoothly.

Stage 2: Compiling

Our program is written in Motoko, which is a high-level language, so it is not easily understood by the computer. At this stage, it needs to be compiled into WebAssembly Module (WASM).

WASM is a low-level virtual processor that processes binary code and runs in web browsers. it makes it possible to run high-performance apps on several platforms.

This technology offers a standard language that is tailored for the web and is built on the foundation of JavaScript. Delivering high-performance online applications using WebAssembly is a quick and safe method that works with any browser and doesn't require any extra plugins or software.

Stage 3: Canister creation

After compiling the program into WASM, it is ready to be accessed by everyone, though there is one problem, the program is still on our local machine. There is no way users from other parts of the world can access it. Our goal is to make the program accessible to anyone anywhere on the internet.

So, we have to create an address on the internet computer that will be used in case someone wants to access our program. And this comes in form of a canister. A canister is a special permanent house on the internet computer where programs are stored. Each canister can store one program at a time.

The canister has a unique address, meaning you can access the program using the canister's address. The canister also provides memory to the program to store its data during the execution.

Stage 4: Installing

When a canister is created for the first time on the Internet Computer, it is always empty. So we have to migrate the program from the local machine to its new home; the canister.

Once inside the canister, the program can now be accessed online using the unique address of the canister where it is stored. If the program has public functions, it can now be called by specifying the canister address and the function name to call.

There you have it, our program is now live on the internet computer(IC) and can be accessed by anyone that wants to interact with it.

Some tools have been developed to perform these steps automatically, from stages 2 to 4. One of them is the Motoko playground. This is an online Motoko code editor that lets you run code in the browser.

After writing the code, it automatically compiles it, creates an empty canister and installs the compiled program in it. It has an extra feature that allows you to interact with the deployed code from the browser through a candid interface.

You can also perform the above steps one at a time using the dfx module installed on the local machine.

Conclusion

In this article, you have learned the different stages that a Motoko program passes through to go live on the Internet Computer. You have also learned about the functionality that actors and canisters provide to the program.

Thank you for reaching this far, let's connect on Twitter.