Coder Social home page Coder Social logo

starknet-cairo-101's Introduction

StarkNet Cairo 101

Get started with Cairo with this simple tutorial. Complete the puzzles/exercises, get tokens and learn about StarkNet smart contracts!

Introduction​

Disclaimer

​Don't expect any benefit from using this other than learning some cool stuff about StarkNet, the first general-purpose validity rollup on the Ethereum mainnnet.

​StarkNet is still in Alpha. This means that development is ongoing, and the paint is not dry everywhere. Things will get better, and in the meanwhile, we make things work with a bit of duct tape here and there!​

How it works

Complete the exercises and get tokens!

This workshop is a set of smart contracts deployed on StarkNet Alpha on testnet. Each smart contract is an exercise/puzzle - which outlines a feature of the Cairo Smart contract language.

Completing the exercise will credit you with points in the form of an ERC20 token.

​ This workshop focuses on reading Cairo code and StarkNet smart contracts to understand their syntax. You do not need to code or install anything on your machine to follow and complete it.​

​ It will take you some time in order to get started (doing the first two exercises) in the tutorial. Hang on! Once there, things will flow more easily. You're learning!​

Where am I?

This workshop is the first in a series aimed at teaching how to build on StarkNet. Checkout out the following:

Topic GitHub repo
Learn how to read Cairo code (you are here) Cairo 101
Deploy and customize an ERC721 NFT StarkNet ERC721
Deploy and customize an ERC20 token StarkNet ERC20
Build a cross-layer application StarkNet messaging bridge
Debug your Cairo contracts easily StarkNet debug
Design your own account contract StarkNet account abstraction

Providing feedback & getting help

Once you are done working on this tutorial, your feedback would be greatly appreciated!

Please fill out this form to let us know what we can do to make it better.

​ And if you struggle to move forward, do let us know! This workshop is meant to be as accessible as possible; we want to know if it's not the case.

​ Do you have a question? Join our Discord server, register, and join channel #tutorials-support.

Are you interested in following online workshops about learning how to dev on StarkNet? Subscribe here

Contributing

This project can be made better and will evolve as StarkNet matures. Your contributions are welcome! Here are things that you can do to help:

  • Create a branch with a translation to your language
  • Correct bugs if you find some
  • Add an explanation in the comments of the exercise if you feel it needs more explanation
  • Add exercises showcasing your favorite Cairo feature

Languages

  • A Spanish version is available here.
  • A Portuguese version is available here.
  • A Korean version is available here.

Getting started

Creating and setting up a smart contract wallet

To complete the tutorial you need to collect points. These points will be owned by a smart contract wallet you need to deploy.

  • The easiest way to set one up is to use Argent X (download the chrome extension or check their repo) or Braavos (download the chrome extension). These wallet solutions are similar to what Metamask is for Ethereum and allow users to initiate transactions and interact with applications on StarkNet.

  • Here are the steps to install the Braavos extension

    • Step 1

      Visit the braavos download page and install the appropriate extension for the browser. Braavos also come with Android and IOS apps. download-page

    • Step 2

      Click on the extension on your browser, open the extension dropdown opens up, and click on Get Started to start the installation.

      get-started

    • Step 3

      Create a wallet login password and keep it safe. password

    • Step 4

      Copy the autogenerated secret recovery phrases and keep them safe. You might need it to recover your account and its assets in the future. Do not share the password or the recovery phrase with anyone password

    • Step 5

      Make sure to connect to the Goerli testnet network. password

    • Step 6

      Get yourself a coffee, your account deployment begins, it may take up to 5 minutes. As expected, the account balance will be zero for a new account.

  • The new account which you just created is a smart contract account. Please note that in StarkNet there is only one type of account - smart contract accounts (this is called Account Abstraction), in contrast to Ethereum where there are wallets and smart contracts.

  • In other words, every wallet on StarkNet is a smart contract and there is no distinction between them and other smart contracts. On finishing the setup, the wallet extension automatically deploys a transaction that publishes your smart contract wallet to the network. You can view this transaction in the activity of your wallet. On clicking the activity, it redirects to Starkscan where you can view the details of the transaction. transaction activity

  • You can track all of the account/wallet using the activity with the help of the block explorer (Starkscan or Voyager). These block explorers for StarkNet are the equivalent of Etherscan for Ethereum. They allow you to browse the state of the blockchain, and view all transactions and their status. Here's how you can view your account on Starkscan. starkscan

  • Smart contracts on Starknet can be used to create and hold new special-purpose tokens. For the purpose of the tutorial, we've created a token called SC-101 (starknet-cairo-101) to use for tutorial points. - The token is deployed using a contract with address 0x5c6b1379f1d4c8a4f5db781a706b63a885f3f9570f7863629e99e2342ac344c (Starkscan link, Voyager link).

  • Your next step is to add the SC101 token to your newly installed wallet using the contract address 0x5c6b1379f1d4c8a4f5db781a706b63a885f3f9570f7863629e99e2342ac344c. Click on "Add Token", copy the contract address, and enter the token address to add the token to your wallet. A new token called SC101 (starknet-cairo-101) will appear in your wallet. add token

  • To execute transactions on the Goerli StarkNet testnet you'll need testnet ETH to pay for gas. To get some, first copy your account/wallet address, go to the faucet site, paste your the account address and click send request.

    get test token

It could take several minutes, but you should receive some L2 Goerli ETH in your wallet that you can use to execute transactions on the testnet. You should see the following confirmation along with the transaction ID on successful transfer of the L2 Goerli ETH.

Faucet

Now we've got enough gas in the tank to march ahead🚀 Let's move onto the next steps.

Solving exercises & Getting points​

Structure of the exercise.

There are 14 exercises in total, and each exercise contains a file with a smart contract written in Cairo. All 14 smart contracts are already deployed on the testnet. As we already know, every smart contract on testnet or the mainnet can be tracked on block explorers. Along with the smart contract code we also share the Starkscan and Voyager links pointing to the deployed smart contracts. Each smart contract code contains instructions in comments about the objective of the coding exercise. Not to worry, the task and the instructions are simple and aimed at absolute beginners.

Using a block explorer

Please make sure to connect the block explorer to your account contract! This will allow you to broadcast your transactions through your wallet. This is fairly simple! In the case of the Starkscan page of the deployed contract, Just click on the connect to wallet button from the Write Contract tab. connect wallet

When looking for a contract/transaction, always ensure you are on the Goerli version of the block explorer!

How does the smart contract in the exercise work

The smart contracts contain code that when executed correctly, will distribute points to your wallet address. ​ If you observe the smart contract code in any of the exercises, you'll find the distribute_points() and validate_exercise functions. Points are distributed by the function distribute_points() while the function validate_exercise records that you completed the exercise (you can get points only once).

Where do I submit the code for the exercises

You don't have to install anything on your machine or use a code editor. Once you understand the instructions inside the comments of the smart contract code, just click the block explorer link of the corresponding exercise. Then directly interact directly with the deployed smart contract of the exercise using the Read Contract and Write contract tabs of the block explorer page as per instructions. Your goal is to:

Graph

Here are the Starkscan Read contract and write Contract tabs corresponding to the deployed smart contract of the first exercise: Read Contract and Write Contract.

Notice that you can directly invoke/interact with the exported read/write functions of the smart contract using the Read Contract and Write Contract tabs.

read write contract

Link to the ERC20 smart contract

Here is the link to the smart contract code and its deployed link which is responsible for verifying your exercise and adding points to your wallet on success. Don't worry about understanding the contract yet. Just focus on learning Cairo basics in this workshop, we'll dwell on writing contracts in another workshop.

Topic Contract code Contract on Starkscan Contract on Voyager
Points counter ERC20 Points counter ERC20 Link Link

Addresses of the exercises and contracts

Here are the links to the exercises, and the instructions to complete them.

  • Open a contract code.
  • Understand the instructions inside the comments at the beginning of the code file.
  • Go to the corresponding deployed link of the smart contract using either Starkscan or Voyager.
  • Interact with the smart contract as per the instructions in the Contract code using the Read Contract or Write Contract tabs.
  • Execute the instructions, if your interaction is successful, you'll execute a transaction that will create a token for you.

For your convenience, here's a quick preview of how to solve the first exercise. ex1

You're all set :) Again, please don't hesitate to reach out if you're stuck or have some questions. Join our Discord server, register, and share your thoughts/questions in the channel #tutorials-support. All the best with your Cairo journey.

Topic Contract code Contract on Starkscan Contract on Voyager
General syntax Ex01 Link Link
Storage variables, getters, asserts Ex02 Link Link
Reading and writing storage variables Ex03 Link Link
Mappings Ex04 Link Link
Variable visibility Ex05 Link Link
Functions visibility Ex06 Link Link
Comparing values Ex07 Link Link
Recursions level 1 Ex08 Link Link
Recursions level 2 Ex09 Link Link
Composability Ex10 Link Link
Importing functions Ex11 Link Link
Events Ex12 Link Link
Privacy on StarkNet Ex13 Link Link
Multicall Ex14 Link Link

Counting your points

Your points will get credited to your installed wallet; though this may take some time. If you want to monitor your points count in real-time, you can also see your balance in a block explorer! ​

  • Go to the ERC20 counter on [Voyager](https://goerli.voyager.online/contract/ 0x5c6b1379f1d4c8a4f5db781a706b63a885f3f9570f7863629e99e2342ac344c#readContract) or Starkscan, in the "read contract" tab.
  • Enter your address in the "balanceOf" function.

You can also check your overall progress here. ​

Transaction status

​ You can also check the status of your transaction with the following URL https://alpha4.starknet.io/feeder_gateway/get_transaction_receipt?transactionHash= or https://alpha4.starknet.io/feeder_gateway/get_transaction?transactionHash=, where you can append your transaction hash. ​ ​

Reusing this project

While it's not necessary to compile the smart contracts for completing the exercise, you're welcome to reuse the project for any purpose.

  • Clone the repo on your machine.
  • Set up the environment following these instructions.
  • Test that you can compile the project.
starknet-compile contracts/ex01.cairo

starknet-cairo-101's People

Contributors

l-henri avatar 0xlucqs avatar hackintoshrao avatar omarespejel avatar barretodavid avatar dhruvkelawala avatar nekocl avatar othmansefraoui avatar nomisto avatar avimak avatar pybast avatar brozorec avatar ftupas avatar gabsn avatar giwook-han avatar jejomstark avatar lth-elm avatar mcarlomagno avatar qbzzt avatar rootulp avatar magicking avatar gaetbout avatar qd-qd avatar davidmitesh avatar rbkhmrcr avatar trevis-dev avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.