fix: Bring back swapper_pk for backwards compatibility

Took 16 minutes
This commit is contained in:
Diana Carvalho
2025-05-26 11:36:32 +01:00
parent f5d9ab03da
commit d12e3d3b5f
4 changed files with 35 additions and 19 deletions

View File

@@ -1,10 +1,5 @@
use std::{ use std::io::{self, Read};
io::{self, Read},
str::FromStr,
};
use alloy::signers::local::PrivateKeySigner;
use alloy_primitives::B256;
use alloy_sol_types::SolValue; use alloy_sol_types::SolValue;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use tycho_common::{hex_bytes::Bytes, models::Chain}; use tycho_common::{hex_bytes::Bytes, models::Chain};
@@ -58,6 +53,8 @@ pub struct Cli {
router_address: Option<Bytes>, router_address: Option<Bytes>,
#[arg(short, long)] #[arg(short, long)]
user_transfer_type: Option<UserTransferType>, user_transfer_type: Option<UserTransferType>,
#[arg(short, long)]
swapper_pk: Option<String>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@@ -94,6 +91,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(user_transfer_type) = cli.user_transfer_type { if let Some(user_transfer_type) = cli.user_transfer_type {
builder = builder.user_transfer_type(user_transfer_type); builder = builder.user_transfer_type(user_transfer_type);
} }
#[allow(deprecated)]
if let Some(swapper_pk) = cli.swapper_pk {
builder = builder.swapper_pk(swapper_pk);
}
builder.build()? builder.build()?
} }
Commands::TychoExecutor => TychoExecutorEncoderBuilder::new() Commands::TychoExecutor => TychoExecutorEncoderBuilder::new()

View File

@@ -175,7 +175,7 @@ impl Permit2 {
sigDeadline: sig_deadline, sigDeadline: sig_deadline,
}; };
Ok(models::PermitSingle::try_from(&permit_single)?) models::PermitSingle::try_from(&permit_single)
} }
} }

View File

@@ -1,6 +1,7 @@
use std::collections::HashMap; use std::{collections::HashMap, str::FromStr};
use alloy::signers::local::PrivateKeySigner; use alloy::signers::local::PrivateKeySigner;
use alloy_primitives::B256;
use tycho_common::{models::Chain as TychoCommonChain, Bytes}; use tycho_common::{models::Chain as TychoCommonChain, Bytes};
use crate::encoding::{ use crate::encoding::{
@@ -22,7 +23,7 @@ pub struct TychoRouterEncoderBuilder {
user_transfer_type: Option<UserTransferType>, user_transfer_type: Option<UserTransferType>,
executors_file_path: Option<String>, executors_file_path: Option<String>,
router_address: Option<Bytes>, router_address: Option<Bytes>,
signer: Option<PrivateKeySigner>, swapper_pk: Option<String>,
} }
impl Default for TychoRouterEncoderBuilder { impl Default for TychoRouterEncoderBuilder {
@@ -37,7 +38,7 @@ impl TychoRouterEncoderBuilder {
chain: None, chain: None,
executors_file_path: None, executors_file_path: None,
router_address: None, router_address: None,
signer: None, swapper_pk: None,
user_transfer_type: None, user_transfer_type: None,
} }
} }
@@ -65,11 +66,15 @@ impl TychoRouterEncoderBuilder {
self self
} }
/// Sets the `signer` for the encoder. This is used to sign permit2 objects. This is only needed /// Sets the `swapper_pk` for the encoder. This is used to sign permit2 objects. This is only
/// if you intend to get the full calldata for the transfer. We do not recommend using this /// needed if you intend to get the full calldata for the transfer. We do not recommend
/// option, you should sign and create the function calldata entirely on your own. /// using this option, you should sign and create the function calldata entirely on your
pub fn signer(mut self, signer: PrivateKeySigner) -> Self { /// own.
self.signer = Some(signer); #[deprecated(
note = "This is deprecated and will be removed in the future. You should sign and create the function calldata on your own."
)]
pub fn swapper_pk(mut self, swapper_pk: String) -> Self {
self.swapper_pk = Some(swapper_pk);
self self
} }
@@ -94,12 +99,23 @@ impl TychoRouterEncoderBuilder {
let swap_encoder_registry = let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain.clone())?; SwapEncoderRegistry::new(self.executors_file_path.clone(), chain.clone())?;
let signer = if let Some(pk) = self.swapper_pk {
let pk = B256::from_str(&pk).map_err(|_| {
EncodingError::FatalError("Invalid swapper private key provided".to_string())
})?;
Some(PrivateKeySigner::from_bytes(&pk).map_err(|_| {
EncodingError::FatalError("Failed to create signer".to_string())
})?)
} else {
None
};
Ok(Box::new(TychoRouterEncoder::new( Ok(Box::new(TychoRouterEncoder::new(
chain, chain,
swap_encoder_registry, swap_encoder_registry,
tycho_router_address, tycho_router_address,
user_transfer_type, user_transfer_type,
self.signer, signer,
)?)) )?))
} else { } else {
Err(EncodingError::FatalError( Err(EncodingError::FatalError(

View File

@@ -551,15 +551,14 @@ mod tests {
fn get_permit(router_address: Bytes, solution: &Solution) -> PermitSingle { fn get_permit(router_address: Bytes, solution: &Solution) -> PermitSingle {
let permit2 = Permit2::new().unwrap(); let permit2 = Permit2::new().unwrap();
let permit_single = permit2 permit2
.get_permit( .get_permit(
&router_address, &router_address,
&solution.sender, &solution.sender,
&solution.given_token, &solution.given_token,
&solution.given_amount, &solution.given_amount,
) )
.unwrap(); .unwrap()
permit_single
} }
fn get_signer() -> PrivateKeySigner { fn get_signer() -> PrivateKeySigner {