What is the difference between initializer, reinitializer(1), and reinitializer(2) modifiers?
These modifiers come from the Initializable contract. Both initialize
and reinitialize
are meant to execute logic only once when the modified function is called. They're similar to regular constructors but they're meant to be used within upgradeable contracts.
Usually an initializer
is used first and then a reinitializer
MAY be used in future upgrades if a module that requires initialization is added. The difference between them relies on that initializer
allows nesting with other initializers
. This is because a contract may initialize multiple contracts in the inheritance tree.
Often, reinitializer(1)
is not used because each initialization has a version number that starts in 0 and is updated to 1 with initializer
, so you can't reuse version 1. You can theoretically use reinitializer(1)
but it'll revert if there are nested initializers.
You can find more details in the API documentation.
Thanks for the clear explanation.
How will an individual know if there is a nested initilaizer in the contract?
Do you mean calling a contract that uses initialize() as Is
in a contract that also wants to apply that?
@ernestognw I'm kinda confused🤔
The rule of thumb is that you'll need nested initializers in an upgradeable contract if you'd require multiple constructors its non-upgradeable version.
Reinitializing is when only one of the inherited modules is updated, and you need to reset values only on the upgraded module.
Ohh roger that, thanks!