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 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;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
pub struct TokenApprovalsManager {
|
pub struct ProtocolApprovalsManager {
|
||||||
client: Arc<RootProvider<BoxTransport>>,
|
client: Arc<RootProvider<BoxTransport>>,
|
||||||
}
|
}
|
||||||
impl TokenApprovalsManager {
|
impl ProtocolApprovalsManager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
client: get_client(),
|
client: get_client(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub async fn approval_needed(&self, approval: Approval) -> bool {
|
pub async fn approval_needed(
|
||||||
todo!()
|
&self,
|
||||||
}
|
token: Address,
|
||||||
}
|
spender_address: Address,
|
||||||
|
router_address: Address,
|
||||||
impl ApprovalsManager for TokenApprovalsManager {
|
) -> bool {
|
||||||
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8> {
|
|
||||||
todo!()
|
todo!()
|
||||||
// should be something like
|
// should be something like
|
||||||
// let allowance = self
|
// let allowance = self
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ pub struct Approval {
|
|||||||
pub amount: BigUint,
|
pub amount: BigUint,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ApprovalsManager {
|
pub trait UserApprovalsManager {
|
||||||
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8>;
|
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 alloy_primitives::U256;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tycho_core::Bytes;
|
use tycho_core::Bytes;
|
||||||
@@ -25,7 +25,7 @@ impl Permit2 {
|
|||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl ApprovalsManager for Permit2 {
|
impl UserApprovalsManager for Permit2 {
|
||||||
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8> {
|
fn encode_approvals(&self, approvals: Vec<Approval>) -> Vec<u8> {
|
||||||
// calls get_allowance_data to get nonce
|
// calls get_allowance_data to get nonce
|
||||||
// checks if we are not permitted already
|
// 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::models::{Solution, PROPELLER_ROUTER_ADDRESS};
|
||||||
use crate::encoding::strategy_encoder::StrategyEncoder;
|
use crate::encoding::strategy_encoder::StrategyEncoder;
|
||||||
use crate::encoding::strategy_selector::StrategySelector;
|
use crate::encoding::strategy_selector::StrategySelector;
|
||||||
@@ -6,11 +6,11 @@ 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, A: ApprovalsManager> {
|
struct RouterEncoder<S: StrategySelector, A: UserApprovalsManager> {
|
||||||
strategy_selector: S,
|
strategy_selector: S,
|
||||||
approvals_manager: A,
|
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 {
|
pub fn new(strategy_selector: S, approvals_manager: A) -> Self {
|
||||||
RouterEncoder {
|
RouterEncoder {
|
||||||
strategy_selector,
|
strategy_selector,
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
use crate::encoding::approvals::approvals_manager::TokenApprovalsManager;
|
use crate::encoding::approvals::approvals_manager::ProtocolApprovalsManager;
|
||||||
use crate::encoding::approvals::interface::Approval;
|
|
||||||
use crate::encoding::models::{EncodingContext, Swap};
|
use crate::encoding::models::{EncodingContext, Swap};
|
||||||
use crate::encoding::utils::bytes_to_address;
|
use crate::encoding::utils::bytes_to_address;
|
||||||
use alloy_primitives::Address;
|
use alloy_primitives::Address;
|
||||||
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;
|
|
||||||
|
|
||||||
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>;
|
||||||
@@ -23,13 +19,13 @@ impl SwapEncoder for UniswapV2SwapEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct BalancerV2SwapEncoder {
|
struct BalancerV2SwapEncoder {
|
||||||
vault_address: Bytes,
|
vault_address: Address,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BalancerV2SwapEncoder {
|
impl BalancerV2SwapEncoder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
vault_address: Bytes::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
vault_address: Address::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
||||||
.expect("Invalid string for balancer vault address"),
|
.expect("Invalid string for balancer vault address"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,19 +33,16 @@ 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 token_approvals_manager = TokenApprovalsManager::new();
|
let token_approvals_manager = ProtocolApprovalsManager::new();
|
||||||
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 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 {
|
let approval_needed = runtime.block_on(async {
|
||||||
token_approvals_manager
|
token_approvals_manager
|
||||||
.approval_needed(Approval {
|
.approval_needed(token, self.vault_address.clone(), router_address)
|
||||||
spender: self.vault_address.clone(),
|
|
||||||
owner: encoding_context.address_for_approvals,
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user