feat: Use Arc instead of & for protocol_state in Swap

- dealing with lifetimes makes refactoring painful for our users.
This commit is contained in:
TAMARA LIPOWSKI
2025-08-20 15:25:16 -04:00
parent 2a68dd117b
commit a2a53195ed
5 changed files with 26 additions and 23 deletions

View File

@@ -1,3 +1,5 @@
use std::sync::Arc;
use clap::ValueEnum;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
@@ -35,7 +37,7 @@ pub enum UserTransferType {
/// Represents a solution containing details describing an order, and instructions for filling
/// the order.
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
pub struct Solution<'a> {
pub struct Solution {
/// Address of the sender.
pub sender: Bytes,
/// Address of the receiver.
@@ -55,7 +57,7 @@ pub struct Solution<'a> {
#[serde(with = "biguint_string")]
pub checked_amount: BigUint,
/// List of swaps to fulfill the solution.
pub swaps: Vec<Swap<'a>>,
pub swaps: Vec<Swap>,
/// If set, the corresponding native action will be executed.
pub native_action: Option<NativeAction>,
}
@@ -74,7 +76,7 @@ pub enum NativeAction {
/// Represents a swap operation to be performed on a pool.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Swap<'a> {
pub struct Swap {
/// Protocol component from tycho indexer
pub component: ProtocolComponent,
/// Token being input into the pool.
@@ -88,20 +90,20 @@ pub struct Swap<'a> {
pub user_data: Option<Bytes>,
/// Optional protocol state used to perform the swap.
#[serde(skip)]
pub protocol_state: Option<&'a dyn ProtocolSim>,
pub protocol_state: Option<Arc<dyn ProtocolSim>>,
/// Optional estimated amount in for this Swap. This is necessary for RFQ protocols. This value
/// is used to request the quote
pub estimated_amount_in: Option<BigUint>,
}
impl<'a> Swap<'a> {
impl Swap {
pub fn new<T: Into<ProtocolComponent>>(
component: T,
token_in: Bytes,
token_out: Bytes,
split: f64,
user_data: Option<Bytes>,
protocol_state: Option<&'a dyn ProtocolSim>,
protocol_state: Option<Arc<dyn ProtocolSim>>,
estimated_amount_in: Option<BigUint>,
) -> Self {
Self {
@@ -116,28 +118,29 @@ impl<'a> Swap<'a> {
}
}
impl<'a> PartialEq for Swap<'a> {
impl PartialEq for Swap {
fn eq(&self, other: &Self) -> bool {
self.component == other.component &&
self.token_in == other.token_in &&
self.token_out == other.token_out &&
self.split == other.split &&
self.user_data == other.user_data
self.user_data == other.user_data &&
self.estimated_amount_in == other.estimated_amount_in
// Skip protocol_state comparison since trait objects don't implement PartialEq
}
}
pub struct SwapBuilder<'a> {
pub struct SwapBuilder {
component: ProtocolComponent,
token_in: Bytes,
token_out: Bytes,
split: f64,
user_data: Option<Bytes>,
protocol_state: Option<&'a dyn ProtocolSim>,
protocol_state: Option<Arc<dyn ProtocolSim>>,
estimated_amount_in: Option<BigUint>,
}
impl<'a> SwapBuilder<'a> {
impl SwapBuilder {
pub fn new<T: Into<ProtocolComponent>>(
component: T,
token_in: Bytes,
@@ -164,7 +167,7 @@ impl<'a> SwapBuilder<'a> {
self
}
pub fn protocol_state(mut self, protocol_state: &'a dyn ProtocolSim) -> Self {
pub fn protocol_state(mut self, protocol_state: Arc<dyn ProtocolSim>) -> Self {
self.protocol_state = Some(protocol_state);
self
}
@@ -174,7 +177,7 @@ impl<'a> SwapBuilder<'a> {
self
}
pub fn build(self) -> Swap<'a> {
pub fn build(self) -> Swap {
Swap {
component: self.component,
token_in: self.token_in,