Skip to content

Commit

Permalink
Merge pull request #45 from CosmWasm/implement-cw2-contracts
Browse files Browse the repository at this point in the history
Add migration info to contracts
  • Loading branch information
ethanfrey committed Aug 13, 2020
2 parents a05187d + 85f179b commit 335c61c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 8 deletions.
4 changes: 4 additions & 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/cw1-subkeys/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ library = []
[dependencies]
cosmwasm-std = { version = "0.10.0", features = ["iterator"] }
cosmwasm-storage = { version = "0.10.0", features = ["iterator"] }
cw2 = { path = "../../packages/cw2", version = "0.1.0" }
cw20 = { path = "../../packages/cw20", version = "0.1.0" }
cw1-whitelist = { path = "../cw1-whitelist", version = "0.1.0", features = ["library"] }
schemars = "0.7"
Expand Down
10 changes: 10 additions & 0 deletions contracts/cw1-subkeys/src/contract.rs
Expand Up @@ -10,17 +10,27 @@ use cw1_whitelist::{
msg::InitMsg,
state::admin_list_read,
};
use cw2::{set_contract_version, ContractVersion};
use cw20::Expiration;

use crate::msg::{HandleMsg, QueryMsg};
use crate::state::{allowances, allowances_read, Allowance};
use std::ops::{AddAssign, Sub};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw1-subkeys";
const CONTRACT_VERSION: &str = "v0.1.0";

pub fn init<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
msg: InitMsg,
) -> StdResult<InitResponse> {
let version = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: CONTRACT_VERSION.to_string(),
};
set_contract_version(&mut deps.storage, &version)?;
whitelist_init(deps, env, msg)
}

Expand Down
1 change: 1 addition & 0 deletions contracts/cw1-whitelist/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ library = []
[dependencies]
cosmwasm-std = { version = "0.10.0", features = ["iterator"] }
cosmwasm-storage = { version = "0.10.0", features = ["iterator"] }
cw2 = { path = "../../packages/cw2", version = "0.1.0" }
schemars = "0.7"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
snafu = { version = "0.6.3" }
Expand Down
10 changes: 10 additions & 0 deletions contracts/cw1-whitelist/src/contract.rs
Expand Up @@ -5,15 +5,25 @@ use cosmwasm_std::{
log, to_binary, Api, Binary, CanonicalAddr, CosmosMsg, Empty, Env, Extern, HandleResponse,
HumanAddr, InitResponse, Querier, StdError, StdResult, Storage,
};
use cw2::{set_contract_version, ContractVersion};

use crate::msg::{AdminListResponse, HandleMsg, InitMsg, QueryMsg};
use crate::state::{admin_list, admin_list_read, AdminList};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw1-whitelist";
const CONTRACT_VERSION: &str = "v0.1.0";

pub fn init<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
_env: Env,
msg: InitMsg,
) -> StdResult<InitResponse> {
let version = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: CONTRACT_VERSION.to_string(),
};
set_contract_version(&mut deps.storage, &version)?;
let cfg = AdminList {
admins: map_canonical(&deps.api, &msg.admins)?,
mutable: msg.mutable,
Expand Down
1 change: 1 addition & 0 deletions contracts/cw20-base/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw2 = { path = "../../packages/cw2", version = "0.1.0" }
cw20 = { path = "../../packages/cw20", version = "0.1.0" }
cosmwasm-std = { version = "0.10.0" }
cosmwasm-storage = { version = "0.10.0" }
Expand Down
10 changes: 10 additions & 0 deletions contracts/cw20-base/src/contract.rs
Expand Up @@ -2,6 +2,7 @@ use cosmwasm_std::{
log, to_binary, Api, Binary, Env, Extern, HandleResponse, HumanAddr, InitResponse, Querier,
StdError, StdResult, Storage, Uint128,
};
use cw2::{set_contract_version, ContractVersion};
use cw20::{BalanceResponse, Cw20ReceiveMsg, MinterResponse, TokenInfoResponse};

use crate::allowances::{
Expand All @@ -11,11 +12,20 @@ use crate::allowances::{
use crate::msg::{HandleMsg, InitMsg, InitialBalance, QueryMsg};
use crate::state::{balances, balances_read, token_info, token_info_read, MinterData, TokenInfo};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw20-base";
const CONTRACT_VERSION: &str = "v0.1.0";

pub fn init<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
_env: Env,
msg: InitMsg,
) -> StdResult<InitResponse> {
let version = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: CONTRACT_VERSION.to_string(),
};
set_contract_version(&mut deps.storage, &version)?;
// check valid token info
msg.validate()?;
// create initial accounts
Expand Down
1 change: 1 addition & 0 deletions contracts/cw20-escrow/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw2 = { path = "../../packages/cw2", version = "0.1.0" }
cw20 = { path = "../../packages/cw20", version = "0.1.0" }
cosmwasm-std = { version = "0.10.0", features = ["iterator"] }
cosmwasm-storage = { version = "0.10.0", features = ["iterator"] }
Expand Down
12 changes: 11 additions & 1 deletion contracts/cw20-escrow/src/contract.rs
Expand Up @@ -2,6 +2,7 @@ use cosmwasm_std::{
from_binary, log, to_binary, Api, BankMsg, Binary, Coin, CosmosMsg, Env, Extern,
HandleResponse, HumanAddr, InitResponse, Querier, StdError, StdResult, Storage, WasmMsg,
};
use cw2::{set_contract_version, ContractVersion};
use cw20::{Cw20HandleMsg, Cw20ReceiveMsg};

use crate::msg::{
Expand All @@ -11,11 +12,20 @@ use crate::msg::{
use crate::state::{all_escrow_ids, escrows, escrows_read, Cw20Coin, Escrow, PREFIX_ESCROW};
use cosmwasm_storage::prefixed;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw20-escrow";
const CONTRACT_VERSION: &str = "v0.1.0";

pub fn init<S: Storage, A: Api, Q: Querier>(
_deps: &mut Extern<S, A, Q>,
deps: &mut Extern<S, A, Q>,
_env: Env,
_msg: InitMsg,
) -> StdResult<InitResponse> {
let version = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: CONTRACT_VERSION.to_string(),
};
set_contract_version(&mut deps.storage, &version)?;
// no setup
Ok(InitResponse::default())
}
Expand Down
14 changes: 7 additions & 7 deletions packages/cw2/src/lib.rs
Expand Up @@ -20,14 +20,14 @@ pub struct ContractVersion {
pub version: String,
}

/// get_contract_info can be use in migrate to read the previous version of this contract
pub fn get_contract_info<S: ReadonlyStorage>(storage: &S) -> StdResult<ContractVersion> {
/// get_contract_version can be use in migrate to read the previous version of this contract
pub fn get_contract_version<S: ReadonlyStorage>(storage: &S) -> StdResult<ContractVersion> {
ReadonlySingleton::new(storage, PREFIX_INFO).load()
}

/// set_contract_info should be used in init to store the original version, and after a successful
/// set_contract_version should be used in init to store the original version, and after a successful
/// migrate to update it
pub fn set_contract_info<S: Storage>(storage: &mut S, info: &ContractVersion) -> StdResult<()> {
pub fn set_contract_version<S: Storage>(storage: &mut S, info: &ContractVersion) -> StdResult<()> {
Singleton::new(storage, PREFIX_INFO).save(info)
}

Expand Down Expand Up @@ -57,15 +57,15 @@ mod tests {
let mut store = MockStorage::new();

// error if not set
assert!(get_contract_info(&store).is_err());
assert!(get_contract_version(&store).is_err());

// set and get
let info = ContractVersion {
contract: "crate:cw20-base".to_string(),
version: "v0.1.0".to_string(),
};
set_contract_info(&mut store, &info).unwrap();
let loaded = get_contract_info(&store).unwrap();
set_contract_version(&mut store, &info).unwrap();
let loaded = get_contract_version(&store).unwrap();
assert_eq!(info, loaded);
}
}

0 comments on commit 335c61c

Please sign in to comment.