1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::{
abi::IUniswapV3Factory,
global::{
BASE_FACTORY_V2, BASE_FACTORY_V3, BSC_FACTORY_V2, BSC_FACTORY_V3, ETHEREUM_FACTORY_V2,
ETHEREUM_FACTORY_V3,
},
};
use ethers::{
middleware::SignerMiddleware,
types::{Address, H256, U256},
};
use evm_client::EvmType;
use evm_sdk::Evm;
use evm_sdk::types::EvmError;
use std::sync::Arc;
/// pancakeswap factory service
pub struct FactoryService {
evm: Arc<Evm>,
}
impl FactoryService {
/// create a factory service
pub fn new(evm: Arc<Evm>) -> Self {
Self { evm: evm }
}
/// Retrieves all liquidity pools (V2 and V3) for a given token address
///
/// # Example
/// ```
/// use ethers::types::Address;
/// use std::sync::Arc;
/// let factory_service = FactoryService::new(Arc::clone(&client));
/// let token_address = "0x...".parse::<Address>().unwrap();
/// async {
/// let pools = factory_service.get_pools_by_token(token_address).await?;
/// Ok::<(), EvmError>(())
/// };
/// ```
pub async fn get_pools_by_token(
&self,
token_address: Address,
) -> Result<Vec<Address>, EvmError> {
let mut pools = Vec::new();
if let Ok(v2_pools) = self.get_v2_pools_by_token(token_address).await {
pools.extend(v2_pools);
}
if let Ok(v3_pools) = self.get_v3_pools_by_token(token_address).await {
pools.extend(v3_pools);
}
Ok(pools)
}
/// Get the V2 liquidity pool address
async fn get_v2_pools_by_token(
&self,
token_address: Address,
) -> Result<Vec<Address>, EvmError> {
let factory_address = match self.evm.client.evm_type {
Some(EvmType::BSC_MAINNET) => BSC_FACTORY_V2.parse::<Address>().unwrap(),
Some(EvmType::ETHEREUM_MAINNET) => ETHEREUM_FACTORY_V2.parse::<Address>().unwrap(),
Some(EvmType::BASE_MAINNET) => BASE_FACTORY_V2.parse::<Address>().unwrap(),
_ => return Err(EvmError::ConfigError("Unsupported chain".to_string())),
};
let factory = crate::abi::IPancakeFactory::new(
factory_address,
Arc::clone(&self.evm.client.provider),
);
let total_pairs =
factory.all_pairs_length().call().await.map_err(|e| {
EvmError::ContractError(format!("Failed to get total pairs: {}", e))
})?;
let mut pools = Vec::new();
let max_check = 500u64;
for i in 0..std::cmp::min(total_pairs.as_u64(), max_check) {
if let Ok(pair_address) = factory.all_pairs(i.into()).call().await {
let pair = crate::abi::IPancakePair::new(
pair_address,
Arc::clone(&self.evm.client.provider),
);
if let Ok(token0) = pair.token_0().call().await {
if let Ok(token1) = pair.token_1().call().await {
if token0 == token_address || token1 == token_address {
pools.push(pair_address);
}
}
}
}
}
Ok(pools)
}
/// Get the V3 liquidity pool address
async fn get_v3_pools_by_token(
&self,
token_address: Address,
) -> Result<Vec<Address>, EvmError> {
let factory_address = match self.evm.client.evm_type {
Some(EvmType::BSC_MAINNET) => BSC_FACTORY_V3.parse::<Address>().unwrap(),
Some(EvmType::ETHEREUM_MAINNET) => ETHEREUM_FACTORY_V3.parse::<Address>().unwrap(),
Some(EvmType::BASE_MAINNET) => BASE_FACTORY_V3.parse::<Address>().unwrap(),
_ => return Err(EvmError::ConfigError("Unsupported chain".to_string())),
};
let factory =
IUniswapV3Factory::new(factory_address, Arc::clone(&self.evm.client.provider));
let fee_tiers = vec![100, 500, 2500, 10000];
let mut pools = Vec::new();
let common_tokens = vec![match self.evm.client.evm_type {
Some(EvmType::BSC_MAINNET) => "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
.parse()
.unwrap(), // WBNB
Some(EvmType::ETHEREUM_MAINNET) => "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
.parse()
.unwrap(), // WETH
_ => Address::zero(),
}];
for other_token in common_tokens {
if other_token == Address::zero() || other_token == token_address {
continue;
}
for &fee in &fee_tiers {
if let Ok(pool_address) = factory
.get_pool(token_address, other_token, fee)
.call()
.await
{
if pool_address != Address::zero() {
pools.push(pool_address);
}
}
}
}
Ok(pools)
}
/// Gets the pair address for two tokens
///
/// # Example
/// ```
/// use ethers::types::Address;
/// use std::sync::Arc;
/// let factory_service = FactoryService::new(Arc::clone(&client));
/// let factory_address = "0x...".parse::<Address>().unwrap();
/// let token_a = "0x...".parse::<Address>().unwrap();
/// let token_b = "0x...".parse::<Address>().unwrap();
/// async {
/// let pair = factory_service.get_pair(factory_address, token_a, token_b).await?;
/// Ok::<(), EvmError>(())
/// };
/// ```
pub async fn get_pair(
&self,
factory_address: Address,
token_a: Address,
token_b: Address,
) -> Result<Option<Address>, EvmError> {
let factory =
crate::abi::IPancakeFactory::new(factory_address, self.evm.client.provider.clone());
let pair = factory
.get_pair(token_a, token_b)
.call()
.await
.map_err(|e| EvmError::ContractError(format!("Failed to get pair: {}", e)))?;
Ok(if pair == Address::zero() {
None
} else {
Some(pair)
})
}
/// Creates a new pair for two tokens
///
/// # Example
/// ```
/// use ethers::types::Address;
/// use std::sync::Arc;
/// let factory_service = FactoryService::new(Arc::clone(&client));
/// let factory_address = "0x...".parse::<Address>().unwrap();
/// let token_a = "0x...".parse::<Address>().unwrap();
/// let token_b = "0x...".parse::<Address>().unwrap();
/// async {
/// let pair_address = factory_service.create_pair(factory_address, token_a, token_b).await?;
/// Ok::<(), EvmError>(())
/// };
/// ```
pub async fn create_pair(
&self,
factory_address: Address,
token_a: Address,
token_b: Address,
) -> Result<Address, EvmError> {
if self.evm.client.wallet.is_none() {
return Err(EvmError::WalletError("No wallet configured".to_string()));
}
let wallet = self.evm.client.wallet.as_ref().unwrap();
let signer_middleware =
SignerMiddleware::new(self.evm.client.provider.clone(), wallet.clone());
let factory =
crate::abi::IPancakeFactory::new(factory_address, Arc::new(signer_middleware));
let tx = factory.create_pair(token_a, token_b);
let pending_tx = tx
.send()
.await
.map_err(|e| EvmError::TransactionError(format!("Failed to create pair: {}", e)))?;
let receipt = pending_tx
.await
.map_err(|e| EvmError::TransactionError(format!("Failed to get receipt: {}", e)))?
.ok_or_else(|| EvmError::TransactionError("Transaction failed".to_string()))?;
// Get the newly created transaction pair address from the event log
let pair_created_topic = H256::from_slice(ðers::utils::keccak256(
b"PairCreated(address,address,address,uint256)",
));
if let Some(log) = receipt.logs.iter().find(|log| log.topics.len() >= 3) {
if log.topics[0] == pair_created_topic {
let pair_address = Address::from_slice(&log.data[12..32]);
return Ok(pair_address);
}
}
Err(EvmError::TransactionError(
"Failed to extract pair address from logs".to_string(),
))
}
/// Gets the total number of pairs in the factory
///
/// # Example
/// ```
/// use ethers::types::Address;
/// use std::sync::Arc;
/// let factory_service = FactoryService::new(Arc::clone(&client));
/// let factory_address = "0x...".parse::<Address>().unwrap();
/// async {
/// let total_pairs = factory_service.all_pairs_length(factory_address).await?;
/// Ok::<(), EvmError>(())
/// };
/// ```
pub async fn all_pairs_length(&self, factory_address: Address) -> Result<U256, EvmError> {
let factory =
crate::abi::IPancakeFactory::new(factory_address, self.evm.client.provider.clone());
factory
.all_pairs_length()
.call()
.await
.map_err(|e| EvmError::ContractError(format!("Failed to get pairs length: {}", e)))
}
/// Gets the pair address at a specific index
///
/// # Example
/// ```
/// use ethers::types::{Address, U256};
/// use std::sync::Arc;
/// let factory_service = FactoryService::new(Arc::clone(&client));
/// let factory_address = "0x...".parse::<Address>().unwrap();
/// let index = U256::from(0);
/// async {
/// let pair_address = factory_service.all_pairs(factory_address, index).await?;
/// Ok::<(), EvmError>(())
/// };
/// ```
pub async fn all_pairs(
&self,
factory_address: Address,
index: U256,
) -> Result<Address, EvmError> {
let factory =
crate::abi::IPancakeFactory::new(factory_address, self.evm.client.provider.clone());
factory.all_pairs(index).call().await.map_err(|e| {
EvmError::ContractError(format!("Failed to get pair at index {}: {}", index, e))
})
}
/// Get the fee receiving address
pub async fn fee_to(&self, factory_address: Address) -> Result<Address, EvmError> {
let factory =
crate::abi::IPancakeFactory::new(factory_address, self.evm.client.provider.clone());
factory
.fee_to()
.call()
.await
.map_err(|e| EvmError::ContractError(format!("Failed to get fee to: {}", e)))
}
/// Get the address of the person who set the fee
pub async fn fee_to_setter(&self, factory_address: Address) -> Result<Address, EvmError> {
let factory =
crate::abi::IPancakeFactory::new(factory_address, self.evm.client.provider.clone());
factory
.fee_to_setter()
.call()
.await
.map_err(|e| EvmError::ContractError(format!("Failed to get fee to setter: {}", e)))
}
/// Gets multiple pairs in batch
///
/// # Example
/// ```
/// # use ethers::types::Address;
/// # use std::sync::Arc;
/// # let factory_service = FactoryService::new(Arc::clone(&client));
/// # let factory_address = "0x...".parse::<Address>().unwrap();
/// # async {
/// let pairs = factory_service.get_all_pairs(factory_address, 0, 10).await?;
/// # Ok::<(), EvmError>(())
/// # };
/// ```
pub async fn get_all_pairs(
&self,
factory_address: Address,
start_index: u64,
count: u64,
) -> Result<Vec<Address>, EvmError> {
let total_pairs = self.all_pairs_length(factory_address).await?;
let end_index = std::cmp::min(start_index + count, total_pairs.as_u64());
let mut pairs = Vec::new();
for i in start_index..end_index {
let pair_address = self.all_pairs(factory_address, i.into()).await?;
pairs.push(pair_address);
}
Ok(pairs)
}
/// Checks if a pair exists for two tokens
///
/// # Example
/// ```
/// use ethers::types::Address;
/// use std::sync::Arc;
/// let factory_service = FactoryService::new(Arc::clone(&client));
/// let factory_address = "0x...".parse::<Address>().unwrap();
/// let token_a = "0x...".parse::<Address>().unwrap();
/// let token_b = "0x...".parse::<Address>().unwrap();
/// async {
/// let exists = factory_service.pair_exists(factory_address, token_a, token_b).await?;
/// Ok::<(), EvmError>(())
/// };
/// ```
pub async fn pair_exists(
&self,
factory_address: Address,
token_a: Address,
token_b: Address,
) -> Result<bool, EvmError> {
let pair = self.get_pair(factory_address, token_a, token_b).await?;
Ok(pair.is_some())
}
}