Developer Resources

Motoko Playground

Learn more about the Motoko Playground which allows developers to build, test, and deploy canister smart contracts directly from their browser

IC Academy » Developer Resources » Motoko Playground

Introducing the Motoko Playground

Online development environment for the Internet Computer

The following focuses on the Motoko Playground and how developers can utilize it. To learn more about Motoko itself, have a look at Introduction to Motoko

With the help of the Motoko Playground, developers can seamlessly test, build, and deploy their canister smart contracts to the Internet Computer – directly from within their web browser.

What is the Motoko Playground?

The Motoko Playground is an integrated development environment (IDE) running on the Internet Computer blockchain. It is designed to simplify the onboarding experience of developers new to the Internet Computer.

The playground can be accessed through the following URL:

Motoko Playground

The playground utilizes the Motoko programming language and allows developers to build and deploy canisters without the need for the DFINITY Canister SDK or a wallet. As such, it provides a tool for developers that helps them to explore the Internet Computer and Motoko without having to set up their own development environment.

Getting Started with the Playground – Example Workflow

To get started, simply navigate to the playground dApp and select the type of project would like to work on. You can choose between a variety of example projects but you can also import your own project from Github. The following outlines an example workflow:

Step #1: Choosing example project

Upon entering the dApp, the UI will ask you to choose an example project. You can find a list of sample projects selected from DFINITY’s example repo. These examples include counter, calculator, Hello example, random maze, phonebook, as well as Super Heroes. You can also select a new Motoko project without any existing code.

For this walk-through, let us choose the Counter example from the list. You can view the code of the example project in the Main.mo file located in the navigation. This is also the name of the canister, in this case Main.

Step #2: Deploying canister on the Internet Computer

In the next step, you can build and deploy the Counter canister on the Internet Computer by clicking the Deploy button. The interface will then ask you to specify the canister name you want to deploy. In the case of our Counter example, the canister you want to deploy is named Main.

It takes several seconds to deploy the canister as a free canister ID is fetched from the Internet Computer while the compiled WebAssembly module is being installed.

Step #3: Testing via the Candid UI

Once deployed, you can test that canister’s code with the Candid UI. Candid allows you to communicate with the canister via your web browser. It allows you to query the initial value, increase the counter, etc. Alternatively, you can also interact with your canister through command-line interface tools (like dfx or ic-repl).

Step #4: Making code changes

You can make changes to your code with the playground editor. In the example below, we are changing the get() method to get2():

Step #5: Redeploying changed canister

Once you are finished editing, simply click on Deploy to redeploy the canister. The UI will then let you choose between installing a new canister or upgrading the existing one. For the purpose of our example, upgrading the existing canister is sufficient.

The playground counter is in the stable memory. As a result, calling the new get2() method returns the previous counter and not a new counter with 0 as initial value.

Step #6: Installation arguments

The playground gives you the option to add installation arguments. In our example, we are going to add an actor class to the playground counter:

Copy to Clipboard
Step #7: Redeploying and setting starting value

In order to update the canister, we are going to redeploy it again. The system will now ask you for the starting value of the counter because of the installation argument. You can select the starting value of your choice, for example 42. Click on Install to confirm.

Querying the initial value will now return 42, as defined during the deployment.

Third-Party Motoko Libraries

Larger projects require developers to use a third-party library. The vessel package set is supported by the Motoko Playground. It is a set of different Motoko libraries maintained by the community. These libraries include parsing, SHA256, as well as data structure libraries and can be imported via the + button in the packages section.

Supported Features

Supported by the playground are the same features as in a local development environment. These features include:

  • Inter-canister calls
  • Canister upgrades
  • Actor classes with initialization arguments
  • Third-party libraries

Lightweight Playground

An embedded lightweight version of the Motoko Playground is used in the Motoko language manual. The lightweight version allows users to interactively change the featured code of the documentation and to run it instantly from within their web browser.

Understanding how the Motoko Playground Works

The source code of the playground can be found on GitHub. It consists of a full-stack dApp that includes a JavaScript front end as well as a backend canister for managing free cycles. The playground is available in the two programming languages Motoko and Rust.

The compiler runs directly in the user’s browser and not on a remote server. This is because the Motoko compiler is being hosted on the front end with the help of the js-of-ocaml compiler. In doing so, security and scalability concerns are automatically solved.

Components of the Motoko Playground front end:

The front end of the playground consists of these components:

  • Motoko compiler running in the web browser (produced by js-of-ocaml)
  • Monaco (VSCode) editor (for Motoko syntax highlighting and validation)
  • Vessel package manager (for loading third-party libraries from the package set)
  • Deploy module (for integrating actor class, canister install/upgrade, and Candid UI)
  • Code import module (for importing Motoko code from Github)

Free Cycles

The Motoko Playground is powered by free cycles so that developers new to the Internet Computer can explore the ecosystem without having to set up a cycle wallet.

Backend Canister

The back-end canister for managing the free cycles used by deployed canisters is written in Motoko. Three restrictions help to ensure that the free cycles are fairly used and distributed:

Initial balance

PLayground canisters have an initial cycle balance of 0.5T cycles

Runtime

Canisters can run 10 minutes at the maximum

Transfer lock

It is not possible to transfer cycles across canisters

The process in detail

Let’s have a look at the process when a new canister is requested by the front end:

By requiring a proof of work when a new canister is requested, the playground prevents a single person from using too many canisters at the same time.

Preventing cycle transfers

Users are unable to transfer large cycle amounts to their own canisters. This is enforced by inspecting submitted WebAssembly modules and removing cycle transfers at the Wasm level. This is accomplished by using a Rust canister. Every time a install request is received by the Motoko back and, the Wasm module will be forwarded to the Rust canister for further inspection.

Technical Overview of the Internet Computer

Learn more about the infrastructure and architecture of the Internet Computer blockchain network from a technical perspective

Technical Overview

Introduction to the Motoko Programming Language

Find out more about Motoko, the native programming language of the Internet Computer

Discover Motoko