If functions outside of the implementation block are private. What's the point of having a public function that is outside of ABI block?

The ABI is for external calls only therefore you cannot define a method in the ABI and call it in the same contract. If you want to define a function for a contract, but keep it private so that only your contract can call it, you can define it outside of the impl and call it inside the contract, similar to the return_45() function above.

If functions outside of the implementation block are private. What’s the point of having a public function that is outside of ABI block?

#[storage(read, write)]
pub fn _mint(recipient: Identity, amount: u64) {
    use std::asset::mint_to;
 
    let supply = storage.total_supply.read();
    storage.total_supply.write(supply + amount);
    mint_to(recipient, PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount);
}
 
#[storage(read, write)]
pub fn _burn(asset_id: AssetId, amount: u64) {
    use std::{asset::burn, context::this_balance};
 
    require(
        this_balance(asset_id) >= amount,
        "BurnError::NotEnoughCoins",
    );
    // If we pass the check above, we can assume it is safe to unwrap.
    let supply = storage.total_supply.read();
    storage.total_supply.write(supply - amount);
    burn(PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount);
}
5 Likes

Functions declared outside the impl block are not part of the ABI and cannot be called externally. They are accessible only within the contract, which serves two main purposes:

  1. Access Control: These functions are “internal,” ensuring that only the contract’s logic can invoke them, improving security by preventing external interaction.

  2. Code Reusability: Internal functions can be reused across multiple public functions, simplifying the code and reducing duplication.

Declaring such functions as pub allows them to be accessed within the project (e.g., for testing or auxiliary logic) but not externally, as they are not part of the ABI. Therefore, “public” functions outside the ABI block are beneficial for internal use and modular design but cannot be called from outside the contract. If external accessibility is required, they must be included in the impl block and ABI.

1 Like

Can you paste the documentation where explicitly stating pub allow them for testing/auxiliary logic? I don’t think there is a purpose to specify pub outside of ABI, and the compiler.

1 Like