fix: TokenApprovalsManager should not implement ApprovalsManager
They should be separate. ApprovalsManager is only for user approvals
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
use std::{env, sync::Arc};
|
||||
|
||||
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
|
||||
use alloy::{
|
||||
providers::{Provider, ProviderBuilder, RootProvider},
|
||||
transports::BoxTransport,
|
||||
};
|
||||
use alloy_primitives::Address;
|
||||
use dotenv::dotenv;
|
||||
|
||||
pub struct TokenApprovalsManager {
|
||||
pub struct ProtocolApprovalsManager {
|
||||
client: Arc<RootProvider<BoxTransport>>,
|
||||
}
|
||||
impl TokenApprovalsManager {
|
||||
impl ProtocolApprovalsManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
client: get_client(),
|
||||
}
|
||||
}
|
||||
pub async fn approval_needed(&self, approval: Approval) -> bool {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl ApprovalsManager for TokenApprovalsManager {
|
||||
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8> {
|
||||
pub async fn approval_needed(
|
||||
&self,
|
||||
token: Address,
|
||||
spender_address: Address,
|
||||
router_address: Address,
|
||||
) -> bool {
|
||||
todo!()
|
||||
// should be something like
|
||||
// let allowance = self
|
||||
|
||||
@@ -8,6 +8,6 @@ pub struct Approval {
|
||||
pub amount: BigUint,
|
||||
}
|
||||
|
||||
pub trait ApprovalsManager {
|
||||
pub trait UserApprovalsManager {
|
||||
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8>;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
|
||||
use crate::encoding::approvals::interface::{Approval, UserApprovalsManager};
|
||||
use alloy_primitives::U256;
|
||||
use std::str::FromStr;
|
||||
use tycho_core::Bytes;
|
||||
@@ -25,7 +25,7 @@ impl Permit2 {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl ApprovalsManager for Permit2 {
|
||||
impl UserApprovalsManager 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::encoding::approvals::interface::{Approval, ApprovalsManager};
|
||||
use crate::encoding::approvals::interface::{Approval, UserApprovalsManager};
|
||||
use crate::encoding::models::{Solution, PROPELLER_ROUTER_ADDRESS};
|
||||
use crate::encoding::strategy_encoder::StrategyEncoder;
|
||||
use crate::encoding::strategy_selector::StrategySelector;
|
||||
@@ -6,11 +6,11 @@ use crate::encoding::utils::{encode_input, ple_encode};
|
||||
use alloy_sol_types::SolValue;
|
||||
use anyhow::Error;
|
||||
|
||||
struct RouterEncoder<S: StrategySelector, A: ApprovalsManager> {
|
||||
struct RouterEncoder<S: StrategySelector, A: UserApprovalsManager> {
|
||||
strategy_selector: S,
|
||||
approvals_manager: A,
|
||||
}
|
||||
impl<S: StrategySelector, A: ApprovalsManager> RouterEncoder<S, A> {
|
||||
impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> {
|
||||
pub fn new(strategy_selector: S, approvals_manager: A) -> Self {
|
||||
RouterEncoder {
|
||||
strategy_selector,
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
use crate::encoding::approvals::approvals_manager::TokenApprovalsManager;
|
||||
use crate::encoding::approvals::interface::Approval;
|
||||
use crate::encoding::approvals::approvals_manager::ProtocolApprovalsManager;
|
||||
use crate::encoding::models::{EncodingContext, Swap};
|
||||
use crate::encoding::utils::bytes_to_address;
|
||||
use alloy_primitives::Address;
|
||||
use alloy_sol_types::SolValue;
|
||||
use anyhow::Error;
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::identities::One;
|
||||
use std::str::FromStr;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
pub trait SwapEncoder: Sync + Send {
|
||||
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error>;
|
||||
@@ -23,13 +19,13 @@ impl SwapEncoder for UniswapV2SwapEncoder {
|
||||
}
|
||||
|
||||
struct BalancerV2SwapEncoder {
|
||||
vault_address: Bytes,
|
||||
vault_address: Address,
|
||||
}
|
||||
|
||||
impl BalancerV2SwapEncoder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
vault_address: Bytes::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
||||
vault_address: Address::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
||||
.expect("Invalid string for balancer vault address"),
|
||||
}
|
||||
}
|
||||
@@ -37,19 +33,16 @@ impl BalancerV2SwapEncoder {
|
||||
|
||||
impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
|
||||
let token_approvals_manager = TokenApprovalsManager::new();
|
||||
let token_approvals_manager = ProtocolApprovalsManager::new();
|
||||
let runtime = tokio::runtime::Handle::try_current()
|
||||
.is_err()
|
||||
.then(|| tokio::runtime::Runtime::new().unwrap())
|
||||
.unwrap();
|
||||
let token = bytes_to_address(&swap.token_in)?;
|
||||
let router_address = bytes_to_address(&encoding_context.address_for_approvals)?;
|
||||
let approval_needed = runtime.block_on(async {
|
||||
token_approvals_manager
|
||||
.approval_needed(Approval {
|
||||
spender: self.vault_address.clone(),
|
||||
owner: encoding_context.address_for_approvals,
|
||||
token: swap.token_in.clone(),
|
||||
amount: (BigUint::one() << 256) - BigUint::one(), // max U256
|
||||
})
|
||||
.approval_needed(token, self.vault_address.clone(), router_address)
|
||||
.await
|
||||
});
|
||||
// should we return gas estimation here too?? if there is an approval needed, gas will be
|
||||
|
||||
Reference in New Issue
Block a user