Skip to content

Commit

Permalink
test: ch00/custom-types-entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed May 2, 2024
1 parent cb31be2 commit 8d1b0b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
55 changes: 49 additions & 6 deletions listings/getting-started/custom_type_serde/src/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#[starknet::interface]
pub trait ISerdeCustomType<TContractState> {
fn person_input(ref self: TContractState, person: Person);
fn person_output(self: @TContractState) -> Person;
}

// ANCHOR: contract
// Deriving the `Serde` trait allows us to use
// the Person type as an entrypoint parameter and return value
#[derive(Drop, Serde)]
Expand All @@ -6,12 +13,6 @@ pub struct Person {
pub name: felt252
}

#[starknet::interface]
pub trait ISerdeCustomType<TContractState> {
fn person_input(ref self: TContractState, person: Person);
fn person_output(self: @TContractState) -> Person;
}

#[starknet::contract]
pub mod SerdeCustomType {
use super::Person;
Expand All @@ -28,3 +29,45 @@ pub mod SerdeCustomType {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod tests {
use super::{
SerdeCustomType, Person, ISerdeCustomTypeDispatcher, ISerdeCustomTypeDispatcherTrait
};
use starknet::{ContractAddress, syscalls::deploy_syscall, SyscallResultTrait};

fn deploy() -> ISerdeCustomTypeDispatcher {
let (contract_address, _) = deploy_syscall(
SerdeCustomType::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
ISerdeCustomTypeDispatcher { contract_address }
}

#[test]
fn should_deploy() {
deploy();
}

#[test]
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' };
contract.person_input(expected_person);
}
}
3 changes: 0 additions & 3 deletions listings/getting-started/custom_type_serde/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
mod contract;

#[cfg(test)]
mod tests;
47 changes: 0 additions & 47 deletions listings/getting-started/custom_type_serde/src/tests.cairo

This file was deleted.

5 changes: 4 additions & 1 deletion src/ch00/basics/custom-types-in-entrypoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Using custom types in entrypoints requires our type to implement the `Serde` trait. This is because when calling an entrypoint, the input is sent as an array of `felt252` to the entrypoint, and we need to be able to deserialize it into our custom type. Similarly, when returning a custom type from an entrypoint, we need to be able to serialize it into an array of `felt252`.
Thankfully, we can just derive the `Serde` trait for our custom type.

The purpose is to only show the capability of using custom types as inputs and outputs in contract calls.
We are not employing getters and setters for managing the contract's state in this example for simplicity.

```rust
{{#include ../../../listings/getting-started/custom_type_serde/src/contract.cairo}}
{{#rustdoc_include ../../../listings/getting-started/custom_type_serde/src/contract.cairo:contract}}
```

0 comments on commit 8d1b0b5

Please sign in to comment.