Skip to content

Commit

Permalink
Merge pull request #2 from pyncz/chore/v0.1.1
Browse files Browse the repository at this point in the history
chore: Add `migrate` entry point
  • Loading branch information
pyncz committed Jun 14, 2024
2 parents f19bdd3 + 7466e7a commit 6fffd28
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 15 deletions.
18 changes: 10 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
]

[workspace.package]
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"
authors = [ "Pau Yankovski <[email protected]>" ]
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde = { version = "1.0.103", default-features = false, features = [ "derive" ]
serde_json = "1.0"
schemars = "0.8.1"
thiserror = "1.0"
semver = "1.0"
cosmwasm-schema = "1.1.4"
itertools = "0.12.0"

Expand Down
23 changes: 23 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

// Migration errors
#[error("Semver parsing error: {0}")]
SemVer(String),

#[error("Cannot change contract name from \"{original_name}\" to \"{new_name}\"! The name should be immutable")]
InvalidMigrationContractName {
original_name: String,
new_name: String,
},

#[error("Cannot change contract version from {current_version} to {new_version}! New version should be higher than the current one")]
InvalidMigrationVersion {
current_version: String,
new_version: String,
},

// Execution errors
#[error("Unauthorized! {sender} is not contract admin")]
NotAdmin { sender: Addr },

Expand Down Expand Up @@ -40,5 +57,11 @@ pub enum ContractError {
NotEquipped {},
}

impl From<semver::Error> for ContractError {
fn from(err: semver::Error) -> Self {
Self::SemVer(err.to_string())
}
}

pub type ContractResult<T = ()> = Result<T, ContractError>;
pub type ContractResponse = ContractResult<Response>;
2 changes: 1 addition & 1 deletion example/client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fiend-frens-client",
"type": "module",
"version": "0.1.0",
"version": "0.1.1",
"description": "A demo website to view, equip and unequip trait tokens for the **Fiend Frens** NFTs",
"scripts": {
"dev": "nuxt dev",
Expand Down
1 change: 1 addition & 0 deletions example/contracts/cw-fiend-frens-constructor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ cosmwasm-schema = "^1.2"
cosmwasm-std = "^1.2"
cw2 = "^1.1"
cw-fiend-frens-shared = { path = "../shared" }
semver = "1"
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use cosmwasm_schema::write_api;
use cosmwasm_std::Empty;
use cw_constructor::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg
query: QueryMsg,
migrate: Empty
}
}
35 changes: 34 additions & 1 deletion example/contracts/cw-fiend-frens-constructor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cw_constructor::contract::Contract as ConstructorContract;
use cw_constructor::error::ContractResponse;
use cw_constructor::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use cw_fiend_frens_shared::metadata::{Extension, MergedExtension, TraitExtension};
use semver::Version;

// Version info for migration
const CONTRACT_NAME: &str = "fiend-frens-constructor";
Expand All @@ -14,7 +15,8 @@ pub mod entry {

#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, StdResult};
use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdResult};
use cw_constructor::error::ContractError;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -45,4 +47,35 @@ pub mod entry {
let contract = Contract::default();
contract.query(deps, env, msg)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> ContractResponse {
// Current contract state
let current = cw2::get_contract_version(deps.storage)?;
let original_name = current.contract;
let current_version = current.version;
let current_version_semver: Version = current_version.parse()?;

// New state to migrate to
let new_name = CONTRACT_NAME.to_string();
let new_version: String = CONTRACT_VERSION.to_string();
let new_version_semver: Version = new_version.parse()?;

// Validate migration params
if original_name != new_name {
return Err(ContractError::InvalidMigrationContractName {
original_name,
new_name,
});
}
if current_version_semver >= new_version_semver {
return Err(ContractError::InvalidMigrationVersion {
current_version,
new_version,
});
}

cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
}
}
2 changes: 1 addition & 1 deletion example/scripts/instantiate.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
"tx": "278E32692E897F8EF1EC27C214572DD095DC7973913D6102D57F65753B77BAEC"
},
"fiend-frens-constructor": {
"code_id": "2566",
"code_id": "2912",
"address": "archway13cj0z0zgca02wdlum855fapu9u7hfprwqcczefr2mf0ep75lh7ms3anpjy",
"admin": {
"$var": "sender"
Expand Down
2 changes: 1 addition & 1 deletion example/scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fiend-frens-scripts",
"type": "module",
"version": "0.1.0",
"version": "0.1.1",
"description": "Scripts to upload NFTs' assets on IPFS, instantiate example contracts etc",
"scripts": {
"upload": "esno ./src/upload/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cw-constructor",
"type": "module",
"version": "0.1.0",
"version": "0.1.1",
"description": "Constructor contract to bind NFTs as traits for another NFT",
"author": "Pau Yankovski <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit 6fffd28

Please sign in to comment.