use crate::{
domain::evm::{Address, ChainId},
error::{Error, ValidationError},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct V3DeploymentData {
pub chain_id: ChainId,
pub factory: Address,
pub quoter_v2: Address,
pub universal_router_2_1_2: Address,
pub permit2: Address,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "V3DeploymentData", into = "V3DeploymentData")]
pub struct V3Deployment(V3DeploymentData);
impl V3Deployment {
pub fn new(data: V3DeploymentData) -> Result<Self, Error> {
let addresses = [
data.factory,
data.quoter_v2,
data.universal_router_2_1_2,
data.permit2,
];
for (index, address) in addresses.iter().enumerate() {
if *address == Address::from_bytes([0; 20]) || addresses[..index].contains(address) {
return Err(ValidationError::InvalidUniswapDeployment.into());
}
}
Ok(Self(data))
}
#[must_use]
pub const fn data(&self) -> &V3DeploymentData {
&self.0
}
#[must_use]
pub const fn chain_id(&self) -> ChainId {
self.0.chain_id
}
pub(super) const fn chain_id_ref(&self) -> &ChainId {
&self.0.chain_id
}
pub fn ethereum_mainnet() -> Result<Self, Error> {
Self::new(V3DeploymentData {
chain_id: ChainId::from(1),
factory: Address::parse("0x1f98431c8ad98523631ae4a59f267346ea31f984")?,
quoter_v2: Address::parse("0x61ffe014ba17989e743c5f6cb21bf9697530b21e")?,
universal_router_2_1_2: Address::parse("0x23617e59a5925b2a4bf75d73ff6711cd0b29de85")?,
permit2: Address::parse("0x000000000022d473030f116ddee9f6b43ac78ba3")?,
})
}
}
impl TryFrom<V3DeploymentData> for V3Deployment {
type Error = Error;
fn try_from(data: V3DeploymentData) -> Result<Self, Error> {
Self::new(data)
}
}
impl From<V3Deployment> for V3DeploymentData {
fn from(value: V3Deployment) -> Self {
value.0
}
}