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

Commit

Permalink
Update to the latest (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimryndin authored Jun 9, 2024
1 parent 9c872a2 commit a1b5818
Show file tree
Hide file tree
Showing 19 changed files with 348 additions and 365 deletions.
49 changes: 28 additions & 21 deletions src/ch01-00-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Starknet is a scalable layer 2 solution on Ethereum. This guide will walk you th

We will use the [Starknet Remix Plugin](https://github.com/NethermindEth/starknet-remix-plugin?tab=readme-ov-file) to compile, deploy, and interact with our smart contract. It's a great tool for getting started with Starknet development because you don't need to install anything on your computer.

1. Visit the [Remix IDE](https://remix.ethereum.org/#activate=Starknet&lang=en&optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.24+commit.e11b9ed9.js) website with the Starknet plugin enabled.
1. Visit the [Remix IDE](https://remix.ethereum.org/#activate=Starknet&lang=en&optimize=false&runs=200) website with the Starknet plugin enabled.

<img alt="Start" src="img/ch01-remix-start.png" class="center" style="width: 100%;" />

2. Then go to **settings** option and choose the Cairo version as shown in the image below. The latest version available in Remix is `v2.5.4`.
2. Then go to **settings** option and choose the Cairo version as shown in the image below. The latest version available in Remix is `v2.6.3`.

<img alt="Settings" src="img/ch01-remix-settings.png" class="center" style="width: 100%" />

3. Now click on the `file explorer` tab to check the sample project details. On the `Scarb.toml` file you can find the version of this sample project. Since we want to use version 2.5.4 for this project, we have to verify that it matches in our `Scarb.toml`, otherwise modify to the correct version, `starknet = "2.5.4"`.
3. Now click on the `file explorer` tab to check the sample project details. On the `Scarb.toml` file you can find the version of this sample project. Since we want to use version 2.6.3 for this project, we have to verify that it matches in our `Scarb.toml`, otherwise modify to the correct version, `starknet = "2.6.3"`.

<img alt="File default" src="img/ch01-remix-file-default.png" class="center" style="width: 100%;" />

Expand All @@ -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 Expand Up @@ -148,7 +148,7 @@ To compile using Remix:

- Post-compilation, an "artifacts" folder emerges containing the compiled contract in two distinct formats: Sierra (JSON file) and CASM. For Starknet deployment, Remix will use the Sierra file. Do not worry about this process for now; we will cover it in detail in a later chapter. For now, Remix will handle the compilation and deployment for us.

<img alt="Artifacts folder after compilation" src="img/ch01-remix-file.png" class="center" style="width: 100%;" />
<img alt="Artifacts folder after compilation" src="img/ch01-remix-artifacts.png" class="center" style="width: 100%;" />

<span class="caption">Artifacts folder after compilation</span>

Expand Down Expand Up @@ -212,12 +212,23 @@ Post-deployment, Remix's terminal will send various logs. These logs provide cru
"contract_address": [
"0x699952dc736661d0ed573cd2b0956c80a1602169e034fdaa3515bfbc36d6410"
]
...
"data": [
}

{
"type": "DEPLOY",
...
"events": [
{
...,
"data": [
...,
"0x6b0ee6f418e47408cf56c6f98261c1c5693276943be12db9597b933d363df",
...
...
]
},
...
],
...
}
```

Expand All @@ -242,11 +253,9 @@ With the contract now active on the development network, interaction becomes pos

```json
{
"resp": {
"result": [
"resp": [
"0x6b0ee6f418e47408cf56c6f98261c1c5693276943be12db9597b933d363df"
]
},
],
"contract": "lib.cairo",
"function": "get_owner"
}
Expand All @@ -264,15 +273,13 @@ This call currently doesn't spend gas because the function does not change the s
- Enter this address into the `new_owner` field. (For this, use any address from the "Devnet account selection" listed in the Environment tab.)
- Click the **"Call"** button. The terminal then showcases the transaction hash indicating the contract's state alteration. Since we are altering the contract's state this type of interaction is called an "invoke" and needs to be signed by the account that is calling the function.

For these transactions, the terminal logs will exhibit a "status" variable, indicating the transaction's fate. If the status is "ACCEPTED_ON_L2", it means the Sequencer, the component that receives and processes transactions, has accepted the transaction, which is now awaiting inclusion in an upcoming block. However, a "REJECTED" status signifies the Sequencer's disapproval, and the transaction won't feature in the upcoming block. More often than not, this transaction gains acceptance, leading to a contract state modification. See [this chapter](https://book.starknet.io/ch03-00-architecture.html) for more on Starknet's architecture and the Sequencer. On calling the **`get_owner`** function again we get this:
For these transactions, the terminal logs will exhibit a `finality_status` variable, indicating the transaction's fate. If the status is "ACCEPTED_ON_L2", it means the Sequencer, the component that receives and processes transactions, has accepted the transaction, which is now awaiting inclusion in an upcoming block. However, a "REJECTED" status signifies the Sequencer's disapproval, and the transaction won't feature in the upcoming block. More often than not, this transaction gains acceptance, leading to a contract state modification. See [this chapter](https://book.starknet.io/ch03-00-architecture.html) for more on Starknet's architecture and the Sequencer. On calling the **`get_owner`** function again we get this:

```json
{
"resp": {
"result": [
"resp": [
"0x5495d56633745aa3b97bdb89c255d522e98fd2cb481974efe898560839aa472"
]
},
],
"contract": "lib.cairo",
"function": "get_owner"
}
Expand All @@ -297,7 +304,7 @@ Both are reliable Starknet wallets offering enhanced security and accessibility

1. Install the recommended chrome/brave extension for your chosen wallet.
2. Follow your wallet provider's instructions to deploy your account.
3. Use the [Starknet Faucet](https://faucet.goerli.starknet.io/) to fund your account.
3. Use the [Starknet Faucet](https://starknet-faucet.vercel.app/) to fund your account with ETH.
4. Deploy the account to the network. This usually takes around 10 seconds.

Once set up, you're ready to deploy your smart contracts to the Starknet Testnet.
Expand All @@ -310,10 +317,10 @@ Once set up, you're ready to deploy your smart contracts to the Starknet Testnet

<img alt="Environment selection" src="img/ch01-remix-wallet.png" class="center" style="width: 100%; max-width: 300px;" />

You can monitor transaction hashes and addresses using any Starknet block explorers like:
You can monitor transaction hashes and addresses using any Starknet block explorers like (pay attention to the selected network - either Mainnet or Sepolia):

- [Starkscan](https://testnet.starkscan.co/)
- [Voyager](https://goerli.voyager.online/)
- [Starkscan](https://sepolia.starkscan.co/)
- [Voyager](https://sepolia.voyager.online/)
- [ViewBlock](https://viewblock.io/starknet)
- [oklink](https://www.oklink.com/starknet)

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.
Loading

0 comments on commit a1b5818

Please sign in to comment.