Skip to content

Commit

Permalink
refactor: modify V4Planner methods to return self for chaining (#15)
Browse files Browse the repository at this point in the history
Updated methods in `V4Planner` to return `&mut Self` for improved method chaining. Adjusted related implementations and tests to ensure consistency and functionality with the new return values.
  • Loading branch information
shuhuiluo authored Dec 15, 2024
1 parent 95bb0a0 commit 90e4e5a
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/utils/v4_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,18 @@ pub struct V4Planner {

impl V4Planner {
#[inline]
pub fn add_action(&mut self, action: &Actions) {
pub fn add_action(&mut self, action: &Actions) -> &mut Self {
self.actions.push(action.command());
self.params.push(action.abi_encode());
self
}

#[inline]
pub fn add_trade<TInput, TOutput, TP>(
&mut self,
trade: &Trade<TInput, TOutput, TP>,
slippage_tolerance: Option<Percent>,
) -> Result<(), Error>
) -> Result<&mut Self, Error>
where
TInput: BaseCurrency,
TOutput: BaseCurrency,
Expand All @@ -140,11 +141,12 @@ impl V4Planner {
"Only accepts Trades with 1 swap (must break swaps into individual trades)"
);

let currency_in = currency_address(trade.input_currency());
let currency_out = currency_address(trade.output_currency());
let path = encode_route_to_path(trade.route(), exact_output);
let route = trade.route();
let currency_in = currency_address(&route.path_input);
let currency_out = currency_address(&route.path_output);
let path = encode_route_to_path(route, exact_output);

self.add_action(
Ok(self.add_action(
&(if exact_output {
Actions::SWAP_EXACT_OUT(SwapExactOutParams {
currencyOut: currency_out,
Expand Down Expand Up @@ -172,8 +174,7 @@ impl V4Planner {
},
})
}),
);
Ok(())
))
}

#[inline]
Expand All @@ -182,12 +183,12 @@ impl V4Planner {
currency: &impl BaseCurrency,
payer_is_user: bool,
amount: Option<U256>,
) {
) -> &mut Self {
self.add_action(&Actions::SETTLE(SettleParams {
currency: currency_address(currency),
amount: amount.unwrap_or_default(),
payerIsUser: payer_is_user,
}));
}))
}

#[inline]
Expand All @@ -196,12 +197,12 @@ impl V4Planner {
currency: &impl BaseCurrency,
recipient: Address,
amount: Option<U256>,
) {
) -> &mut Self {
self.add_action(&Actions::TAKE(TakeParams {
currency: currency_address(currency),
recipient,
amount: amount.unwrap_or_default(),
}));
}))
}

#[inline]
Expand Down Expand Up @@ -436,7 +437,7 @@ mod tests {
assert_eq!(planner.actions, vec![0x07]);
assert_eq!(
planner.params[0],
hex!("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000ea8d524a2a4ae240000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000").to_vec()
hex!("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000ea8d524a2a4ae240000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000").to_vec()
);
}

Expand All @@ -461,7 +462,7 @@ mod tests {
assert_eq!(planner.actions, vec![0x05]);
assert_eq!(
planner.params[0],
hex!("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000d23441c93fad7ca000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000").to_vec()
hex!("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000d23441c93fad7ca000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000").to_vec()
);
}

Expand Down

0 comments on commit 90e4e5a

Please sign in to comment.