Skip to content

Commit

Permalink
update migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tsilva-figure committed Jul 26, 2022
1 parent 7d9de7f commit 9ea7347
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 97 deletions.
8 changes: 8 additions & 0 deletions schema/instantiate_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"required": [
"capital_denom",
"capital_per_share",
"commitment_denom",
"investment_denom",
"lp",
"recovery_admin"
],
Expand All @@ -17,6 +19,12 @@
"format": "uint64",
"minimum": 0.0
},
"commitment_denom": {
"type": "string"
},
"investment_denom": {
"type": "string"
},
"lp": {
"$ref": "#/definitions/Addr"
},
Expand Down
8 changes: 8 additions & 0 deletions schema/state.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"required": [
"capital_denom",
"capital_per_share",
"commitment_denom",
"investment_denom",
"lp",
"raise",
"recovery_admin"
Expand All @@ -18,6 +20,12 @@
"format": "uint64",
"minimum": 0.0
},
"commitment_denom": {
"type": "string"
},
"investment_denom": {
"type": "string"
},
"lp": {
"$ref": "#/definitions/Addr"
},
Expand Down
10 changes: 5 additions & 5 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ pub fn try_close_remaining_commitment(

let remaining_commitment = deps
.querier
.query_balance(env.contract.address, state.commitment_denom())?;
.query_balance(env.contract.address, state.commitment_denom.clone())?;

Ok(Response::new().add_message(wasm_execute(
state.raise.clone(),
&RaiseExecuteMsg::CloseRemainingCommitment {},
coins(remaining_commitment.amount.into(), state.commitment_denom()),
coins(remaining_commitment.amount.into(), state.commitment_denom),
)?))
}

Expand All @@ -96,7 +96,7 @@ pub fn try_accept_commitment_update(
state.raise.clone(),
&RaiseExecuteMsg::AcceptCommitmentUpdate {},
match forfeit_commitment {
Some(amount) => coins(amount.into(), state.commitment_denom()),
Some(amount) => coins(amount.into(), state.commitment_denom),
None => vec![],
},
)?))
Expand Down Expand Up @@ -124,14 +124,14 @@ pub fn try_claim_investment(
coin(amount.into(), state.capital_denom.clone()),
coin(
state.capital_to_shares(amount).into(),
state.commitment_denom(),
state.commitment_denom.clone(),
),
];
funds.sort_by_key(|coin| coin.denom.clone());

let remaining_commitment = deps
.querier
.query_balance(env.contract.address, state.commitment_denom())?;
.query_balance(env.contract.address, state.commitment_denom)?;

let response = if remaining_commitment.amount.u128() == 0 {
Response::new().add_message(wasm_execute(
Expand Down
4 changes: 4 additions & 0 deletions src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub fn instantiate(
raise: info.sender,
recovery_admin: msg.recovery_admin,
lp: msg.lp.clone(),
commitment_denom: msg.commitment_denom,
investment_denom: msg.investment_denom,
capital_denom: msg.capital_denom,
capital_per_share: msg.capital_per_share,
};
Expand Down Expand Up @@ -57,6 +59,8 @@ mod tests {
InstantiateMsg {
recovery_admin: Addr::unchecked("admin"),
lp: Addr::unchecked("lp"),
commitment_denom: String::from("raise_1.commitment"),
investment_denom: String::from("raise_1.investment"),
capital_denom: String::from("stable_coin"),
capital_per_share: 100,
},
Expand Down
190 changes: 189 additions & 1 deletion src/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,209 @@
use std::collections::HashSet;
use std::hash::Hash;

use crate::error::ContractError;
use crate::msg::MigrateMsg;
use crate::state::config;
use crate::state::State;
use crate::state::CONFIG_KEY;
use crate::version::CONTRACT_NAME;
use crate::version::CONTRACT_VERSION;
use cosmwasm_std::entry_point;
use cosmwasm_std::Addr;
use cosmwasm_std::DepsMut;
use cosmwasm_std::Env;
use cosmwasm_std::Response;
use cosmwasm_storage::singleton_read;
use cw2::set_contract_version;
use provwasm_std::ProvenanceMsg;
use provwasm_std::ProvenanceQuery;
use serde::Deserialize;
use serde::Serialize;

#[entry_point]
pub fn migrate(
deps: DepsMut,
deps: DepsMut<ProvenanceQuery>,
_: Env,
_: MigrateMsg,
) -> Result<Response<ProvenanceMsg>, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let old_state: StateV1_0_0 = singleton_read(deps.storage, CONFIG_KEY).load()?;

let new_state = State {
recovery_admin: old_state.recovery_admin,
lp: old_state.lp,
raise: old_state.raise.clone(),
commitment_denom: format!("{}.commitment", old_state.raise),
investment_denom: format!("{}.investment", old_state.raise),
capital_denom: old_state.capital_denom,
capital_per_share: old_state.capital_per_share,
};

config(deps.storage).save(&new_state)?;

Ok(Response::default())
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct StateV1_0_0 {
pub recovery_admin: Addr,
pub lp: Addr,
pub status: Status,
pub raise: Addr,
pub capital_denom: String,
pub capital_per_share: u64,
pub min_commitment: u64,
pub max_commitment: u64,
pub min_days_of_notice: Option<u16>,
pub sequence: u16,
pub active_capital_call: Option<CapitalCall>,
pub closed_capital_calls: HashSet<CapitalCall>,
pub cancelled_capital_calls: HashSet<CapitalCall>,
pub redemptions: HashSet<Redemption>,
pub distributions: HashSet<Distribution>,
pub withdrawals: HashSet<Withdrawal>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum Status {
Draft,
Accepted,
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq)]
pub struct CapitalCall {
pub sequence: u16,
pub amount: u64,
pub days_of_notice: Option<u16>,
}

impl PartialEq for CapitalCall {
fn eq(&self, other: &Self) -> bool {
self.sequence == other.sequence
}
}

impl Hash for CapitalCall {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
self.sequence.hash(state);
}
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq)]
pub struct Redemption {
pub sequence: u16,
pub asset: u64,
pub capital: u64,
}

impl PartialEq for Redemption {
fn eq(&self, other: &Self) -> bool {
self.sequence == other.sequence
}
}

impl Hash for Redemption {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
self.sequence.hash(state);
}
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq)]
pub struct Distribution {
pub sequence: u16,
pub amount: u64,
}

impl PartialEq for Distribution {
fn eq(&self, other: &Self) -> bool {
self.sequence == other.sequence
}
}

impl Hash for Distribution {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
self.sequence.hash(state);
}
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq)]
pub struct Withdrawal {
pub sequence: u16,
pub to: Addr,
pub amount: u64,
}

impl PartialEq for Withdrawal {
fn eq(&self, other: &Self) -> bool {
self.sequence == other.sequence
}
}

impl Hash for Withdrawal {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
self.sequence.hash(state);
}
}

#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::mock_env;
use cosmwasm_storage::singleton;
use provwasm_mocks::mock_dependencies;

use super::StateV1_0_0;

#[test]
fn migration() {
let mut deps = mock_dependencies(&[]);
singleton(&mut deps.storage, CONFIG_KEY)
.save(&StateV1_0_0 {
recovery_admin: Addr::unchecked("marketpalace"),
status: Status::Accepted,
lp: Addr::unchecked("lp"),
raise: Addr::unchecked("raise_1"),
capital_denom: String::from("stable_coin"),
capital_per_share: 100,
min_commitment: 0,
max_commitment: 0,
min_days_of_notice: None,
sequence: 0,
active_capital_call: None,
closed_capital_calls: HashSet::new(),
cancelled_capital_calls: HashSet::new(),
redemptions: HashSet::new(),
distributions: HashSet::new(),
withdrawals: HashSet::new(),
})
.unwrap();

migrate(deps.as_mut(), mock_env(), MigrateMsg {}).unwrap();

assert_eq!(
State {
recovery_admin: Addr::unchecked("marketpalace"),
lp: Addr::unchecked("lp"),
raise: Addr::unchecked("raise_1"),
commitment_denom: String::from("raise_1.commitment"),
investment_denom: String::from("raise_1.investment"),
capital_denom: String::from("stable_coin"),
capital_per_share: 100,
},
singleton_read(&deps.storage, CONFIG_KEY).load().unwrap()
);
}
}
2 changes: 2 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use cosmwasm_std::Addr;
pub struct InstantiateMsg {
pub recovery_admin: Addr,
pub lp: Addr,
pub commitment_denom: String,
pub investment_denom: String,
pub capital_denom: String,
pub capital_per_share: u64,
}
Expand Down
Loading

0 comments on commit 9ea7347

Please sign in to comment.