nautilus_blockchain/exchanges/arbitrum/
camelot_v3.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::sync::LazyLock;
17
18use alloy::primitives::Address;
19use nautilus_model::defi::{
20    PoolIdentifier,
21    chain::chains,
22    dex::{AmmType, Dex, DexType},
23};
24use ustr::Ustr;
25
26use crate::{
27    events::pool_created::PoolCreatedEvent,
28    exchanges::extended::DexExtended,
29    hypersync::{
30        HypersyncLog,
31        helpers::{
32            extract_address_from_topic, extract_block_number, validate_event_signature_hash,
33        },
34    },
35};
36
37const POOL_CREATED_EVENT_SIGNATURE_HASH: &str =
38    "91ccaa7a278130b65168c3a0c8d3bcae84cf5e43704342bd3ec0b59e59c036db";
39
40/// Camelot V3 DEX on Arbitrum.
41pub static CAMELOT_V3: LazyLock<DexExtended> = LazyLock::new(|| {
42    let mut dex = DexExtended::new(Dex::new(
43        chains::ARBITRUM.clone(),
44        DexType::CamelotV3,
45        "0x1a3c9B1d2F0529D97f2afC5136Cc23e58f1FD35B",
46        102286676,
47        AmmType::CLAMM,
48        "Pool(address,address,address)",
49        "",
50        "",
51        "",
52        "",
53    ));
54    dex.set_pool_created_event_hypersync_parsing(parse_camelot_v3_pool_created_event_hypersync);
55    dex
56});
57
58fn parse_camelot_v3_pool_created_event_hypersync(
59    log: HypersyncLog,
60) -> anyhow::Result<PoolCreatedEvent> {
61    validate_event_signature_hash("Pool", POOL_CREATED_EVENT_SIGNATURE_HASH, &log)?;
62
63    let block_number = extract_block_number(&log)?;
64    let token = extract_address_from_topic(&log, 1, "token0")?;
65    let token1 = extract_address_from_topic(&log, 2, "token1")?;
66
67    if let Some(data) = log.data {
68        let data_bytes = data.as_ref();
69
70        // Extract pool address (only 32 bytes)
71        let pool_address = Address::from_slice(&data_bytes[12..32]);
72        let pool_identifier = PoolIdentifier::Address(Ustr::from(&pool_address.to_string()));
73        Ok(PoolCreatedEvent::new(
74            block_number,
75            token,
76            token1,
77            pool_address,
78            pool_identifier,
79            None,
80            None,
81        ))
82    } else {
83        anyhow::bail!("Missing data in the pool created event log")
84    }
85}