Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
ch02
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimryndin committed Jun 8, 2024
1 parent 4a390db commit f95c63b
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 345 deletions.
2 changes: 1 addition & 1 deletion src/ch01-00-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The script below is a simple `Ownable` contract pattern written in Cairo for Sta

### Cairo Example Contract

```rust
```rust,noplayground
use starknet::ContractAddress;
#[starknet::interface]
Expand Down
4 changes: 2 additions & 2 deletions src/ch02-00-starknet-tooling.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ In this chapter, you’ll explore:

- **Frameworks:** Build using Starknet-Foundry

- **SDKs:** Discover multi-language support through Starknet.js,
Starknet-rs, Starknet_py, and Cairo
- **SDKs:** Discover multi-language support through [Starknet.js](https://github.com/starknet-io/starknet.js),
[Starknet.rs](https://github.com/xJonathanLEI/starknet-rs), [Starknet.py](https://github.com/software-mansion/starknet.py), and [Cairo](https://github.com/starkware-libs/cairo)

- **Front-end Development:** Use Starknet.js and React

Expand Down
16 changes: 10 additions & 6 deletions src/ch02-01-basic-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Essential tools to install:
manager that compiles code to [Sierra](https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/cairo-and-sierra), a mid-level language between
Cairo and CASM.

3. [Katana](https://github.com/dojoengine/dojo) - Katana is a Starknet node, built for local development.
3. [Katana](https://book.dojoengine.org/toolchain/katana) - Katana is a Starknet node, built for local development.

For support or queries, visit our [GitHub
Issues](https://github.com/starknet-edu/starknetbook/issues) or contact
Expand All @@ -36,6 +36,8 @@ starkli --version

To upgrade Starkli, simply repeat the steps.

*Note:* to install from source refer to the [installation instructions](https://book.starkli.rs/installation#install-from-source)

## Scarb Package Manager Installation

Scarb is also Cairo's package manager and is heavily inspired by [Cargo](https://doc.rust-lang.org/cargo/),
Expand Down Expand Up @@ -69,13 +71,13 @@ asdf plugin add scarb
This will allow you to download specific versions:

```bash
asdf install scarb 2.5.4
asdf install scarb 2.6.4
```

and set a global version:

```bash
asdf global scarb 2.5.4
asdf global scarb 2.6.4
```

Otherwise, you can simply run the following command in your terminal, and follow the onscreen instructions. This
Expand All @@ -89,9 +91,9 @@ curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh

```bash
scarb --version
scarb 2.5.4 (28dee92c8 2024-02-14)
cairo: 2.5.4 (https://crates.io/crates/cairo-lang-compiler/2.5.4)
sierra: 1.4.0
scarb 2.6.4 (c4c7c0bac 2024-03-19)
cairo: 2.6.3 (https://crates.io/crates/cairo-lang-compiler/2.6.3)
sierra: 1.5.0
```

For Windows, follow manual setup in the [Scarb
Expand All @@ -114,4 +116,6 @@ katana --version

To upgrade Katana, rerun the installation command.

*Note:* to install from source refer to the [installation instructions](https://book.dojoengine.org/toolchain/katana#installing-from-source)

You are now set to code in Cairo and deploy to Starknet.
2 changes: 1 addition & 1 deletion src/ch02-02-01-Starknet-devnet-rs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ don’t, refer to Basic Installation in this chapter.

- Install [rust](#https://www.rust-lang.org/tools/install)
- The required Rust version is specified in [rust-toolchain.toml](#https://github.com/0xSpaceShard/starknet-devnet-rs/blob/main/rust-toolchain.toml) and handled automatically by cargo.
- Run `rustc --version ` to comfirm the rust version.
- Run `rustc --version` to comfirm the rust version.

## Run as a binary

Expand Down
42 changes: 21 additions & 21 deletions src/ch02-02-starkli-scarb-katana.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ don’t, refer to Basic Installation in this chapter.
**Important:** Before we proceed with this example, please ensure that the versions of both `katana` and `starkli` match the specified versions provided below.

```console
katana --version # 0.6.0-alpha.7
starkli --version # 0.2.8 (f59724e)
katana --version # 0.7.0-alpha.5
starkli --version # 0.2.9 (0535f44)
```

If this is not your case, you have to install them like this:

```sh
dojoup -v 0.6.0-alpha.7
starkliup -v 0.2.8
dojoup -v 0.7.0-alpha.5
starkliup -v 0.2.9
```

Now begin by initiating a Scarb project:
Expand All @@ -49,14 +49,14 @@ Amend the `Scarb.toml` file to integrate the `starknet` dependency and introduce

```toml
[dependencies]
starknet = ">=2.5.4"
starknet = ">=2.6.3"

[[target.starknet-contract]]
```

For streamlined Starkli command execution, establish environment variables. Two primary variables are essential:

- One for your account, a pre-funded account on the local development network
- One for your account, a pre-funded account on the local development network (you can refer to [this chapter](/ch01-00-getting-started.html#smart-wallet-setup))
- Another for designating the network, specifically the local katana devnet

In the `src/` directory, create a `.env` file with the following:
Expand All @@ -77,7 +77,7 @@ Deploying a Starknet smart contract requires two primary steps:

Begin with the `src/lib.cairo` file, which provides a foundational template. Remove its contents and insert the following:

```rust
```rust,noplayground
#[starknet::interface]
trait IHello<T> {
fn get_name(self: @T) -> felt252;
Expand Down Expand Up @@ -134,7 +134,7 @@ katana
To declare your contract, execute:

```bash
starkli declare target/dev/my_contract_hello.contract_class.json
starkli declare target/dev/my_contract_hello.contract_class.json --compiler-version 2.6.2
```

Facing an "Error: Invalid contract class"? It indicates a version mismatch between Scarb's compiler and Starkli. Refer to the earlier steps to sync the versions. Typically, Starkli supports compiler versions approved by mainnet, even if the most recent Scarb version isn't compatible.
Expand All @@ -144,7 +144,7 @@ unique hash serves as the identifier for your contract class within
Starknet. For example:

```bash
Class hash declared: 0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418
Class hash declared: 0x01df629957ef5568db6dab46618947063e7a28dad5e1ffbac97ba643d3c861cc
```

Consider this hash as the contract class's _address_.
Expand All @@ -153,7 +153,7 @@ If you try to declare an already existing contract class, don't fret. Just proce

```bash
Not declaring class as its already declared. Class hash:
0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418
0x01df629957ef5568db6dab46618947063e7a28dad5e1ffbac97ba643d3c861cc
```

## Deploying Starknet Smart Contracts
Expand Down Expand Up @@ -193,17 +193,18 @@ Now deploy using a class hash and constructor input:

```bash
starkli deploy \
0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418 \
0x01df629957ef5568db6dab46618947063e7a28dad5e1ffbac97ba643d3c861cc \
0x737461726b6e6574626f6f6b
```

After running, expect an output similar to:

```bash
Deploying class 0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418 with salt 0x054645c0d1e766ddd927b3bde150c0a3dc0081af7fb82160c1582e05f6018794...
The contract will be deployed at address 0x07cdd583619462c2b14532eddb2b169b8f8d94b63bfb5271dae6090f95147a44
Contract deployment transaction: 0x00413d9638fecb75eb07593b5c76d13a68e4af7962c368c5c2e810e7a310d54c
Contract deployed: 0x07cdd583619462c2b14532eddb2b169b8f8d94b63bfb5271dae6090f95147a44
Deploying class 0x01df629957ef5568db6dab46618947063e7a28dad5e1ffbac97ba643d3c861cc with salt 0x07976cd9b291256305e866ed7fcce7dec2930d939ea8b62ae6da7f5c6d344306...
The contract will be deployed at address 0x0111595c7be27d2096ecfb49eee217d562303562cbfbfdefcc6f84936967e8f6
Contract deployment transaction: 0x01a699a18c1586f72f467bb8a144d74d646e90c3c9ff88bc58bcf8cf831c2ad5
Contract deployed:
0x0111595c7be27d2096ecfb49eee217d562303562cbfbfdefcc6f84936967e8f6
```

## Interacting with Starknet Contracts
Expand All @@ -220,12 +221,11 @@ requires no arguments:

```bash
starkli call \
<CONTRACT_ADDRESS> \
0x0111595c7be27d2096ecfb49eee217d562303562cbfbfdefcc6f84936967e8f6 \
get_name
```

Replace `<CONTRACT_ADDRESS>` with the address of your contract. The
command will return the owner’s address, which was initially set during
The command will return the owner’s address, which was initially set during
the contract’s deployment:

```bash
Expand All @@ -252,14 +252,14 @@ To alter the contract's state, use the `invoke` command. For instance, if you wa

```bash
starkli invoke \
<CONTRACT_ADDRESS> \
0x0111595c7be27d2096ecfb49eee217d562303562cbfbfdefcc6f84936967e8f6 \
set_name \
<felt252>
```

Where:

- **`<CONTRACT_ADDRESS>`** is the address of your contract.
- **`0x0111595c7be27d2096ecfb49eee217d562303562cbfbfdefcc6f84936967e8f6`** is the address of your contract (from output of `starkli deploy`).
- **`<felt252>`** is the new value for the **`name`** field, in felt252 format.

For example, to update the name to "Omar", first convert the string "Omar" to its felt252 representation:
Expand All @@ -277,7 +277,7 @@ This will return:
Now, proceed with the `invoke` command:

```bash
starkli invoke 0x07cdd583619462c2b14532eddb2b169b8f8d94b63bfb5271dae6090f95147a44 set_name 0x4f6d6172
starkli invoke 0x0111595c7be27d2096ecfb49eee217d562303562cbfbfdefcc6f84936967e8f6 set_name 0x4f6d6172
```

Bravo! You've adeptly modified and interfaced with your Starknet contract.
43 changes: 27 additions & 16 deletions src/ch02-03-scarb.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ we can use, and a `Scarb.toml` file in the top-level directory.

Within the `Scarb.toml` file, you might have:

```toml
[package]
name = "my_package"
version = "0.1.0"

[dependencies]
starknet = ">=2.0.1"
starknet = ">=2.6.3"
snips = { path = "snips" }
```

Here starknet and snips are the dependencies of the package. The
`starknet` dependency is hosted on the Scarb registry (we do not need to
Expand All @@ -103,29 +105,35 @@ inside, and initialize a new Git repository with a `.gitignore` file.
Upon opening `Scarb.toml` in a text editor, you should see something
similar to the code snippet below:

```toml
[package]
name = "hello_scarb"
version = "0.1.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
[dependencies]
# foo = { path = "vendor/foo" }
```

# Building a Scarb Project

Clear all content in `src/lib.cairo` and replace with the following:

// src/lib.cairo
mod hello_scarb;

```rust,noplayground
// src/lib.cairo
mod hello_scarb;
```

Next, create a new file titled `src/hello_scarb.cairo` and add the
following:

// src/hello_scarb.cairo
use debug::PrintTrait;
fn main() {
'Hello, Scarb!'.print();
}
```rust,noplayground
// src/hello_scarb.cairo
fn main() {
println!("Hello, Scarb!");
}
```

In this instance, the `lib.cairo` file contains a module declaration
referencing _hello_scarb_, which includes the _hello_scarb.cairo_
Expand Down Expand Up @@ -178,7 +186,7 @@ After adding the dependency, remember to save the file.
Alternatively, you can use the `scarb add` command to add dependencies
to your project. Open your terminal and execute the following command:

$ scarb add alexandria_math --git https://github.com/keep-starknet-strange/alexandria.git
scarb add alexandria_math --git https://github.com/keep-starknet-strange/alexandria.git

This command will add the alexandria_math dependency from the specified
Git repository to your project.
Expand All @@ -197,12 +205,15 @@ For example, let’s assume you have added the alexandria_math
dependency. Now, you can import and utilize functions from the
alexandria_math library in your `src/hello_scarb.cairo` file:

// src/hello_scarb.cairo
use alexandria_math::fibonacci;

fn main() -> felt252 {
fibonacci::fib(0, 1, 10)
}
```rust,noplayground
// src/hello_scarb.cairo
use alexandria_math::fibonacci;
fn main() -> felt252 {
fibonacci::fib(0, 1, 10)
}
```

In the above example, we import the fibonacci function from the
alexandria_math library and utilize it in the main function.
Expand Down Expand Up @@ -235,11 +246,11 @@ commands:

One of the most important features since `scarb 2.3.0` version is `Components`. Think of components as Lego blocks. They allow you to enrich your contracts by plugging in a module that you or someone else wrote.

Lets see and example. Recover our project from [Testnet Deployment](./ch02-05-testnet-deployment.md) section. We used the `Ownable-Starknet` example to interact with the blockchain, now we are going to use the same project, but we will refactor the code in order to use `components`
Lets see and example. Recover our project from [Getting Started](./ch01-00-getting-started.html#cairo-example-contract) section. We used the `Ownable-Starknet` example to interact with the blockchain, now we are going to use the same project, but we will refactor the code in order to use `components`

This is how our smart contract looks now

```rust
```rust,noplayground
// ...rest of the code
#[starknet::component]
Expand Down
17 changes: 2 additions & 15 deletions src/ch02-04-katana.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ subchapter. The `starknet-devnet` is a public testnet, maintained by the
these tools offer an effective environment for development and testing.

For an example of how to use `katana` to deploy and interact with a
contract, see the introduction subchapter of this Chapter or a voting contract example in [The Cairo Book](https://book.cairo-lang.org/ch99-01-04-01-voting-contract.html).
contract, see the introduction subchapter of this Chapter or a voting contract example in [The Cairo Book](https://book.cairo-lang.org/ch16-06-01-deploying-and-interacting-with-a-voting-contract.html).

## Understanding RPC in Starknet

Expand All @@ -33,20 +33,7 @@ used.

## Getting Started with Katana

To install Katana, use the `dojoup` installer from the command line:

```bash
curl -L https://install.dojoengine.org | bash
dojoup
```

After restarting your terminal, verify the installation with:

```bash
katana --version
```

To upgrade Katana, rerun the installation command.
To install Katana refer to [this chapter](/ch02-01-basic-installation.html#katana-node-installation).

To initialize a local Starknet node, execute the following command:

Expand Down
Loading

0 comments on commit f95c63b

Please sign in to comment.