NEWTON

NEWTON


Popular tags

    Namespaces in Cairo

    Asked

    6 months ago

    5

    views


    0

    What are the use cases for namespaces? Why would someone use a namespace?

    Please give examples

    This question was originally posted on Triality

      cairocairo-lang

    1 answers

    0

    A main use case for namespaces is to define an interface to your contract that the outside world (other contracts) can call. For example, if in your contract you had the 2 functions increase_balance and get_balance, you could then create a namespace with the function names (omit the bodies):

    @contract_interface

    namespace IBalanceContract:

    func increase_balance(amount : felt):

    end

    

    func get_balance() -> (res : felt):

    end

    end

    Now other contracts can call your contract like:

    IBalanceContract.increaseBalance(contract_address=contract_address,amount=amount)

    Notice how the contract_address was passed in as the first argument to the interface function, even though it was not explicitly defined in the interface/namespace

    This answer was originally posted on Triality

    answered

    6 months ago

    Your answer

    NEWTON

    NEWTON