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 OdosRouterV2::{inputTokenInfo, outputTokenInfo, swapTokenInfo};
13use OdosV2Router::{swapCall, OdosV2RouterCalls, OdosV2RouterInstance, Swap, SwapMulti};
14
15#[derive(Debug, Clone)]
17pub struct V2Router<P: Provider<Ethereum>> {
18 instance: OdosV2RouterInstance<P>,
19}
20
21impl<P: Provider<Ethereum>> V2Router<P> {
22 pub fn new(address: Address, provider: P) -> Self {
23 Self {
24 instance: OdosV2RouterInstance::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 fn build_swap_router_funds_call(
33 &self,
34 input_token_info: inputTokenInfo,
35 output_token_info: outputTokenInfo,
36 inputs: &SwapInputs,
37 from: Address,
38 ) -> CallBuilder<&P, PhantomData<OdosV2Router::swapRouterFundsCall>> {
39 self.instance
40 .swapRouterFunds(
41 vec![input_token_info],
42 vec![output_token_info],
43 inputs.value_out_min(),
44 inputs.path_definition().clone(),
45 inputs.executor(),
46 )
47 .from(from)
48 }
49
50 pub fn transfer_router_funds(
51 &self,
52 from: Address,
53 token: Address,
54 amount: U256,
55 output_recipient: Address,
56 ) -> CallBuilder<&P, PhantomData<OdosV2Router::transferRouterFundsCall>> {
57 self.instance
58 .transferRouterFunds(vec![token], vec![amount], output_recipient)
59 .from(from)
60 }
61
62 pub fn transfer_router_funds_calldata(
63 &self,
64 from: Address,
65 token: Address,
66 amount: U256,
67 output_recipient: Address,
68 ) -> Vec<u8> {
69 self.transfer_router_funds(from, token, amount, output_recipient)
70 .calldata()
71 .to_vec()
72 }
73}
74
75sol!(
77 #[allow(clippy::too_many_arguments)]
78 #[allow(missing_docs)]
79 #[sol(rpc)]
80 OdosV2Router,
81 "abis/odos_v2_router.json"
82);
83
84impl Debug for OdosV2Router::swapRouterFundsReturn {
85 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 write!(f, "amountsOut: {:?}", self.amountsOut)
87 }
88}
89
90impl Debug for OdosRouterV2::inputTokenInfo {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 f.debug_struct("inputTokenInfo")
93 .field("tokenAddress", &self.tokenAddress)
94 .field("amountIn", &self.amountIn)
95 .field("receiver", &self.receiver)
96 .finish()
97 }
98}
99
100impl Debug for OdosRouterV2::outputTokenInfo {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 f.debug_struct("outputTokenInfo")
103 .field("tokenAddress", &self.tokenAddress)
104 .field("relativeValue", &self.relativeValue)
105 .field("receiver", &self.receiver)
106 .finish()
107 }
108}
109
110impl Debug for swapCall {
111 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112 f.debug_struct("swapCall")
113 .field("executor", &self.executor)
114 .field("pathDefinition", &self.pathDefinition)
115 .field("referralCode", &self.referralCode)
116 .field("tokenInfo", &self.tokenInfo)
117 .finish()
118 }
119}
120
121impl Debug for swapTokenInfo {
122 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123 f.debug_struct("swapTokenInfo")
124 .field("inputToken", &self.inputToken)
125 .field("inputAmount", &self.inputAmount)
126 .field("inputReceiver", &self.inputReceiver)
127 .field("outputMin", &self.outputMin)
128 .field("outputQuote", &self.outputQuote)
129 .field("outputReceiver", &self.outputReceiver)
130 .finish()
131 }
132}
133
134impl Debug for SwapMulti {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 f.debug_struct("SwapMulti")
137 .field("sender", &self.sender)
138 .field("amountsIn", &self.amountsIn)
139 .field("tokensIn", &self.tokensIn)
140 .field("amountsOut", &self.amountsOut)
141 .field("tokensOut", &self.tokensOut)
142 .finish()
143 }
144}
145
146impl Debug for Swap {
147 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148 f.debug_struct("Swap")
149 .field("sender", &self.sender)
150 .field("inputAmount", &self.inputAmount)
151 .field("inputToken", &self.inputToken)
152 .field("amountOut", &self.amountOut)
153 .field("outputToken", &self.outputToken)
154 .field("slippage", &self.slippage)
155 .field("referralCode", &self.referralCode)
156 .finish()
157 }
158}
159
160impl TryFrom<&Bytes> for OdosV2RouterCalls {
161 type Error = alloy_sol_types::Error;
162
163 fn try_from(input: &Bytes) -> Result<Self, Self::Error> {
164 OdosV2RouterCalls::abi_decode(input)
165 }
166}