Prevent repetitive write of cw2 version info during instantiation #110
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
To understand what the problem is, let's first talk about how to extend/inherit/customize the base cw721 contract. Using Rust's "composition" pattern, the way to do this is:
(this is what we do with stargaze's sg721-base, btw!)
Then, during instantiation, I simply invoke the base contract's
instantiate
method:The problem
cw721-base initializes the cw2 version info inside the
Cw721Contract::instantiate
method. This means, by runningtract.base.instantiate
, the base contract overwrites my custom cw2 info!To avoid this, we have to run
cw2::set_contract_version
AFTERtract.base.instantiate
. This means the cw2 storage slot is written TWICE during instantiation, once by the base contract, once by my custom contract.This problem is present in cw721-metadata-onchain for example, see the comment on L54:
https://github.com/CosmWasm/cw-nfts/blob/a9e4ca67a0d13f56bd38cfcdd69cdaa2548f5de9/contracts/cw721-metadata-onchain/src/lib.rs#L53-L57
The solution
The initialization of cw2 version should NOT be in
Cw721Contract::instantiate
, but incw721_base::entry::instantiate
. This way, by runningCw721Contract::instantiate
the base contract does NOT overwrite the custom cw2 info.