From ee41c81a205414d106c37833eee77d5e821f8c1c Mon Sep 17 00:00:00 2001 From: julio4 Date: Fri, 18 Aug 2023 16:05:55 +0200 Subject: [PATCH 1/6] ci: use `setup-scarb` action, update scarb 0.6.2 --- .github/workflows/mdbook.yml | 11 +---------- .github/workflows/setup-rust-cache/action.yml | 9 --------- .github/workflows/verify-cairo-programs/action.yml | 6 +++--- .github/workflows/verify_cairo_programs.yml | 8 +++----- 4 files changed, 7 insertions(+), 27 deletions(-) delete mode 100644 .github/workflows/setup-rust-cache/action.yml diff --git a/.github/workflows/mdbook.yml b/.github/workflows/mdbook.yml index c0233d08..15f5b8dd 100644 --- a/.github/workflows/mdbook.yml +++ b/.github/workflows/mdbook.yml @@ -39,19 +39,10 @@ jobs: - name: Install mdbook uses: ./.github/workflows/install-mdbook - - name: Build in English + - name: Build book run: | mdbook build -d book - - name: Build all translations - run: | - for po_lang in $(cat ./LANGUAGES); do - echo "::group::Building $po_lang translation" - MDBOOK_BOOK__LANGUAGE=$po_lang \ - mdbook build -d book/$po_lang - echo "::endgroup::" - done - - name: Verify Cairo programs uses: ./.github/workflows/verify-cairo-programs diff --git a/.github/workflows/setup-rust-cache/action.yml b/.github/workflows/setup-rust-cache/action.yml deleted file mode 100644 index f359ed35..00000000 --- a/.github/workflows/setup-rust-cache/action.yml +++ /dev/null @@ -1,9 +0,0 @@ -name: Setup Rust cache - -description: Configure the rust-cache workflow. - -runs: - using: composite - steps: - - name: Setup Rust cache - uses: Swatinem/rust-cache@v2 diff --git a/.github/workflows/verify-cairo-programs/action.yml b/.github/workflows/verify-cairo-programs/action.yml index 1a5dc7c3..72c435cf 100644 --- a/.github/workflows/verify-cairo-programs/action.yml +++ b/.github/workflows/verify-cairo-programs/action.yml @@ -6,9 +6,9 @@ runs: using: composite steps: - name: Install scarb - shell: bash - run: | - curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 0.5.1 + uses: software-mansion/setup-scarb@v1 + with: + scarb-version: "0.6.2" - name: Run build script shell: bash diff --git a/.github/workflows/verify_cairo_programs.yml b/.github/workflows/verify_cairo_programs.yml index 151a0814..3b651d0d 100644 --- a/.github/workflows/verify_cairo_programs.yml +++ b/.github/workflows/verify_cairo_programs.yml @@ -13,12 +13,10 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Setup Rust cache - uses: ./.github/workflows/setup-rust-cache - - name: Install scarb - run: | - curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 0.5.1 + uses: software-mansion/setup-scarb@v1 + with: + scarb-version: "0.6.2" - name: Run build script run: | From 67e8fe16943abc019daa349f4604cd6fadeacabf Mon Sep 17 00:00:00 2001 From: julio4 Date: Fri, 18 Aug 2023 16:06:35 +0200 Subject: [PATCH 2/6] ci: add `scarb fmt` and `scarb test` --- scripts/cairo_programs_format.sh | 26 ++++++++++++++++++++++++++ scripts/cairo_programs_verifier.sh | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100755 scripts/cairo_programs_format.sh diff --git a/scripts/cairo_programs_format.sh b/scripts/cairo_programs_format.sh new file mode 100755 index 00000000..5ab0e1c0 --- /dev/null +++ b/scripts/cairo_programs_format.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +GREEN='\033[1;32m' +RED='\033[1;31m' +NC='\033[0m' + +process_directory() { + for dir in "$1"/* + do + if [ -d "$dir" ]; then + echo "Processing $dir" + (cd "$dir" && scarb fmt) + fi + done +} + +# only start processing directories that match the pattern ch*-* +for dir in listings/ch*-* +do + if [ -d "$dir" ]; then + process_directory "$dir" + fi +done + +echo -e "\n${GREEN}Formats finished.${NC}.\n" +exit 0 diff --git a/scripts/cairo_programs_verifier.sh b/scripts/cairo_programs_verifier.sh index 57ef4bb7..3d0b056b 100755 --- a/scripts/cairo_programs_verifier.sh +++ b/scripts/cairo_programs_verifier.sh @@ -13,7 +13,7 @@ process_directory() { do if [ -d "$dir" ]; then echo "Processing $dir" - (cd "$dir" && scarb build 0>/dev/null 1> error.log) + (cd "$dir" && scarb build 0>/dev/null 1> error.log && scarb fmt -c 0>/dev/null 1>> error.log && scarb test 0>/dev/null 1>> error.log) if [ $? -ne 0 ]; then has_errors=true echo "Error while processing $dir" @@ -34,7 +34,7 @@ done # check if any errors were encountered if $has_errors ; then - echo "\n${RED}Some scarb builds have errors, please check the list above.${NC}\n" + echo "\n${RED}Some projects have errors, please check the list above.${NC}\n" exit 1 else echo -e "\n${GREEN}All scarb builds were completed successfully${NC}.\n" From 138c3dc4d19042eaf14c075d9598fcb210dffd8f Mon Sep 17 00:00:00 2001 From: julio4 Date: Fri, 18 Aug 2023 16:06:57 +0200 Subject: [PATCH 3/6] doc: use asdf for scarb, update README --- .tool-versions | 1 + README.md | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..46b52fd3 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +scarb 0.6.2 \ No newline at end of file diff --git a/README.md b/README.md index 22616e10..3ec2b5fd 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,15 @@ Starknet by Example is a collection of examples of how to use the [Cairo](https: ### Setup -1. Rust related packages: +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) -2. Host machine packages: + - Install [mdBook](https://rust-lang.github.io/mdBook/guide/installation.html). +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/). - - Install [gettext](https://www.gnu.org/software/gettext/) for translations, usually available with regular package manager: - `sudo apt install gettext`. +### Local development -3. Clone this repository. - -### Work locally +#### MdBook All the Markdown files **MUST** be edited in english. To work locally in english: @@ -29,7 +27,7 @@ All the Markdown files **MUST** be edited in english. To work locally in english - Open a PR with your changes. -### Work locally (Cairo programs verification) +#### 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. @@ -39,3 +37,12 @@ To run the script locally, ensure that you are at the root of the repository (sa 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. From 0c156545ebb7057787bfb44c72d3ea0a83a3df49 Mon Sep 17 00:00:00 2001 From: julio4 Date: Fri, 18 Aug 2023 16:15:39 +0200 Subject: [PATCH 4/6] fix: test failing #64 --- listings/ch00-introduction/testing/src/lib.cairo | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/listings/ch00-introduction/testing/src/lib.cairo b/listings/ch00-introduction/testing/src/lib.cairo index b91c0bcb..abfda802 100644 --- a/listings/ch00-introduction/testing/src/lib.cairo +++ b/listings/ch00-introduction/testing/src/lib.cairo @@ -101,7 +101,7 @@ mod simple_contract_tests { fn test_set_as_owner() { // Fake the caller address to address 1 let owner = contract_address_const::<1>(); - set_caller_address(owner); + set_contract_address(owner); let contract = deploy(10); assert(contract.get_owner() == owner, 'wrong owner'); @@ -132,3 +132,4 @@ mod simple_contract_tests { } // ANCHOR_END: test + From 9f42f1dfccbace6c968c076c7f71eec9e75e7eee Mon Sep 17 00:00:00 2001 From: julio4 Date: Fri, 18 Aug 2023 16:16:31 +0200 Subject: [PATCH 5/6] feat: update to v2.1.0 #63 --- .../calling_other_contracts/Scarb.toml | 6 +-- .../ch00-introduction/constructor/Scarb.toml | 4 +- listings/ch00-introduction/counter/Scarb.toml | 2 +- .../ch00-introduction/counter/src/lib.cairo | 2 +- .../custom_type_serde/Scarb.toml | 4 +- listings/ch00-introduction/errors/Scarb.toml | 4 +- .../errors/src/custom_errors.cairo | 2 +- listings/ch00-introduction/events/Scarb.toml | 2 +- .../interfaces_traits/Scarb.toml | 2 +- .../interfaces_traits/src/implicit.cairo | 4 +- .../src/implicit_internal.cairo | 4 +- .../interfaces_traits/src/lib.cairo | 2 +- .../ch00-introduction/mappings/Scarb.toml | 4 +- .../storing_custom_types/Scarb.toml | 4 +- .../storing_custom_types/src/lib.cairo | 4 +- listings/ch00-introduction/testing/Scarb.toml | 2 +- .../ch00-introduction/variables/Scarb.toml | 4 +- .../ch00-introduction/visibility/Scarb.toml | 4 +- .../storing_arrays/Scarb.toml | 4 +- .../storing_arrays/src/storing_arrays.cairo | 40 ++++++++----------- .../struct_as_mapping_key/Scarb.toml | 4 +- .../struct_as_mapping_key/src/lib.cairo | 2 +- .../write_to_any_slot/Scarb.toml | 4 +- .../ch02-applications/simple_vault/Scarb.toml | 4 +- .../simple_vault/src/lib.cairo | 2 +- .../upgradeable_contract/Scarb.toml | 4 +- src/ch00-12-storing-custom-types.md | 2 +- src/ch01-02-storing_arrays.md | 2 +- 28 files changed, 48 insertions(+), 80 deletions(-) diff --git a/listings/ch00-introduction/calling_other_contracts/Scarb.toml b/listings/ch00-introduction/calling_other_contracts/Scarb.toml index 484c25eb..0b7d46d4 100644 --- a/listings/ch00-introduction/calling_other_contracts/Scarb.toml +++ b/listings/ch00-introduction/calling_other_contracts/Scarb.toml @@ -2,9 +2,7 @@ name = "calling_other_contracts" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" -[[target.starknet-contract]] \ No newline at end of file +[[target.starknet-contract]] diff --git a/listings/ch00-introduction/constructor/Scarb.toml b/listings/ch00-introduction/constructor/Scarb.toml index d79578b1..09bf379d 100644 --- a/listings/ch00-introduction/constructor/Scarb.toml +++ b/listings/ch00-introduction/constructor/Scarb.toml @@ -2,9 +2,7 @@ name = "constructor" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/counter/Scarb.toml b/listings/ch00-introduction/counter/Scarb.toml index c3fa70cd..17af25ef 100644 --- a/listings/ch00-introduction/counter/Scarb.toml +++ b/listings/ch00-introduction/counter/Scarb.toml @@ -3,6 +3,6 @@ name = "counter" version = "0.1.0" [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] diff --git a/listings/ch00-introduction/counter/src/lib.cairo b/listings/ch00-introduction/counter/src/lib.cairo index ba8e24da..f5271637 100644 --- a/listings/ch00-introduction/counter/src/lib.cairo +++ b/listings/ch00-introduction/counter/src/lib.cairo @@ -1 +1 @@ -mod counter; \ No newline at end of file +mod counter; diff --git a/listings/ch00-introduction/custom_type_serde/Scarb.toml b/listings/ch00-introduction/custom_type_serde/Scarb.toml index a6d38cff..6409afae 100644 --- a/listings/ch00-introduction/custom_type_serde/Scarb.toml +++ b/listings/ch00-introduction/custom_type_serde/Scarb.toml @@ -2,9 +2,7 @@ name = "custom_type_serde" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/errors/Scarb.toml b/listings/ch00-introduction/errors/Scarb.toml index d3731cd1..26c77f66 100644 --- a/listings/ch00-introduction/errors/Scarb.toml +++ b/listings/ch00-introduction/errors/Scarb.toml @@ -2,9 +2,7 @@ name = "errors" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/errors/src/custom_errors.cairo b/listings/ch00-introduction/errors/src/custom_errors.cairo index 303f5738..693cf3d4 100644 --- a/listings/ch00-introduction/errors/src/custom_errors.cairo +++ b/listings/ch00-introduction/errors/src/custom_errors.cairo @@ -13,7 +13,7 @@ mod CustomErrorsExample { #[generate_trait] #[external(v0)] - impl CustomErrorsExample of ICustomErrorsExample{ + impl CustomErrorsExample of ICustomErrorsExample { fn test_assert(self: @ContractState, i: u256) { assert(i > 0, Errors::NOT_POSITIVE); } diff --git a/listings/ch00-introduction/events/Scarb.toml b/listings/ch00-introduction/events/Scarb.toml index e5da91fb..436dfcac 100644 --- a/listings/ch00-introduction/events/Scarb.toml +++ b/listings/ch00-introduction/events/Scarb.toml @@ -3,6 +3,6 @@ name = "counter" version = "0.1.0" [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/interfaces_traits/Scarb.toml b/listings/ch00-introduction/interfaces_traits/Scarb.toml index 972c094c..a95ed96f 100644 --- a/listings/ch00-introduction/interfaces_traits/Scarb.toml +++ b/listings/ch00-introduction/interfaces_traits/Scarb.toml @@ -3,6 +3,6 @@ name = "interfaces_traits" version = "0.1.0" [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] diff --git a/listings/ch00-introduction/interfaces_traits/src/implicit.cairo b/listings/ch00-introduction/interfaces_traits/src/implicit.cairo index 33b094c3..0856eb7c 100644 --- a/listings/ch00-introduction/interfaces_traits/src/implicit.cairo +++ b/listings/ch00-introduction/interfaces_traits/src/implicit.cairo @@ -18,4 +18,6 @@ mod ImplicitInterfaceContract { } } } -// ANCHOR_END: code \ No newline at end of file +// ANCHOR_END: code + + diff --git a/listings/ch00-introduction/interfaces_traits/src/implicit_internal.cairo b/listings/ch00-introduction/interfaces_traits/src/implicit_internal.cairo index 8447cb7f..c905fa69 100644 --- a/listings/ch00-introduction/interfaces_traits/src/implicit_internal.cairo +++ b/listings/ch00-introduction/interfaces_traits/src/implicit_internal.cairo @@ -75,9 +75,9 @@ mod implicit_internal_contract_tests { let initial_value: u32 = 0; assert(contract.get_value() == initial_value, 'wrong value'); - let add_value: u32 = 10; + let add_value: u32 = 10; contract.add(add_value); assert(contract.get_value() == initial_value + add_value, 'wrong value after add'); } -} \ No newline at end of file +} diff --git a/listings/ch00-introduction/interfaces_traits/src/lib.cairo b/listings/ch00-introduction/interfaces_traits/src/lib.cairo index be95a946..8408ac50 100644 --- a/listings/ch00-introduction/interfaces_traits/src/lib.cairo +++ b/listings/ch00-introduction/interfaces_traits/src/lib.cairo @@ -1,3 +1,3 @@ mod explicit; mod implicit; -mod implicit_internal; \ No newline at end of file +mod implicit_internal; diff --git a/listings/ch00-introduction/mappings/Scarb.toml b/listings/ch00-introduction/mappings/Scarb.toml index def4de80..e2978822 100644 --- a/listings/ch00-introduction/mappings/Scarb.toml +++ b/listings/ch00-introduction/mappings/Scarb.toml @@ -2,9 +2,7 @@ name = "maps" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] diff --git a/listings/ch00-introduction/storing_custom_types/Scarb.toml b/listings/ch00-introduction/storing_custom_types/Scarb.toml index dac52cb1..7a0b5140 100644 --- a/listings/ch00-introduction/storing_custom_types/Scarb.toml +++ b/listings/ch00-introduction/storing_custom_types/Scarb.toml @@ -2,9 +2,7 @@ name = "storing_custom_types" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/storing_custom_types/src/lib.cairo b/listings/ch00-introduction/storing_custom_types/src/lib.cairo index 0a76c16e..5460114e 100644 --- a/listings/ch00-introduction/storing_custom_types/src/lib.cairo +++ b/listings/ch00-introduction/storing_custom_types/src/lib.cairo @@ -5,9 +5,9 @@ mod StoringCustomType { person: Person } - // Deriving the storage_access::StorageAccess trait + // Deriving the starknet::Store trait // allows us to store the `Person` struct in the contract's storage. - #[derive(Drop, storage_access::StorageAccess)] + #[derive(Drop, starknet::Store)] struct Person { age: u8, name: felt252 diff --git a/listings/ch00-introduction/testing/Scarb.toml b/listings/ch00-introduction/testing/Scarb.toml index 12c0910d..2e791d77 100644 --- a/listings/ch00-introduction/testing/Scarb.toml +++ b/listings/ch00-introduction/testing/Scarb.toml @@ -3,6 +3,6 @@ name = "tests" version = "0.1.0" [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] diff --git a/listings/ch00-introduction/variables/Scarb.toml b/listings/ch00-introduction/variables/Scarb.toml index d5a11173..b5a198c9 100644 --- a/listings/ch00-introduction/variables/Scarb.toml +++ b/listings/ch00-introduction/variables/Scarb.toml @@ -2,9 +2,7 @@ name = "variables" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/visibility/Scarb.toml b/listings/ch00-introduction/visibility/Scarb.toml index 5bd6e265..18a704e6 100644 --- a/listings/ch00-introduction/visibility/Scarb.toml +++ b/listings/ch00-introduction/visibility/Scarb.toml @@ -2,9 +2,7 @@ name = "visibility" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch01-advanced-concepts/storing_arrays/Scarb.toml b/listings/ch01-advanced-concepts/storing_arrays/Scarb.toml index a7f9a809..952ed454 100644 --- a/listings/ch01-advanced-concepts/storing_arrays/Scarb.toml +++ b/listings/ch01-advanced-concepts/storing_arrays/Scarb.toml @@ -2,9 +2,7 @@ name = "storing_arrays" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo b/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo index ff9cba27..060e0db0 100644 --- a/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo +++ b/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo @@ -4,29 +4,30 @@ use option::OptionTrait; use array::ArrayTrait; use array::SpanTrait; use starknet::{ - StorageBaseAddress, StorageAccess, SyscallResult, storage_read_syscall, storage_write_syscall, + StorageBaseAddress, Store, SyscallResult, storage_read_syscall, storage_write_syscall, storage_address_from_base_and_offset }; use debug::PrintTrait; + // ANCHOR: StorageAccessImpl -impl StorageAccessFelt252Array of StorageAccess> { +impl StoreFelt252Array of Store> { fn read(address_domain: u32, base: StorageBaseAddress) -> SyscallResult> { - StorageAccessFelt252Array::read_at_offset_internal(address_domain, base, 0) + StoreFelt252Array::read_at_offset(address_domain, base, 0) } fn write( address_domain: u32, base: StorageBaseAddress, value: Array ) -> SyscallResult<()> { - StorageAccessFelt252Array::write_at_offset_internal(address_domain, base, 0, value) + StoreFelt252Array::write_at_offset(address_domain, base, 0, value) } - fn read_at_offset_internal( + fn read_at_offset( address_domain: u32, base: StorageBaseAddress, mut offset: u8 ) -> SyscallResult> { let mut arr: Array = ArrayTrait::new(); // Read the stored array's length. If the length is superior to 255, the read will fail. - let len: u8 = StorageAccess::::read_at_offset_internal(address_domain, base, offset) + let len: u8 = Store::::read_at_offset(address_domain, base, offset) .expect('Storage Span too large'); offset += 1; @@ -37,34 +38,29 @@ impl StorageAccessFelt252Array of StorageAccess> { break; } - let value = StorageAccess::::read_at_offset_internal( - address_domain, base, offset - ) - .unwrap(); + let value = Store::::read_at_offset(address_domain, base, offset).unwrap(); arr.append(value); - offset += StorageAccess::::size_internal(value); + offset += Store::::size(); }; // Return the array. Result::Ok(arr) } - fn write_at_offset_internal( + fn write_at_offset( address_domain: u32, base: StorageBaseAddress, mut offset: u8, mut value: Array ) -> SyscallResult<()> { // // Store the length of the array in the first storage slot. let len: u8 = value.len().try_into().expect('Storage - Span too large'); - StorageAccess::::write_at_offset_internal(address_domain, base, offset, len); + Store::::write_at_offset(address_domain, base, offset, len); offset += 1; // Store the array elements sequentially loop { match value.pop_front() { Option::Some(element) => { - StorageAccess::::write_at_offset_internal( - address_domain, base, offset, element - )?; - offset += StorageAccess::::size_internal(element); + Store::::write_at_offset(address_domain, base, offset, element)?; + offset += Store::::size(); }, Option::None(_) => { break Result::Ok(()); @@ -73,11 +69,8 @@ impl StorageAccessFelt252Array of StorageAccess> { } } - fn size_internal(value: Array) -> u8 { - if value.len() == 0 { - return 1; - } - 1_u8 + StorageAccess::::size_internal(*value[0]) * value.len().try_into().unwrap() + fn size() -> u8 { + Store::::size() } } // ANCHOR_END: StorageAccessImpl @@ -85,14 +78,13 @@ impl StorageAccessFelt252Array of StorageAccess> { // ANCHOR: StoreArrayContract #[starknet::contract] mod StoreArrayContract { - use super::StorageAccessFelt252Array; + use super::StoreFelt252Array; #[storage] struct Storage { arr: Array } - #[generate_trait] #[external(v0)] impl StoreArrayImpl of IStoreArrayContract { diff --git a/listings/ch01-advanced-concepts/struct_as_mapping_key/Scarb.toml b/listings/ch01-advanced-concepts/struct_as_mapping_key/Scarb.toml index aad15579..5f2ff191 100644 --- a/listings/ch01-advanced-concepts/struct_as_mapping_key/Scarb.toml +++ b/listings/ch01-advanced-concepts/struct_as_mapping_key/Scarb.toml @@ -2,9 +2,7 @@ name = "src" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch01-advanced-concepts/struct_as_mapping_key/src/lib.cairo b/listings/ch01-advanced-concepts/struct_as_mapping_key/src/lib.cairo index 27e13e88..b95c7ad0 100644 --- a/listings/ch01-advanced-concepts/struct_as_mapping_key/src/lib.cairo +++ b/listings/ch01-advanced-concepts/struct_as_mapping_key/src/lib.cairo @@ -1,2 +1,2 @@ mod contract; -mod test; \ No newline at end of file +mod test; diff --git a/listings/ch01-advanced-concepts/write_to_any_slot/Scarb.toml b/listings/ch01-advanced-concepts/write_to_any_slot/Scarb.toml index 96fcc699..30d48fbf 100644 --- a/listings/ch01-advanced-concepts/write_to_any_slot/Scarb.toml +++ b/listings/ch01-advanced-concepts/write_to_any_slot/Scarb.toml @@ -2,9 +2,7 @@ name = "write_to_any_slot" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch02-applications/simple_vault/Scarb.toml b/listings/ch02-applications/simple_vault/Scarb.toml index 5df740cb..6c874953 100644 --- a/listings/ch02-applications/simple_vault/Scarb.toml +++ b/listings/ch02-applications/simple_vault/Scarb.toml @@ -2,9 +2,7 @@ name = "simple_vault" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch02-applications/simple_vault/src/lib.cairo b/listings/ch02-applications/simple_vault/src/lib.cairo index d07cc512..1745c06d 100644 --- a/listings/ch02-applications/simple_vault/src/lib.cairo +++ b/listings/ch02-applications/simple_vault/src/lib.cairo @@ -1 +1 @@ -mod simple_vault; \ No newline at end of file +mod simple_vault; diff --git a/listings/ch02-applications/upgradeable_contract/Scarb.toml b/listings/ch02-applications/upgradeable_contract/Scarb.toml index 2203f76b..36840c05 100644 --- a/listings/ch02-applications/upgradeable_contract/Scarb.toml +++ b/listings/ch02-applications/upgradeable_contract/Scarb.toml @@ -2,9 +2,7 @@ name = "upgradeable_contract" version = "0.1.0" -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest - [dependencies] -starknet = ">=2.0.1" +starknet = ">=2.1.0" [[target.starknet-contract]] \ No newline at end of file diff --git a/src/ch00-12-storing-custom-types.md b/src/ch00-12-storing-custom-types.md index 2d28c6e8..4c035cbc 100644 --- a/src/ch00-12-storing-custom-types.md +++ b/src/ch00-12-storing-custom-types.md @@ -1,6 +1,6 @@ # Storing Custom Types -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 `StorageAccess` trait for our custom type. Hopefully, we can just derive this trait for our custom type - unless it contains arrays or dictionaries. +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/lib.cairo}} diff --git a/src/ch01-02-storing_arrays.md b/src/ch01-02-storing_arrays.md index c57d7f70..c9f4d3e6 100644 --- a/src/ch01-02-storing_arrays.md +++ b/src/ch01-02-storing_arrays.md @@ -1,6 +1,6 @@ # Storing Arrays -On Starknet, complex values (e.g., tuples or structs), are stored in a continuous segment starting from the address of the storage variable. There is a 256 field elements limitation to the maximal size of a complex storage value, meaning that to store arrays of more than 255 elements in storage, we would need to split it into segments of size `n <= 255` and store these segments in multiple storage addresses. There is currently no native support for storing arrays in Cairo, so you will need to write your own implementation of the `StorageAccess` trait for the type of array you wish to store. +On Starknet, complex values (e.g., tuples or structs), are stored in a continuous segment starting from the address of the storage variable. There is a 256 field elements limitation to the maximal size of a complex storage value, meaning that to store arrays of more than 255 elements in storage, we would need to split it into segments of size `n <= 255` and store these segments in multiple storage addresses. There is currently no native support for storing arrays in Cairo, so you will need to write your own implementation of the `Store` trait for the type of array you wish to store. > Note: While storing arrays in storage is possible, it is not always recommended, as the read and write operations can get very costly. For example, reading an array of size `n` requires `n` storage reads, and writing to an array of size `n` requires `n` storage writes. If you only need to access a single element of the array at a time, it is recommended to use a `LegacyMap` and store the length in another variable instead. From e0bd756916430792429540b748748681419feb9b Mon Sep 17 00:00:00 2001 From: julio4 Date: Mon, 21 Aug 2023 21:16:41 +0200 Subject: [PATCH 6/6] fix: storing array size() return max size --- .../storing_arrays/src/storing_arrays.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo b/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo index 060e0db0..975f2ba6 100644 --- a/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo +++ b/listings/ch01-advanced-concepts/storing_arrays/src/storing_arrays.cairo @@ -70,7 +70,7 @@ impl StoreFelt252Array of Store> { } fn size() -> u8 { - Store::::size() + 255 * Store::::size() } } // ANCHOR_END: StorageAccessImpl