SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
Intro to building on the IC in
Motoko
Get started with the DFX and Motoko
DFINITY Confidential: For Internal Use Only
Not Mac User?
The command line examples in
this presentation are based on
the macOS Terminal. Using
another OS? No problem!
Other operating systems:
Linux: Generally same as macOS,
but can be different for
some distributions
macOS (M1): It may be necessary to
use Rosetta
Windows: Use WSL2
DFINITY Confidential: For Internal Use Only
What is the
Internet
Computer?
DFINITY Confidential: For Internal Use Only
What is the Internet Computer?
The Internet Computer is a
blockchain that enables
developers to build and deploy
secure, autonomous, unstoppable
and tamper-proof software,
running at web speed, and
capable of scaling without limits.
On-chain
The entire dapp is running on-chain, which means that
you are not only storing and retrieving data on
blockchain, you are running your entire dapp on
blockchain.
Smart contracts
The Internet Computer deploys dapps as canister
smart contracts, which contains frontend and backend
code, dependencies and the state of the dapp.
Programming
Motoko is native language for the Internet Computer,
but other languages are also supported. Any language
that can be compiled into WebAssembly can be used.
DFINITY Confidential: For Internal Use Only
DFINITY
Canister SDK
DFINITY Confidential: For Internal Use Only
DFINITY Canister SDK
Create and deploy canister smart contracts
with the canister SDK
DFINITY Confidential: For Internal Use Only
DFINITY Canister SDK
The SDK provides tools, sample code, and documentation to help you create
canister smart contract dapps and deploy them on the Internet Computer
blockchain mainnet.
Install the SDK:
DFINITY Confidential: For Internal Use Only
DFINITY Canister SDK
Verify the SDK installation:
DFX is the SDK’s command-line execution environment, and a list of the available
commands can be found in the documentation’s Getting Started section.
DFINITY Confidential: For Internal Use Only
Motoko
DFINITY Confidential: For Internal Use Only
The Motoko programming
language is a new, modern and
type safe language for
developers who want to build
the next generation of
decentralized applications to run
on the Internet Computer
blockchain network.
What is
Motoko?
DFINITY Confidential: For Internal Use Only
Motoko concepts
The Actor model
In Motoko a canister smart contract is
expressed as an actor.
An actor is an autonomous object
that fully encapsulates its state and
communicates with other actors only
through asynchronous messages.
DFINITY Confidential: For Internal Use Only
Motoko concepts
Futures
Motoko supports asynchronous
programming, and makes it much
simpler by applying a sequential
style.
Asynchronous function calls return a
future, and the await construct allows
you to suspend execution until a
future has completed.
DFINITY Confidential: For Internal Use Only
Motoko concepts
Memory persistence
The state of a Motoko actor, including its in-memory data
structures, can survive indefinitely.
Actor state does not need to be explicitly "restored" and
"saved" to external storage, with every message.
The state is preserved across calls, even though the state
of the actor is replicated across many Internet Computer
node machines.
DFINITY Confidential: For Internal Use Only
Motoko concepts
Autogenerated IDL files
Candid provides a language-agnostic
way to interact with canisters, which
means the backend functions are
exposed to other canisters, web
browsers, other program languages
and command-line commands.
DFINITY Confidential: For Internal Use Only
Let’s build a
dapp!
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
We are going to build a simple counter website, where a counter is increased
every time we click a button.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
What we will do
● Create new canister smart contract using the
SDK (DFX)
● Use the default project as a template as the
starting point for the new project
● Add backend functions for a counter (count,
get count and reset count)
● Implement backend functions in the frontend
● Deploy the canister smart contract locally
● Test backend with Candid UI and command
line using DFX, and test frontend in browser
What we will learn:
● Use the SDK for creating a canister smart
contract, testing the backend and
deploying the canister smart contract
● Use the SDK for testing the backend
● Create backend functions in Motoko
● Testing the backend using Candid UI
● Implement backend functions in the
frontend, using plain Javascript.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
[ DEMO ]
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
Step 1: Create new project using DFX
Run this command to create project:
DFX will create a new directory called hack, and in this directory you will find all
the files, both frontend, backend, configurations etc. for the default project. The
default project can be deployed without any changes as it is.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
The src directory will contain the
default frontend and backend code.
The dfx.json file contains the
canister configuration. It defines the
canister(s), where the source code for
the canister(s) is located, the type of
canister(s) and which version of DFX
the project was created with.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
Step 2: Modify the backend
As the first step, let’s add a few
backend functions. The existing code
from the default project is not needed,
so the greet() function is deleted.
At the top a variable counter is
defined to hold the counter value.
The functions count(), getCount()
and reset() are added.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
The function count()increments the
counter variable. The function
getCount() returns the value of the
counter variable. The function
reset() resets the counter value.
Note: Motoko is a strongly typed
language, so the type is always
defined. Also, all functions are called
asynchronously.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
Step 3: Test backend
If we deploy the project to the local network we can at this point test the backend,
so let’s do that. First start the local network:
The --background is not needed if the local network is started in another
terminal.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
Now the local network is running, deploy the canisters:
When the canisters have been deployed, there are two easy ways to test the
backend. The first method is to use DFX to call the backend functions, which have
been exposed by Candid, and he second method is to use the Candid UI.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
DFX has a subset of commands for
canister operations, and one of them
enables us to call the public functions we
added to the main Motoko file in step 2.
The command is:
dfx canister call <project> <func>
Example:
The initial value is 0, and count will increment value and return 1,
getCount will return the current value and reset will set the value to 0.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
The Candid UI provides an easy, user
friendly interface for testing the backend.
The UI is automatically generated, and the
canister ID can be found in the
canister_ids.json file. The localhost
version of the canister_ids.json file
can be found in:
.dfx/local/canister_ids.json
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
Step 4: Modify the frontend
The default project has an index.html
file with page HTML and an index.js
file with an implementation of the
backend functions.
For this demo the changes to the
index.html file is minor. The button is
kept and so is the section showing
the result, just simplified.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
The existing event listener for button click is modified to call the count() function,
and an event listener for page load is added to get the initial value of the counter
with getCount(). The backend functions are still imported through the Candid
interface.
DFINITY Confidential: For Internal Use Only
Let’s build a dapp
Step 5: Test Frontend
The canisters are re-deployed, which will
update the canisters, using the same dfx
deploy command as in step 2.
The URL for the frontend is depending on
the canister ID. As described before, get the
canister ID from the canister_IDs.json
file, look for the assets canister. The URL will
look like this:
https://<canister ID>.localhost:8000
DFINITY Confidential: For Internal Use Only
Thank you!
smartcontracts.org
forum.dfinity.org
Dfinity Dev Official (Discord)

Weitere ähnliche Inhalte

Ähnlich wie Encode x ICH: Intro to Building on the IC in Motoko

Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel Tools
Xavier Hallade
 
GoogleDSC_ GHRCE_ flutter_firebase.pptx
GoogleDSC_ GHRCE_  flutter_firebase.pptxGoogleDSC_ GHRCE_  flutter_firebase.pptx
GoogleDSC_ GHRCE_ flutter_firebase.pptx
GoogleDeveloperStude22
 
Week 4 B IP Subnetting Lab Essay
Week 4 B IP Subnetting Lab EssayWeek 4 B IP Subnetting Lab Essay
Week 4 B IP Subnetting Lab Essay
Amanda Brady
 

Ähnlich wie Encode x ICH: Intro to Building on the IC in Motoko (20)

Hybrid Apps in a Snap
Hybrid Apps in a SnapHybrid Apps in a Snap
Hybrid Apps in a Snap
 
Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel Tools
 
Building CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptxBuilding CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptx
 
GoogleDSC_ GHRCE_ flutter_firebase.pptx
GoogleDSC_ GHRCE_  flutter_firebase.pptxGoogleDSC_ GHRCE_  flutter_firebase.pptx
GoogleDSC_ GHRCE_ flutter_firebase.pptx
 
Green Custard Friday Talk 22: Flutter
Green Custard Friday Talk 22: FlutterGreen Custard Friday Talk 22: Flutter
Green Custard Friday Talk 22: Flutter
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
Week 4 B IP Subnetting Lab Essay
Week 4 B IP Subnetting Lab EssayWeek 4 B IP Subnetting Lab Essay
Week 4 B IP Subnetting Lab Essay
 
Mobile CICD
Mobile CICD Mobile CICD
Mobile CICD
 
How to develop a Flutter app.pdf
How to develop a Flutter app.pdfHow to develop a Flutter app.pdf
How to develop a Flutter app.pdf
 
Compose Camp - Session2.pdf
Compose Camp - Session2.pdfCompose Camp - Session2.pdf
Compose Camp - Session2.pdf
 
MongoDB Mobile: Bringing the Power of MongoDB to Your Device
MongoDB Mobile: Bringing the Power of MongoDB to Your DeviceMongoDB Mobile: Bringing the Power of MongoDB to Your Device
MongoDB Mobile: Bringing the Power of MongoDB to Your Device
 
Software Developer’s Project Documentation Template
Software Developer’s Project Documentation TemplateSoftware Developer’s Project Documentation Template
Software Developer’s Project Documentation Template
 
Deep Learning Edge
Deep Learning Edge Deep Learning Edge
Deep Learning Edge
 
Democratizing Serverless: the New Open Source, Cloud Agnostic Functions Platf...
Democratizing Serverless: the New Open Source, Cloud Agnostic Functions Platf...Democratizing Serverless: the New Open Source, Cloud Agnostic Functions Platf...
Democratizing Serverless: the New Open Source, Cloud Agnostic Functions Platf...
 
Import golang; struct microservice
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microservice
 
Webinar - Analyzing Video
Webinar - Analyzing VideoWebinar - Analyzing Video
Webinar - Analyzing Video
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
 
Targeting Android with Qt
Targeting Android with QtTargeting Android with Qt
Targeting Android with Qt
 
ITB2019 CommandBox vs Node.js - Nolan Erck
ITB2019  CommandBox vs Node.js - Nolan ErckITB2019  CommandBox vs Node.js - Nolan Erck
ITB2019 CommandBox vs Node.js - Nolan Erck
 
Developing new zynq based instruments
Developing new zynq based instrumentsDeveloping new zynq based instruments
Developing new zynq based instruments
 

Mehr von KlaraOrban

Encode x Tezos: Building a dApp on Tezos
Encode x Tezos: Building a dApp on TezosEncode x Tezos: Building a dApp on Tezos
Encode x Tezos: Building a dApp on Tezos
KlaraOrban
 
Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1
KlaraOrban
 
Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1
KlaraOrban
 
Encode x The Graph: How to get involved with The Graph
Encode x The Graph: How to get involved with The GraphEncode x The Graph: How to get involved with The Graph
Encode x The Graph: How to get involved with The Graph
KlaraOrban
 
Encode x The Graph: Curating and delegating on The Graph
Encode x The Graph: Curating and delegating on The GraphEncode x The Graph: Curating and delegating on The Graph
Encode x The Graph: Curating and delegating on The Graph
KlaraOrban
 
Encode x The Graph - Introduction to the Graph
Encode x The Graph - Introduction to the GraphEncode x The Graph - Introduction to the Graph
Encode x The Graph - Introduction to the Graph
KlaraOrban
 
Best practices for canisters in rust
Best practices for canisters in rustBest practices for canisters in rust
Best practices for canisters in rust
KlaraOrban
 
Decentralized possibilities with filecoin &amp; ipfs_encode filecoin club
Decentralized possibilities with filecoin &amp; ipfs_encode filecoin clubDecentralized possibilities with filecoin &amp; ipfs_encode filecoin club
Decentralized possibilities with filecoin &amp; ipfs_encode filecoin club
KlaraOrban
 

Mehr von KlaraOrban (20)

Encode x Tezos Hack: Hands-on dApp Training
Encode x Tezos Hack: Hands-on dApp Training Encode x Tezos Hack: Hands-on dApp Training
Encode x Tezos Hack: Hands-on dApp Training
 
Encode x Tezos: Building a dApp on Tezos
Encode x Tezos: Building a dApp on TezosEncode x Tezos: Building a dApp on Tezos
Encode x Tezos: Building a dApp on Tezos
 
Building Applications on Avalanche
Building Applications on AvalancheBuilding Applications on Avalanche
Building Applications on Avalanche
 
Building on NEAR, Part 1
Building on NEAR, Part 1Building on NEAR, Part 1
Building on NEAR, Part 1
 
Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1
 
Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1Encode x NEAR: Technical Overview of NEAR 1
Encode x NEAR: Technical Overview of NEAR 1
 
Encode x The Graph: How to get involved with The Graph
Encode x The Graph: How to get involved with The GraphEncode x The Graph: How to get involved with The Graph
Encode x The Graph: How to get involved with The Graph
 
Encode x NEAR: Intro to NEAR
Encode x NEAR: Intro to NEAREncode x NEAR: Intro to NEAR
Encode x NEAR: Intro to NEAR
 
Encode x The Graph: Curating and delegating on The Graph
Encode x The Graph: Curating and delegating on The GraphEncode x The Graph: Curating and delegating on The Graph
Encode x The Graph: Curating and delegating on The Graph
 
Encode x NEAR: Intro to Blockchain
Encode x NEAR: Intro to BlockchainEncode x NEAR: Intro to Blockchain
Encode x NEAR: Intro to Blockchain
 
bounties4bandits: Information event
bounties4bandits: Information eventbounties4bandits: Information event
bounties4bandits: Information event
 
Intro to avalanche
Intro to avalancheIntro to avalanche
Intro to avalanche
 
Encode x Graph: The Data Economy
Encode x Graph: The Data EconomyEncode x Graph: The Data Economy
Encode x Graph: The Data Economy
 
Encode x BitDAO Intro Event
Encode x BitDAO Intro EventEncode x BitDAO Intro Event
Encode x BitDAO Intro Event
 
Encode x The Graph - Introduction to the Graph
Encode x The Graph - Introduction to the GraphEncode x The Graph - Introduction to the Graph
Encode x The Graph - Introduction to the Graph
 
Hack DeFi: DeFi with Wintermute
Hack DeFi: DeFi with WintermuteHack DeFi: DeFi with Wintermute
Hack DeFi: DeFi with Wintermute
 
Hack DeFi Launch
Hack DeFi LaunchHack DeFi Launch
Hack DeFi Launch
 
Nft Hack Ideation Workshop
Nft Hack Ideation WorkshopNft Hack Ideation Workshop
Nft Hack Ideation Workshop
 
Best practices for canisters in rust
Best practices for canisters in rustBest practices for canisters in rust
Best practices for canisters in rust
 
Decentralized possibilities with filecoin &amp; ipfs_encode filecoin club
Decentralized possibilities with filecoin &amp; ipfs_encode filecoin clubDecentralized possibilities with filecoin &amp; ipfs_encode filecoin club
Decentralized possibilities with filecoin &amp; ipfs_encode filecoin club
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

Encode x ICH: Intro to Building on the IC in Motoko

  • 1. Intro to building on the IC in Motoko Get started with the DFX and Motoko
  • 2. DFINITY Confidential: For Internal Use Only Not Mac User? The command line examples in this presentation are based on the macOS Terminal. Using another OS? No problem! Other operating systems: Linux: Generally same as macOS, but can be different for some distributions macOS (M1): It may be necessary to use Rosetta Windows: Use WSL2
  • 3. DFINITY Confidential: For Internal Use Only What is the Internet Computer?
  • 4. DFINITY Confidential: For Internal Use Only What is the Internet Computer? The Internet Computer is a blockchain that enables developers to build and deploy secure, autonomous, unstoppable and tamper-proof software, running at web speed, and capable of scaling without limits. On-chain The entire dapp is running on-chain, which means that you are not only storing and retrieving data on blockchain, you are running your entire dapp on blockchain. Smart contracts The Internet Computer deploys dapps as canister smart contracts, which contains frontend and backend code, dependencies and the state of the dapp. Programming Motoko is native language for the Internet Computer, but other languages are also supported. Any language that can be compiled into WebAssembly can be used.
  • 5. DFINITY Confidential: For Internal Use Only DFINITY Canister SDK
  • 6. DFINITY Confidential: For Internal Use Only DFINITY Canister SDK Create and deploy canister smart contracts with the canister SDK
  • 7. DFINITY Confidential: For Internal Use Only DFINITY Canister SDK The SDK provides tools, sample code, and documentation to help you create canister smart contract dapps and deploy them on the Internet Computer blockchain mainnet. Install the SDK:
  • 8. DFINITY Confidential: For Internal Use Only DFINITY Canister SDK Verify the SDK installation: DFX is the SDK’s command-line execution environment, and a list of the available commands can be found in the documentation’s Getting Started section.
  • 9. DFINITY Confidential: For Internal Use Only Motoko
  • 10. DFINITY Confidential: For Internal Use Only The Motoko programming language is a new, modern and type safe language for developers who want to build the next generation of decentralized applications to run on the Internet Computer blockchain network. What is Motoko?
  • 11. DFINITY Confidential: For Internal Use Only Motoko concepts The Actor model In Motoko a canister smart contract is expressed as an actor. An actor is an autonomous object that fully encapsulates its state and communicates with other actors only through asynchronous messages.
  • 12. DFINITY Confidential: For Internal Use Only Motoko concepts Futures Motoko supports asynchronous programming, and makes it much simpler by applying a sequential style. Asynchronous function calls return a future, and the await construct allows you to suspend execution until a future has completed.
  • 13. DFINITY Confidential: For Internal Use Only Motoko concepts Memory persistence The state of a Motoko actor, including its in-memory data structures, can survive indefinitely. Actor state does not need to be explicitly "restored" and "saved" to external storage, with every message. The state is preserved across calls, even though the state of the actor is replicated across many Internet Computer node machines.
  • 14. DFINITY Confidential: For Internal Use Only Motoko concepts Autogenerated IDL files Candid provides a language-agnostic way to interact with canisters, which means the backend functions are exposed to other canisters, web browsers, other program languages and command-line commands.
  • 15. DFINITY Confidential: For Internal Use Only Let’s build a dapp!
  • 16. DFINITY Confidential: For Internal Use Only Let’s build a dapp We are going to build a simple counter website, where a counter is increased every time we click a button.
  • 17. DFINITY Confidential: For Internal Use Only Let’s build a dapp What we will do ● Create new canister smart contract using the SDK (DFX) ● Use the default project as a template as the starting point for the new project ● Add backend functions for a counter (count, get count and reset count) ● Implement backend functions in the frontend ● Deploy the canister smart contract locally ● Test backend with Candid UI and command line using DFX, and test frontend in browser What we will learn: ● Use the SDK for creating a canister smart contract, testing the backend and deploying the canister smart contract ● Use the SDK for testing the backend ● Create backend functions in Motoko ● Testing the backend using Candid UI ● Implement backend functions in the frontend, using plain Javascript.
  • 18. DFINITY Confidential: For Internal Use Only Let’s build a dapp [ DEMO ]
  • 19. DFINITY Confidential: For Internal Use Only Let’s build a dapp Step 1: Create new project using DFX Run this command to create project: DFX will create a new directory called hack, and in this directory you will find all the files, both frontend, backend, configurations etc. for the default project. The default project can be deployed without any changes as it is.
  • 20. DFINITY Confidential: For Internal Use Only Let’s build a dapp The src directory will contain the default frontend and backend code. The dfx.json file contains the canister configuration. It defines the canister(s), where the source code for the canister(s) is located, the type of canister(s) and which version of DFX the project was created with.
  • 21. DFINITY Confidential: For Internal Use Only Let’s build a dapp Step 2: Modify the backend As the first step, let’s add a few backend functions. The existing code from the default project is not needed, so the greet() function is deleted. At the top a variable counter is defined to hold the counter value. The functions count(), getCount() and reset() are added.
  • 22. DFINITY Confidential: For Internal Use Only Let’s build a dapp The function count()increments the counter variable. The function getCount() returns the value of the counter variable. The function reset() resets the counter value. Note: Motoko is a strongly typed language, so the type is always defined. Also, all functions are called asynchronously.
  • 23. DFINITY Confidential: For Internal Use Only Let’s build a dapp Step 3: Test backend If we deploy the project to the local network we can at this point test the backend, so let’s do that. First start the local network: The --background is not needed if the local network is started in another terminal.
  • 24. DFINITY Confidential: For Internal Use Only Let’s build a dapp Now the local network is running, deploy the canisters: When the canisters have been deployed, there are two easy ways to test the backend. The first method is to use DFX to call the backend functions, which have been exposed by Candid, and he second method is to use the Candid UI.
  • 25. DFINITY Confidential: For Internal Use Only Let’s build a dapp DFX has a subset of commands for canister operations, and one of them enables us to call the public functions we added to the main Motoko file in step 2. The command is: dfx canister call <project> <func> Example: The initial value is 0, and count will increment value and return 1, getCount will return the current value and reset will set the value to 0.
  • 26. DFINITY Confidential: For Internal Use Only Let’s build a dapp The Candid UI provides an easy, user friendly interface for testing the backend. The UI is automatically generated, and the canister ID can be found in the canister_ids.json file. The localhost version of the canister_ids.json file can be found in: .dfx/local/canister_ids.json
  • 27. DFINITY Confidential: For Internal Use Only Let’s build a dapp Step 4: Modify the frontend The default project has an index.html file with page HTML and an index.js file with an implementation of the backend functions. For this demo the changes to the index.html file is minor. The button is kept and so is the section showing the result, just simplified.
  • 28. DFINITY Confidential: For Internal Use Only Let’s build a dapp The existing event listener for button click is modified to call the count() function, and an event listener for page load is added to get the initial value of the counter with getCount(). The backend functions are still imported through the Candid interface.
  • 29. DFINITY Confidential: For Internal Use Only Let’s build a dapp Step 5: Test Frontend The canisters are re-deployed, which will update the canisters, using the same dfx deploy command as in step 2. The URL for the frontend is depending on the canister ID. As described before, get the canister ID from the canister_IDs.json file, look for the assets canister. The URL will look like this: https://<canister ID>.localhost:8000
  • 30. DFINITY Confidential: For Internal Use Only Thank you! smartcontracts.org forum.dfinity.org Dfinity Dev Official (Discord)