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,20 +1,24 @@
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
use crate::encoding::models::{Solution, PROPELLER_ROUTER_ADDRESS};
use crate::encoding::permit2::{Permit2, PermitRequest};
use crate::encoding::strategy_encoder::StrategyEncoder;
use crate::encoding::strategy_selector::StrategySelector;
use crate::encoding::utils::{encode_input, ple_encode};
use alloy_sol_types::SolValue;
use anyhow::Error;
struct RouterEncoder<S: StrategySelector> {
struct RouterEncoder<S: StrategySelector, A: ApprovalsManager> {
strategy_selector: S,
approvals_manager: A,
}
impl<S: StrategySelector> RouterEncoder<S> {
pub fn new(strategy_selector: S) -> Self {
RouterEncoder { strategy_selector }
impl<S: StrategySelector, A: ApprovalsManager> RouterEncoder<S, A> {
pub fn new(strategy_selector: S, approvals_manager: A) -> Self {
RouterEncoder {
strategy_selector,
approvals_manager,
}
}
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 encode_for_batch_execute = solution.orders.len() > 1;
for order in solution.orders {
@@ -31,18 +35,18 @@ impl<S: StrategySelector> RouterEncoder<S> {
}
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() {
permits.push(PermitRequest {
approvals.push(Approval {
token: order.given_token.clone(),
spender: order.sender.clone(),
amount: order.given_amount.clone(),
router_address: order
spender: order
.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))
}
}