Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Scarb workspace, CI improvements #141

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ output

# Editors tmp files.
*~
.idea/
.idea/

# Others
.snfoundry_cache
115 changes: 105 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@ Join the telegram channel: https://t.me/StarknetByExample

Please note we have a code of conduct, please follow it in all your interactions with the project.

## Table of Contents

- [Contributing](#contributing)
- [Table of Contents](#table-of-contents)
- [Setup](#setup)
- [MdBook](#mdbook)
- [Adding a new chapter](#adding-a-new-chapter)
- [Adding a new Cairo program](#adding-a-new-cairo-program)
- [Verification script](#verification-script)
- [Tests](#tests)
- [Use of anchor](#use-of-anchor)
- [Translations](#translations)
- [Initiate a new translation for your language](#initiate-a-new-translation-for-your-language)
- [Code of Conduct](#code-of-conduct)
- [Our Pledge](#our-pledge)
- [Our Standards](#our-standards)
- [Our Responsibilities](#our-responsibilities)
- [Scope](#scope)
- [Enforcement](#enforcement)
- [Attribution](#attribution)

## Setup

1. Clone this repository.

2. Rust related packages:
- Install toolchain providing `cargo` using [rustup](https://rustup.rs/).
- Install [mdBook](https://rust-lang.github.io/mdBook/guide/installation.html) and the required extension with `cargo install mdbook mdbook-last-changed`.

3. Install `scarb` using [asdf](https://asdf-vm.com/) with `asdf install`. Alternatively, you can install `scarb` manually by following the instructions [here](https://docs.swmansion.com/scarb/).

## MdBook

All the Markdown files **MUST** be edited in english. To work locally in english:

- Start a local server with `mdbook serve` and visit [localhost:3000](http://localhost:3000) to view the book.
You can use the `--open` flag to open the browser automatically: `mdbook serve --open`.

- Make changes to the book and refresh the browser to see the changes.

- Open a PR with your changes.

## Adding a new chapter

To add a new chapter, create a new markdown file in the `src` directory. All the Markdown files **MUST** be edited in english. In order to add them to the book, you need to add a link to it in the `src/SUMMARY.md` file.
Expand All @@ -14,19 +56,57 @@ Do not write directly Cairo program inside the markdown files. Instead, use code

## Adding a new Cairo program

You can add or modify examples in the `listings` directory. Each listing is a scarb project. You can use `scarb init` to create a new scarb project (You can remove the generated git repository, `rm -rf .git`). Here's the minimal `Scarb.toml` configuration:
You can add or modify examples in the `listings` directory. Each listing is a scarb project.
You can find a template of a blank scarb project in the `listings/template` directory.
(You can also use `scarb init` to create a new scarb project, but be sure to remove the generated git repository)

Here's the required `Scarb.toml` configuration:

```toml
[package]
name = "pkg_name"
version = "0.1.0"
version.workspace = true

# Specify that this can be used as a dependency in another scarb project:
[lib]

[dependencies]
starknet = ">=2.2.0"
starknet.workspace = true
# Uncomment the following lines if you want to use additional dependencies:
# Starknet Foundry:
# snforge_std.workspace = true
# OpenZeppelin:
# openzeppelin.workspace = true

# If you want to use another Starknet By Example's listing, you can add it as a dependency like this:
# erc20 = { path = "../../ch00-getting-started/erc20" }

[scripts]
test.workspace = true

[[target.starknet-contract]]
casm = true
```
Be sure to adapt it to your needs.

You also NEED to do the following:
- Remove the generated git repository, `rm -rf .git` (this is important!)
- Double check that the `pkg_name` is the same as the name of the directory

### Verification script

The current book has script that verifies the compilation of all Cairo programs in the book.
Instead of directly writing Cairo programs in the markdown files, we use code blocks that import the Cairo programs from the `listing` directory.
These programs are bundled into scarb packages, which makes it easier to test and build entire packages.

To run the script locally, ensure that you are at the root of the repository, and run:

`bash scripts/cairo_programs_verifier.sh`

This will check that all the Cairo programs in the book compile successfully using `scarb build`, that every tests passes using `scarb test`, and that the `scarb fmt -c` command does not identify any formatting issues.

You can also use `scarb fmt` to format all the Cairo programs.

### Tests

Every listing needs to have atleast integration tests:

Expand All @@ -47,6 +127,8 @@ mod contract;
mod tests;
```

> About Starknet Foundry: It is currently not possible to use Starknet Foundry but we are working on it.

### Use of anchor

You can add delimiting comments to select part of the code in the book.
Expand All @@ -65,7 +147,7 @@ Then, in the markdown file, you can use the following syntax to include only the
```markdown
```rust
{{#include ../../listings/path/to/listing/src/contract.cairo:anchor_name}}
```
\```
```

This will result in the following code being included in the book:
Expand All @@ -74,13 +156,26 @@ This will result in the following code being included in the book:
b
```

## Pull Request
## Translations

To work with translations, those are the steps to update the translated content:

- Run a local server for the language you want to edit: `./translations.sh zh-cn` for instance. If no language is provided, the script will only extract translations from english.

- Open the translation file you are interested in `po/zh-cn.po` for instance. You can also use editors like [poedit](https://poedit.net/) to help you on this task.

- When you are done, you should only have changes into the `po/xx.po` file. Commit them and open a PR.
The PR must stars with `i18n` to let the maintainers know that the PR is only changing translation.

The translation work is inspired from [Comprehensive Rust repository](https://github.com/google/comprehensive-rust/blob/main/TRANSLATIONS.md).

### Initiate a new translation for your language

Please follow and check the pull request template requirements before submitting a pull request.
If you wish to initiate a new translation for your language without running a local server, consider the following tips:

Additionally, please ensure your pull request adheres to the following guidelines:
1. Ensure any install or build dependencies are not committed to the repository.
2. Update the documentation (README.md, CONTRIBUTING.md, ...) if needed.
- Execute the command `./translations.sh new xx` (replace `xx` with your language code). This method can generate the `xx.po` file of your language for you.
- To update your `xx.po` file, execute the command `./translations.sh xx` (replace `xx` with your language code), as mentioned in the previous chapter.
- If the `xx.po` file already exists (which means you are not initiating a new translation), you should not run this command.

## Code of Conduct

Expand Down
80 changes: 19 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,22 @@ Starknet by Example is a collection of examples of how to use the [Cairo](https:

## Contribute

### Setup

1. Clone this repository.
2. Rust related packages:
- Install toolchain providing `cargo` using [rustup](https://rustup.rs/).
- Install [mdBook](https://rust-lang.github.io/mdBook/guide/installation.html) and the required extension with `cargo install mdbook mdbook-last-changed`.
3. Install `scarb` using [asdf](https://asdf-vm.com/) with `asdf install`. Alternatively, you can install `scarb` manually by following the instructions [here](https://docs.swmansion.com/scarb/).

### Local development

#### MdBook

All the Markdown files **MUST** be edited in english. To work locally in english:

- Start a local server with `mdbook serve` and visit [localhost:3000](http://localhost:3000) to view the book.
You can use the `--open` flag to open the browser automatically: `mdbook serve --open`.

- Make changes to the book and refresh the browser to see the changes.

- Open a PR with your changes.

##### Work locally (translations)

To work with translations, those are the steps to update the translated content:

- Run a local server for the language you want to edit: `./translations.sh zh-cn` for instance. If no language is provided, the script will only extract translations from english.

- Open the translation file you are interested in `po/zh-cn.po` for instance. You can also use editors like [poedit](https://poedit.net/) to help you on this task.

- When you are done, you should only have changes into the `po/xx.po` file. Commit them and open a PR.
The PR must stars with `i18n` to let the maintainers know that the PR is only changing translation.

The translation work is inspired from [Comprehensive Rust repository](https://github.com/google/comprehensive-rust/blob/main/TRANSLATIONS.md).

##### Initiate a new translation for your language

If you wish to initiate a new translation for your language without running a local server, consider the following tips:

- Execute the command `./translations.sh new xx` (replace `xx` with your language code). This method can generate the `xx.po` file of your language for you.
- To update your `xx.po` file, execute the command `./translations.sh xx` (replace `xx` with your language code), as mentioned in the previous chapter.
- If the `xx.po` file already exists (which means you are not initiating a new translation), you should not run this command.

#### Cairo programs

The current book has script that verifies the compilation of all Cairo programs in the book.
Instead of directly writing Cairo programs in the markdown files, we use code blocks that import the Cairo programs from the `listing` directory.
These programs are bundled into scarb packages, which makes it easier to test and build entire packages.

To run the script locally, ensure that you are at the root of the repository (same directory as this `README.md` file),
and run:

`bash scripts/cairo_programs_verifier.sh`

This will check that all the Cairo programs in the book compile successfully using `scarb build`, that every tests passes using `scarb test`, and that the `scarb fmt -c` command does not identify any formatting issues.

You can also use `bash scripts/cairo_programs_format.sh` to format all the Cairo programs in the book using `scarb fmt` automatically.

If you want to add a new smart contract to the book, you can follow these steps:
1. Create a new directory in the `listing` directory.
2. Initialize a scarb project in the new directory with `scarb init`.
3. Add `[[target.starknet-contract]]` and the starknet dependency to the `Scarb.toml` file.
Please refer to the Contribution Guidelines page:
- [Contributing](CONTRIBUTING.md#contributing)
- [Table of Contents](CONTRIBUTING.md#table-of-contents)
- [Setup](CONTRIBUTING.md#setup)
- [MdBook](CONTRIBUTING.md#mdbook)
- [Adding a new chapter](CONTRIBUTING.md#adding-a-new-chapter)
- [Adding a new Cairo program](CONTRIBUTING.md#adding-a-new-cairo-program)
- [Verification script](CONTRIBUTING.md#verification-script)
- [Tests](CONTRIBUTING.md#tests)
- [Use of anchor](CONTRIBUTING.md#use-of-anchor)
- [Translations](CONTRIBUTING.md#translations)
- [Initiate a new translation for your language](CONTRIBUTING.md#initiate-a-new-translation-for-your-language)
- [Code of Conduct](CONTRIBUTING.md#code-of-conduct)
- [Our Pledge](CONTRIBUTING.md#our-pledge)
- [Our Standards](CONTRIBUTING.md#our-standards)
- [Our Responsibilities](CONTRIBUTING.md#our-responsibilities)
- [Scope](CONTRIBUTING.md#scope)
- [Enforcement](CONTRIBUTING.md#enforcement)
- [Attribution](CONTRIBUTING.md#attribution)
118 changes: 117 additions & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,121 @@
version = 1

[[package]]
name = "sbe"
name = "alexandria_storage"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=ae1d514#ae1d5149ff601a7ac5b39edc867d33ebd83d7f4f"

[[package]]
name = "cairo_cheatsheet"
version = "0.1.0"

[[package]]
name = "calling_other_contracts"
version = "0.1.0"

[[package]]
name = "constant_product_amm"
version = "0.1.0"
dependencies = [
"openzeppelin",
]

[[package]]
name = "constructor"
version = "0.1.0"

[[package]]
name = "counter"
version = "0.1.0"

[[package]]
name = "custom_type_serde"
version = "0.1.0"

[[package]]
name = "erc20"
version = "0.1.0"

[[package]]
name = "errors"
version = "0.1.0"

[[package]]
name = "events"
version = "0.1.0"

[[package]]
name = "factory"
version = "0.1.0"

[[package]]
name = "hash_solidity_compatible"
version = "0.1.0"

[[package]]
name = "interfaces_traits"
version = "0.1.0"

[[package]]
name = "mappings"
version = "0.1.0"

[[package]]
name = "openzeppelin"
version = "0.8.0-beta.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.8.0-beta.0#4c95981a7178e43730f152c85a92336dc6a22d62"

[[package]]
name = "scarb"
version = "0.1.0"

[[package]]
name = "simple_vault"
version = "0.1.0"

[[package]]
name = "storage"
version = "0.1.0"

[[package]]
name = "store_using_packing"
version = "0.1.0"

[[package]]
name = "storing_arrays"
version = "0.1.0"

[[package]]
name = "storing_custom_types"
version = "0.1.0"

[[package]]
name = "struct_as_mapping_key"
version = "0.1.0"

[[package]]
name = "testing_how_to"
version = "0.1.0"

[[package]]
name = "upgradeable_contract"
version = "0.1.0"

[[package]]
name = "using_lists"
version = "0.1.0"
dependencies = [
"alexandria_storage",
]

[[package]]
name = "variables"
version = "0.1.0"

[[package]]
name = "visibility"
version = "0.1.0"

[[package]]
name = "write_to_any_slot"
version = "0.1.0"
Loading