fix: replace all unwraps with proper error handling

- Or more elegant assignments in place of the `.is_some()` and `.unwrap()` method.
This commit is contained in:
TAMARA LIPOWSKI
2025-01-30 17:19:54 -05:00
parent 01d101acb5
commit 5f3d4406bd
3 changed files with 29 additions and 23 deletions

View File

@@ -57,11 +57,11 @@ impl<S: StrategySelector> RouterEncoder<S> for EVMRouterEncoder<S> {
let (contract_interaction, target_address) = let (contract_interaction, target_address) =
strategy.encode_strategy(solution.clone(), router_address)?; strategy.encode_strategy(solution.clone(), router_address)?;
let value = if solution.native_action.clone().unwrap() == NativeAction::Wrap { let value = match solution.native_action.as_ref() {
solution.given_amount.clone() Some(NativeAction::Wrap) => solution.given_amount.clone(),
} else { _ => BigUint::ZERO,
BigUint::ZERO
}; };
transactions.push(Transaction { transactions.push(Transaction {
value, value,
data: contract_interaction, data: contract_interaction,

View File

@@ -77,20 +77,17 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
&solution.given_token, &solution.given_token,
&solution.given_amount, &solution.given_amount,
)?; )?;
let min_amount_out = if solution.check_amount.is_some() { let mut min_amount_out = BigUint::ZERO;
let mut min_amount_out = solution.check_amount.clone().unwrap(); if let Some(user_specified_min_amount) = solution.check_amount {
if solution.slippage.is_some() { if let Some(slippage) = solution.slippage {
let one_hundred = BigUint::from(100u32); let one_hundred = BigUint::from(100u32);
let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32); let slippage_percent = BigUint::from((slippage * 100.0) as u32);
let multiplier = &one_hundred - slippage_percent; let multiplier = &one_hundred - slippage_percent;
let expected_amount_with_slippage = let expected_amount_with_slippage =
(&solution.expected_amount * multiplier) / one_hundred; (&solution.expected_amount * multiplier) / one_hundred;
min_amount_out = max(min_amount_out, expected_amount_with_slippage); min_amount_out = max(user_specified_min_amount, expected_amount_with_slippage);
}
} }
min_amount_out
} else {
BigUint::ZERO
};
let mut tokens: Vec<Bytes> = solution let mut tokens: Vec<Bytes> = solution
.swaps .swaps
@@ -106,7 +103,13 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
let mut swaps = vec![]; let mut swaps = vec![];
for swap in solution.swaps.iter() { for swap in solution.swaps.iter() {
let registry = SWAP_ENCODER_REGISTRY.read().unwrap(); let registry = SWAP_ENCODER_REGISTRY
.read()
.map_err(|_| {
EncodingError::FatalError(
"Failed to read the swap encoder registry".to_string(),
)
})?;
let swap_encoder = registry let swap_encoder = registry
.get_encoder(&swap.component.protocol_system) .get_encoder(&swap.component.protocol_system)
.expect("Swap encoder not found"); .expect("Swap encoder not found");
@@ -150,8 +153,8 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
let encoded_swaps = self.ple_encode(swaps); let encoded_swaps = self.ple_encode(swaps);
let (mut unwrap, mut wrap) = (false, false); let (mut unwrap, mut wrap) = (false, false);
if solution.native_action.is_some() { if let Some(action) = solution.native_action {
match solution.native_action.unwrap() { match action {
NativeAction::Wrap => wrap = true, NativeAction::Wrap => wrap = true,
NativeAction::Unwrap => unwrap = true, NativeAction::Unwrap => unwrap = true,
} }
@@ -186,12 +189,16 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
solution: Solution, solution: Solution,
_router_address: Bytes, _router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> { ) -> Result<(Vec<u8>, Bytes), EncodingError> {
if solution.router_address.is_none() { let router_address = solution.router_address.ok_or_else(|| {
return Err(EncodingError::InvalidInput( EncodingError::InvalidInput(
"Router address is required for straight to pool solutions".to_string(), "Router address is required for straight to pool solutions".to_string(),
)); )
} })?;
let swap = solution.swaps.first().unwrap();
let swap = solution
.swaps
.first()
.ok_or_else(|| EncodingError::InvalidInput("No swaps found in solution".to_string()))?;
let registry = SWAP_ENCODER_REGISTRY let registry = SWAP_ENCODER_REGISTRY
.read() .read()
.map_err(|_| { .map_err(|_| {
@@ -205,7 +212,6 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
swap.component.protocol_system swap.component.protocol_system
)) ))
})?; })?;
let router_address = solution.router_address.unwrap();
let encoding_context = EncodingContext { let encoding_context = EncodingContext {
receiver: solution.receiver, receiver: solution.receiver,

View File

@@ -24,7 +24,7 @@ impl StrategySelector for EVMStrategySelector {
"Signer is required for SplitSwapStrategyEncoder".to_string(), "Signer is required for SplitSwapStrategyEncoder".to_string(),
) )
})?; })?;
Ok(Box::new(SplitSwapStrategyEncoder::new(signer_pk, chain).unwrap())) Ok(Box::new(SplitSwapStrategyEncoder::new(signer_pk, chain)?))
} }
} }
} }