A deeper dive into contracts

In the previous section, we gave an introductory example of a smart contract written in Cairo. 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 public functions, external functions and view functions, and we mentioned how to interact with storage.

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

  • How do I define internal/private functions?
  • How can I emit events? How can I index them?
  • Where should I define functions that do not need to access the contract's state?
  • Is there a way to reduce the boilerplate?
  • How can I store more complex data types?

Luckily, we'll be answering all these questions in this chapter. Let's consider the following example contract that we'll be using throughout this chapter:

use starknet::ContractAddress;

trait INameRegistry<TContractState> {
    fn store_name(ref self: TContractState, name: felt252);
    fn get_name(self: @TContractState, address: ContractAddress) -> felt252;

mod NameRegistry {
    use starknet::{ContractAddress, get_caller_address};

    struct Storage {
        names: LegacyMap::<ContractAddress, felt252>,
        total_names: u128,
        owner: Person

    #[derive(Drop, starknet::Event)]
    enum Event {
        StoredName: StoredName,

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

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

    fn constructor(ref self: ContractState, owner: Person) {
        self.names.write(owner.address, owner.name);

    impl NameRegistry of super::INameRegistry<ContractState> {
        fn store_name(ref self: ContractState, name: felt252) {
            let caller = get_caller_address();
            self._store_name(caller, name);

        fn get_name(self: @ContractState, address: ContractAddress) -> felt252 {
            let name = self.names.read(address);

    impl InternalFunctions of InternalFunctionsTrait {
        fn _store_name(ref self: ContractState, user: ContractAddress, name: felt252) {
            let mut total_names = self.total_names.read();
            self.names.write(user, name);
            self.total_names.write(total_names + 1);
            self.emit(StoredName { user: user, name: name });


    fn _get_contract_name() -> felt252 {
        'Name Registry'

Listing 99-1bis: Our reference contract for this chapter