From 82e4c953058953b671f397efba86aaf1a15b5664 Mon Sep 17 00:00:00 2001 From: julienbrs Date: Mon, 13 Nov 2023 12:31:28 +0100 Subject: [PATCH 1/5] feat: add Tuples in cheatsheet --- .../cairo_cheatsheet/src/tuple_example.cairo | 24 +++++++++++++++++++ src/SUMMARY.md | 1 + src/ch00/cairo_cheatsheet/tuples.md | 8 +++++++ 3 files changed, 33 insertions(+) create mode 100644 listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo create mode 100644 src/ch00/cairo_cheatsheet/tuples.md diff --git a/listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo b/listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo new file mode 100644 index 00000000..b795c1b9 --- /dev/null +++ b/listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo @@ -0,0 +1,24 @@ +#[starknet::contract] +mod TupleExample { + use starknet::{ContractAddress, get_caller_address}; + + #[storage] + struct Storage { + user_data: (ContractAddress, u64, bool) + } + + #[external(v0)] + #[generate_trait] + impl TupleExampleImpl of ITupleExampleImpl { + fn store_tuple(ref self: ContractState, address: ContractAddress, age: u64, active: bool) { + let user_tuple = (address, age, active); + self.user_data.write(user_tuple); + } + + fn read_tuple(self: @ContractState) -> (ContractAddress, u64, bool) { + let stored_tuple = self.user_data.read(); + let (address, age, active) = stored_tuple; + (address, age, active) + } + } +} diff --git a/src/SUMMARY.md b/src/SUMMARY.md index c32252a2..85a76907 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -26,6 +26,7 @@ Summary - [Arrays](./ch00/cairo_cheatsheet/arrays.md) - [Loop](./ch00/cairo_cheatsheet/loop.md) - [Match](./ch00/cairo_cheatsheet/match.md) + - [Tuples](./ch00/cairo_cheatsheet/tuples.md) - [Struct](./ch00/cairo_cheatsheet/struct.md) - [Type casting](./ch00/cairo_cheatsheet/type_casting.md) diff --git a/src/ch00/cairo_cheatsheet/tuples.md b/src/ch00/cairo_cheatsheet/tuples.md new file mode 100644 index 00000000..01eb8866 --- /dev/null +++ b/src/ch00/cairo_cheatsheet/tuples.md @@ -0,0 +1,8 @@ +# Tuples + +Tuples is a data type to group a fixed number of items of potentially different types into a single compound structure. Unlike arrays, tuples have a set length and can contain elements of varying types. Once a tuple is created, its size cannot change. +For example: + +```rust +{{#include ../../../listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo}} +``` \ No newline at end of file From 82c008f7dbcea756fbe6e2ae4a721715a131fd0a Mon Sep 17 00:00:00 2001 From: julienbrs Date: Mon, 13 Nov 2023 20:02:31 +0100 Subject: [PATCH 2/5] nit: trigger ci --- src/ch00/cairo_cheatsheet/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch00/cairo_cheatsheet/tuples.md b/src/ch00/cairo_cheatsheet/tuples.md index 01eb8866..b72c8c89 100644 --- a/src/ch00/cairo_cheatsheet/tuples.md +++ b/src/ch00/cairo_cheatsheet/tuples.md @@ -5,4 +5,4 @@ For example: ```rust {{#include ../../../listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo}} -``` \ No newline at end of file +``` From 727ecf05dfff4c73bbfbdf5dcfb23eb043dffe61 Mon Sep 17 00:00:00 2001 From: julienbrs Date: Tue, 14 Nov 2023 22:21:56 +0100 Subject: [PATCH 3/5] feat: tests of custom type serde --- .../custom_type_serde/src/contract.cairo | 6 +++ .../custom_type_serde/src/tests.cairo | 50 ++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo index 2b35f76d..ae81cd5a 100644 --- a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo +++ b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo @@ -1,3 +1,9 @@ +#[starknet::interface] +trait ISerdeCustomType { + fn person_input(ref self: TContractState, person: SerdeCustomType::Person); + fn person_output(ref self: TContractState) -> SerdeCustomType::Person; +} + #[starknet::contract] mod SerdeCustomType { #[storage] diff --git a/listings/ch00-getting-started/custom_type_serde/src/tests.cairo b/listings/ch00-getting-started/custom_type_serde/src/tests.cairo index 361dba07..0ef4b63d 100644 --- a/listings/ch00-getting-started/custom_type_serde/src/tests.cairo +++ b/listings/ch00-getting-started/custom_type_serde/src/tests.cairo @@ -1,2 +1,50 @@ -mod tests { // TODO +// The purpose of these tests is to demonstrate the capability of using custom types as inputs and outputs in contract calls. +// Therefore, we are not employing getters and setters for managing the contract's state. + +mod tests { + use custom_type_serde::contract::{ + SerdeCustomType, SerdeCustomType::Person, ISerdeCustomTypeDispatcher, + ISerdeCustomTypeDispatcherTrait + }; + use core::result::ResultTrait; + use debug::PrintTrait; + use starknet::{deploy_syscall, ContractAddress}; + use starknet::class_hash::Felt252TryIntoClassHash; + + fn deploy() -> ISerdeCustomTypeDispatcher { + let calldata: Array = array![]; + let (address0, _) = deploy_syscall( + SerdeCustomType::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false + ) + .unwrap(); + ISerdeCustomTypeDispatcher { contract_address: address0 } + } + + #[test] + #[available_gas(2000000000)] + fn should_deploy() { + let init_value = 10; + let contract = deploy(); + } + + #[test] + #[available_gas(2000000000)] + fn should_get_person_output() { + let contract = deploy(); + let expected_person = Person { age: 10, name: 'Joe' }; + let received_person = contract.person_output(); + let age_received = received_person.age; + let name_received = received_person.name; + + assert(age_received == expected_person.age, 'Wrong age value'); + assert(name_received == expected_person.name, 'Wrong name value'); + } + + #[test] + #[available_gas(2000000000)] + fn should_call_person_input() { + let contract = deploy(); + let expected_person = Person { age: 10, name: 'Joe' }; + let received_person = contract.person_input(expected_person); + } } From 22fe713e7e805296fb66f2620ec36b6720d9147f Mon Sep 17 00:00:00 2001 From: julienbrs Date: Wed, 15 Nov 2023 01:24:36 +0100 Subject: [PATCH 4/5] appying improvement --- .../custom_type_serde/src/contract.cairo | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo index ae81cd5a..0a70122a 100644 --- a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo +++ b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo @@ -1,7 +1,7 @@ #[starknet::interface] trait ISerdeCustomType { fn person_input(ref self: TContractState, person: SerdeCustomType::Person); - fn person_output(ref self: TContractState) -> SerdeCustomType::Person; + fn person_output(self: @TContractState) -> SerdeCustomType::Person; } #[starknet::contract] @@ -18,8 +18,7 @@ mod SerdeCustomType { } #[abi(per_item)] - #[generate_trait] - impl SerdeCustomType of ISerdeCustomType { + impl SerdeCustomType of super::ISerdeCustomType { #[external(v0)] fn person_input(ref self: ContractState, person: Person) {} From 8ccf643241d7026230062fcae4f06ebf924c4d0b Mon Sep 17 00:00:00 2001 From: julienbrs Date: Wed, 15 Nov 2023 01:39:45 +0100 Subject: [PATCH 5/5] nit: fix embed --- .../ch00-getting-started/custom_type_serde/src/contract.cairo | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo index 0a70122a..e4981b29 100644 --- a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo +++ b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo @@ -17,12 +17,10 @@ mod SerdeCustomType { name: felt252 } - #[abi(per_item)] + #[abi(embed_v0)] impl SerdeCustomType of super::ISerdeCustomType { - #[external(v0)] fn person_input(ref self: ContractState, person: Person) {} - #[external(v0)] fn person_output(self: @ContractState) -> Person { Person { age: 10, name: 'Joe' } }