-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asi-migration preparation #50
base: master
Are you sure you want to change the base?
Changes from all commits
cf8b882
dd1cb1d
8c4255e
1cf0f1d
bf1fa86
964af19
b5a9830
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,9 @@ use crate::msg::{ | |
}; | ||
use crate::state::{config, config_read, refunds_add, refunds_have, State}; | ||
|
||
pub const DEFAULT_DENOM: &str = "afet"; | ||
pub const DEFAULT_DENOM: &str = "aasi"; | ||
pub const DEFAULT_RELAY_EON: u64 = 0; | ||
pub const DEFAULT_FEES_ACCRUED: Uint128 = Uint128::zero(); | ||
|
||
/* *************************************************** | ||
* ************** Initialization ************* | ||
|
@@ -30,7 +32,30 @@ pub fn instantiate( | |
info: MessageInfo, | ||
msg: InstantiateMsg, | ||
) -> StdResult<Response> { | ||
let env_message_sender = info.sender; | ||
let supply = | ||
amount_from_funds(&info.funds, msg.get_denom()).unwrap_or_else(|_| Uint128::zero()); | ||
initialise_contract_state( | ||
deps.storage, | ||
&env, | ||
&info.sender, | ||
supply, | ||
DEFAULT_RELAY_EON, | ||
DEFAULT_FEES_ACCRUED, | ||
&msg, | ||
)?; | ||
|
||
Ok(Response::default()) | ||
} | ||
|
||
pub fn initialise_contract_state( | ||
storage: &mut dyn Storage, | ||
env: &Env, | ||
admin: &Addr, | ||
supply: Uint128, | ||
relay_eon: u64, | ||
fees_accrued: Uint128, | ||
msg: &InstantiateMsg, | ||
) -> StdResult<()> { | ||
let current_block_number = env.block.height; | ||
|
||
let mut paused_since_block_public_api = msg.paused_since_block.unwrap_or(u64::MAX); | ||
|
@@ -39,24 +64,20 @@ pub fn instantiate( | |
} | ||
let paused_since_block_relayer_api = paused_since_block_public_api; | ||
|
||
let contract_addr_human = env.contract.address; | ||
|
||
if msg.lower_swap_limit > msg.upper_swap_limit || msg.lower_swap_limit <= msg.swap_fee { | ||
return Err(StdError::generic_err(ERR_SWAP_LIMITS_INCONSISTENT)); | ||
} | ||
|
||
let denom = msg.denom.unwrap_or_else(|| DEFAULT_DENOM.to_string()); | ||
let denom = msg.denom.as_deref().unwrap_or(DEFAULT_DENOM); | ||
|
||
ac_add_role(deps.storage, &env_message_sender, &AccessRole::Admin)?; | ||
|
||
let supply = amount_from_funds(&info.funds, denom.clone()); | ||
ac_add_role(storage, admin, &AccessRole::Admin)?; | ||
|
||
let state = State { | ||
supply: supply.unwrap_or_else(|_| Uint128::zero()), | ||
fees_accrued: Uint128::zero(), | ||
supply, | ||
fees_accrued, | ||
next_swap_id: msg.next_swap_id, | ||
sealed_reverse_swap_id: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep it. I think it was not implemented, and this is just remnant of that idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am keeping it, but it will just be always 0. It was always 0 anyway. |
||
relay_eon: 0, | ||
relay_eon, | ||
upper_swap_limit: msg.upper_swap_limit, | ||
lower_swap_limit: msg.lower_swap_limit, | ||
reverse_aggregated_allowance: msg.reverse_aggregated_allowance, | ||
|
@@ -65,13 +86,12 @@ pub fn instantiate( | |
swap_fee: msg.swap_fee, | ||
paused_since_block_public_api, | ||
paused_since_block_relayer_api, | ||
denom, | ||
contract_addr_human, // optimization FIXME(LR) not needed any more (version 0.10.0) | ||
denom: denom.to_string(), | ||
}; | ||
|
||
config(deps.storage).save(&state)?; | ||
config(storage).save(&state)?; | ||
|
||
Ok(Response::default()) | ||
Ok(()) | ||
} | ||
|
||
/* *************************************************** | ||
|
@@ -83,7 +103,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S | |
|
||
match msg { | ||
ExecuteMsg::Swap { destination } => { | ||
let amount = amount_from_funds(&info.funds, state.denom.clone())?; | ||
let amount = amount_from_funds(&info.funds, &state.denom)?; | ||
try_swap(deps, &env, &state, amount, destination) | ||
} | ||
ExecuteMsg::ReverseSwap { | ||
|
@@ -481,7 +501,7 @@ fn try_deposit(deps: DepsMut, info: &MessageInfo, state: &State) -> StdResult<Re | |
|
||
let env_message_sender = &info.sender; | ||
|
||
let amount = amount_from_funds(&info.funds, state.denom.clone())?; | ||
let amount = amount_from_funds(&info.funds, &state.denom)?; | ||
config(deps.storage).update(|mut state| -> StdResult<_> { | ||
state.supply += amount; | ||
Ok(state) | ||
|
@@ -719,7 +739,7 @@ fn try_renounce_role(deps: DepsMut, info: &MessageInfo, role: String) -> StdResu | |
* ***************** Helpers ***************** | ||
* ***************************************************/ | ||
|
||
pub fn amount_from_funds(funds: &[Coin], denom: String) -> StdResult<Uint128> { | ||
pub fn amount_from_funds(funds: &[Coin], denom: &str) -> StdResult<Uint128> { | ||
for coin in funds { | ||
if coin.denom == denom { | ||
return Ok(coin.amount); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::msg::Uint128; | ||
use cosmwasm_std::{Addr, Storage}; | ||
use cosmwasm_storage::{singleton, singleton_read, ReadonlySingleton, Singleton}; | ||
|
||
pub static CONFIG_KEY: &[u8] = b"config"; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct LegacyState { | ||
pub supply: Uint128, // amount of token migrated to the other chain | ||
pub fees_accrued: Uint128, // fees | ||
pub next_swap_id: u64, | ||
pub sealed_reverse_swap_id: u64, | ||
pub relay_eon: u64, | ||
pub upper_swap_limit: Uint128, | ||
pub lower_swap_limit: Uint128, | ||
pub reverse_aggregated_allowance: Uint128, | ||
pub reverse_aggregated_allowance_approver_cap: Uint128, | ||
pub cap: Uint128, | ||
pub swap_fee: Uint128, | ||
pub paused_since_block_public_api: u64, | ||
pub paused_since_block_relayer_api: u64, | ||
pub denom: String, | ||
|
||
// optimization FIXME(LR) Not needed any more with version 0.10.0 | ||
pub contract_addr_human: Addr, | ||
} | ||
|
||
pub fn legacy_config_read(storage: &dyn Storage) -> ReadonlySingleton<LegacyState> { | ||
singleton_read(storage, CONFIG_KEY) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn legacy_config(storage: &mut dyn Storage) -> Singleton<LegacyState> { | ||
singleton(storage, crate::state::CONFIG_KEY) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ pub mod error; | |
pub mod msg; | ||
pub mod state; | ||
|
||
mod legacy_state; | ||
pub mod migration; | ||
#[cfg(test)] | ||
pub mod tests; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use crate::access_control::wipe_all_roles_from_storage; | ||
use crate::contract::initialise_contract_state; | ||
use crate::error::ERR_STATE_ERROR; | ||
use crate::legacy_state::legacy_config_read; | ||
use crate::msg::MigrateMsg; | ||
use crate::state::is_state_valid; | ||
use cosmwasm_std::{entry_point, DepsMut, Env, Response, StdError, StdResult}; | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason you can't have |
||
// Remove all previously registered roles | ||
wipe_all_roles_from_storage(deps.storage); | ||
|
||
let legacy_state = legacy_config_read(deps.storage).load()?; | ||
|
||
// Ensure correct contract state | ||
if let Some(re_init) = msg.re_init { | ||
initialise_contract_state( | ||
deps.storage, | ||
&env, | ||
&re_init.admin, | ||
legacy_state.supply, | ||
legacy_state.relay_eon, | ||
legacy_state.fees_accrued, | ||
&re_init.init_msg, | ||
)?; | ||
} else if !is_state_valid(deps.storage) { | ||
return Err(StdError::generic_err(ERR_STATE_ERROR)); | ||
} | ||
|
||
Ok(Response::default()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::legacy_state::{legacy_config, LegacyState}; | ||
use crate::msg::{InstantiateMsg, ReInitMsg}; | ||
use crate::state::config_read; | ||
use cosmwasm_std::testing::mock_dependencies; | ||
use cosmwasm_std::{Addr, Uint128}; | ||
use cosmwasm_vm::testing::mock_env; | ||
|
||
#[test] | ||
fn test_can_migrate() { | ||
let mut deps = mock_dependencies(); | ||
|
||
// Prepare legacy storage | ||
let legacy_state = LegacyState { | ||
supply: Uint128::new(1), | ||
fees_accrued: Uint128::new(2), | ||
next_swap_id: 3, | ||
sealed_reverse_swap_id: 4, | ||
relay_eon: 5, | ||
upper_swap_limit: Uint128::new(7), | ||
lower_swap_limit: Uint128::new(6), | ||
reverse_aggregated_allowance: Uint128::new(8), | ||
reverse_aggregated_allowance_approver_cap: Uint128::new(9), | ||
cap: Uint128::new(10), | ||
swap_fee: Uint128::new(2), | ||
paused_since_block_public_api: 12, | ||
paused_since_block_relayer_api: 13, | ||
denom: "ABC".to_string(), | ||
contract_addr_human: Addr::unchecked("DEF"), | ||
}; | ||
assert!(legacy_config(deps.as_mut().storage) | ||
.save(&legacy_state) | ||
.is_ok()); | ||
|
||
let admin = Addr::unchecked("admin"); | ||
let init_msg = InstantiateMsg { | ||
next_swap_id: 10, | ||
cap: Uint128::new(20), | ||
upper_swap_limit: Uint128::new(40), | ||
lower_swap_limit: Uint128::new(30), | ||
swap_fee: Uint128::new(10), | ||
reverse_aggregated_allowance: Uint128::new(60), | ||
reverse_aggregated_allowance_approver_cap: Uint128::new(70), | ||
paused_since_block: Some(80), | ||
denom: Some("ASI".to_string()), | ||
}; | ||
|
||
let re_init_msg = ReInitMsg { | ||
init_msg: init_msg.clone(), | ||
admin: admin.clone(), | ||
}; | ||
|
||
let migrate_msg = MigrateMsg { | ||
re_init: Some(re_init_msg), | ||
}; | ||
|
||
let mut env = mock_env(); | ||
env.block.height = 1; | ||
assert!(migrate(deps.as_mut(), env, migrate_msg).is_ok()); | ||
|
||
let new_state = config_read(deps.as_ref().storage).load().unwrap(); | ||
|
||
// Transferred legacy values | ||
assert_eq!(new_state.supply, legacy_state.supply); | ||
assert_eq!(new_state.relay_eon, legacy_state.relay_eon); | ||
assert_eq!(new_state.fees_accrued, legacy_state.fees_accrued); | ||
|
||
// New values | ||
assert_eq!(new_state.cap, init_msg.cap); | ||
assert_eq!(new_state.denom, init_msg.denom.unwrap()); | ||
assert_eq!(new_state.next_swap_id, init_msg.next_swap_id); | ||
assert_eq!(new_state.lower_swap_limit, init_msg.lower_swap_limit); | ||
assert_eq!( | ||
new_state.paused_since_block_public_api, | ||
init_msg.paused_since_block.unwrap() | ||
); | ||
assert_eq!( | ||
new_state.paused_since_block_relayer_api, | ||
init_msg.paused_since_block.unwrap() | ||
); | ||
assert_eq!( | ||
new_state.reverse_aggregated_allowance, | ||
init_msg.reverse_aggregated_allowance | ||
); | ||
assert_eq!( | ||
new_state.reverse_aggregated_allowance_approver_cap, | ||
init_msg.reverse_aggregated_allowance_approver_cap | ||
); | ||
assert_eq!(new_state.sealed_reverse_swap_id, 0); | ||
assert_eq!(new_state.swap_fee, init_msg.swap_fee); | ||
assert_eq!(new_state.upper_swap_limit, init_msg.upper_swap_limit); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is ok because this migration will be one-time thing, but this means that during migration we will have to set all parameters to contract manually and it will rewrite the original contract State.
One solution would be to keep the legacy state Struct definition, load legacy state value with it and rewrite only parameters we want to change. Or make it to change only denom.
Maybe my implementation is unnecessary too general.