7po3j-syaaa-aaaal-qbqea-cai.icp0.io Open in urlscan Pro
193.118.59.140  Public Scan

URL: https://7po3j-syaaa-aaaal-qbqea-cai.icp0.io/internet-computer-programming-concepts/actors/actor-to-canister.html
Submission: On December 01 via api from US — Scanned from CH

Form analysis 1 forms found in the DOM

<form id="searchbar-outer" class="searchbar-outer">
  <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>

Text Content

 1.  The Motoko Programming Language
 2.  
 3.  Part 1
 4.  1. Introduction
     ❱
 5.  1. 1.1. Getting Started
 6.  2. Common Programming Concepts
     ❱
 7.  1.  2.1. Variables
     2.  2.2. Mutability
     3.  2.3. Comments
     4.  2.4. Types
         ❱
     5.  1. 2.4.1. Tuples
         2. 2.4.2. Records
         3. 2.4.3. Variants
         4. 2.4.4. Immutable Arrays
         5. 2.4.5. Mutable Arrays
     6.  2.5. Operators
         ❱
     7.  1. 2.5.1. Numeric operators
         2. 2.5.2. Relational operators
         3. 2.5.3. Assignment operators
         4. 2.5.4. Text concatenation
         5. 2.5.5. Logical expressions
         6. 2.5.6. Bitwise operators
         7. 2.5.7. Operator precedence
     8.  2.6. Pattern Matching
     9.  2.7. Functions
     10. 2.8. Options and Results
     11. 2.9. Control Flow
         ❱
     12. 1. 2.9.1. If Expression
         2. 2.9.2. If Else Expression
         3. 2.9.3. Switch Expression
     13. 2.10. Objects and Classes
         ❱
     14. 1. 2.10.1. Objects
         2. 2.10.2. Classes
     15. 2.11. Modules and Imports
     16. 2.12. Assertions
 8.  3. Internet Computer Programming Concepts
     ❱
 9.  1. 3.1. Actors
        ❱
     2. 1. 3.1.1. From Actor to Canister
        2. 3.1.2. Canister Calls from Clients
     3. 3.2. Principals and Authentication
     4. 3.3. Async Data
        ❱
     5. 1. 3.3.1. Shared Types
        2. 3.3.2. Candid
     6. 3.4. Basic Memory Persistence
        ❱
     7. 1. 3.4.1. Upgrades
        2. 3.4.2. Stable Variables
 10. 
 11. Part 2
 12. 4. Advanced Types
     ❱
 13. 1. 4.1. Generic Types
     2. 4.2. Subtyping
     3. 4.3. Recursive Types
     4. 4.4. Type Bounds
 14. 5. The Base Library
     ❱
 15. 1. 5.1. Primitive Types
        ❱
     2. 1.  5.1.1. Bool
        2.  5.1.2. Nat
        3.  5.1.3. Int
        4.  5.1.4. Float
        5.  5.1.5. Principal
        6.  5.1.6. Text
        7.  5.1.7. Char
        8.  5.1.8. Bounded Number Types
            ❱
        9.  1. 5.1.8.1. Nat8
            2. 5.1.8.2. Nat16
            3. 5.1.8.3. Nat32
            4. 5.1.8.4. Nat64
            5. 5.1.8.5. Int8
            6. 5.1.8.6. Int16
            7. 5.1.8.7. Int32
            8. 5.1.8.8. Int64
        10. 5.1.9. Blob
     3. 5.2. Utility Modules
        ❱
     4. 1. 5.2.1. Iterators
        2. 5.2.2. Hash
        3. 5.2.3. Option
        4. 5.2.4. Result
        5. 5.2.5. Order
        6. 5.2.6. Error
        7. 5.2.7. Debug
     5. 5.3. Data Structures
        ❱
     6. 1. 5.3.1. Array
        2. 5.3.2. List
        3. 5.3.3. Buffer
        4. 5.3.4. HashMap
        5. 5.3.5. RBTree
     7. 5.4. More Data Structures
     8. 5.5. IC APIs
        ❱
     9. 1. 5.5.1. Time
        2. 5.5.2. Timer
        3. 5.5.3. CertifiedData
        4. 5.5.4. Random
        5. 5.5.5. Experimental
 16. 6. Advanced Concepts
     ❱
 17. 1. 6.1. Async Programming
     2. 6.2. Scalability
        ❱
     3. 1. 6.2.1. Actor Classes
        2. 6.2.2. Stable Storage
     4. 6.3. System API's
        ❱
     5. 1. 6.3.1. Message Inspection
        2. 6.3.2. Timers
        3. 6.3.3. Certified Variables
        4. 6.3.4. Pre-upgrade and Post-upgrade
        5. 6.3.5. Cryptographic Randomness
 18. 
 19. Part 3
 20. 7. Project Deployment
     ❱
 21. 1. 7.1. Installing the SDK
     2. 7.2. Local Deployment
     3. 7.3. Canister Status
     4. 7.4. Identities and PEM Files
     5. 7.5. Cycles and ICP
     6. 7.6. Cycles Wallet
     7. 7.7. IC Deployment
 22. 8. Common Internet Computer Canisters
     ❱
 23. 1. 8.1. IC Management Canister
     2. 8.2. ICP Ledger Canister
     3. 8.3. Cycle Minting Canister
 24. 9. Internet Computer Standards
     ❱
 25. 1. 9.1. ICRC1
 26. 10. Tokenized Comments Example
 27. 
 28. APPENDIX
 29. 11. TABLES


 * Light
 * Rust
 * Coal
 * Navy
 * Ayu


THE MOTOKO PROGRAMMING LANGUAGE BOOK





FROM ACTOR TO CANISTER

An actor is written in Motoko code. It defines public shared functions that can
be accessed from outside the Internet Computer (IC). A client, like a laptop or
a mobile phone, can send a request over the internet to call one of the public
functions defined in an actor.

Here is the code for one actor defined in its own Motoko source file. It
contains one public function.

// main.mo
actor {
    public shared query func hello() : async Text {
        "Hello world";
    };
};


We will deploy this actor to the Internet Computer!


CANISTER

A canister is like a home for an actor. It lives on the Internet Computer
Blockchain. A canister is meant to 'host' actors and make their public functions
available to other canisters and the wider Internet beyond the Internet Computer
itself.

Each canister can host one actor. The public interface of the canister is
defined by the actor type of the actor it hosts. The public interface is
described using an Interface Definition Language. Every canister has a unique
id.

The IC provides system resources to the canister, like:

 * Connectivity: A canister can receive inbound and make outbound canister
   calls.
 * Memory: A canister has main working memory and also has access to stable
   memory.
 * Computation: The code running in a canister is executed by one processor
   thread and consumes cycles.


CODE COMPILING AND WASM MODULES

Before an actor can be deployed to the Internet Computer, the Motoko code has to
be compiled into a special kind of code. Compilation transforms the Motoko code
into code that can run (be executed) on the Internet Computer. This code is
called Webassembly bytecode. The bytecode is just a file on your computer called
a Wasm module.

It is this Wasm module that gets installed in a canister.


DEPLOYMENT STEPS

To go from Motoko code to a running canister on the IC, the following happens:

 1. The Motoko code is compiled into a Wasm module
 2. An empty canister is registered on the Internet Computer
 3. The Wasm module is uploaded to the canister

The public functions of the actor are now accessible on the Internet from any
client!


MOTOKO PLAYGROUND

Currently (Jan 2023), there are two main tools to achieve the deployment steps.
The more advanced one is the Canister Development Kit called DFX. For now, we
will use a simpler tool called Motoko Playground.

The actor at the beginning of this chapter is deployed using Motoko Playground.
To repeat the deployment for yourself, open the this link and do the following:

 1. Check that there is one Main.mo file in the left side of the window
 2. Check the Motoko actor code from this chapter
 3. Click the blue Deploy button
 4. In the popup window choose Install

The deployment steps will now take place and you can follow the process in the
output log.


CALLING THE ACTOR FROM MOTOKO PLAYGROUND

After the successful deployment of the actor in a new canister, we can now call
our public function from the browser. On the right hand side of the window,
there is a Candid UI with the name of our function hello and a button QUERY.
Clicking the button returns the return value of our function to the browser
window.

The next chapter will explain what is actually happening when you interact with
a canister from a browser.