Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make upgrade logics public #119

Merged
merged 1 commit into from
Apr 24, 2023
Merged

Make upgrade logics public #119

merged 1 commit into from
Apr 24, 2023

Conversation

larry0x
Copy link
Collaborator

@larry0x larry0x commented Apr 22, 2023

Currently cw721_base::upgrades is not exported, but it should be. It is needed by custom NFT contracts that extends cw721-base to implement their custom migration logics.

Let's consider a contract called sg721-base that extends cw721-base. Also assume:

  • sg721-base 0.25 uses cw721-base 0.16
  • sg721-base 0.26 uses cw721-base 0.17

How should sg721-base implement its 0.25 => 0.26 migration? Imo, the cleanest way is this:

const CONTRACT_NAME: &str = "crates.io:sg721-base";
const FROM_VERSION: &str = "0.25.0";
const TO_VERSION: &str = "0.26.0"

pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response> {
    // check the correct contract is being upgraded,
    // and it's being migrated from the correct version
    cw2::assert_contract_version(deps.as_ref(), CONTRACT_NAME, FROM_VERSION)?;

    // update contract version info
    cw2::set_contract_version(deps, CONTRACT_NAME, TO_VERSION)?;

    // run the base contract's migration logics
    cw721_base::upgrades::v0_17::migrate(deps)?;

    // other custom migration logics specific to sg721...
}

This is somewhat similar to how inheritance works in Python:

Class Cw721Base:
    def migrate(self, deps):
        # base contract migration logics...

Class Sg721Base(Cw721Base):
    def migrate(self, deps):
        # run base contract migration logics:
        __super__().migrate(deps)

        # other custom migration logics specific to sg721...

See how the child contract needs to invoke cw721_base::upgrades::v0_17::migrate similar to how the python class invokes its parent's method by __super__.

Similarly, if a contract extends sg721-base, then it should also invoke sg721_base::upgrades::v0_26::migrate in its custom migration method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants