-
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 5 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,29 @@ 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(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 +63,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 +85,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 +102,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 +500,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 +738,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 |
---|---|---|
|
@@ -4,5 +4,6 @@ pub mod error; | |
pub mod msg; | ||
pub mod state; | ||
|
||
pub mod migration; | ||
#[cfg(test)] | ||
pub mod tests; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::access_control::wipe_all_roles_from_storage; | ||
use crate::contract::initialise_contract_state; | ||
use crate::error::ERR_ALREADY_REFUNDED; | ||
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); | ||
|
||
// Ensure correct contract state | ||
if let Some(re_init) = msg.re_init { | ||
initialise_contract_state( | ||
deps.storage, | ||
&env, | ||
&re_init.admin, | ||
re_init.supply, | ||
re_init.relay_eon, | ||
re_init.fees_accrued, | ||
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. These 3 attributes should be simply read from the current contract state (= no change in their value). 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. We are be able to read it from contract state only if legacy contract state is compatible with new contract state or if we keep the original contract state definition for migration sake. 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've added legacy state handling |
||
&re_init.init_msg, | ||
)?; | ||
} else if !is_state_valid(deps.storage) { | ||
return Err(StdError::generic_err(ERR_ALREADY_REFUNDED)); | ||
} | ||
|
||
Ok(Response::default()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::contract::DEFAULT_DENOM; | ||
use cosmwasm_std::Addr; | ||
|
||
//use crate::cosmwasm_bignumber::{Uint256}; | ||
|
@@ -20,6 +21,12 @@ pub struct InstantiateMsg { | |
pub denom: Option<String>, | ||
} | ||
|
||
impl InstantiateMsg { | ||
pub fn get_denom(&self) -> &str { | ||
self.denom.as_deref().unwrap_or(DEFAULT_DENOM) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum ExecuteMsg { | ||
|
@@ -155,3 +162,17 @@ pub struct PausedSinceBlockResponse { | |
pub struct DenomResponse { | ||
pub denom: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] | ||
pub struct ReInitMsg { | ||
pub init_msg: InstantiateMsg, | ||
pub admin: Addr, | ||
pub supply: Uint128, | ||
pub relay_eon: u64, | ||
pub fees_accrued: Uint128, | ||
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. See my previous comment. |
||
} | ||
|
||
#[derive(Serialize, Deserialize, JsonSchema)] | ||
pub struct MigrateMsg { | ||
pub re_init: Option<ReInitMsg>, | ||
} |
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.