Skip to content

Commit

Permalink
Return single interaction from Interaction::encode() instead of `Ve…
Browse files Browse the repository at this point in the history
…c` (#2415)

# Description
`Interaction::encode()` seems overly flexible. There is not a single
instance where we actually return multiple `EncodedInteraction`s from
`encode()`.

# Changes
Now we only return a single `EncodedInteraction` from `encode()`.

## How to test
CI
  • Loading branch information
MartinquaXD authored Feb 22, 2024
1 parent a0e3ec7 commit 3316205
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 86 deletions.
17 changes: 6 additions & 11 deletions crates/driver/src/boundary/liquidity/uniswap/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use {
anyhow::Context,
contracts::{GPv2Settlement, UniswapV3SwapRouter},
ethrpc::current_block::BlockRetrieving,
itertools::Itertools,
shared::{
http_solver::model::TokenAmount,
interaction::Interaction,
Expand Down Expand Up @@ -92,16 +91,12 @@ pub fn to_interaction(
TokenAmount::new(output.0.token.into(), output.0.amount),
);

interaction
.encode()
.into_iter()
.map(|(target, value, call_data)| eth::Interaction {
target: eth::Address(target),
value: eth::Ether(value),
call_data: call_data.0.into(),
})
.exactly_one()
.unwrap()
let encoded = interaction.encode();
eth::Interaction {
target: eth::Address(encoded.0),
value: eth::Ether(encoded.1),
call_data: crate::util::Bytes(encoded.2 .0),
}
}

pub fn collector(
Expand Down
4 changes: 2 additions & 2 deletions crates/shared/src/http_solver/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ pub struct InteractionData {
}

impl Interaction for InteractionData {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![(self.target, self.value, Bytes(self.call_data.clone()))]
fn encode(&self) -> EncodedInteraction {
(self.target, self.value, Bytes(self.call_data.clone()))
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/shared/src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub trait Interaction: std::fmt::Debug + Send + Sync {
// Write::write returns a result but we know we write to a vector in memory so
// we know it will never fail. Then the question becomes whether
// interactions should be allowed to fail encoding for other reasons.
fn encode(&self) -> Vec<EncodedInteraction>;
fn encode(&self) -> EncodedInteraction;
}

impl Interaction for Box<dyn Interaction> {
fn encode(&self) -> Vec<EncodedInteraction> {
fn encode(&self) -> EncodedInteraction {
self.as_ref().encode()
}
}
Expand All @@ -26,14 +26,14 @@ pub type EncodedInteraction = (
);

impl Interaction for EncodedInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![self.clone()]
fn encode(&self) -> EncodedInteraction {
self.clone()
}
}

impl Interaction for InteractionData {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![(self.target, self.value, Bytes(self.call_data.clone()))]
fn encode(&self) -> EncodedInteraction {
(self.target, self.value, Bytes(self.call_data.clone()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/shared/src/oneinch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ pub struct Swap {
}

impl Interaction for Swap {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![(self.tx.to, self.tx.value, Bytes(self.tx.data.clone()))]
fn encode(&self) -> EncodedInteraction {
(self.tx.to, self.tx.value, Bytes(self.tx.data.clone()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/shared/src/paraswap_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ pub struct TransactionBuilderResponse {
}

impl Interaction for TransactionBuilderResponse {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![(self.to, self.value, Bytes(self.data.clone()))]
fn encode(&self) -> EncodedInteraction {
(self.to, self.value, Bytes(self.data.clone()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/shared/src/zeroex_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ pub struct SwapResponse {
}

impl Interaction for SwapResponse {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![(self.to, self.value, Bytes(self.data.clone()))]
fn encode(&self) -> EncodedInteraction {
(self.to, self.value, Bytes(self.data.clone()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/solver/src/interactions/allowances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub struct Approval {
}

impl Interaction for Approval {
fn encode(&self) -> Vec<EncodedInteraction> {
fn encode(&self) -> EncodedInteraction {
// Use a "dummy" contract - unfortunately `ethcontract` doesn't
// allow you use the generated contract intances to encode
// transaction data without a `Web3` instance. Hopefully, this
Expand Down Expand Up @@ -365,7 +365,7 @@ mod tests {
let spender = H160([0x02; 20]);
assert_eq!(
Approval { token, spender }.encode(),
vec![(
(
token,
0.into(),
Bytes(
Expand All @@ -376,7 +376,7 @@ mod tests {
)
.unwrap()
)
)]
)
);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/solver/src/interactions/balancer_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl BalancerSwapGivenOutInteraction {
}

impl Interaction for BalancerSwapGivenOutInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![self.encode_swap()]
fn encode(&self) -> EncodedInteraction {
self.encode_swap()
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ mod tests {
// ```
assert_eq!(
interaction.encode(),
vec![(
(
vault.address(),
0.into(),
Bytes(
Expand All @@ -118,7 +118,7 @@ mod tests {
)
.unwrap()
),
)]
)
);
}
}
4 changes: 2 additions & 2 deletions crates/solver/src/interactions/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl Erc20ApproveInteraction {
}

impl Interaction for Erc20ApproveInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![self.as_encoded()]
fn encode(&self) -> EncodedInteraction {
self.as_encoded()
}
}

Expand Down
10 changes: 3 additions & 7 deletions crates/solver/src/interactions/uniswap_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub struct UniswapInteraction {
}

impl Interaction for UniswapInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
vec![self.encode_swap()]
fn encode(&self) -> EncodedInteraction {
self.encode_swap()
}
}

Expand Down Expand Up @@ -68,13 +68,9 @@ mod tests {
token_in,
token_out: H160::from_low_u64_be(token_out as u64),
};
let interactions = interaction.encode();

// Single interaction
assert_eq!(interactions.len(), 1);
let swap_call = interaction.encode();

// Verify Swap
let swap_call = &interactions[0];
assert_eq!(swap_call.0, router.address());
let call = &swap_call.2 .0;
let swap_signature = hex!("8803dbee");
Expand Down
10 changes: 3 additions & 7 deletions crates/solver/src/interactions/uniswap_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct ExactOutputSingleParams {
pub sqrt_price_limit_x96: U256,
}
impl Interaction for UniswapV3Interaction {
fn encode(&self) -> Vec<EncodedInteraction> {
fn encode(&self) -> EncodedInteraction {
let method = self.router.exact_output_single((
self.params.token_amount_in_max.token,
self.params.token_amount_out.token,
Expand All @@ -36,7 +36,7 @@ impl Interaction for UniswapV3Interaction {
self.params.sqrt_price_limit_x96,
));
let calldata = method.tx.data.expect("no calldata").0;
vec![(self.router.address(), 0.into(), Bytes(calldata))]
(self.router.address(), 0.into(), Bytes(calldata))
}
}

Expand Down Expand Up @@ -81,13 +81,9 @@ mod tests {
sqrt_price_limit_x96: U256::zero(),
},
};
let interactions = interaction.encode();

// Single interaction
assert_eq!(interactions.len(), 1);
let swap_call = interaction.encode();

// Verify Swap
let swap_call = &interactions[0];
assert_eq!(swap_call.0, router.address());
let call = &swap_call.2 .0;
let swap_signature = hex!("db3e2198");
Expand Down
7 changes: 3 additions & 4 deletions crates/solver/src/interactions/weth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl UnwrapWethInteraction {
}

impl Interaction for UnwrapWethInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
fn encode(&self) -> EncodedInteraction {
let method = self.weth.withdraw(self.amount);
let calldata = method.tx.data.expect("no calldata").0;
vec![(self.weth.address(), 0.into(), Bytes(calldata))]
(self.weth.address(), 0.into(), Bytes(calldata))
}
}

Expand All @@ -51,9 +51,8 @@ mod tests {
weth: weth.clone(),
amount,
};
let encoded_interactions = interaction.encode();
let withdraw_call = interaction.encode();

let withdraw_call = &encoded_interactions[0];
assert_eq!(withdraw_call.0, weth.address());
assert_eq!(withdraw_call.1, U256::from(0));
let call = &withdraw_call.2 .0;
Expand Down
4 changes: 2 additions & 2 deletions crates/solver/src/interactions/zeroex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ZeroExInteraction {
}

impl Interaction for ZeroExInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
fn encode(&self) -> EncodedInteraction {
let method = self.zeroex.fill_or_kill_limit_order(
(
self.order.maker_token,
Expand All @@ -40,6 +40,6 @@ impl Interaction for ZeroExInteraction {
self.taker_token_fill_amount,
);
let calldata = method.tx.data.expect("no calldata").0;
vec![(self.zeroex.address(), 0.into(), Bytes(calldata))]
(self.zeroex.address(), 0.into(), Bytes(calldata))
}
}
3 changes: 1 addition & 2 deletions crates/solver/src/liquidity/balancer_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,7 @@ mod tests {
user_data: Default::default(),
}
.encode(),
]
.concat(),
],
);
}
}
6 changes: 2 additions & 4 deletions crates/solver/src/liquidity/zeroex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ pub mod tests {
zeroex
}
.encode(),
]
.concat(),
],
);
}

Expand Down Expand Up @@ -420,8 +419,7 @@ pub mod tests {
taker_token_fill_amount: 100,
zeroex
}
.encode(),]
.concat(),
.encode()],
);
}
}
12 changes: 0 additions & 12 deletions crates/solver/src/settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,7 @@ impl Trade {
}
}

#[cfg(test)]
use shared::interaction::{EncodedInteraction, Interaction};
use shared::{external_prices::ExternalPrices, http_solver::model::Score};
#[cfg(test)]
#[derive(Debug)]
pub struct NoopInteraction;

#[cfg(test)]
impl Interaction for NoopInteraction {
fn encode(&self) -> Vec<EncodedInteraction> {
Vec::new()
}
}

#[derive(Debug, Clone, Default)]
pub struct Settlement {
Expand Down
Loading

0 comments on commit 3316205

Please sign in to comment.