gweiyser/protocols/uniswap/v3/
quoter.rs

1use alloy::network::Network;
2use alloy::primitives::{Address, U256};
3use alloy::providers::Provider;
4use alloy::transports::Transport;
5use std::sync::Arc;
6
7use super::gen::{IQuoter, IQuoter::IQuoterInstance};
8use crate::addresses::amm_addrs::uniswap_v3::{base, ethereum};
9use crate::Chain;
10
11pub struct UniswapV3Quoter<P, T, N>
12where
13    P: Provider<T, N>,
14    T: Transport + Clone,
15    N: Network,
16{
17    quoter_contract: IQuoterInstance<T, Arc<P>, N>,
18}
19
20impl<P, T, N> UniswapV3Quoter<P, T, N>
21where
22    P: Provider<T, N>,
23    T: Transport + Clone,
24    N: Network,
25{
26    pub fn new(provider: Arc<P>, chain: Chain) -> Self {
27        let addr = if chain == Chain::Ethereum {
28            ethereum::QUOTER
29        } else {
30            base::QUOTER
31        };
32
33        let quoter_contract = IQuoter::new(addr, provider.clone());
34        Self { quoter_contract }
35    }
36
37    pub async fn quote_exact_input_single(
38        &self,
39        amount_in: U256,
40        token_in: Address,
41        token_out: Address,
42        fee: u32,
43    ) -> U256 {
44        let params = IQuoter::QuoteExactInputSingleParams {
45            tokenIn: token_in,
46            tokenOut: token_out,
47            fee: fee,
48            amountIn: amount_in,
49            sqrtPriceLimitX96: U256::ZERO,
50        };
51        let IQuoter::quoteExactInputSingleReturn {
52            amountOut,
53            sqrtPriceX96After,
54            initializedTicksCrossed,
55            gasEstimate,
56        } = self
57            .quoter_contract
58            .quoteExactInputSingle(params)
59            .call()
60            .await
61            .unwrap();
62        amountOut
63    }
64}