1use std::{fmt::Debug, marker::PhantomData};
2
3use alloy_contract::CallBuilder;
4use alloy_network::Ethereum;
5use alloy_primitives::{Address, Bytes, U256};
6use alloy_provider::Provider;
7use alloy_sol_types::{sol, SolInterface};
8
9use crate::SwapInputs;
10
11use IOdosRouterV3::{inputTokenInfo, outputTokenInfo, swapReferralInfo, swapTokenInfo};
13use OdosV3Router::{swapCall, OdosV3RouterCalls, OdosV3RouterInstance, Swap, SwapMulti};
14
15#[derive(Debug, Clone)]
17pub struct V3Router<P: Provider<Ethereum>> {
18 instance: OdosV3RouterInstance<P>,
19}
20
21impl<P: Provider<Ethereum>> V3Router<P> {
22 pub fn new(address: Address, provider: P) -> Self {
23 Self {
24 instance: OdosV3RouterInstance::new(address, provider),
25 }
26 }
27
28 pub async fn owner(&self) -> Result<Address, alloy_contract::Error> {
29 self.instance.owner().call().await
30 }
31
32 pub async fn liquidator_address(&self) -> Result<Address, alloy_contract::Error> {
33 self.instance.liquidatorAddress().call().await
34 }
35
36 pub fn build_swap_router_funds_call(
37 &self,
38 input_token_info: inputTokenInfo,
39 output_token_info: outputTokenInfo,
40 inputs: &SwapInputs,
41 from: Address,
42 ) -> CallBuilder<&P, PhantomData<OdosV3Router::swapRouterFundsCall>> {
43 self.instance
44 .swapRouterFunds(
45 vec![input_token_info],
46 vec![output_token_info],
47 inputs.path_definition().clone(),
48 inputs.executor(),
49 )
50 .from(from)
51 }
52
53 pub fn transfer_router_funds(
54 &self,
55 from: Address,
56 token: Address,
57 amount: U256,
58 output_recipient: Address,
59 ) -> CallBuilder<&P, PhantomData<OdosV3Router::transferRouterFundsCall>> {
60 self.instance
61 .transferRouterFunds(vec![token], vec![amount], output_recipient)
62 .from(from)
63 }
64
65 pub fn transfer_router_funds_calldata(
66 &self,
67 from: Address,
68 token: Address,
69 amount: U256,
70 output_recipient: Address,
71 ) -> Vec<u8> {
72 self.transfer_router_funds(from, token, amount, output_recipient)
73 .calldata()
74 .to_vec()
75 }
76}
77
78sol!(
80 #[allow(clippy::too_many_arguments)]
81 #[allow(missing_docs)]
82 #[sol(rpc)]
83 OdosV3Router,
84 "abis/v3.json"
85);
86
87impl Debug for OdosV3Router::swapRouterFundsReturn {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 write!(f, "amountsOut: {:?}", self.amountsOut)
90 }
91}
92
93impl Debug for inputTokenInfo {
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 f.debug_struct("inputTokenInfo")
96 .field("tokenAddress", &self.tokenAddress)
97 .field("amountIn", &self.amountIn)
98 .field("receiver", &self.receiver)
99 .finish()
100 }
101}
102
103impl Debug for outputTokenInfo {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 f.debug_struct("outputTokenInfo")
106 .field("tokenAddress", &self.tokenAddress)
107 .field("amountQuote", &self.amountQuote)
108 .field("amountMin", &self.amountMin)
109 .field("receiver", &self.receiver)
110 .finish()
111 }
112}
113
114impl Debug for swapCall {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 f.debug_struct("swapCall")
117 .field("executor", &self.executor)
118 .field("pathDefinition", &self.pathDefinition)
119 .field("referralInfo", &self.referralInfo)
120 .field("tokenInfo", &self.tokenInfo)
121 .finish()
122 }
123}
124
125impl Debug for swapReferralInfo {
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 f.debug_struct("swapReferralInfo")
128 .field("code", &self.code)
129 .field("fee", &self.fee)
130 .field("feeRecipient", &self.feeRecipient)
131 .finish()
132 }
133}
134
135impl Debug for SwapMulti {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 f.debug_struct("SwapMulti")
138 .field("sender", &self.sender)
139 .field("amountsIn", &self.amountsIn)
140 .field("tokensIn", &self.tokensIn)
141 .field("amountsOut", &self.amountsOut)
142 .field("tokensOut", &self.tokensOut)
143 .finish()
144 }
145}
146
147impl Debug for Swap {
148 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149 f.debug_struct("Swap")
150 .field("sender", &self.sender)
151 .field("inputAmount", &self.inputAmount)
152 .field("inputToken", &self.inputToken)
153 .field("amountOut", &self.amountOut)
154 .field("outputToken", &self.outputToken)
155 .field("slippage", &self.slippage)
156 .field("referralCode", &self.referralCode)
157 .finish()
158 }
159}
160
161impl Debug for swapTokenInfo {
162 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163 f.debug_struct("swapTokenInfo")
164 .field("inputToken", &self.inputToken)
165 .field("inputAmount", &self.inputAmount)
166 .field("inputReceiver", &self.inputReceiver)
167 .field("outputToken", &self.outputToken)
168 .field("outputQuote", &self.outputQuote)
169 .field("outputMin", &self.outputMin)
170 .field("outputReceiver", &self.outputReceiver)
171 .finish()
172 }
173}
174
175impl TryFrom<&Bytes> for OdosV3RouterCalls {
176 type Error = alloy_sol_types::Error;
177
178 fn try_from(input: &Bytes) -> Result<Self, Self::Error> {
179 OdosV3RouterCalls::abi_decode(input)
180 }
181}