Skip to content

Commit

Permalink
chore: use .mul_floor for multiplying Uint128 and Decimal, and other …
Browse files Browse the repository at this point in the history
…adjustments
  • Loading branch information
joemonem committed Apr 18, 2024
1 parent be84488 commit 97b4652
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
12 changes: 4 additions & 8 deletions contracts/os/andromeda-kernel/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ pub fn ibc_packet_receive(
// in a seprate function and on error write out an error ack.
match do_ibc_packet_receive(deps, env, msg) {
Ok(response) => Ok(response),
Err(error) => Ok(IbcReceiveResponse::new()
Err(error) => Ok(IbcReceiveResponse::new(make_ack_fail(error.to_string()))
.add_attribute("method", "ibc_packet_receive")
.add_attribute("error", error.to_string())
.set_ack(make_ack_fail(error.to_string()))),
.add_attribute("error", error.to_string())),
}
}

Expand Down Expand Up @@ -146,8 +145,7 @@ pub fn do_ibc_packet_receive(
let amp_msg = AMPMsg::new(recipient, message, None);
let res = execute::send(execute_env, amp_msg)?;

Ok(IbcReceiveResponse::new()
.set_ack(make_ack_success())
Ok(IbcReceiveResponse::new(make_ack_success())
.add_attributes(res.attributes)
.add_submessages(res.messages)
.add_events(res.events))
Expand Down Expand Up @@ -196,9 +194,7 @@ pub fn ibc_register_username(
},
ReplyId::RegisterUsername.repr(),
);
Ok(IbcReceiveResponse::new()
.add_submessage(sub_msg)
.set_ack(make_ack_success()))
Ok(IbcReceiveResponse::new(make_ack_success()).add_submessage(sub_msg))
}

pub fn validate_order_and_version(
Expand Down
15 changes: 10 additions & 5 deletions contracts/os/andromeda-kernel/src/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use andromeda_std::{
os::aos_querier::AOSQuerier,
};
use cosmwasm_std::{
ensure, wasm_execute, Addr, DepsMut, Empty, Env, Reply, Response, SubMsg, SubMsgResponse,
SubMsgResult,
ensure, to_json_binary, wasm_execute, Addr, DepsMut, Empty, Env, Reply, Response, SubMsg,
SubMsgResponse, SubMsgResult,
};

/// Handles the reply from an ADO creation
Expand Down Expand Up @@ -50,15 +50,20 @@ pub fn on_reply_ibc_hooks_packet_send(
deps: DepsMut,
msg: Reply,
) -> Result<Response, ContractError> {
let SubMsgResult::Ok(SubMsgResponse { data: Some(b), .. }) = msg.result else {
let SubMsgResult::Ok(SubMsgResponse { msg_responses, .. }) = msg.result else {
return Err(ContractError::InvalidPacket {
error: Some(format!("ibc hooks: failed reply: {:?}", msg.result)),
});
};

let MsgTransferResponse { sequence } =
MsgTransferResponse::decode(&b[..]).map_err(|_e| ContractError::InvalidPacket {
error: Some(format!("ibc hooks: could not decode response: {b}")),
MsgTransferResponse::decode(&to_json_binary(&msg_responses)?[..]).map_err(|_e| {
ContractError::InvalidPacket {
error: Some(format!(
"ibc hooks: could not decode response: {:?}",
msg_responses
)),
}
})?;

let mut outgoing_packets = OUTGOING_IBC_HOOKS_PACKETS
Expand Down
16 changes: 5 additions & 11 deletions packages/andromeda-data-storage/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ pub enum PrimitiveRestriction {
}

fn parse_error(type_name: String) -> StdError {
StdError::ParseErr {
target_type: type_name.clone(),
msg: format!("Primitive is not a {type_name}"),
}
StdError::parse_err(type_name.clone(), format!("Primitive is not a {type_name}"))
}

impl From<String> for Primitive {
Expand Down Expand Up @@ -202,13 +199,10 @@ mod tests {

#[test]
fn test_parse_error() {
assert_eq!(
StdError::ParseErr {
target_type: "target_type".to_string(),
msg: "Primitive is not a target_type".to_string()
},
parse_error("target_type".to_string())
);
assert_eq!(StdError::parse_err(
"target_type".to_string(),
"Primitive is not a target_type".to_string()
));
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions packages/andromeda-ecosystem/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl StrategyType {
msg: CosmosMsg::Wasm(msg),
gas_limit: None,
reply_on: ReplyOn::Error,
payload: Binary::default(),
};

Ok(sub_msg)
Expand Down
4 changes: 2 additions & 2 deletions packages/andromeda-modules/src/rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ pub fn calculate_fee(fee_rate: Rate, payment: &Coin) -> Result<Coin, ContractErr
percent <= Decimal::one() && !percent.is_zero(),
ContractError::InvalidRate {}
);
let mut fee_amount = payment.amount * percent;
let mut fee_amount = payment.amount.mul_floor(percent);

// Always round any remainder up and prioritise the fee receiver.
// Inverse of percent will always exist.
let reversed_fee = fee_amount * percent.inv().unwrap();
let reversed_fee = fee_amount.mul_floor(percent.inv().unwrap());
if payment.amount > reversed_fee {
// [COM-1] Added checked add to fee_amount rather than direct increment
fee_amount = fee_amount.checked_add(1u128.into())?;
Expand Down

0 comments on commit 97b4652

Please sign in to comment.