Building Starknet Smart Contracts

In the previous section, we gave an introductory example of a smart contract written in Cairo, describing the basic blocks to build smart contracts on Starknet. In this section, we'll be taking a deeper look at all the components of a smart contract, step by step.

When we discussed interfaces, we specified the difference between the two types of public functions, i.e., external functions and view functions, and we mentioned how to interact with the storage of a contract.

At this point, you should have multiple questions that come to mind:

  • How can I store more complex data types?
  • How do I define internal/private functions?
  • How can I emit events? How can I index them?
  • Is there a way to reduce the boilerplate?

Luckily, we'll be answering all these questions in this chapter. Let's consider the NameRegistry contract in Listing 14-1 that we'll be using throughout this chapter:

use core::starknet::ContractAddress;

#[starknet::interface]
pub trait INameRegistry<TContractState> {
    fn store_name(
        ref self: TContractState, name: felt252, registration_type: NameRegistry::RegistrationType,
    );
    fn get_name(self: @TContractState, address: ContractAddress) -> felt252;
    fn get_owner(self: @TContractState) -> NameRegistry::Person;
    fn get_owner_name(self: @TContractState) -> felt252;
    fn get_registration_info(
        self: @TContractState, address: ContractAddress,
    ) -> NameRegistry::RegistrationInfo;
}

#[starknet::contract]
mod NameRegistry {
    use core::starknet::{ContractAddress, get_caller_address};
    use core::starknet::storage::{
        Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess,
    };

    #[storage]
    struct Storage {
        names: Map::<ContractAddress, felt252>,
        owner: Person,
        registrations: Map<ContractAddress, RegistrationNode>,
        total_names: u128,
    }

    #[event]
    #[derive(Drop, starknet::Event)]
    enum Event {
        StoredName: StoredName,
    }
    #[derive(Drop, starknet::Event)]
    struct StoredName {
        #[key]
        user: ContractAddress,
        name: felt252,
    }

    #[derive(Drop, Serde, starknet::Store)]
    pub struct Person {
        address: ContractAddress,
        name: felt252,
    }

    #[derive(Copy, Drop, Serde, starknet::Store)]
    pub enum RegistrationType {
        Finite: u64,
        #[default]
        Infinite,
    }

    #[starknet::storage_node]
    struct RegistrationNode {
        count: u64,
        info: RegistrationInfo,
        history: Map<u64, RegistrationInfo>,
    }

    #[derive(Copy, Drop, Serde, starknet::Store)]
    pub struct RegistrationInfo {
        name: felt252,
        registration_type: RegistrationType,
        registration_date: u64,
    }

    #[constructor]
    fn constructor(ref self: ContractState, owner: Person) {
        self.names.entry(owner.address).write(owner.name);
        self.total_names.write(1);
        self.owner.write(owner);
    }

    // Public functions inside an impl block
    #[abi(embed_v0)]
    impl NameRegistry of super::INameRegistry<ContractState> {
        fn store_name(ref self: ContractState, name: felt252, registration_type: RegistrationType) {
            let caller = get_caller_address();
            self._store_name(caller, name, registration_type);
        }

        fn get_name(self: @ContractState, address: ContractAddress) -> felt252 {
            self.names.entry(address).read()
        }

        fn get_owner(self: @ContractState) -> Person {
            self.owner.read()
        }

        fn get_owner_name(self: @ContractState) -> felt252 {
            self.owner.name.read()
        }

        fn get_registration_info(
            self: @ContractState, address: ContractAddress,
        ) -> RegistrationInfo {
            self.registrations.entry(address).info.read()
        }
    }

    // Standalone public function
    #[external(v0)]
    fn get_contract_name(self: @ContractState) -> felt252 {
        'Name Registry'
    }

    // Could be a group of functions about a same topic
    #[generate_trait]
    impl InternalFunctions of InternalFunctionsTrait {
        fn _store_name(
            ref self: ContractState,
            user: ContractAddress,
            name: felt252,
            registration_type: RegistrationType,
        ) {
            let total_names = self.total_names.read();

            self.names.entry(user).write(name);

            let registration_info = RegistrationInfo {
                name: name,
                registration_type: registration_type,
                registration_date: starknet::get_block_timestamp(),
            };
            let mut registration_node = self.registrations.entry(user);
            registration_node.info.write(registration_info);

            let count = registration_node.count.read();
            registration_node.history.entry(count).write(registration_info);
            registration_node.count.write(count + 1);

            self.total_names.write(total_names + 1);

            self.emit(StoredName { user: user, name: name });
        }
    }

    // Free function
    fn get_owner_storage_address(self: @ContractState) -> felt252 {
        self.owner.__base_address__
    }
}

Listing 14-1: Our reference contract for this chapter