Skip to content

Commit

Permalink
feat: revised book structure
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Oct 11, 2023
1 parent fe95c46 commit e3ae137
Show file tree
Hide file tree
Showing 125 changed files with 118 additions and 96 deletions.
9 changes: 5 additions & 4 deletions book.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[book]
authors = ["msaug"]
authors = ["msaug", "julio4"]
language = "en"
multilingual = false
src = "src"
title = "Starknet by Example"

[build]
# Eventual translations
# extra-watch-dirs = ["po"]
extra-watch-dirs = ["listings"]

[preprocessor.gettext]
after = ["links"]

[output.html]
git-repository-url = "https://github.com/NethermindEth/StarknetByExample/"
edit-url-template = "https://github.com/NethermindEth/StarknetByExample/edit/main/{path}"
playground.runnable = false
playground.runnable = false
fold.enable = true
fold.level = 2
63 changes: 32 additions & 31 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
Summary

- [Starknet by Example](./starknet-by-example.md)

- [Basics of Smart Contracts in Cairo](./ch00/introduction.md)

- [Constructor](./ch00/constructor.md)
- [Visibility and Mutability](./ch00/visibility-mutability.md)
- [Events](./ch00/events.md)
- [Variables](./ch00/variables.md)
- [Mappings](./ch00/mappings.md)
- [Counter contract example](./ch00/counter.md)
- [Storing Custom Types](./ch00/storing-custom-types.md)
- [Custom types in entrypoints](./ch00/custom-types-in-entrypoints.md)
- [Errors](./ch00/errors.md)
- [Calling other contracts](./ch00/calling_other_contracts.md)
- [Contract interfaces and Traits generation](./ch00/interfaces-traits.md)
- [Contract testing](./ch00/contract-testing.md)

- [Advanced concepts](./ch01/advanced-concepts.md)

- [Writing to any storage slot](./ch01/write_to_any_slot.md)
- [Storing Arrays](./ch01/storing_arrays.md)
- [Struct as mapping key](./ch01/struct-mapping-key.md)

- [Applications](./ch02/applications.md)

- [Upgradeable Contract](./ch02/upgradeable_contract.md)
- [Defi Vault](./ch02/simple_vault.md)

- [Optimisations](./ch03/optimisations.md)

- [Storage Optimisations](./ch03/store_using_packing.md)
[Introduction](./starknet-by-example.md)

<!-- ch00 -->
# Getting Started
<!-- - [Local environnement setup](./ch00/env_setup.md) -->
- [Basics of a Starknet contract](./ch00/basics/introduction.md)
- [Variables](./ch00/basics/variables.md)
- [Constructor](./ch00/basics/constructor.md)
- [Visibility and Mutability](./ch00/basics/visibility-mutability.md)
- [Counter contract example](./ch00/basics/counter.md)
- [Mappings](./ch00/basics/mappings.md)
- [Errors](./ch00/basics/errors.md)
- [Events](./ch00/basics/events.md)
- [Storing Custom Types](./ch00/basics/storing-custom-types.md)
- [Custom types in entrypoints](./ch00/basics/custom-types-in-entrypoints.md)
- [Interfaces and interacting with contracts](./ch00/interacting/interacting.md)
- [Contract interfaces and Traits generation](./ch00/interacting/interfaces-traits.md)
- [Calling other contracts](./ch00/interacting/calling_other_contracts.md)
- [Testing contracts](./ch00/testing/contract-testing.md)

<!-- ch01 -->
# Applications examples
- [Upgradeable Contract](./ch01/upgradeable_contract.md)
- [Defi Vault](./ch01/simple_vault.md)

<!-- ch02 -->
# Advanced concepts
- [Writing to any storage slot](./ch02/write_to_any_slot.md)
- [Storing Arrays](./ch02/storing_arrays.md)
- [Struct as mapping key](./ch02/struct-mapping-key.md)
- [Optimisations](./ch02/optimisations/optimisations.md)
- [Storage Optimisations](./ch02/optimisations/store_using_packing.md)
4 changes: 2 additions & 2 deletions src/ch00/constructor.md → src/ch00/basics/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Constructors are a special type of function that runs only once when deploying a
Here's a simple example that demonstrates how to initialize the state of a contract on deployment by defining logic inside a constructor.

```rust
{{#include ../../listings/ch00-introduction/constructor/src/constructor.cairo}}
{{#include ../../../listings/ch00-getting-started/constructor/src/constructor.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x017fd6558e67451dA583d123D77F4e2651E91502D08F8F8432355293b11e1f8F) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/constructor/src/constructor.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x017fd6558e67451dA583d123D77F4e2651E91502D08F8F8432355293b11e1f8F) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/constructor/src/constructor.cairo).
4 changes: 2 additions & 2 deletions src/ch00/counter.md → src/ch00/basics/counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Here's how it works:
- When a user calls 'decrement', the contract decrements the counter by 1.

```rust
{{#include ../../listings/ch00-introduction/counter/src/counter.cairo}}
{{#include ../../../listings/ch00-getting-started/counter/src/counter.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x01664a69Fe701a1df7Bb0ae4A353792d0cf4E27146ee860075cbf6108b1D5718) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/counter/src/counter.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x01664a69Fe701a1df7Bb0ae4A353792d0cf4E27146ee860075cbf6108b1D5718) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/counter/src/counter.cairo).
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Using custom types in entrypoints requires our type to implement the `Serde` tra
Thankfully, we can just derive the `Serde` trait for our custom type.

```rust
{{#include ../../listings/ch00-introduction/custom_type_serde/src/contract.cairo}}
{{#include ../../../listings/ch00-getting-started/custom_type_serde/src/contract.cairo}}
```

Play with this contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/custom_type_serde/src/contract.cairo).
Play with this contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/custom_type_serde/src/contract.cairo).
12 changes: 6 additions & 6 deletions src/ch00/errors.md → src/ch00/basics/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ To throw an error, use the `assert` or `panic` functions:
Here's a simple example that demonstrates the use of these functions:

```rust
{{#include ../../listings/ch00-introduction/errors/src/simple_errors.cairo}}
{{#include ../../../listings/ch00-getting-started/errors/src/simple_errors.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x0022664463FF0b711CC9B549a9E87d65A0882bB1D29338C4108696B8F2216a40) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/errors/src/simple_errors.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x0022664463FF0b711CC9B549a9E87d65A0882bB1D29338C4108696B8F2216a40) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/errors/src/simple_errors.cairo).

## Custom errors

You can make error handling easier by defining your error codes in a specific module.

```rust
{{#include ../../listings/ch00-introduction/errors/src/custom_errors.cairo}}
{{#include ../../../listings/ch00-getting-started/errors/src/custom_errors.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x0501CD5da5B453a18515B5A20b8029bd7583DFE7a399ad9f79c284F7829e4A57) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/errors/src/custom_errors.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x0501CD5da5B453a18515B5A20b8029bd7583DFE7a399ad9f79c284F7829e4A57) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/errors/src/custom_errors.cairo).

## Vault example

Here's another example that demonstrates the use of errors in a more complex contract:

```rust
{{#include ../../listings/ch00-introduction/errors/src/vault_errors.cairo}}
{{#include ../../../listings/ch00-getting-started/errors/src/vault_errors.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x020C2da26F42A28Ef54ED428eF1810FE433784b055f9bF315C5d992b1579C268) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/errors/src/vault_errors.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x020C2da26F42A28Ef54ED428eF1810FE433784b055f9bF315C5d992b1579C268) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/errors/src/vault_errors.cairo).
4 changes: 2 additions & 2 deletions src/ch00/events.md → src/ch00/basics/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ An event is defined as struct that derives the `#[starknet::Event]` trait. The f
Here's a simple example of a contract using events that emit an event each time a counter is incremented by the "increment" function:

```rust
{{#include ../../listings/ch00-introduction/events/src/counter.cairo}}
{{#include ../../../listings/ch00-getting-started/events/src/counter.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x022e3B59518EA04aBb5da671ea04ecC3a154400f226d2Df38eFE146741b9E2F6) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/events/src/counter.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x022e3B59518EA04aBb5da671ea04ecC3a154400f226d2Df38eFE146741b9E2F6) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/events/src/counter.cairo).
File renamed without changes.
4 changes: 2 additions & 2 deletions src/ch00/mappings.md → src/ch00/basics/mappings.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Some additional notes:
- In mappings, the address of the value at key `k_1,...,k_n` is `h(...h(h(sn_keccak(variable_name),k_1),k_2),...,k_n)` where `` is the Pedersen hash and the final value is taken `mod2251−256`. You can learn more about the contract storage layout in the [Starknet Documentation](https://docs.starknet.io/documentation/architecture_and_concepts/Contracts/contract-storage/#storage_variables)

```rust
{{#include ../../listings/ch00-introduction/mappings/src/mappings.cairo}}
{{#include ../../../listings/ch00-getting-started/mappings/src/mappings.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x06214AB4c23Cc545bf2221D465eB83aFb7412779AD498BD48a724B3F645E3505) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/mappings/src/mappings.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x06214AB4c23Cc545bf2221D465eB83aFb7412779AD498BD48a724B3F645E3505) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/mappings/src/mappings.cairo).
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
While native types can be stored in a contract's storage without any additional work, custom types require a bit more work. This is because at compile time, the compiler does not know how to store custom types in storage. To solve this, we need to implement the `Store` trait for our custom type. Hopefully, we can just derive this trait for our custom type - unless it contains arrays or dictionaries.

```rust
{{#include ../../listings/ch00-introduction/storing_custom_types/src/contract.cairo}}
{{#include ../../../listings/ch00-getting-started/storing_custom_types/src/contract.cairo}}
```

Play with this contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/storing_custom_types/src/contract.cairo).
Play with this contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/storing_custom_types/src/contract.cairo).
12 changes: 6 additions & 6 deletions src/ch00/variables.md → src/ch00/basics/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Local variables are stored in memory and are not stored on the blockchain. This
Here's a simple example of a contract with only local variables:

```rust
{{#include ../../listings/ch00-introduction/variables/src/local_variables.cairo}}
{{#include ../../../listings/ch00-getting-started/variables/src/local_variables.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x015B3a10F9689BeD741Ca3C210017BC097122CeF76f3cAA191A20ff8b9b56b96) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/variables/src/local_variables.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x015B3a10F9689BeD741Ca3C210017BC097122CeF76f3cAA191A20ff8b9b56b96) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/variables/src/local_variables.cairo).

## Storage Variables

Expand All @@ -36,9 +36,9 @@ On the other hand, you can read state variables, for free, without any transacti
Here's a simple example of a contract with one storage variable:

```rust
{{#include ../../listings/ch00-introduction/variables/src/storage_variables.cairo}}
{{#include ../../../listings/ch00-getting-started/variables/src/storage_variables.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x06eA827B32875483709b785A7F9e846a52776Cd8D42C3fE696218c2624b0DCCa) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/variables/src/storage_variables.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x06eA827B32875483709b785A7F9e846a52776Cd8D42C3fE696218c2624b0DCCa) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/variables/src/storage_variables.cairo).

## Global Variables

Expand All @@ -49,6 +49,6 @@ In Starknet, you can access global variables by using specific functions contain
For example, the `get_caller_address` function returns the address of the caller of the current transaction, and the `get_contract_address` function returns the address of the current contract.

```rust
{{#include ../../listings/ch00-introduction/variables/src/global_variables.cairo}}
{{#include ../../../listings/ch00-getting-started/variables/src/global_variables.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x05bD2F3943bd4e030f85678b55b2EC2C1be939e32388530FB20ED967B3Be433F) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/variables/src/global_variables.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x05bD2F3943bd4e030f85678b55b2EC2C1be939e32388530FB20ED967B3Be433F) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/variables/src/global_variables.cairo).
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Internal functions can't be called externally, but the same principle applies re
Let's take a look at a simple example contract to see these in action:

```rust
{{#include ../../listings/ch00-introduction/visibility/src/visibility.cairo}}
{{#include ../../../listings/ch00-getting-started/visibility/src/visibility.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x0071dE3093AB58053b0292C225aa0eED40293e7694A0042685FF6D813d39889F) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/visibility/src/visibility.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x0071dE3093AB58053b0292C225aa0eED40293e7694A0042685FF6D813d39889F) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/visibility/src/visibility.cairo).
5 changes: 5 additions & 0 deletions src/ch00/env_setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Local environnement setup

The current version of this book use `{{#include ../../.tool-versions}}`

<!-- TODO -->
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ The other way is to use the `starknet::call_contract_syscall` syscall yourself.
In order to call other contracts using dispatchers, you will need to define the called contract's interface as a trait annotated with the `#[starknet::interface]` attribute, and then import the `IContractDispatcher` and `IContractDispatcherTrait` items in your contract.

```rust
{{#include ../../listings/ch00-introduction/calling_other_contracts/src/callee.cairo}}
{{#include ../../../listings/ch00-getting-started/calling_other_contracts/src/callee.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x015c3Bb6D0DE26b64FEAF9A8f4655CfADb5c128bF4510398972704ee12775DB1) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/calling_other_contracts/src/callee.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x015c3Bb6D0DE26b64FEAF9A8f4655CfADb5c128bF4510398972704ee12775DB1) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/calling_other_contracts/src/callee.cairo).

```rust
{{#include ../../listings/ch00-introduction/calling_other_contracts/src/caller.cairo}}
{{#include ../../../listings/ch00-getting-started/calling_other_contracts/src/caller.cairo}}
```
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x05fa8aF796343d2f22c53C17149386b67B7AC4aB52D9e308Aa507C185aA44778) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-introduction/calling_other_contracts/src/caller.cairo).
Visit contract on [Voyager](https://goerli.voyager.online/contract/0x05fa8aF796343d2f22c53C17149386b67B7AC4aB52D9e308Aa507C185aA44778) or play with it in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch00-getting-started/calling_other_contracts/src/caller.cairo).
3 changes: 3 additions & 0 deletions src/ch00/interacting/interacting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Interacting with contracts

In this chapter, we will see how to interact with contracts.
Loading

0 comments on commit e3ae137

Please sign in to comment.