Skip to content

Commit 6fffd28

Browse files
authored
Merge pull request #2 from pyncz/chore/v0.1.1
chore: Add `migrate` entry point
2 parents f19bdd3 + 7466e7a commit 6fffd28

File tree

11 files changed

+77
-15
lines changed

11 files changed

+77
-15
lines changed

Cargo.lock

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ members = [
55
]
66

77
[workspace.package]
8-
version = "0.1.0"
8+
version = "0.1.1"
99
edition = "2021"
1010
license = "MIT"
1111
authors = [ "Pau Yankovski <pyncz.dev@gmail.com>" ]

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ serde = { version = "1.0.103", default-features = false, features = [ "derive" ]
2828
serde_json = "1.0"
2929
schemars = "0.8.1"
3030
thiserror = "1.0"
31+
semver = "1.0"
3132
cosmwasm-schema = "1.1.4"
3233
itertools = "0.12.0"
3334

core/src/error.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ pub enum ContractError {
66
#[error("{0}")]
77
Std(#[from] StdError),
88

9+
// Migration errors
10+
#[error("Semver parsing error: {0}")]
11+
SemVer(String),
12+
13+
#[error("Cannot change contract name from \"{original_name}\" to \"{new_name}\"! The name should be immutable")]
14+
InvalidMigrationContractName {
15+
original_name: String,
16+
new_name: String,
17+
},
18+
19+
#[error("Cannot change contract version from {current_version} to {new_version}! New version should be higher than the current one")]
20+
InvalidMigrationVersion {
21+
current_version: String,
22+
new_version: String,
23+
},
24+
25+
// Execution errors
926
#[error("Unauthorized! {sender} is not contract admin")]
1027
NotAdmin { sender: Addr },
1128

@@ -40,5 +57,11 @@ pub enum ContractError {
4057
NotEquipped {},
4158
}
4259

60+
impl From<semver::Error> for ContractError {
61+
fn from(err: semver::Error) -> Self {
62+
Self::SemVer(err.to_string())
63+
}
64+
}
65+
4366
pub type ContractResult<T = ()> = Result<T, ContractError>;
4467
pub type ContractResponse = ContractResult<Response>;

example/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fiend-frens-client",
33
"type": "module",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"description": "A demo website to view, equip and unequip trait tokens for the **Fiend Frens** NFTs",
66
"scripts": {
77
"dev": "nuxt dev",

example/contracts/cw-fiend-frens-constructor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ cosmwasm-schema = "^1.2"
2525
cosmwasm-std = "^1.2"
2626
cw2 = "^1.1"
2727
cw-fiend-frens-shared = { path = "../shared" }
28+
semver = "1"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use cosmwasm_schema::write_api;
2+
use cosmwasm_std::Empty;
23
use cw_constructor::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
34

45
fn main() {
56
write_api! {
67
instantiate: InstantiateMsg,
78
execute: ExecuteMsg,
8-
query: QueryMsg
9+
query: QueryMsg,
10+
migrate: Empty
911
}
1012
}

example/contracts/cw-fiend-frens-constructor/src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use cw_constructor::contract::Contract as ConstructorContract;
22
use cw_constructor::error::ContractResponse;
33
use cw_constructor::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
44
use cw_fiend_frens_shared::metadata::{Extension, MergedExtension, TraitExtension};
5+
use semver::Version;
56

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

1516
#[cfg(not(feature = "library"))]
1617
use cosmwasm_std::entry_point;
17-
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, StdResult};
18+
use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdResult};
19+
use cw_constructor::error::ContractError;
1820

1921
#[cfg_attr(not(feature = "library"), entry_point)]
2022
pub fn instantiate(
@@ -45,4 +47,35 @@ pub mod entry {
4547
let contract = Contract::default();
4648
contract.query(deps, env, msg)
4749
}
50+
51+
#[cfg_attr(not(feature = "library"), entry_point)]
52+
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> ContractResponse {
53+
// Current contract state
54+
let current = cw2::get_contract_version(deps.storage)?;
55+
let original_name = current.contract;
56+
let current_version = current.version;
57+
let current_version_semver: Version = current_version.parse()?;
58+
59+
// New state to migrate to
60+
let new_name = CONTRACT_NAME.to_string();
61+
let new_version: String = CONTRACT_VERSION.to_string();
62+
let new_version_semver: Version = new_version.parse()?;
63+
64+
// Validate migration params
65+
if original_name != new_name {
66+
return Err(ContractError::InvalidMigrationContractName {
67+
original_name,
68+
new_name,
69+
});
70+
}
71+
if current_version_semver >= new_version_semver {
72+
return Err(ContractError::InvalidMigrationVersion {
73+
current_version,
74+
new_version,
75+
});
76+
}
77+
78+
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
79+
Ok(Response::default())
80+
}
4881
}

example/scripts/instantiate.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
"tx": "278E32692E897F8EF1EC27C214572DD095DC7973913D6102D57F65753B77BAEC"
327327
},
328328
"fiend-frens-constructor": {
329-
"code_id": "2566",
329+
"code_id": "2912",
330330
"address": "archway13cj0z0zgca02wdlum855fapu9u7hfprwqcczefr2mf0ep75lh7ms3anpjy",
331331
"admin": {
332332
"$var": "sender"

example/scripts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fiend-frens-scripts",
33
"type": "module",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"description": "Scripts to upload NFTs' assets on IPFS, instantiate example contracts etc",
66
"scripts": {
77
"upload": "esno ./src/upload/index.ts",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cw-constructor",
33
"type": "module",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"description": "Constructor contract to bind NFTs as traits for another NFT",
66
"author": "Pau Yankovski <pyncz.dev@gmail.com>",
77
"license": "MIT",

0 commit comments

Comments
 (0)