Let's compare 2 things how we can deploy a contractA
.
Way 1:
contract A {
constructor(uint a, uint b) {
aState = a;
bState = b;
}
}
// deploy from contractB
A a = new A(5, 10);
Way 2:
contract A {
initialize(uint a, uint b) initializer {
aState = a;
bState = b;
}
}
A a = new A();
a.initialize(5, 10);
I was wondering if there's any difference between the above 2.
The thing I am trying to achieve is to use the second way, though, I am not using second way to just have a way of deploying proxies and then calling initialize
on it. I want to be able to deploy contracts (completely new, their own ones - without proxies and still use initialize
)
Do you find any downside on the Way 2 ?
It could be a bummer and some people might forget to call initialize after calling A a = new A();
So maybe the following way might be the way to go.
contract Test {
constructor(uint _a) public {
initialize(_a);
}
function initialize(uint _a) public initializer {
a = _a;
}
}
The thing I am trying to achieve is to have the possibility so that:
a. I can create a plain(without any proxies) contract
b. I can create a proxy contract.
So at first, Test
will be deployed as a base contract.
- If I need to create a proxy contract, I will use OZ's
clone
and then call initialize
on this.
- If i need a plain contract, I will just directly again deploy
Test
with a new
keyword.
Is there any downside or bug in my scenario ? Even if with this way I will use UUPS
upgradablity as the 3rd option so I support all 3 options.
@frangio
1 Like
Please don't tag me specifically. The forum is open for anyone to answer.
I don't see a downside to having only an initializer function and no constructor, as long as have a factory contract that can deploy the contract and initialize it in one go to avoid being frontrun.
Your new proposal is a good alternative. Define an initializer function, and call the initializer in the constructor. The only downside is that you will need to provide arguments even when you deploy the implementation for a proxy. You want to make sure the implementation contract is not usable directly so you need to choose the arguments carefully for that.
1 Like