From 68be11e37f9b8ac9e20bf36c88d6c7bb8de2de69 Mon Sep 17 00:00:00 2001 From: julio4 Date: Mon, 20 Nov 2023 11:21:08 +0900 Subject: [PATCH 1/2] feat: Storage minimal + fixes --- src/SUMMARY.md | 9 +++++---- src/ch00/basics/storage.md | 20 ++++++++++++++++++++ src/ch00/basics/variables.md | 2 +- src/ch01/constant-product-amm.md | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/ch00/basics/storage.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 85a76907..b19c0822 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -6,23 +6,24 @@ Summary # Getting Started - [Basics of a Starknet contract](./ch00/basics/introduction.md) - - [Variables](./ch00/basics/variables.md) + - [Storage](./ch00/basics/storage.md) - [Constructor](./ch00/basics/constructor.md) + - [Variables](./ch00/basics/variables.md) - [Visibility and Mutability](./ch00/basics/visibility-mutability.md) - - [Counter contract example](./ch00/basics/counter.md) + - [Counter 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) - [Deploy and interact with contracts](./ch00/interacting/interacting.md) - - [Factory pattern](./ch00/interacting/factory.md) - [Contract interfaces and Traits generation](./ch00/interacting/interfaces-traits.md) - [Calling other contracts](./ch00/interacting/calling_other_contracts.md) + - [Factory pattern](./ch00/interacting/factory.md) - [Testing contracts](./ch00/testing/contract-testing.md) - [Cairo cheatsheet](./ch00/cairo_cheatsheet/cairo_cheatsheet.md) - [Felt](./ch00/cairo_cheatsheet/felt.md) - - [Mapping](./ch00/cairo_cheatsheet/mapping.md) + - [LegacyMap](./ch00/cairo_cheatsheet/mapping.md) - [Arrays](./ch00/cairo_cheatsheet/arrays.md) - [Loop](./ch00/cairo_cheatsheet/loop.md) - [Match](./ch00/cairo_cheatsheet/match.md) diff --git a/src/ch00/basics/storage.md b/src/ch00/basics/storage.md new file mode 100644 index 00000000..95dab63a --- /dev/null +++ b/src/ch00/basics/storage.md @@ -0,0 +1,20 @@ +# Storage + +Here's the most minimal contract you can write in Cairo: + +```rust +{{#include ../../../listings/ch00-getting-started/storage/src/minimal_contract.cairo}} +``` + +Storage is a struct annoted with `#[storage]`. Every contract must have one and only one storage. +It's a key-value store, where each key will be mapped to a storage address of the contract's storage space. + +You can define [storage variables](./variables.md#storage-variables) in your contract, and then use them to store and retrieve data. +```rust +{{#include ../../../listings/ch00-getting-started/storage/src/contract.cairo}} +``` + +> Actually these two contracts have the same underlying sierra program. +> From the compiler's perspective, the storage variables don't exist until they are used. + +You can also read about [storing custom types](./storing-custom-types.md) diff --git a/src/ch00/basics/variables.md b/src/ch00/basics/variables.md index 6c7ba4ad..cdfbfb68 100644 --- a/src/ch00/basics/variables.md +++ b/src/ch00/basics/variables.md @@ -6,7 +6,7 @@ There are 3 types of variables in Cairo contracts: - declared inside a function - not stored on the blockchain - Storage - - declared in the `Storage` struct of a contract + - declared in the [Storage](./storage.md) of a contract - can be accessed from one execution to another - Global - provides information about the blockchain diff --git a/src/ch01/constant-product-amm.md b/src/ch01/constant-product-amm.md index fffa3c9d..7d7dba51 100644 --- a/src/ch01/constant-product-amm.md +++ b/src/ch01/constant-product-amm.md @@ -3,7 +3,7 @@ This is the Cairo adaptation of the [Solidity by example Constant Product AMM](https://solidity-by-example.org/defi/constant-product-amm/). ```rust -{{#include ../listings/ch01-applications/constant_product_amm/src/constant_product_amm.cairo:ConstantProductAmmContract}} +{{#include ../../listings/ch01-applications/constant_product_amm/src/contracts.cairo:ConstantProductAmmContract}} ``` Play with this contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch01-applications/constant_product_amm/src/constant_product_amm.cairo). \ No newline at end of file From 232cc95a701e6414f2d339b803291a49648899f4 Mon Sep 17 00:00:00 2001 From: julio4 Date: Mon, 20 Nov 2023 15:10:43 +0900 Subject: [PATCH 2/2] fix: add missing storage listing --- .../ch00-getting-started/storage/.gitignore | 1 + .../ch00-getting-started/storage/Scarb.lock | 6 ++++ .../ch00-getting-started/storage/Scarb.toml | 8 +++++ .../MinimalContract.2.3.0.contract_class.json | 34 +++++++++++++++++++ .../storage/src/contract.cairo | 9 +++++ .../storage/src/lib.cairo | 2 ++ .../storage/src/minimal_contract.cairo | 5 +++ 7 files changed, 65 insertions(+) create mode 100644 listings/ch00-getting-started/storage/.gitignore create mode 100644 listings/ch00-getting-started/storage/Scarb.lock create mode 100644 listings/ch00-getting-started/storage/Scarb.toml create mode 100644 listings/ch00-getting-started/storage/assets/MinimalContract.2.3.0.contract_class.json create mode 100644 listings/ch00-getting-started/storage/src/contract.cairo create mode 100644 listings/ch00-getting-started/storage/src/lib.cairo create mode 100644 listings/ch00-getting-started/storage/src/minimal_contract.cairo diff --git a/listings/ch00-getting-started/storage/.gitignore b/listings/ch00-getting-started/storage/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/listings/ch00-getting-started/storage/.gitignore @@ -0,0 +1 @@ +target diff --git a/listings/ch00-getting-started/storage/Scarb.lock b/listings/ch00-getting-started/storage/Scarb.lock new file mode 100644 index 00000000..9b4d3726 --- /dev/null +++ b/listings/ch00-getting-started/storage/Scarb.lock @@ -0,0 +1,6 @@ +# Code generated by scarb DO NOT EDIT. +version = 1 + +[[package]] +name = "storage" +version = "0.1.0" diff --git a/listings/ch00-getting-started/storage/Scarb.toml b/listings/ch00-getting-started/storage/Scarb.toml new file mode 100644 index 00000000..733ef08d --- /dev/null +++ b/listings/ch00-getting-started/storage/Scarb.toml @@ -0,0 +1,8 @@ +[package] +name = "storage" +version = "0.1.0" + +[dependencies] +starknet = ">=2.3.0" + +[[target.starknet-contract]] diff --git a/listings/ch00-getting-started/storage/assets/MinimalContract.2.3.0.contract_class.json b/listings/ch00-getting-started/storage/assets/MinimalContract.2.3.0.contract_class.json new file mode 100644 index 00000000..82be378d --- /dev/null +++ b/listings/ch00-getting-started/storage/assets/MinimalContract.2.3.0.contract_class.json @@ -0,0 +1,34 @@ +{ + "sierra_program": [ + "0x1", + "0x3", + "0x0", + "0x2", + "0x3", + "0x0", + "0x1", + "0xff", + "0x0", + "0x4", + "0x0" + ], + "sierra_program_debug_info": { + "type_names": [], + "libfunc_names": [], + "user_func_names": [] + }, + "contract_class_version": "0.1.0", + "entry_points_by_type": { + "EXTERNAL": [], + "L1_HANDLER": [], + "CONSTRUCTOR": [] + }, + "abi": [ + { + "type": "event", + "name": "storage::minimal_contract::MinimalContract::Event", + "kind": "enum", + "variants": [] + } + ] +} diff --git a/listings/ch00-getting-started/storage/src/contract.cairo b/listings/ch00-getting-started/storage/src/contract.cairo new file mode 100644 index 00000000..e66252f0 --- /dev/null +++ b/listings/ch00-getting-started/storage/src/contract.cairo @@ -0,0 +1,9 @@ +#[starknet::contract] +mod Contract { + #[storage] + struct Storage { + a: u128, + b: u8, + c: u256 + } +} diff --git a/listings/ch00-getting-started/storage/src/lib.cairo b/listings/ch00-getting-started/storage/src/lib.cairo new file mode 100644 index 00000000..ec8c6136 --- /dev/null +++ b/listings/ch00-getting-started/storage/src/lib.cairo @@ -0,0 +1,2 @@ +mod minimal_contract; +mod contract; diff --git a/listings/ch00-getting-started/storage/src/minimal_contract.cairo b/listings/ch00-getting-started/storage/src/minimal_contract.cairo new file mode 100644 index 00000000..22291808 --- /dev/null +++ b/listings/ch00-getting-started/storage/src/minimal_contract.cairo @@ -0,0 +1,5 @@ +#[starknet::contract] +mod Contract { + #[storage] + struct Storage {} +}