feat: ApprovalsManager trait

Make Permit2 and TokenApprovalsManager implement it.
This way in the RouternEncoder we can use either one (I'm not exactly sure what this would mean in the contract though)

I'm not sure I like this generalisation. The TokenApprovalsManager was made with a different purpose: to approve token allowances for the pools (like balancer and curve) for our router
This commit is contained in:
Diana Carvalho
2025-01-14 11:37:30 +00:00
parent 6e67875821
commit 4991883fc8
7 changed files with 72 additions and 58 deletions

View File

@@ -1,28 +1,29 @@
use std::{env, sync::Arc}; use std::{env, sync::Arc};
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
use alloy::{ use alloy::{
providers::{Provider, ProviderBuilder, RootProvider}, providers::{Provider, ProviderBuilder, RootProvider},
transports::BoxTransport, transports::BoxTransport,
}; };
use alloy_primitives::{Address, U256};
use dotenv::dotenv; use dotenv::dotenv;
use tycho_core::Bytes;
pub struct TokenApprovalsManager { pub struct TokenApprovalsManager {
client: Arc<RootProvider<BoxTransport>>, client: Arc<RootProvider<BoxTransport>>,
} }
impl TokenApprovalsManager { impl TokenApprovalsManager {
pub fn new(client: Arc<RootProvider<BoxTransport>>) -> Self { pub fn new() -> Self {
Self { client } Self {
client: get_client(),
}
} }
pub async fn approval_needed(&self, approval: Approval) -> bool {
todo!()
}
}
pub async fn approval_needed( impl ApprovalsManager for TokenApprovalsManager {
&self, fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8> {
token: Bytes, todo!()
spender_address: Bytes,
router_address: Bytes,
) -> bool {
// should be something like // should be something like
// let allowance = self // let allowance = self
// .client // .client
@@ -30,7 +31,6 @@ impl TokenApprovalsManager {
// .await; // .await;
// //
// allowance == U256::ZERO // If allowance is 0, approval is needed // allowance == U256::ZERO // If allowance is 0, approval is needed
todo!()
} }
} }

View File

@@ -0,0 +1,13 @@
use num_bigint::BigUint;
use tycho_core::Bytes;
pub struct Approval {
pub spender: Bytes,
pub owner: Bytes,
pub token: Bytes,
pub amount: BigUint,
}
pub trait ApprovalsManager {
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8>;
}

View File

@@ -0,0 +1,3 @@
pub(crate) mod approvals_manager;
pub(crate) mod interface;
mod permit2;

View File

@@ -1,15 +1,8 @@
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
use alloy_primitives::U256; use alloy_primitives::U256;
use num_bigint::BigUint;
use std::str::FromStr; use std::str::FromStr;
use tycho_core::Bytes; use tycho_core::Bytes;
pub struct PermitRequest {
pub token: Bytes,
pub amount: BigUint,
pub spender: Bytes,
pub router_address: Bytes,
}
pub struct Permit2 { pub struct Permit2 {
pub address: Bytes, pub address: Bytes,
} }
@@ -21,16 +14,6 @@ impl Permit2 {
.expect("Permit2 address not valid"), .expect("Permit2 address not valid"),
} }
} }
pub fn encode_permit(&self, details: Vec<PermitRequest>) -> Vec<u8> {
// calls get_allowance_data to get nonce
// checks if we are not permitted already
// puts data into a permitSingle struct if there is only 1 PermitDetails, if there are several, use PermitBatch
// adds the nonce and the expiration (uniswap recommends 30 days for expiration)
// signs data
// returns encoded data
todo!()
}
fn get_allowance_data( fn get_allowance_data(
&self, &self,
user: Bytes, user: Bytes,
@@ -42,3 +25,14 @@ impl Permit2 {
todo!() todo!()
} }
} }
impl ApprovalsManager for Permit2 {
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8> {
// calls get_allowance_data to get nonce
// checks if we are not permitted already
// puts data into a permitSingle struct if there is only 1 PermitDetails, if there are several, use PermitBatch
// adds the nonce and the expiration (uniswap recommends 30 days for expiration)
// signs data
// returns encoded data
todo!()
}
}

View File

@@ -1,6 +1,5 @@
mod approvals_manager; mod approvals;
mod models; mod models;
mod permit2;
mod router_encoder; mod router_encoder;
mod strategy_encoder; mod strategy_encoder;
mod strategy_selector; mod strategy_selector;

View File

@@ -1,20 +1,24 @@
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
use crate::encoding::models::{Solution, PROPELLER_ROUTER_ADDRESS}; use crate::encoding::models::{Solution, PROPELLER_ROUTER_ADDRESS};
use crate::encoding::permit2::{Permit2, PermitRequest};
use crate::encoding::strategy_encoder::StrategyEncoder; use crate::encoding::strategy_encoder::StrategyEncoder;
use crate::encoding::strategy_selector::StrategySelector; use crate::encoding::strategy_selector::StrategySelector;
use crate::encoding::utils::{encode_input, ple_encode}; use crate::encoding::utils::{encode_input, ple_encode};
use alloy_sol_types::SolValue; use alloy_sol_types::SolValue;
use anyhow::Error; use anyhow::Error;
struct RouterEncoder<S: StrategySelector> { struct RouterEncoder<S: StrategySelector, A: ApprovalsManager> {
strategy_selector: S, strategy_selector: S,
approvals_manager: A,
} }
impl<S: StrategySelector> RouterEncoder<S> { impl<S: StrategySelector, A: ApprovalsManager> RouterEncoder<S, A> {
pub fn new(strategy_selector: S) -> Self { pub fn new(strategy_selector: S, approvals_manager: A) -> Self {
RouterEncoder { strategy_selector } RouterEncoder {
strategy_selector,
approvals_manager,
}
} }
pub fn encode_router_calldata(&self, solution: Solution) -> Result<Vec<u8>, Error> { pub fn encode_router_calldata(&self, solution: Solution) -> Result<Vec<u8>, Error> {
let permit_calldata = self.handle_approvals(&solution)?; // TODO: where should we append this? let approvals_calldata = self.handle_approvals(&solution)?; // TODO: where should we append this?
let mut calldata_list: Vec<Vec<u8>> = Vec::new(); let mut calldata_list: Vec<Vec<u8>> = Vec::new();
let encode_for_batch_execute = solution.orders.len() > 1; let encode_for_batch_execute = solution.orders.len() > 1;
for order in solution.orders { for order in solution.orders {
@@ -31,18 +35,18 @@ impl<S: StrategySelector> RouterEncoder<S> {
} }
fn handle_approvals(&self, solution: &Solution) -> Result<Vec<u8>, Error> { fn handle_approvals(&self, solution: &Solution) -> Result<Vec<u8>, Error> {
let mut permits = Vec::new(); let mut approvals = Vec::new();
for order in solution.orders.iter() { for order in solution.orders.iter() {
permits.push(PermitRequest { approvals.push(Approval {
token: order.given_token.clone(), token: order.given_token.clone(),
spender: order.sender.clone(), spender: order
amount: order.given_amount.clone(),
router_address: order
.router_address .router_address
.clone() .clone()
.unwrap_or(PROPELLER_ROUTER_ADDRESS.clone()), .unwrap_or(PROPELLER_ROUTER_ADDRESS.clone()),
amount: order.given_amount.clone(),
owner: order.sender.clone(),
}); });
} }
Ok(Permit2::new().encode_permit(permits)) Ok(self.approvals_manager.encode_approvals(approvals))
} }
} }

View File

@@ -1,15 +1,16 @@
use alloy_primitives::Address; use crate::encoding::approvals::approvals_manager::TokenApprovalsManager;
use crate::encoding::approvals::interface::Approval;
use crate::encoding::approvals::interface::ApprovalsManager;
use crate::encoding::models::{EncodingContext, Swap};
use crate::encoding::utils::bytes_to_address;
use alloy_primitives::{Address, U256};
use alloy_sol_types::SolValue; use alloy_sol_types::SolValue;
use anyhow::Error; use anyhow::Error;
use num_bigint::BigUint;
use num_traits::identities::One;
use std::str::FromStr; use std::str::FromStr;
use tycho_core::Bytes; use tycho_core::Bytes;
use crate::encoding::utils::bytes_to_address;
use crate::encoding::{
approvals_manager::{get_client, TokenApprovalsManager},
models::{EncodingContext, Swap},
};
pub trait SwapEncoder: Sync + Send { pub trait SwapEncoder: Sync + Send {
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error>; fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error>;
} }
@@ -37,19 +38,19 @@ impl BalancerV2SwapEncoder {
impl SwapEncoder for BalancerV2SwapEncoder { impl SwapEncoder for BalancerV2SwapEncoder {
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> { fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
let client = get_client(); let token_approvals_manager = TokenApprovalsManager::new();
let token_approvals_manager = TokenApprovalsManager::new(client);
let runtime = tokio::runtime::Handle::try_current() let runtime = tokio::runtime::Handle::try_current()
.is_err() .is_err()
.then(|| tokio::runtime::Runtime::new().unwrap()) .then(|| tokio::runtime::Runtime::new().unwrap())
.unwrap(); .unwrap();
let approval_needed = runtime.block_on(async { let approval_needed = runtime.block_on(async {
token_approvals_manager token_approvals_manager
.approval_needed( .approval_needed(Approval {
swap.token_in.clone(), spender: self.vault_address.clone(),
encoding_context.router_address, owner: encoding_context.router_address,
self.vault_address.clone(), token: swap.token_in.clone(),
) amount: (BigUint::one() << 256) - BigUint::one(), // max U256
})
.await .await
}); });
// should we return gas estimation here too?? if there is an approval needed, gas will be // should we return gas estimation here too?? if there is an approval needed, gas will be