Skip to main content

evm_dex_pool/erc4626/
verio_ip.rs

1use crate::contracts::IERC4626;
2use crate::erc4626::{ERC4626Pool, ERC4626Standard};
3use crate::pool::base::{EventApplicable, PoolInterface, PoolType, PoolTypeTrait, TopicList};
4use alloy::sol_types::SolEvent;
5use alloy::{
6    primitives::{Address, FixedBytes, U256},
7    rpc::types::Log,
8};
9use anyhow::{anyhow, Result};
10use serde::{Deserialize, Serialize};
11use std::any::Any;
12use std::fmt;
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct VerioIP {
16    base: ERC4626Standard,
17}
18
19impl VerioIP {
20    pub fn new(
21        address: Address,
22        vault_token: Address,
23        asset_token: Address,
24        vault_reserve: U256,
25        asset_reserve: U256,
26        deposit_fee: u32,
27        withdraw_fee: u32,
28    ) -> Self {
29        Self {
30            base: ERC4626Standard::new(
31                address,
32                vault_token,
33                asset_token,
34                vault_reserve,
35                asset_reserve,
36                deposit_fee,
37                withdraw_fee,
38            ),
39        }
40    }
41}
42
43impl PoolInterface for VerioIP {
44    fn address(&self) -> Address {
45        self.base.address
46    }
47
48    fn calculate_output(&self, token_in: &Address, amount_in: U256) -> Result<U256> {
49        self.base.calculate_output(token_in, amount_in)
50    }
51
52    fn calculate_input(&self, token_out: &Address, amount_out: U256) -> Result<U256> {
53        self.base.calculate_input(token_out, amount_out)
54    }
55
56    fn apply_swap(
57        &mut self,
58        _token_in: &Address,
59        _amount_in: U256,
60        _amount_out: U256,
61    ) -> Result<()> {
62        Err(anyhow!("Not implemented"))
63    }
64
65    fn tokens(&self) -> (Address, Address) {
66        self.base.tokens()
67    }
68
69    fn fee(&self) -> f64 {
70        self.base.fee()
71    }
72
73    fn fee_raw(&self) -> u64 {
74        self.base.fee_raw()
75    }
76
77    fn id(&self) -> String {
78        self.base.id()
79    }
80
81    fn contains_token(&self, token: &Address) -> bool {
82        self.base.contains_token(token)
83    }
84
85    fn clone_box(&self) -> Box<dyn PoolInterface + Send + Sync> {
86        Box::new(self.clone())
87    }
88
89    fn log_summary(&self) -> String {
90        self.base.log_summary()
91    }
92
93    fn as_any(&self) -> &dyn Any {
94        self
95    }
96
97    fn as_any_mut(&mut self) -> &mut dyn Any {
98        self
99    }
100}
101
102impl PoolTypeTrait for VerioIP {
103    fn pool_type(&self) -> PoolType {
104        PoolType::ERC4626(ERC4626Pool::VerioIP)
105    }
106}
107
108impl EventApplicable for VerioIP {
109    fn apply_log(&mut self, log: &Log) -> Result<()> {
110        match log.topic0() {
111            Some(&IERC4626::Deposit::SIGNATURE_HASH) => {
112                let deposit_data: IERC4626::Deposit = log.log_decode()?.inner.data;
113                self.base.vault_reserve += deposit_data.shares;
114                self.base.asset_reserve += deposit_data.assets;
115            }
116            Some(&IERC4626::Withdraw::SIGNATURE_HASH) => {
117                let withdraw_data: IERC4626::Withdraw = log.log_decode()?.inner.data;
118                self.base.vault_reserve -= withdraw_data.shares;
119                self.base.asset_reserve -= withdraw_data.assets;
120            }
121            _ => return Ok(()),
122        }
123        Ok(())
124    }
125}
126
127impl TopicList for VerioIP {
128    fn topics() -> Vec<FixedBytes<32>> {
129        vec![
130            IERC4626::Deposit::SIGNATURE_HASH,
131            IERC4626::Withdraw::SIGNATURE_HASH,
132        ]
133    }
134
135    fn profitable_topics() -> Vec<FixedBytes<32>> {
136        vec![
137            IERC4626::Deposit::SIGNATURE_HASH,
138            IERC4626::Withdraw::SIGNATURE_HASH,
139        ]
140    }
141}
142
143impl fmt::Display for VerioIP {
144    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145        write!(
146            f,
147            "Verio IP Pool {} - {} (reserves: {}, {})",
148            self.base.vault_token,
149            self.base.asset_token,
150            self.base.vault_reserve,
151            self.base.asset_reserve
152        )
153    }
154}