Merge pull request #60 from propeller-heads/encoding/dc/extend-transaction-to-have-selector
feat: Add selector to Transaction
This commit is contained in:
@@ -3,4 +3,4 @@ mod constants;
|
|||||||
pub mod strategy_encoder;
|
pub mod strategy_encoder;
|
||||||
mod swap_encoder;
|
mod swap_encoder;
|
||||||
pub mod tycho_encoder;
|
pub mod tycho_encoder;
|
||||||
mod utils;
|
pub mod utils;
|
||||||
|
|||||||
@@ -261,7 +261,10 @@ impl SplitSwapStrategyEncoder {
|
|||||||
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
|
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
|
||||||
|
|
||||||
impl StrategyEncoder for SplitSwapStrategyEncoder {
|
impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
fn encode_strategy(
|
||||||
|
&self,
|
||||||
|
solution: Solution,
|
||||||
|
) -> Result<(Vec<u8>, Bytes, Option<String>), EncodingError> {
|
||||||
self.validate_split_percentages(&solution.swaps)?;
|
self.validate_split_percentages(&solution.swaps)?;
|
||||||
self.validate_swap_path(
|
self.validate_swap_path(
|
||||||
&solution.swaps,
|
&solution.swaps,
|
||||||
@@ -395,7 +398,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
|||||||
.abi_encode();
|
.abi_encode();
|
||||||
|
|
||||||
let contract_interaction = encode_input(&self.selector, method_calldata);
|
let contract_interaction = encode_input(&self.selector, method_calldata);
|
||||||
Ok((contract_interaction, solution.router_address))
|
Ok((contract_interaction, solution.router_address, None))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||||
@@ -425,7 +428,10 @@ impl ExecutorStrategyEncoder {
|
|||||||
}
|
}
|
||||||
impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
|
impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
|
||||||
impl StrategyEncoder for ExecutorStrategyEncoder {
|
impl StrategyEncoder for ExecutorStrategyEncoder {
|
||||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
fn encode_strategy(
|
||||||
|
&self,
|
||||||
|
solution: Solution,
|
||||||
|
) -> Result<(Vec<u8>, Bytes, Option<String>), EncodingError> {
|
||||||
let swap = solution
|
let swap = solution
|
||||||
.swaps
|
.swaps
|
||||||
.first()
|
.first()
|
||||||
@@ -449,7 +455,15 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
|
|||||||
|
|
||||||
let executor_address = Bytes::from_str(swap_encoder.executor_address())
|
let executor_address = Bytes::from_str(swap_encoder.executor_address())
|
||||||
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
|
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
|
||||||
Ok((protocol_data, executor_address))
|
Ok((
|
||||||
|
protocol_data,
|
||||||
|
executor_address,
|
||||||
|
Some(
|
||||||
|
swap_encoder
|
||||||
|
.executor_selector()
|
||||||
|
.to_string(),
|
||||||
|
),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||||
@@ -531,7 +545,7 @@ mod tests {
|
|||||||
native_action: None,
|
native_action: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (protocol_data, executor_address) = encoder
|
let (protocol_data, executor_address, selector) = encoder
|
||||||
.encode_strategy(solution)
|
.encode_strategy(solution)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let hex_protocol_data = encode(&protocol_data);
|
let hex_protocol_data = encode(&protocol_data);
|
||||||
@@ -552,8 +566,8 @@ mod tests {
|
|||||||
"00",
|
"00",
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
assert_eq!(selector, Some("swap(uint256,bytes)".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[case::no_check_no_slippage(
|
#[case::no_check_no_slippage(
|
||||||
None,
|
None,
|
||||||
@@ -622,7 +636,7 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (calldata, _) = encoder
|
let (calldata, _, _) = encoder
|
||||||
.encode_strategy(solution)
|
.encode_strategy(solution)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
|
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
|
||||||
@@ -723,7 +737,7 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (calldata, _) = encoder
|
let (calldata, _, _) = encoder
|
||||||
.encode_strategy(solution)
|
.encode_strategy(solution)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -771,7 +785,7 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (calldata, _) = encoder
|
let (calldata, _, _) = encoder
|
||||||
.encode_strategy(solution)
|
.encode_strategy(solution)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -859,7 +873,7 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (calldata, _) = encoder
|
let (calldata, _, _) = encoder
|
||||||
.encode_strategy(solution)
|
.encode_strategy(solution)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
|
|||||||
let strategy = self
|
let strategy = self
|
||||||
.strategy_registry
|
.strategy_registry
|
||||||
.get_encoder(solution)?;
|
.get_encoder(solution)?;
|
||||||
let (contract_interaction, target_address) =
|
let (contract_interaction, target_address, selector) =
|
||||||
strategy.encode_strategy(solution.clone())?;
|
strategy.encode_strategy(solution.clone())?;
|
||||||
|
|
||||||
let value = match solution.native_action.as_ref() {
|
let value = match solution.native_action.as_ref() {
|
||||||
@@ -116,6 +116,7 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
|
|||||||
value,
|
value,
|
||||||
data: contract_interaction,
|
data: contract_interaction,
|
||||||
to: target_address,
|
to: target_address,
|
||||||
|
selector,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(transactions)
|
Ok(transactions)
|
||||||
@@ -170,12 +171,16 @@ mod tests {
|
|||||||
struct MockStrategy;
|
struct MockStrategy;
|
||||||
|
|
||||||
impl StrategyEncoder for MockStrategy {
|
impl StrategyEncoder for MockStrategy {
|
||||||
fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
fn encode_strategy(
|
||||||
|
&self,
|
||||||
|
_solution: Solution,
|
||||||
|
) -> Result<(Vec<u8>, Bytes, Option<String>), EncodingError> {
|
||||||
Ok((
|
Ok((
|
||||||
Bytes::from_str("0x1234")
|
Bytes::from_str("0x1234")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_vec(),
|
.to_vec(),
|
||||||
Bytes::from_str("0xabcd").unwrap(),
|
Bytes::from_str("0xabcd").unwrap(),
|
||||||
|
None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,11 +95,13 @@ impl Swap {
|
|||||||
/// * `to`: Address of the contract to call with the calldata
|
/// * `to`: Address of the contract to call with the calldata
|
||||||
/// * `value`: Native token value to be sent with the transaction.
|
/// * `value`: Native token value to be sent with the transaction.
|
||||||
/// * `data`: Encoded calldata for the transaction.
|
/// * `data`: Encoded calldata for the transaction.
|
||||||
|
/// * `selector`: Only relevant for direct executions. The selector of the function to be called.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Transaction {
|
pub struct Transaction {
|
||||||
pub to: Bytes,
|
pub to: Bytes,
|
||||||
pub value: BigUint,
|
pub value: BigUint,
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
|
pub selector: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents necessary attributes for encoding an order.
|
/// Represents necessary attributes for encoding an order.
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::Swa
|
|||||||
|
|
||||||
/// Encodes a solution using a specific strategy.
|
/// Encodes a solution using a specific strategy.
|
||||||
pub trait StrategyEncoder {
|
pub trait StrategyEncoder {
|
||||||
fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec<u8>, Bytes), EncodingError>;
|
fn encode_strategy(
|
||||||
|
&self,
|
||||||
|
to_encode: Solution,
|
||||||
|
) -> Result<(Vec<u8>, Bytes, Option<String>), EncodingError>;
|
||||||
|
|
||||||
#[allow(clippy::borrowed_box)]
|
#[allow(clippy::borrowed_box)]
|
||||||
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;
|
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;
|
||||||
|
|||||||
Reference in New Issue
Block a user