cairo-contracts icon indicating copy to clipboard operation
cairo-contracts copied to clipboard

Add `assert_not_initialized` and `assert_initialized` to initializable.cairo

Open 0xtonysprocket opened this issue 2 years ago • 3 comments

🧐 Motivation It is easy to import Initializable library and is used often especially with proxys but lacks these assert functions

0xtonysprocket avatar Jul 11 '22 19:07 0xtonysprocket

I don't think that's present even in the solidity version of OpenZeppelin Contracts. Can you share an example of how would you use this feature?

martriay avatar Jul 12 '22 21:07 martriay

Sure, when I'm deploying a proxy contract I want to get rid of the constructor in the library contract. So I replace it with an initialize function. In that initialize function I then want to check that the contract hasn't been previously initialized like this:

func setup_pool{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
        router : felt, name : felt, symbol : felt, decimals : felt):
    assert_not_initialized()

    # set lp token name, symbol, and decimals
    ERC20.initializer(name, symbol, decimals)

    # set factory as owner
    Ownable.initializer(router)
    
    # set as initialized
    Initializable.initialize()
    
    return ()
end

So currently to do this I"m importing the Initialized library and the storage var and then creating the assert function myself. It would be nice to just import the library and call the assert functions there.

0xtonysprocket avatar Jul 13 '22 14:07 0xtonysprocket

Ah, this made me go look at the initialize function again and I see it performs this check internally. I guess the only time this would actually be useful is to make sure that other functions aren't called before the contract is initialized. Probably not as important as I thought. Thanks for your reply!

0xtonysprocket avatar Jul 13 '22 14:07 0xtonysprocket

Closing this since it will not be relevant anymore after the ongoing Cairo 1.0 migration. If you think this is a mistake, feel free to open a new issue.

martriay avatar Feb 16 '23 21:02 martriay