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 Store trait for our custom type. Hopefully, we can just derive this trait for our custom type - unless it contains arrays or dictionaries.

pub trait IStoringCustomType<TContractState> {
    fn set_person(ref self: TContractState, person: Person);

// Deriving the starknet::Store trait
// allows us to store the `Person` struct in the contract's storage.
#[derive(Drop, Serde, Copy, starknet::Store)]
pub struct Person {
    pub age: u8,
    pub name: felt252

pub mod StoringCustomType {
    use super::Person;

    pub struct Storage {
        pub person: Person

    impl StoringCustomType of super::IStoringCustomType<ContractState> {
        fn set_person(ref self: ContractState, person: Person) {

mod tests {
    use super::{
        IStoringCustomType, StoringCustomType, Person,

    fn can_call_set_person() {
        let mut state = StoringCustomType::contract_state_for_testing();

        let person = Person { age: 10, name: 'Joe' };

        let read_person =;

        assert(person.age == read_person.age, 'wrong age');
        assert( ==, 'wrong name');
Last change: 2024-05-03, commit: cb31be2