Skip to content

Commit

Permalink
feat: migrate listings and book to vocs
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Nov 2, 2024
1 parent 10360dd commit 7cb2db1
Show file tree
Hide file tree
Showing 98 changed files with 509 additions and 851 deletions.
16 changes: 8 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ All Markdown files (\*.md) MUST be edited in English. Follow these steps to work

Be sure to check for typos with `typos`:

```shell
```bash [Terminal]
cargo install typos-cli
typos src/
```
Expand Down Expand Up @@ -169,14 +169,14 @@ You should add the tests in the same file as the contract, using the `#[cfg(test

Here's a sample `lib.cairo` file:

```cairo
```rust
mod contract;
// any other modules you want
```

And in the `contract.cairo` file:

```cairo
```rust
// [!region contract]
// Write your contract here
// [!endregion contract]
Expand All @@ -193,7 +193,7 @@ You can use Starknet Foundry to write and run your tests.

You can add delimiting comments to select part of the code in the book.

```cairo
```rust
file.cairo:

a
Expand All @@ -206,14 +206,14 @@ c
Then, in the markdown file, you can use the following syntax to include only the code between the delimiting comments:

````markdown
```cairo
```rust
// [!include ~/listings/src/contract.cairo:region_name]
```
````

This will result in the following code being included in the book:

```cairo
```rust
b
```

Expand All @@ -222,11 +222,11 @@ To render code in tabs format you can use `:::code-group`. Example you can rende
````
:::code-group
```cairo [contract]
```rust [contract]
// [!include ~/listings/src/contract.cairo:contract]
```
```cairo [tests]
```rust [tests]
// [!include ~/listings/src/contract.cairo:tests]
```
Expand Down
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [
"listings/getting-started/*",
"listings/applications/*",
"listings/advanced-concepts/*",
"listings/templates/*",
"listings/cairo_cheatsheet",
]

[workspace.scripts]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions listings/getting-started/constructor/src/constructor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod ExampleConstructor {
}
// [!endregion contract]

// [!region tests]
#[cfg(test)]
mod tests {
use super::ExampleConstructor;
Expand Down Expand Up @@ -45,3 +46,6 @@ mod tests {
assert_eq!(name, 'bob');
}
}
// [!endregion tests]


3 changes: 3 additions & 0 deletions listings/getting-started/storage/src/minimal_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod Contract {
}
// [!endregion contract]

// [!region tests]
#[cfg(test)]
mod test {
use super::Contract;
Expand All @@ -20,4 +21,6 @@ mod test {
// Not much to test
}
}
// [!endregion tests]


Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ In practice, this means that the contract must implement the `SRC6` and `SRC5` i

## SNIP-6: SRC6 + SRC5

```cairo
```rust
/// @title Represents a call to a target contract
/// @param to The target contract address
/// @param selector The target function selector
Expand All @@ -19,7 +19,7 @@ struct Call {

The `Call` struct is used to represent a call to a function (`selector`) in a target contract (`to`) with parameters (`calldata`). It is available under the `starknet::account` module.

```cairo
```rust
/// @title SRC-6 Standard Account
trait ISRC6 {
/// @notice Execute a transaction through the account
Expand Down Expand Up @@ -52,7 +52,7 @@ Both `__execute__` and `__validate__` functions are exclusively called by the St

<!-- TODO replace with link to SRC5 example #109 -->

```cairo
```rust
/// @title SRC-5 Standard Interface Detection
trait ISRC5 {
/// @notice Query if a contract implements an interface
Expand All @@ -68,6 +68,6 @@ The interface identifiers of both `SRC5` and `SRC6` must be published with `supp

In this example, we will implement a minimal account contract that can validate and execute transactions.

```cairo
{{#rustdoc_include ../../../listings/advanced-concepts/simple_account/src/simple_account.cairo}}
```rust
// [!include ~/listings/advanced-concepts/simple_account/src/simple_account.cairo]
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Account Abstraction
# Account Abstraction on Starknet

An account is an unique entity that can send transactions, users usually use wallets to manage their accounts.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Some use cases:

## Minimal commit-reveal contract:

```cairo
{{#rustdoc_include ../../listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo:contract}}
```rust
// [!include ~/listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo:contract]
```

Usage example:
```cairo
{{#include ../../listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo:offchain}}
```rust
// [!include ~/listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo:offchain]
```

Some considerations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ This contract demonstrates Keccak hashing in Cairo to match Solidity's keccak256

For example:

```cairo
{{#include ../../listings/advanced-concepts/hash_solidity_compatible/src/contract.cairo}}
```rust
// [!include ~/listings/advanced-concepts/hash_solidity_compatible/src/contract.cairo]
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ In Cairo, it's possible to hash all types that can be converted to `felt252` sin

To hash a value, you first need to initialize a hash state with the `new` method of the `HashStateTrait`. Then, you can update the hash state with the `update` method. You can accumulate multiple updates if necessary. Finally, the `finalize` method returns the final hash value as a `felt252`.

```cairo
{{#rustdoc_include ../../listings/advanced-concepts/hash_trait/src/hash_trait.cairo:hash}}
```rust
// [!include ~/listings/advanced-concepts/hash_trait/src/hash_trait.cairo:hash]
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ Contract dispatcher call is synonymous to external calls in Solidity, while libr

For further reading: [Cairo book](https://book.cairo-lang.org/ch15-03-executing-code-from-another-class.html#library-calls)

```cairo
{{#rustdoc_include ../../listings/advanced-concepts/library_calls/src/library_call.cairo:library_dispatcher}}
```rust
// [!include ~/listings/advanced-concepts/library_calls/src/library_call.cairo:library_dispatcher]
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For example, if we want to store two `u8` values, we can use the first 8 bits of

Cairo provides a built-in store using packing that you can use with the `StorePacking` trait.

```cairo
```rust
trait StorePacking<T, PackedT> {
fn pack(value: T) -> PackedT;
fn unpack(value: PackedT) -> T;
Expand All @@ -25,11 +25,6 @@ This allows us to store the type `T` by first packing it into the type `PackedT`

Here's an example of storing a `Time` struct with two `u8` values using the `StorePacking` trait:

<<<<<<<< HEAD:src/advanced-concepts/optimisations/store_using_packing.md
```cairo
{{#include ../../../listings/advanced-concepts/store_using_packing/src/contract.cairo}}
========
```rust
// [!include ~/listings/advanced-concepts/store_using_packing/src/contract.cairo]
>>>>>>>> 261b110 (feat: migrate frontend framework from mdbook to vocs (#185)):pages/ch02/optimisations/store_using_packing.md
```
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
This is the Cairo adaptation of the [Solidity by Example - Verifying Signature](https://solidity-by-example.org/signature/).
Messages can be signed off chain and then verified on chain using a smart contract.

```cairo
{{#rustdoc_include ../../listings/advanced-concepts/ecdsa_verification/src/ecdsa_verification.cairo:contract}}
```rust
// [!include ~/listings/advanced-concepts/ecdsa_verification/src/ecdsa_verification.cairo:contract]
```
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
---
content:
horizontalPadding: 50px
width: 100%
verticalPadding: 30px
---

# Structs as mapping keys

In order to use structs as mapping keys, you can use `#[derive(Hash)]` on the struct definition. This will automatically generate a hash function for the struct that can be used to represent the struct as a key in a `Map`.

Consider the following example in which we would like to use an object of
type `Pet` as a key in a `Map`. The `Pet` struct has three fields: `name`, `age` and `owner`. We consider that the combination of these three fields uniquely identifies a pet.

<<<<<<<< HEAD:src/advanced-concepts/struct-mapping-key.md
```cairo
{{#include ../../listings/advanced-concepts/struct_as_mapping_key/src/contract.cairo}}
========
```rust
// [!include ~/listings/advanced-concepts/struct_as_mapping_key/src/contract.cairo]
>>>>>>>> 261b110 (feat: migrate frontend framework from mdbook to vocs (#185)):pages/ch02/struct-mapping-key.md
```
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
---
content:
horizontalPadding: 50px
width: 100%
verticalPadding: 30px
---

# Writing to any storage slot

On Starknet, a contract's storage is a map with \\( 2^{251} \\) slots, where each slot is a `felt252` which is initialized to 0.
Expand All @@ -15,11 +8,6 @@ This is useful when writing to storage variables that are not known at compile t

In the following example, we use the Poseidon hash function to compute the address of a storage variable. Poseidon is a ZK-friendly hash function that is cheaper and faster than Pedersen, making it an excellent choice for onchain computations. Once the address is computed, we use the storage syscalls to interact with it.

<<<<<<<< HEAD:src/advanced-concepts/write_to_any_slot.md
```cairo
{{#include ../../listings/advanced-concepts/write_to_any_slot/src/contract.cairo}}
========
```rust
// [!include ~/listings/advanced-concepts/write_to_any_slot/src/contract.cairo]
>>>>>>>> 261b110 (feat: migrate frontend framework from mdbook to vocs (#185)):pages/ch02/write_to_any_slot.md
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# AdvancedFactory: Crowdfunding

This is an example of an advanced factory contract that manages crowdfunding Campaign contracts created in the ["Crowdfunding" chapter](./crowdfunding.md). The advanced factory allows for a centralized creation and management of `Campaign` contracts on the Starknet blockchain, ensuring that they adhere to a standard interface and can be easily upgraded.
This is an example of an advanced factory contract that manages crowdfunding Campaign contracts created in the ["Crowdfunding" chapter](/applications/crowdfunding). The advanced factory allows for a centralized creation and management of `Campaign` contracts on the Starknet blockchain, ensuring that they adhere to a standard interface and can be easily upgraded.

Key Features
1. **Campaign Creation**: Users can create new crowdfunding campaigns with specific details such as title, description, goal, and duration.
2. **Campaign Management**: The factory contract stores and manages the campaigns, allowing for upgrades and tracking.
3. **Upgrade Mechanism**: The factory owner can update the implementation of the campaign contract, ensuring that all campaigns benefit from improvements and bug fixes.
- the factory only updates it's `Campaign` class hash and emits an event to notify any listeners, but the `Campaign` creators are in the end responsible for actually upgrading their contracts.

```cairo
{{#include ../../listings/applications/advanced_factory/src/contract.cairo:contract}}
```rust
// [!include ~/listings/applications/advanced_factory/src/contract.cairo:contract]
```
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ liquidity from the pool.
liquidity, users receive back tokens proportional to the number of shares
they burn.

```cairo
{{#include ../../listings/applications/constant_product_amm/src/contracts.cairo:ConstantProductAmmContract}}
```rust
// [!include ~/listings/applications/constant_product_amm/src/contracts.cairo:ConstantProductAmmContract]
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ Crowdfunding is a method of raising capital through the collective effort of man
6. After the campaign ends, the campaign creator can claim the funds if the campaign goal is reached.
7. Otherwise, campaign did not reach it's goal, pledgers can retrieve their funds.
8. The creator can at any point cancel the campaign for whatever reason and refund all of the pledgers.
9. The contract admin can upgrade the contract implementation, refunding all of the users and resetting the campaign state (we will use this in the [Advanced Factory chapter](./advanced_factory.md)).
9. The contract admin can upgrade the contract implementation, refunding all of the users and resetting the campaign state (we will use this in the [Advanced Factory chapter](/applications/advanced_factory)).

Because contract upgrades need to be able to refund all of the pledges, we need to be able to iterate over all of the pledgers and their amounts. Since iteration is not supported by `Map`, we need to create a custom storage type that will encompass pledge management. We use a component for this purpose.

```cairo
{{#include ../../listings/applications/crowdfunding/src/campaign/pledgeable.cairo:component}}
```rust
// [!include ~/listings/applications/crowdfunding/src/campaign/pledgeable.cairo:component]
```

Now we can create the `Campaign` contract.


```cairo
{{#include ../../listings/applications/crowdfunding/src/campaign.cairo:contract}}
```rust
// [!include ~/listings/applications/crowdfunding/src/campaign.cairo:contract]
```
17 changes: 0 additions & 17 deletions src/applications/erc20.md → pages/applications/erc20.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
---
content:
horizontalPadding: 50px
width: 100%
verticalPadding: 30px
---

# ERC20 Token

Contracts that follow the [ERC20 Standard](https://eips.ethereum.org/EIPS/eip-20) are called ERC20 tokens. They are used to represent fungible assets.

To create an ERC20 contract, it must implement the following interface:

<<<<<<<< HEAD:src/applications/erc20.md
```cairo
{{#include ../../listings/applications/erc20/src/token.cairo:interface}}
========
```rust
// [!include ~/listings/applications/erc20/src/token.cairo:interface]
>>>>>>>> 261b110 (feat: migrate frontend framework from mdbook to vocs (#185)):pages/ch01/erc20.md
```

In Starknet, function names should be written in _snake_case_. This is not the case in Solidity, where function names are written in _camelCase_.
The Starknet ERC20 interface is therefore slightly different from the Solidity ERC20 interface.

Here's an implementation of the ERC20 interface in Cairo:

<<<<<<<< HEAD:src/applications/erc20.md
```cairo
{{#include ../../listings/applications/erc20/src/token.cairo:erc20}}
========
```rust
// [!include ~/listings/applications/erc20/src/token.cairo:erc20]
>>>>>>>> 261b110 (feat: migrate frontend framework from mdbook to vocs (#185)):pages/ch01/erc20.md
```

There's several other implementations, such as the [Open Zeppelin](https://docs.openzeppelin.com/contracts-cairo/0.7.0/erc20) or the [Cairo By Example](https://cairo-by-example.com/examples/erc20/) ones.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Here's a quick summary of how it operates and what functionalities it supports:

### Visual example

![Diagram of the Merkle Tree](../assets/merkle_root.png)
![Diagram of the Merkle Tree](/merkle_root.png)

The above diagram represents a merkle tree.\
Each leaf node is the hash of some data.\
Expand All @@ -61,6 +61,6 @@ If we were to `verify` the `hash 6`, the merkle proof would need to contain the

The following implementation is the Cairo adaptation of the [Solidity by Example - Merkle Tree contract](https://solidity-by-example.org/app/merkle-tree/).

```cairo
{{#include ../../listings/applications/merkle_tree/src/contract.cairo}}
```rust
// [!include ~/listings/applications/merkle_tree/src/contract.cairo]
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Here's how it works:
- Participants can purchase NFTs at any time as long as the totalSupply has not been reached.
- The auction ends when either the totalSupply is reached or the duration has elapsed.

```cairo
{{#rustdoc_include ../../listings/applications/nft_dutch_auction/src/nft_dutch_auction.cairo:contract}}
```rust
// [!include ~/listings/applications/nft_dutch_auction/src/nft_dutch_auction.cairo:contract]
```
Loading

0 comments on commit 7cb2db1

Please sign in to comment.