Skip to content

Commit

Permalink
Add migration code
Browse files Browse the repository at this point in the history
  • Loading branch information
MightOfOaks committed Apr 24, 2024
1 parent 5e7d9be commit 3a5ba09
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/dydx-airdrop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ sha2 = { workspace = true }
sha3 = "0.10"
schemars = { workspace = true }
serde = { workspace = true }
semver = { workspace = true }
sg-std = { workspace = true }
thiserror = { workspace = true }
vending-minter = { workspace = true, features = ["library"] }
Expand Down
37 changes: 36 additions & 1 deletion contracts/dydx-airdrop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg};
use crate::state::{AIRDROP_COUNT, CONFIG};

use cosmwasm_std::{attr, ensure, entry_point, BankMsg, Coin, CosmosMsg, Uint128};
use cosmwasm_std::{
attr, ensure, entry_point, BankMsg, Coin, CosmosMsg, Empty, Event, StdError, Uint128,
};
use cosmwasm_std::{DepsMut, Env, MessageInfo};
use cw2::set_contract_version;
use semver::Version;
use sg1::fair_burn;
use sg_std::{Response, NATIVE_DENOM};

Expand Down Expand Up @@ -116,3 +119,35 @@ pub fn withdraw_remaining(
]);
Ok(res)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
let current_version = cw2::get_contract_version(deps.storage)?;
if current_version.contract != CONTRACT_NAME {
return Err(StdError::generic_err("Cannot upgrade to a different contract").into());
}
let version: Version = current_version
.version
.parse()
.map_err(|_| StdError::generic_err("Invalid contract version"))?;
let new_version: Version = CONTRACT_VERSION
.parse()
.map_err(|_| StdError::generic_err("Invalid contract version"))?;

if version > new_version {
return Err(StdError::generic_err("Cannot upgrade to a previous contract version").into());
}
// if same version return
if version == new_version {
return Ok(Response::new());
}

// set new contract version
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let event = Event::new("migrate")
.add_attribute("from_name", current_version.contract)
.add_attribute("from_version", current_version.version)
.add_attribute("to_name", CONTRACT_NAME)
.add_attribute("to_version", CONTRACT_VERSION);
Ok(Response::new().add_event(event))
}

0 comments on commit 3a5ba09

Please sign in to comment.