chore: Refactor models:

- Delete min_checked_amount and use check_amount instead
- Use Bytes and not Address in interfaces
- Move router_address from Solution to Order
This commit is contained in:
Diana Carvalho
2025-01-14 10:37:06 +00:00
parent 5d79da44f3
commit 647f697ea2
6 changed files with 69 additions and 86 deletions

View File

@@ -4,7 +4,9 @@ use anyhow::Error;
use num_bigint::BigUint;
use std::cmp::min;
use crate::encoding::models::{ActionType, EncodingContext, NativeAction, Order};
use crate::encoding::models::{
ActionType, EncodingContext, NativeAction, Order, PROPELLER_ROUTER_ADDRESS,
};
use crate::encoding::swap_encoder::{get_swap_encoder, get_swap_executor_address};
use crate::encoding::utils::{biguint_to_u256, bytes_to_address, encode_input, ple_encode};
@@ -12,7 +14,6 @@ pub trait StrategyEncoder {
fn encode_strategy(
&self,
to_encode: Order,
router_address: Address,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error>;
@@ -37,7 +38,6 @@ impl StrategyEncoder for SingleSwapStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
router_address: Address,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
todo!()
@@ -50,29 +50,31 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
router_address: Address,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
let one_hundred = BigUint::from(100u32);
let slippage_percent = BigUint::from((order.slippage * 100.0) as u32);
let multiplier = &one_hundred - slippage_percent;
let slippage_buy_amount = (&order.given_amount * multiplier) / one_hundred;
let min_checked_amount = if order.min_checked_amount.is_some() {
min(order.min_checked_amount.unwrap(), slippage_buy_amount)
} else {
slippage_buy_amount
};
let mut check_amount = order.check_amount.clone();
if order.slippage.is_some() {
let one_hundred = BigUint::from(100u32);
let slippage_percent = BigUint::from((order.slippage.unwrap() * 100.0) as u32);
let multiplier = &one_hundred - slippage_percent;
check_amount = (&order.check_amount * multiplier) / one_hundred;
}
let mut swaps = vec![];
for (index, swap) in order.swaps.iter().enumerate() {
let is_last = index == order.swaps.len() - 1;
let protocol_system = swap.component.protocol_system.clone();
let swap_encoder = get_swap_encoder(&protocol_system);
let receiver = if is_last {
bytes_to_address(&order.receiver)?
let router_address = if order.router_address.is_some() {
order.router_address.clone().unwrap()
} else {
router_address
PROPELLER_ROUTER_ADDRESS.clone()
};
let receiver = if is_last {
order.receiver.clone()
} else {
router_address.clone()
};
let encoding_context = EncodingContext {
receiver,
exact_out: order.exact_out,
@@ -82,17 +84,10 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
let swap_data = self.encode_protocol_header(protocol_data, protocol_system, 0, 0, 0);
swaps.push(swap_data);
}
let (selector, action_type) = if order.exact_out {
(
"sequentialExactOut(uint256, uint256, bytes[])",
ActionType::SequentialExactOut,
)
} else {
(
"sequentialExactIn(uint256, uint256, bytes[])",
ActionType::SequentialExactIn,
)
};
let selector = "sequentialExactIn(uint256, uint256, bytes[])";
let action_type = ActionType::SequentialExactIn;
let encoded_swaps = ple_encode(swaps);
let (mut unwrap, mut wrap) = (false, false);
@@ -106,7 +101,7 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
wrap,
unwrap,
biguint_to_u256(&order.given_amount),
biguint_to_u256(&min_checked_amount),
biguint_to_u256(&check_amount),
encoded_swaps,
)
.abi_encode();
@@ -125,7 +120,6 @@ impl StrategyEncoder for SlipSwapStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
router_address: Address,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
todo!()