Skip to content

Commit

Permalink
DesProjectMsgs refactoring looks good 👀
Browse files Browse the repository at this point in the history
  • Loading branch information
kakucodes committed Mar 30, 2024
1 parent 4cd623f commit 683640f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
74 changes: 24 additions & 50 deletions contracts/osmostake/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use osmosis_helpers::{
};
use outpost_utils::{
comp_prefs::{CompoundPrefs, DestinationAction, TakeRate},
helpers::{calculate_compound_amounts, is_authorized_compounder, prefs_sum_to_one, sum_coins, DestProjectMsgs},
helpers::{
calculate_compound_amounts, combine_responses, is_authorized_compounder, prefs_sum_to_one, sum_coins,
DestProjectMsgs,
},
msg_gen::create_exec_msg,
};
use sail_destinations::dest_project_gen::mint_eris_lsd_msgs;
Expand Down Expand Up @@ -78,64 +81,35 @@ pub fn compound(
// the list of all the compounding msgs to broadcast on behalf of the user based on their comp prefs
let all_msgs = prefs_to_msgs(
&project_addresses,
// &env.block,
// staking_denom,
&user_addr,
total_rewards.clone(),
comp_prefs,
deps.as_ref(),
env.block.time,
)?;

let combined_msgs = all_msgs.iter().fold(DestProjectMsgs::default(), |mut acc, msg| {
acc.msgs.append(&mut msg.msgs.clone());
acc.sub_msgs.append(&mut msg.sub_msgs.clone());
acc.events.append(&mut msg.events.clone());
acc
});

let amount_automated_event =
Event::new("amount_automated").add_attributes([total_rewards].iter().enumerate().map(|(i, coin)| Attribute {
key: format!("amount_{}", i),
value: coin.to_string(),
}));

// the final exec message that will be broadcast and contains all the sub msgs
let exec_msg = create_exec_msg(&env.contract.address, combined_msgs.msgs)?;

let resp = Response::default()
// prepare the response to the user with the withdraw rewards and other general metadata
let withdraw_response = Response::default()
.add_attribute("action", "outpost compound")
.add_message(withdraw_msg)
.add_attribute("subaction", "withdraw rewards")
.add_attribute("user", user_addr)
.add_event(amount_automated_event)
// .add_attribute("amount_automated", to_json_binary(&[total_rewards])?.to_string())
.add_message(exec_msg)
.add_submessages(
combined_msgs
.sub_msgs
.into_iter()
.filter_map(|sub_msg| {
if let (Ok(exec_msg), false) = (
create_exec_msg(&env.contract.address, sub_msg.1.clone()),
sub_msg.1.is_empty(),
) {
Some((sub_msg.0, exec_msg, sub_msg.2))
} else {
None
}
})
.map(|(id, msg, reply_on)| SubMsg {
msg,
gas_limit: None,
id,
reply_on,
})
.collect::<Vec<SubMsg>>(),
)
.add_events(combined_msgs.events);

Ok(resp)
.add_attributes(vec![("subaction", "withdraw rewards"), ("user", &user_addr.to_string())])
// event to track the amount of rewards that were automated
.add_event(
Event::new("amount_automated").add_attributes([total_rewards].iter().enumerate().map(|(i, coin)| Attribute {
key: format!("amount_{}", i),
value: coin.to_string(),
})),
);

let resps = combine_responses(vec![
withdraw_response,
all_msgs
.into_iter()
.collect::<DestProjectMsgs>()
.to_response(&env.contract.address)?,
]);

Ok(resps)
}

/// Converts the user's compound preferences into a list of
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ impl FromIterator<DestProjectMsgs> for DestProjectMsgs {
}
}

/// Combine multiple Responses into a single Response
pub fn combine_responses(responses: Vec<Response>) -> Response {
responses
.into_iter()
.fold(Response::default(), |mut acc, response| {
acc.messages.extend(response.messages);
acc.attributes.extend(response.attributes);
acc.events.extend(response.events);
acc
})
}

/// Calculates the tax split for a given token amount and tax rate and the
/// send message to move the tax amount to the tax address. Note that
/// the tax will be in addition to the base token amount
Expand Down

0 comments on commit 683640f

Please sign in to comment.