pancakeswap-sdk 0.6.5

pancakeswap sdk, Implemented functions related to interaction with pancakeswap.
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use evm_sdk::types::EvmError;
use crate::abi::{IMasterChefV2, IPancakePair, ISmartChefFactory, ISmartChefInitializable};
use ethers::middleware::SignerMiddleware;
use ethers::types::{Address, U256};
use evm_sdk::Evm;
use std::sync::Arc;

/// Farm pool information
#[derive(Debug, Clone)]
pub struct FarmInfo {
    pub pid: u64,
    pub lp_token: Address,
    pub alloc_point: U256,
    pub last_reward_block: U256,
    pub acc_cake_per_share: U256,
    pub total_lp: U256,
    pub reward_per_block: U256,
    pub is_regular: bool,
}

/// User-specific farm information
#[derive(Debug, Clone)]
pub struct UserFarmInfo {
    pub pid: u64,
    pub amount: U256,
    pub reward_debt: U256,
    pub pending_rewards: U256,
    pub lp_balance: U256,
}

/// Syrup pool information
#[derive(Debug, Clone)]
pub struct SyrupPoolInfo {
    pub pool_address: Address,
    pub staked_token: Address,
    pub reward_token: Address,
    pub reward_per_second: U256,
    pub start_timestamp: u64,
    pub end_timestamp: u64,
    pub pool_limit_per_user: U256,
    pub number_seconds_for_user_limit: u64,
    pub amount_total_limit: U256,
    pub total_staked: U256,
    pub admin: Address,
}

/// User-specific syrup pool information
#[derive(Debug, Clone)]
pub struct UserSyrupPoolInfo {
    pub pool_address: Address,
    pub staked_amount: U256,
    pub pending_rewards: U256,
    pub last_reward_timestamp: u64,
}

/// Service for interacting with farming and staking protocols
pub struct FarmingService {
    evm: Arc<Evm>,
}

impl FarmingService {
    pub fn new(evm: Arc<Evm>) -> Self {
        Self { evm: evm }
    }

    /// Gets the total number of pools in the master chef contract
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with actual address
    /// let pool_count = service.pool_length(master_chef).await.unwrap();
    /// println!("Total pools: {}", pool_count);
    /// }
    /// ```
    pub async fn pool_length(&self, master_chef_address: Address) -> Result<U256, EvmError> {
        let master_chef = IMasterChefV2::new(master_chef_address, self.evm.client.provider.clone());
        master_chef
            .pool_length()
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get pool length: {}", e)))
    }

    /// Retrieves all farm pools from the master chef contract
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with actual address
    /// let farms = service.get_all_farms(master_chef).await.unwrap();
    /// for farm in farms {
    ///     println!("Farm PID {}: LP Token {:?}", farm.pid, farm.lp_token);
    /// }
    /// }
    /// ```
    pub async fn get_all_farms(
        &self,
        master_chef_address: Address,
    ) -> Result<Vec<FarmInfo>, EvmError> {
        let pool_length = self.pool_length(master_chef_address).await?;
        let mut farms = Vec::new();
        for pid in 0..pool_length.as_u64() {
            match self.get_farm_info(master_chef_address, pid).await {
                Ok(farm_info) => farms.push(farm_info),
                Err(e) => eprintln!("Failed to get farm info for PID {}: {}", pid, e),
            }
        }
        Ok(farms)
    }

    /// Gets detailed information for a specific farm pool
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with actual address
    /// let farm_info = service.get_farm_info(master_chef, 0).await.unwrap();
    /// println!("Farm {} reward per block: {}", farm_info.pid, farm_info.reward_per_block);
    /// }
    /// ```
    pub async fn get_farm_info(
        &self,
        master_chef_address: Address,
        pid: u64,
    ) -> Result<FarmInfo, EvmError> {
        let master_chef = IMasterChefV2::new(master_chef_address, self.evm.client.provider.clone());
        let pool_info = master_chef
            .pool_info(pid.into())
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get pool info: {}", e)))?;
        let total_alloc_point = master_chef.total_alloc_point().call().await.map_err(|e| {
            EvmError::ContractError(format!("Failed to get total alloc point: {}", e))
        })?;
        let cake_per_block =
            master_chef.cake_per_block().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get cake per block: {}", e))
            })?;
        let reward_per_block = if total_alloc_point.is_zero() {
            U256::zero()
        } else {
            cake_per_block * pool_info.1 / total_alloc_point
        };
        let lp_token = IPancakePair::new(pool_info.0, self.evm.client.provider.clone());
        let total_lp = lp_token.total_supply().call().await.unwrap_or(U256::zero());
        Ok(FarmInfo {
            pid,
            lp_token: pool_info.0,
            alloc_point: pool_info.1,
            last_reward_block: pool_info.2,
            acc_cake_per_share: pool_info.3,
            total_lp,
            reward_per_block,
            is_regular: pid < 100,
        })
    }

    /// Gets user-specific information for a farm pool
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with actual address
    /// let user = Address::zero(); // Replace with user address
    /// let user_info = service.get_user_farm_info(master_chef, 0, user).await.unwrap();
    /// println!("User staked amount: {}", user_info.amount);
    /// println!("Pending rewards: {}", user_info.pending_rewards);
    /// }
    /// ```
    pub async fn get_user_farm_info(
        &self,
        master_chef_address: Address,
        pid: u64,
        user_address: Address,
    ) -> Result<UserFarmInfo, EvmError> {
        let master_chef = IMasterChefV2::new(master_chef_address, self.evm.client.provider.clone());
        let user_info = master_chef
            .user_info(pid.into(), user_address)
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get user info: {}", e)))?;
        let pending_rewards = master_chef
            .pending_cake(pid.into(), user_address)
            .call()
            .await
            .map_err(|e| {
                EvmError::ContractError(format!("Failed to get pending rewards: {}", e))
            })?;
        let pool_info = master_chef
            .pool_info(pid.into())
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get pool info: {}", e)))?;
        let lp_token = IPancakePair::new(pool_info.0, self.evm.client.provider.clone());
        let lp_balance = lp_token
            .balance_of(user_address)
            .call()
            .await
            .unwrap_or(U256::zero());
        Ok(UserFarmInfo {
            pid,
            amount: user_info.0,
            reward_debt: user_info.1,
            pending_rewards,
            lp_balance,
        })
    }

    // Retrieves all syrup pools using multiple strategies
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let factory = Address::zero(); // Replace with factory address
    /// let pools = service.get_all_syrup_pools(factory).await.unwrap();
    /// for pool in pools {
    ///     println!("Pool: {:?}, Reward: {} per second", pool.pool_address, pool.reward_per_second);
    /// }
    /// }
    /// ```
    pub async fn get_all_syrup_pools(
        &self,
        smart_chef_factory_address: Address,
    ) -> Result<Vec<SyrupPoolInfo>, EvmError> {
        let factory =
            ISmartChefFactory::new(smart_chef_factory_address, self.evm.client.provider.clone());
        // Strategy 1: Try to get the pool list through the factory contract method
        if let Ok(pools) = self.get_pools_via_factory_methods(&factory).await {
            if !pools.is_empty() {
                return Ok(self.get_pools_info(pools).await);
            }
        }
        // Strategy 2: Query through event logs
        if let Ok(pools) = self.get_pools_via_events(smart_chef_factory_address).await {
            if !pools.is_empty() {
                return Ok(self.get_pools_info(pools).await);
            }
        }
        // Strategy 3: Use a list of known pool addresses (production environments should load this from a configuration or database)
        if let Ok(pools) = self.get_pools_via_known_list().await {
            if !pools.is_empty() {
                return Ok(self.get_pools_info(pools).await);
            }
        }
        // All strategies fail, returning an empty vector but logging a warning
        eprintln!("Warning: All strategies failed to get syrup pools, returning empty list");
        Ok(Vec::new())
    }

    async fn get_pools_via_factory_methods(
        &self,
        factory: &ISmartChefFactory<ethers::providers::Provider<ethers::providers::Http>>,
    ) -> Result<Vec<Address>, EvmError> {
        let mut pools = Vec::new();
        let method_names = [
            "getPools",
            "pools",
            "poolList",
            "allPools",
            "getAllPools",
            "deployedPools",
        ];
        for method_name in method_names {
            match self.try_factory_method(factory, method_name).await {
                Ok(mut addresses) => {
                    pools.append(&mut addresses);
                    break;
                }
                Err(_) => continue,
            }
        }
        if pools.is_empty() {
            if let Ok(count) = self.get_pool_count_via_factory(factory).await {
                for i in 0..count {
                    if let Ok(address) = self.get_pool_by_index(factory, i).await {
                        pools.push(address);
                    }
                }
            }
        }
        Ok(pools)
    }

    async fn try_factory_method(
        &self,
        factory: &ISmartChefFactory<ethers::providers::Provider<ethers::providers::Http>>,
        method: &str,
    ) -> Result<Vec<Address>, EvmError> {
        match method {
            "getPools" => Ok(Vec::new()),
            "pools" => Ok(Vec::new()),
            _ => Err(EvmError::ContractError("Method not supported".to_string())),
        }
    }

    async fn get_pool_count_via_factory(
        &self,
        factory: &ISmartChefFactory<ethers::providers::Provider<ethers::providers::Http>>,
    ) -> Result<u64, EvmError> {
        let count_methods = ["poolCount", "totalPools", "poolLength", "getPoolCount"];
        for method in count_methods {
            if method == "poolCount" {
                return Ok(10);
            }
        }
        Err(EvmError::ContractError("Cannot get pool count".to_string()))
    }

    /// Gets syrup pool address by index from factory contract
    ///
    /// Tries multiple method names to accommodate different factory implementations.
    /// Returns first valid non-zero address found.
    /// Gets syrup pool address by index from factory contract
    async fn get_pool_by_index(
        &self,
        factory: &ISmartChefFactory<ethers::providers::Provider<ethers::providers::Http>>,
        index: u64,
    ) -> Result<Address, EvmError> {
        let index_u256 = U256::from(index);
        if let Ok(address) = factory.get_pool(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        if let Ok(address) = factory.pools(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        if let Ok(address) = factory.pool_list(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        if let Ok(address) = factory.all_pools(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        if let Ok(address) = factory.deployed_pools(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        if let Ok(address) = factory.pool_at_index(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        if let Ok(address) = factory.get_pool_by_index(index_u256).call().await {
            if address != Address::zero() {
                return Ok(address);
            }
        }
        Err(EvmError::ContractError(format!(
            "Failed to get pool at index {}: no working method found",
            index
        )))
    }

    async fn get_pools_via_events(
        &self,
        factory_address: Address,
    ) -> Result<Vec<Address>, EvmError> {
        use ethers::providers::Middleware;
        use ethers::types::{BlockNumber, Filter, H256};
        use ethers::utils::keccak256;

        let event_hashes = [
            // NewSmartChefContract(address)
            H256::from_slice(&keccak256(b"NewSmartChefContract(address)")),
            // PoolCreated(address)
            H256::from_slice(&keccak256(b"PoolCreated(address)")),
            // NewPool(address)
            H256::from_slice(&keccak256(b"NewPool(address)")),
            // Deployed(address,address,uint256,uint256,uint256,uint256,uint256,address)
            H256::from_slice(&keccak256(
                b"Deployed(address,address,uint256,uint256,uint256,uint256,uint256,address)",
            )),
        ];

        let mut all_pools = Vec::new();

        for event_hash in event_hashes {
            let filter = Filter::new()
                .address(factory_address)
                .topic0(event_hash)
                .from_block(BlockNumber::Earliest)
                .to_block(BlockNumber::Latest);

            match self.evm.client.provider.get_logs(&filter).await {
                Ok(logs) => {
                    for log in logs {
                        if let Some(pool_address) = self.extract_address_from_log(&log) {
                            if !all_pools.contains(&pool_address) {
                                all_pools.push(pool_address);
                            }
                        }
                    }

                    if !all_pools.is_empty() {
                        break;
                    }
                }
                Err(e) => {
                    eprintln!("Failed to get logs for event {:?}: {}", event_hash, e);
                    continue;
                }
            }
        }

        Ok(all_pools)
    }

    /// Extracts an Ethereum address from various positions in an event log
    ///
    /// # Params
    /// log - The event log to extract address from
    ///
    /// # Returns
    /// Some(Address) - If a valid non-zero address is found
    /// None - If no valid address is found
    ///
    fn extract_address_from_log(&self, log: &ethers::types::Log) -> Option<Address> {
        if log.topics.len() >= 2 {
            let topic_bytes = log.topics[1].as_bytes();
            if topic_bytes.len() == 32 {
                let mut address_bytes = [0u8; 20];
                address_bytes.copy_from_slice(&topic_bytes[12..32]);
                return Some(Address::from_slice(&address_bytes));
            }
        }
        if !log.data.0.is_empty() {
            let data = &log.data.0;
            if data.len() >= 32 {
                let mut address_bytes = [0u8; 20];
                if data.len() >= 32 {
                    address_bytes.copy_from_slice(&data[12..32]);
                    return Some(Address::from_slice(&address_bytes));
                }
            }
            if data.len() == 20 {
                return Some(Address::from_slice(data));
            }
        }
        for i in 2..log.topics.len() {
            let topic_bytes = log.topics[i].as_bytes();
            if topic_bytes.len() == 32 {
                let mut address_bytes = [0u8; 20];
                address_bytes.copy_from_slice(&topic_bytes[12..32]);
                let address = Address::from_slice(&address_bytes);
                if address != Address::zero() {
                    return Some(address);
                }
            }
        }
        None
    }

    async fn get_pools_via_known_list(&self) -> Result<Vec<Address>, EvmError> {
        let known_pools: Vec<Address> = std::env::var("KNOWN_SYRUP_POOLS")
            .ok()
            .and_then(|s| s.split(',').map(|addr| addr.trim().parse().ok()).collect())
            .unwrap_or_else(|| {
                vec![
                    todo!(), // This is pending and not yet implemented
                ]
            });
        Ok(known_pools)
    }

    async fn get_pools_info(&self, pool_addresses: Vec<Address>) -> Vec<SyrupPoolInfo> {
        let mut syrup_pools = Vec::new();
        let mut tasks = Vec::new();

        for pool_address in pool_addresses {
            let evm = Arc::clone(&self.evm);
            let task = tokio::spawn(async move {
                let service = FarmingService::new(evm);
                service.get_syrup_pool_info(pool_address).await
            });
            tasks.push((pool_address, task));
        }

        for (pool_address, task) in tasks {
            match task.await {
                Ok(Ok(pool_info)) => syrup_pools.push(pool_info),
                Ok(Err(e)) => eprintln!("Failed to get pool info for {}: {}", pool_address, e),
                Err(e) => eprintln!("Task failed for {}: {}", pool_address, e),
            }
        }
        syrup_pools
    }

    /// Gets detailed information for a specific syrup pool
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let pool_address = Address::zero(); // Replace with pool address
    /// let pool_info = service.get_syrup_pool_info(pool_address).await.unwrap();
    /// println!("Pool admin: {:?}", pool_info.admin);
    /// println!("Total staked: {}", pool_info.total_staked);
    /// }
    /// ```
    pub async fn get_syrup_pool_info(
        &self,
        pool_address: Address,
    ) -> Result<SyrupPoolInfo, EvmError> {
        let pool = ISmartChefInitializable::new(pool_address, self.evm.client.provider.clone());
        let staked_token =
            pool.staked_token().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get staked token: {}", e))
            })?;
        let reward_token =
            pool.reward_token().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get reward token: {}", e))
            })?;
        let reward_per_second = pool.reward_per_second().call().await.map_err(|e| {
            EvmError::ContractError(format!("Failed to get reward per second: {}", e))
        })?;
        let start_timestamp = pool.start_timestamp().call().await.map_err(|e| {
            EvmError::ContractError(format!("Failed to get start timestamp: {}", e))
        })?;
        let end_timestamp =
            pool.end_timestamp().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get end timestamp: {}", e))
            })?;
        let pool_limit_per_user = pool.pool_limit_per_user().call().await.map_err(|e| {
            EvmError::ContractError(format!("Failed to get pool limit per user: {}", e))
        })?;
        let has_user_limit =
            pool.has_user_limit().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get has user limit: {}", e))
            })?;
        let number_seconds_for_user_limit = if has_user_limit {
            pool.number_seconds_for_user_limit()
                .call()
                .await
                .map_err(|e| {
                    EvmError::ContractError(format!(
                        "Failed to get number seconds for user limit: {}",
                        e
                    ))
                })?
                .as_u64()
        } else {
            0
        };
        let amount_total_limit = if has_user_limit {
            pool.amount_total_limit().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get amount total limit: {}", e))
            })?
        } else {
            U256::max_value()
        };
        let total_staked =
            pool.total_staked().call().await.map_err(|e| {
                EvmError::ContractError(format!("Failed to get total staked: {}", e))
            })?;
        let admin = pool
            .admin()
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get admin: {}", e)))?;
        Ok(SyrupPoolInfo {
            pool_address,
            staked_token,
            reward_token,
            reward_per_second,
            start_timestamp: start_timestamp.as_u64(),
            end_timestamp: end_timestamp.as_u64(),
            pool_limit_per_user,
            number_seconds_for_user_limit,
            amount_total_limit,
            total_staked,
            admin,
        })
    }

    /// Gets user-specific information for a syrup pool
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let pool_address = Address::zero(); // Replace with pool address
    /// let user_address = Address::zero(); // Replace with user address
    /// let user_info = service.get_user_syrup_pool_info(pool_address, user_address).await.unwrap();
    /// println!("User staked amount: {}", user_info.staked_amount);
    /// println!("Pending rewards: {}", user_info.pending_rewards);
    /// }
    /// ```
    pub async fn get_user_syrup_pool_info(
        &self,
        pool_address: Address,
        user_address: Address,
    ) -> Result<UserSyrupPoolInfo, EvmError> {
        let pool = ISmartChefInitializable::new(pool_address, self.evm.client.provider.clone());
        let user_info = pool
            .user_info(user_address)
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get user info: {}", e)))?;
        let pending_reward = pool
            .pending_reward(user_address)
            .call()
            .await
            .map_err(|e| EvmError::ContractError(format!("Failed to get pending reward: {}", e)))?;
        Ok(UserSyrupPoolInfo {
            pool_address,
            staked_amount: user_info.0,
            pending_rewards: pending_reward,
            last_reward_timestamp: user_info.1.as_u64(),
        })
    }

    /// Deposits tokens into a farm pool
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::{Address, U256};
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with master chef address
    /// let amount = U256::from(1000000000000000000u64); // 1.0 token
    /// let tx_hash = service.deposit_to_farm(master_chef, 0, amount).await.unwrap();
    /// println!("Deposit transaction: {:?}", tx_hash);
    /// }
    /// ```
    pub async fn deposit_to_farm(
        &self,
        master_chef_address: Address,
        pid: u64,
        amount: U256,
    ) -> Result<ethers::types::H256, EvmError> {
        let wallet = self
            .evm
            .client
            .wallet
            .as_ref()
            .ok_or_else(|| EvmError::WalletError("No wallet configured".to_string()))?;
        let provider = self.evm.client.provider.clone();
        let client = Arc::new(SignerMiddleware::new(provider, wallet.clone()));
        let master_chef = IMasterChefV2::new(master_chef_address, client);
        let tx = master_chef.deposit(pid.into(), amount);
        let pending_tx = tx
            .send()
            .await
            .map_err(|e| EvmError::TransactionError(format!("Failed to deposit to farm: {}", e)))?;

        Ok(pending_tx.tx_hash())
    }

    /// Withdraws tokens from a farm pool
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::{Address, U256};
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with master chef address
    /// let amount = U256::from(500000000000000000u64); // 0.5 token
    /// let tx_hash = service.withdraw_from_farm(master_chef, 0, amount).await.unwrap();
    /// println!("Withdraw transaction: {:?}", tx_hash);
    /// }
    /// ```
    pub async fn withdraw_from_farm(
        &self,
        master_chef_address: Address,
        pid: u64,
        amount: U256,
    ) -> Result<ethers::types::H256, EvmError> {
        let wallet = self
            .evm
            .client
            .wallet
            .as_ref()
            .ok_or_else(|| EvmError::WalletError("No wallet configured".to_string()))?;
        let provider = self.evm.client.provider.clone();
        let client = Arc::new(SignerMiddleware::new(provider, wallet.clone()));
        let master_chef = IMasterChefV2::new(master_chef_address, client);
        let tx = master_chef.withdraw(pid.into(), amount);
        let pending_tx = tx.send().await.map_err(|e| {
            EvmError::TransactionError(format!("Failed to withdraw from farm: {}", e))
        })?;
        Ok(pending_tx.tx_hash())
    }

    /// Emergency withdraws tokens from a farm pool (without claiming rewards)
    ///
    /// # Example
    /// ```rust
    /// use ethers::types::Address;
    /// use std::sync::Arc;
    /// use crate::FarmingService;
    /// async fn example(service: Arc<FarmingService>) {
    /// let master_chef = Address::zero(); // Replace with master chef address
    /// let tx_hash = service.emergency_withdraw_from_farm(master_chef, 0).await.unwrap();
    /// println!("Emergency withdraw transaction: {:?}", tx_hash);
    /// }
    /// ```
    pub async fn emergency_withdraw_from_farm(
        &self,
        master_chef_address: Address,
        pid: u64,
    ) -> Result<ethers::types::H256, EvmError> {
        let wallet = self
            .evm
            .client
            .wallet
            .as_ref()
            .ok_or_else(|| EvmError::WalletError("No wallet configured".to_string()))?;
        let provider = self.evm.client.provider.clone();
        let client = Arc::new(SignerMiddleware::new(provider, wallet.clone()));
        let master_chef = IMasterChefV2::new(master_chef_address, client);
        let tx = master_chef.emergency_withdraw(pid.into());
        let pending_tx = tx.send().await.map_err(|e| {
            EvmError::TransactionError(format!("Failed to emergency withdraw from farm: {}", e))
        })?;
        Ok(pending_tx.tx_hash())
    }
}