script (⏱,💰)

script (⏱,💰)

NG#0 - Introduction to Cairo

Introduction#

Node Guardians is a website for learning Solidity and Cairo. Behind the fancy game interface is an excellent course design, and it is highly recommended for learning. This series introduces how to complete the course step by step.

Open the website https://nodeguardians.io/dev-hub?s=devhub-campaigns to see the following interface. Each card represents a task on a specific topic, and completing the task will earn rewards.

main page

Build Tutorial: Cairo#

Our first task starts with "Setting Up: Cairo". The purpose of this task is to set up your Cairo development environment and learn how to submit tasks. There are two tutorials available: "Build Tutorial: Cairo" and "CTF Tutorial: Cairo". Let's start with "Build Tutorial: Cairo" by clicking on the "Start Quest" button in the bottom right corner.

build tutorial

First, install git and npm. If you are using MacOS, you can install them by entering brew install node and brew install git. (If you encounter network issues in China, you can use a brew mirror). For Windows, please search for the installation methods.

After the installation is complete, enter git -v && node -v to check if the versions are displayed. The node version should be 16 or later.
git & node version

Create a private repository named ng-questplay on GitHub.

Then click on the link to add the GitHub Application. This application is used to verify if the submitted answer code can pass the tests. Install it on the newly created ng-questplay repository, and other repositories are not required.

add github app

Clone the official code repository and link it to your private repository.

git clone [email protected]:Nodeguardians/ng-questplay.git
cd ng-questplay
git remote set-url origin [email protected]:{GITHUB_USERNAME}/ng-questplay.git
git push -u origin main

In the ng-questplay folder, run the following command to install the necessary dependencies and make the first commit:

npm run start-adventure
git add .
git commit -m "Install initial dependencies"

To develop in Cairo, you need to install scrab, which is a packaging and dependency management tool for Cairo. Install the latest version. Installation guide link

Next, set up the GitHub Token for downloading tasks.

  1. Click on your GitHub profile picture in the top right corner to go to the settings page.
  2. In the left sidebar, click on "Developer Settings" (the last option) > "Personal Access Tokens".
  3. Generate a new Token (classic) (e.g., name it "Node Guardians Questplay").
  4. Give this Token access to public repositories.
  5. Generate the Token.

Public permission

GitHub Token

Copy the Token to the .env file in the project folder, named GITHUB_TOKEN = "ghp_..."

env Token

Run quest find build-tutorial-cairo to download the first task.

download quest

Then cd ./campaigns/starting-cairo/build-tutorial-cairo to enter the task folder.

The first task is to make the hello_guardian() function in src/hello.cairo return 'Hello Guardian'.
hello world

I won't provide the answer, only the approach. Cairo uses Rust syntax, and there are two points to note here:

  1. Function return values, reference
  2. Cairo's string type, reference

Enter quest test 3 to run the local test if the code does not throw any errors. If there are errors, you need to modify the code based on the error messages.

Next is to submit the answer. It is recommended to use Automated Submission. First, commit your code by entering any commit message, for example:

git add .
git commit -m "complete quest 3"

Then enter quest submit and wait for the success message. After that, go back to the website and click on "Verify" to complete the task. Note that there may be hidden test cases for some tasks, so passing the local tests does not necessarily mean the task is completed. After submitting, there will be a summary of the task, where you can review the key points you learned in this task.

summary

The experience gained will increase your account level and global ranking. Gold is used to upgrade characters (not very useful).

CTF Tutorial: Cairo#

Next is the CTF task. In the CTF task, a contract will be deployed to the testnet, and you need to interact with the contract, modify its state to achieve the task goals. The methods are not limited to interacting with the contract through a block explorer. You can also send transactions through the CLI, deploy new contracts to invoke the deployed contract, etc.

Before completing the task, it is recommended to learn how to export wallet private keys, create contracts, and interact with them. You can refer to the official documentation at https://docs.starknet.io/documentation/quick_start/environment_setup/ or the Chinese video tutorial.

Click on "deploy sand devil" in the bottom left corner, connect your wallet, switch to Testnet, and after sending the transaction, the deployed contract address will be displayed.

wallet

For this task, you need to know the value of "count" first, and then call "slay" with the parameter subtracted by "count" to make it 0. The simplest way is to access the address you deployed through a block explorer, and use the "read contract" and "write contract" on the right to achieve the task.

voyager

After setting "count" to 0, click on "Verify" to complete the task.

complete

Next#

The next article will delve into Cairo and complete the difficulty 2 task "Thinking in Cairo" in the "Algorithms and Data Structures" category.

Next

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.