back to Dfinity

02 — Setup

Setting up for local development

In this chapter we will walk you through the steps you need to start working on the application.

Installing Tools

To build and run the project you need to install the following tools:

  • Canister SDK - We will walk you through the installation in the next paragraph.
  • Node.js and NPM - We highly recommend installing it them via NVM. We are currently using Node.js v16 which is the current LTS and we highly recommend using that as well. Newer versions may present some annoying issues.
  • Yarn - You can install yarn by following these steps.
  • Rust - It will be used to compile the Internet Identity and Ledger canisters, so be sure to follow the recommended steps in order to install it.

Installing Canister SDK

Canister SDK contains everything you need to create, build and deploy canisters. It can handle:

  • Local Dfinity network deployment, configuration and management
  • Identity management on the local instance of Internet Computer
  • Interpreting and building Motoko code
  • Canister deployment and management
  • RPC calls to the canisters

Installing the SDK is simple! Just run:

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

Installing Canister SDK

Canister SDK contains everything you need to create, build and deploy canisters. It can handle:

  • Local Dfinity network deployment, configuration and management
  • Identity management on the local instance of Internet Computer
  • Interpreting and building Motoko code
  • Canister deployment and management
  • RPC calls to the canisters

Installing the SDK is simple! Just run:

dfx --version

If the terminal prints the current version (at least 0.9.3), then you are good to go. Congratulations!

Clone our Repo

The name of our NFT marketplace application is Veiling. You can find the repo here. You can explore it in case you want to have a better feel for it and when you are ready, run:

git clone git@github.com:Kryha/NFT-english-auction.git

Then we suggest renaming the directory:

mv NFT-english-auction veiling

Now cd inside the project directory and open it with your favourite editor.

Run the following command to install all the required NPM libraries:

yarn

Then make the deployment scripts executable by your OS:

make scripts-permissions

Project Structure

Let's have a look at the most interesting files and directories in our project:

  • scripts/ - Contains some shell scripts that will facilitate the local deployment of the Ledger and Internet Identity canisters.
  • src/ - Contains our application code.
    • nft/ - Motoko code of the canister handling NFT minting and transactions.
    • backend/ - Motoko code of the canister handling auction logic.
    • frontend/ - Typescript code of our frontend application.
    • types/ - Type declarations for our frontend.
    • declarations/ (will be generated after build) - JS and TS files that allow integration between frontend and actor canisters.
  • dfx.json - This file contains the configuration for the canister SDK. Here we can specify the dfx version, the network configuration and (last but definitely not least) the canisters that will be deployed in our project and their dependencies. More details about dfx.json can be found here.
  • ledger.private.did - The candid interface used when deploying the Ledger canister locally. This is to be used only during development.
  • ledger.public.did - The candid interface used to interact with the main functionalities of the Ledger canister.
  • ledger.wasm - The Web Assembly bytecode that will be executed on the Ledger canister.
  • Makefile - A file that defines some commands to facilitate the development process.

Run the Local Network

Start a dfx network with the following command:

dfx start

This will create an isolated network for local development at the address specified in the dfx.json. We will deploy all of our canisters to this network during development, so be sure it is running. Running this command will also generate a .dfx/ directory in the project root, which will contain all the information regarding the state of our canisters.

Every time we run dfx start, the network will be initialized with the data stored inside .dfx/. Sometimes you want to do a fresh restart with the following command:

dfx start --clean

You can also run the network in the background:

dfx start --background

We highly discourage running it in the background since you may miss very important information about the network status and you also won't be able to see the content you print in the canisters.

Principals

On the Internet Computer, every user and canister is identified through a principal. This is similar to how addresses work on Ethereum. A principal is a unique string which identifies a user or a canister and it has the following format:

t75t3-uzim4-td4f7-fbgo7-p2zeg-phz7l-cchh4-ofc4a-mifjc-p5clx-uae

In local development, you can create a new principal:

dfx identity new <principal_name>

You can list all the local principals:

dfx identity list

You can choose which principal to use for your commands:

dfx identity use <principal_name>

To read more information about identity usage:

dfx identity --help <principal_name>

When executing dfx commands, your identity matters, especially when deploying canisters. By default, dfx will create a wallet canister that is used as the owner of the canisters you deploy. We rarely want that, so we'll often use the --no-wallet flag in order to have our current identity as the owner of the canisters we, deploy.