Skip to content
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

Knots improvements #110

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 58 additions & 47 deletions contracts/core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,7 @@ fn execute_tick_idle(
info: MessageInfo,
config: &Config,
) -> ContractResult<Response<NeutronMsg>> {
let mut attrs = vec![
attr("action", "tick_idle"),
attr("knot", "001"),
attr("knot", "000"),
];
let mut attrs = vec![attr("action", "tick_idle"), attr("knot", "000")];
let last_idle_call = LAST_IDLE_CALL.load(deps.storage)?;
let mut messages = vec![];
cache_exchange_rate(deps.branch(), env.clone(), config)?;
Expand Down Expand Up @@ -566,6 +562,8 @@ fn execute_tick_idle(
}
}
} else {
LAST_IDLE_CALL.save(deps.storage, &env.block.time.seconds())?;
attrs.push(attr("knot", "004"));
let unbonding_batches = unbond_batches_map()
.idx
.status
Expand Down Expand Up @@ -607,10 +605,7 @@ fn execute_tick_idle(

attrs.push(attr("knot", "007"));
let transfer: Option<TransferReadyBatchesMsg> = match unbonded_batches.len() {
0 => {
attrs.push(attr("knot", "045"));
None
} // we have nothing to do
0 => None, // we have nothing to do
1 => {
let (id, mut unbonding_batch) = unbonded_batches
.into_iter()
Expand Down Expand Up @@ -739,16 +734,17 @@ fn execute_tick_idle(
attrs.push(attr("knot", "022"));
attrs.push(attr("state", "staking_rewards"));
} else {
attrs.push(attr("knot", "024"));
if let Some(unbond_message) =
get_unbonding_msg(deps.branch(), &env, config, &info)?
{
let (unbond_message_relut, unbond_attrs) =
get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message_relut {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let (unbond_message_relut, unbond_attrs) =
get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message_relut {
let (unbond_message_result, unbond_attrs) =
get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message_result {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote this code to pass by reference instead of returning it, looks cleaner

attrs.extend(unbond_attrs);
messages.push(unbond_message);
attrs.push(attr("knot", "028"));
FSM.go_to(deps.storage, ContractState::Unbonding)?;
attrs.push(attr("knot", "029"));
attrs.push(attr("state", "unbonding"));
} else {
attrs.extend(unbond_attrs);
attrs.push(attr("state", "idle"));
attrs.push(attr("knot", "000"));
}
Expand All @@ -772,8 +768,6 @@ fn execute_tick_idle(
attrs.push(attr("knot", "012"));
attrs.push(attr("state", "claiming"));
}
LAST_IDLE_CALL.save(deps.storage, &env.block.time.seconds())?;
attrs.push(attr("knot", "004"));
}
Ok(response("execute-tick_idle", CONTRACT_NAME, attrs).add_messages(messages))
}
Expand All @@ -784,7 +778,7 @@ fn execute_tick_peripheral(
_info: MessageInfo,
_config: &Config,
) -> ContractResult<Response<NeutronMsg>> {
let mut attrs = vec![attr("action", "tick_peripheral"), attr("knot", "001")];
let mut attrs = vec![attr("action", "tick_peripheral")];
let res = get_received_puppeteer_response(deps.as_ref())?;

if let drop_puppeteer_base::msg::ResponseHookMsg::Success(msg) = res {
Expand Down Expand Up @@ -815,17 +809,20 @@ fn execute_tick_claiming(
info: MessageInfo,
config: &Config,
) -> ContractResult<Response<NeutronMsg>> {
let mut attrs = vec![attr("action", "tick_claiming"), attr("knot", "001")];
let mut attrs = vec![attr("action", "tick_claiming")];
attrs.push(attr("knot", "012"));
let response_msg = get_received_puppeteer_response(deps.as_ref())?;
LAST_PUPPETEER_RESPONSE.remove(deps.storage);
let mut messages = vec![];
match response_msg {
drop_puppeteer_base::msg::ResponseHookMsg::Success(success_msg) => {
attrs.push(attr("knot", "047"));
match success_msg.transaction {
drop_puppeteer_base::msg::Transaction::ClaimRewardsAndOptionalyTransfer {
transfer,
..
} => {
attrs.push(attr("knot", "013"));
if let Some(transfer) = transfer {
for id in transfer.batch_ids {
let mut batch = unbond_batches_map().load(deps.storage, id)?;
Expand All @@ -840,6 +837,7 @@ fn execute_tick_claiming(
batch.status_timestamps.withdrawn = Some(env.block.time.seconds());
attrs.push(attr("unbond_batch_status", "withdrawn"));
}
attrs.push(attr("knot", "014"));
unbond_batches_map().save(deps.storage, id, &batch)?;
}
}
Expand Down Expand Up @@ -867,14 +865,17 @@ fn execute_tick_claiming(
attrs.push(attr("knot", "022"));
attrs.push(attr("state", "staking_rewards"));
} else {
attrs.push(attr("knot", "024"));
if let Some(unbond_message) = get_unbonding_msg(deps.branch(), &env, config, &info)? {
let (unbond_message_relut, unbond_attrs) =
get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message_relut {
attrs.extend(unbond_attrs);
messages.push(unbond_message);
attrs.push(attr("knot", "028"));
FSM.go_to(deps.storage, ContractState::Unbonding)?;
attrs.push(attr("knot", "029"));
attrs.push(attr("state", "unbonding"));
} else {
attrs.extend(unbond_attrs);
FSM.go_to(deps.storage, ContractState::Idle)?;
attrs.push(attr("knot", "000"));
attrs.push(attr("state", "idle"));
Expand All @@ -891,7 +892,7 @@ fn execute_tick_staking_bond(
info: MessageInfo,
config: &Config,
) -> ContractResult<Response<NeutronMsg>> {
let mut attrs = vec![attr("action", "tick_staking_bond"), attr("knot", "001")];
let mut attrs = vec![attr("action", "tick_staking_bond")];
let response_msg = get_received_staker_response(deps.as_ref())?;
if let drop_staking_base::msg::staker::ResponseHookMsg::Success(response) = response_msg {
let (_, puppeteer_height, _): drop_staking_base::msg::puppeteer::BalancesResponse =
Expand All @@ -918,14 +919,17 @@ fn execute_tick_staking_bond(
attrs.push(attr("knot", "022"));
attrs.push(attr("state", "staking_rewards"));
} else {
attrs.push(attr("knot", "024"));
if let Some(unbond_message) = get_unbonding_msg(deps.branch(), &env, config, &info)? {
let (unbond_message_relut, unbond_attrs) =
get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message_relut {
attrs.extend(unbond_attrs);
messages.push(unbond_message);
attrs.push(attr("knot", "028"));
FSM.go_to(deps.storage, ContractState::Unbonding)?;
attrs.push(attr("knot", "029"));
attrs.push(attr("state", "unbonding"));
} else {
attrs.extend(unbond_attrs);
FSM.go_to(deps.storage, ContractState::Idle)?;
attrs.push(attr("knot", "000"));
attrs.push(attr("state", "idle"));
Expand All @@ -940,23 +944,21 @@ fn execute_tick_staking_rewards(
info: MessageInfo,
config: &Config,
) -> ContractResult<Response<NeutronMsg>> {
let mut attrs = vec![
attr("action", "tick_staking"),
attr("knot", "001"),
attr("knot", "022"),
];
let mut attrs = vec![attr("action", "tick_staking"), attr("knot", "022")];
let _response_msg = get_received_puppeteer_response(deps.as_ref())?;
LAST_PUPPETEER_RESPONSE.remove(deps.storage);
let mut messages = vec![];
attrs.push(attr("knot", "024"));
let unbond_message = get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message {
let (unbond_message_relut, unbond_attrs) =
get_unbonding_msg(deps.branch(), &env, config, &info)?;
if let Some(unbond_message) = unbond_message_relut {
attrs.extend(unbond_attrs);
messages.push(unbond_message);
attrs.push(attr("knot", "028"));
FSM.go_to(deps.storage, ContractState::Unbonding)?;
attrs.push(attr("knot", "029"));
attrs.push(attr("state", "unbonding"));
} else {
attrs.extend(unbond_attrs);
FSM.go_to(deps.storage, ContractState::Idle)?;
attrs.push(attr("knot", "000"));
attrs.push(attr("state", "idle"));
Expand All @@ -970,11 +972,7 @@ fn execute_tick_unbonding(
_info: MessageInfo,
config: &Config,
) -> ContractResult<Response<NeutronMsg>> {
let mut attrs = vec![
attr("action", "tick_unbonding"),
attr("knot", "001"),
attr("knot", "029"),
];
let mut attrs = vec![attr("action", "tick_unbonding"), attr("knot", "029")];
let res = get_received_puppeteer_response(deps.as_ref())?;
match res {
drop_puppeteer_base::msg::ResponseHookMsg::Success(response) => {
Expand Down Expand Up @@ -1414,12 +1412,20 @@ fn get_unbonding_msg<T>(
env: &Env,
config: &Config,
info: &MessageInfo,
) -> ContractResult<Option<CosmosMsg<T>>> {
) -> ContractResult<(Option<CosmosMsg<T>>, Vec<cosmwasm_std::Attribute>)> {
let mut attrs = vec![];
let funds = info.funds.clone();
attrs.push(attr("knot", "024"));
let batch_id = FAILED_BATCH_ID
.may_load(deps.storage)?
.unwrap_or(UNBOND_BATCH_ID.load(deps.storage)?);
let mut unbond = unbond_batches_map().load(deps.storage, batch_id)?;
if unbond.status == UnbondBatchStatus::UnbondFailed {
attrs.push(attr("knot", "025"));
} else {
attrs.push(attr("knot", "026"));
}
attrs.push(attr("knot", "027"));
if (unbond.status_timestamps.new + config.unbond_batch_switch_time < env.block.time.seconds())
&& unbond.total_unbond_items != 0
&& !unbond.total_amount.is_zero()
Expand All @@ -1433,31 +1439,36 @@ fn get_unbonding_msg<T>(
);

if calc_withdraw_query_result.is_err() {
return Ok(None);
return Ok((None, attrs));
}

let undelegations: Vec<(String, Uint128)> = calc_withdraw_query_result?;

attrs.push(attr("knot", "045"));
unbond.status = UnbondBatchStatus::UnbondRequested;
unbond.status_timestamps.unbond_requested = Some(env.block.time.seconds());
unbond_batches_map().save(deps.storage, batch_id, &unbond)?;
attrs.push(attr("knot", "046"));
UNBOND_BATCH_ID.save(deps.storage, &(batch_id + 1))?;
unbond_batches_map().save(
deps.storage,
batch_id + 1,
&new_unbond(env.block.time.seconds()),
)?;
Ok(Some(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.puppeteer_contract.to_string(),
msg: to_json_binary(&drop_staking_base::msg::puppeteer::ExecuteMsg::Undelegate {
items: undelegations,
batch_id,
reply_to: env.contract.address.to_string(),
})?,
funds,
})))
Ok((
Some(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.puppeteer_contract.to_string(),
msg: to_json_binary(&drop_staking_base::msg::puppeteer::ExecuteMsg::Undelegate {
items: undelegations,
batch_id,
reply_to: env.contract.address.to_string(),
})?,
funds,
})),
attrs,
))
} else {
Ok(None)
Ok((None, attrs))
}
}

Expand Down
Loading
Loading