script (⏱,💰)

script (⏱,💰)

NG#0 - Cairo Introduction

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 will introduce 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 the 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 the brew mirror). For Windows, please search for the installation method.

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 the link to add the GitHub Application. This application is used to verify whether the submitted answer code can pass the tests. Just install it in the newly created ng-questplay repository, and other repositories are not needed.

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 the 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 the syntax of Rust, and there are two points to note here:

  1. The return value of a function, reference
  2. The string type in Cairo, reference

Enter quest test 3 to test the code locally. If there are no errors, the local test is complete. If there are errors, you need to modify the code based on the error message.

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 success. After that, go back to the website and click on Verify.
There may be hidden test cases for some tasks, so passing all the tests locally does not necessarily mean the task is completed.
After the submission is complete, there will be a Summary. You can review the key points 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. The CTF task will deploy a contract to the testnet, and you need to interact with the contract to modify its state to achieve the task goal. 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 target 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 browser, and use the "read contract" and "write contract" on the right to complete 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.