Skip to content

Commit

Permalink
pass in executor address as arg
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric committed Aug 13, 2024
1 parent 1259f7b commit 452c826
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/collectors/uniswapx_route_collector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::strategies::shared::EXECUTOR_ADDRESS;
use alloy_primitives::Uint;
use anyhow::Result;
use reqwest::header::ORIGIN;
Expand Down Expand Up @@ -153,6 +152,7 @@ pub struct RouteOrderParams {
pub token_in: String,
pub token_out: String,
pub amount: String,
pub recipient: String,
}

#[derive(Clone, Debug)]
Expand All @@ -174,19 +174,22 @@ pub struct UniswapXRouteCollector {
pub chain_id: u64,
pub route_request_receiver: Mutex<Receiver<Vec<OrderBatchData>>>,
pub route_sender: Sender<RoutedOrder>,
pub executor_address: String,
}

impl UniswapXRouteCollector {
pub fn new(
chain_id: u64,
route_request_receiver: Receiver<Vec<OrderBatchData>>,
route_sender: Sender<RoutedOrder>,
executor_address: String
) -> Self {
Self {
client: Client::new(),
chain_id,
route_request_receiver: Mutex::new(route_request_receiver),
route_sender,
executor_address
}
}
}
Expand Down Expand Up @@ -214,6 +217,7 @@ impl Collector<RoutedOrder> for UniswapXRouteCollector {
token_in: token_in.clone(),
token_out: token_out.clone(),
amount: amount_in.to_string(),
recipient: self.executor_address.clone(),
}).await)
}
}).collect();
Expand Down Expand Up @@ -243,7 +247,7 @@ pub async fn route_order(params: RouteOrderParams) -> Result<OrderRoute> {
token_out_chain_id: params.chain_id,
trade_type: TradeType::ExactIn,
amount: params.amount,
recipient: EXECUTOR_ADDRESS.to_string(),
recipient: params.recipient,
slippage_tolerance: SLIPPAGE_TOLERANCE.to_string(),
enable_universal_router: false,
deadline: DEADLINE,
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub struct Args {
#[arg(long)]
pub bid_percentage: u64,

/// Private key for sending txs.
#[arg(long)]
pub executor_address: String,

/// chain id
#[arg(long)]
pub chain_id: u64,
Expand Down Expand Up @@ -111,7 +115,7 @@ async fn main() -> Result<()> {
engine.add_collector(Box::new(priority_collector));

let uniswapx_route_collector =
Box::new(UniswapXRouteCollector::new(chain_id, batch_receiver, route_sender));
Box::new(UniswapXRouteCollector::new(chain_id, batch_receiver, route_sender, args.executor_address.clone()));
let uniswapx_route_collector = CollectorMap::new(uniswapx_route_collector, |e| {
Event::UniswapXRoute(Box::new(e))
});
Expand All @@ -121,6 +125,7 @@ async fn main() -> Result<()> {
chain_id,
priority_batch_receiver,
priority_route_sender,
args.executor_address.clone(),
));
let priority_route_collector = CollectorMap::new(priority_route_collector, |e| {
Event::UniswapXRoute(Box::new(e))
Expand All @@ -129,6 +134,7 @@ async fn main() -> Result<()> {

let config = Config {
bid_percentage: args.bid_percentage,
executor_address: args.executor_address,
};

let uniswapx_strategy = UniswapXUniswapFill::new(
Expand Down
5 changes: 4 additions & 1 deletion src/strategies/priority_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub const WETH_ADDRESS: &str = "0x4200000000000000000000000000000000000006";
pub struct UniswapXPriorityFill<M> {
/// Ethers client.
client: Arc<M>,
/// executor address
executor_address: String,
/// Amount of profits to bid in gas
bid_percentage: u64,
last_block_number: u64,
Expand All @@ -61,6 +63,7 @@ impl<M: Middleware + 'static> UniswapXPriorityFill<M> {

Self {
client,
executor_address: config.executor_address,
bid_percentage: config.bid_percentage,
last_block_number: 0,
last_block_timestamp: 0,
Expand Down Expand Up @@ -149,7 +152,7 @@ impl<M: Middleware + 'static> UniswapXPriorityFill<M> {
let signed_orders = self.get_signed_orders(orders.clone()).ok()?;
return Some(Action::SubmitPublicTx(SubmitTxToMempool {
tx: self
.build_fill(self.client.clone(), signed_orders, event)
.build_fill(self.client.clone(), &self.executor_address, signed_orders, event)
.await
.ok()?,
gas_bid_info: Some(GasBidInfo {
Expand Down
8 changes: 4 additions & 4 deletions src/strategies/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ use std::{
const REACTOR_ADDRESS: &str = "0x00000011F84B9aa48e5f8aA8B9897600006289Be";
const SWAPROUTER_02_ADDRESS: &str = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45";
pub const WETH_ADDRESS: &str = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
pub const EXECUTOR_ADDRESS: &str = "0xa6b19B30593F6e70eabf6c05f9C96d66da65a0A1";

#[async_trait]
pub trait UniswapXStrategy<M: Middleware + 'static> {
// builds a transaction to fill an order
async fn build_fill(
&self,
client: Arc<M>,
executor_address: &str,
signed_orders: Vec<SignedOrder>,
RoutedOrder { request, route }: RoutedOrder,
) -> Result<TypedTransaction> {
let chain_id: U256 = client.get_chainid().await?;
let fill_contract =
SwapRouter02Executor::new(H160::from_str(EXECUTOR_ADDRESS)?, client.clone());
SwapRouter02Executor::new(H160::from_str(executor_address)?, client.clone());

let token_in: H160 = H160::from_str(&request.token_in)?;
let token_out: H160 = H160::from_str(&request.token_out)?;
Expand All @@ -40,13 +40,13 @@ pub trait UniswapXStrategy<M: Middleware + 'static> {
.get_tokens_to_approve(
client.clone(),
token_in,
EXECUTOR_ADDRESS,
&executor_address,
SWAPROUTER_02_ADDRESS,
)
.await?;

let reactor_approval = self
.get_tokens_to_approve(client.clone(), token_out, EXECUTOR_ADDRESS, REACTOR_ADDRESS)
.get_tokens_to_approve(client.clone(), token_out, &executor_address, REACTOR_ADDRESS)
.await?;

// Strip off function selector
Expand Down
1 change: 1 addition & 0 deletions src/strategies/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Action {
#[derive(Debug, Clone)]
pub struct Config {
pub bid_percentage: u64,
pub executor_address: String
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
Expand Down
5 changes: 4 additions & 1 deletion src/strategies/uniswapx_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const REACTOR_ADDRESS: &str = "0x00000011F84B9aa48e5f8aA8B9897600006289Be";
pub struct UniswapXUniswapFill<M> {
/// Ethers client.
client: Arc<M>,
/// executor address
executor_address: String,
/// Amount of profits to bid in gas
bid_percentage: u64,
last_block_number: u64,
Expand All @@ -60,6 +62,7 @@ impl<M: Middleware + 'static> UniswapXUniswapFill<M> {

Self {
client,
executor_address: config.executor_address,
bid_percentage: config.bid_percentage,
last_block_number: 0,
last_block_timestamp: 0,
Expand Down Expand Up @@ -148,7 +151,7 @@ impl<M: Middleware + 'static> UniswapXUniswapFill<M> {
let signed_orders = self.get_signed_orders(orders.clone()).ok()?;
return Some(Action::SubmitTx(SubmitTxToMempool {
tx: self
.build_fill(self.client.clone(), signed_orders, event)
.build_fill(self.client.clone(), &self.executor_address, signed_orders, event)
.await
.ok()?,
gas_bid_info: Some(GasBidInfo {
Expand Down

0 comments on commit 452c826

Please sign in to comment.