Skip to main content

nautilus_blockchain/exchanges/arbitrum/
camelot_v3.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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        log::{extract_address_from_topic, extract_block_number, validate_event_signature_hash},
32    },
33};
34
35const POOL_CREATED_EVENT_SIGNATURE_HASH: &str =
36    "91ccaa7a278130b65168c3a0c8d3bcae84cf5e43704342bd3ec0b59e59c036db";
37
38/// Camelot V3 DEX on Arbitrum.
39pub static CAMELOT_V3: LazyLock<DexExtended> = LazyLock::new(|| {
40    let mut dex = DexExtended::new(Dex::new(
41        chains::ARBITRUM.clone(),
42        DexType::CamelotV3,
43        "0x1a3c9B1d2F0529D97f2afC5136Cc23e58f1FD35B",
44        102286676,
45        AmmType::CLAMM,
46        "Pool(address,address,address)",
47        "",
48        "",
49        "",
50        "",
51    ));
52    dex.set_pool_created_event_hypersync_parsing(parse_camelot_v3_pool_created_event_hypersync);
53    dex
54});
55
56fn parse_camelot_v3_pool_created_event_hypersync(
57    log: HypersyncLog,
58) -> anyhow::Result<PoolCreatedEvent> {
59    validate_event_signature_hash("Pool", POOL_CREATED_EVENT_SIGNATURE_HASH, &log)?;
60
61    let block_number = extract_block_number(&log)?;
62    let token = extract_address_from_topic(&log, 1, "token0")?;
63    let token1 = extract_address_from_topic(&log, 2, "token1")?;
64
65    if let Some(data) = log.data {
66        let data_bytes = data.as_ref();
67        anyhow::ensure!(
68            data_bytes.len() >= 32,
69            "Pool created event data too short: expected at least 32 bytes, was {}",
70            data_bytes.len()
71        );
72
73        // Pool address is right-aligned in the first 32-byte word
74        let pool_address = Address::from_slice(&data_bytes[12..32]);
75        let pool_identifier = PoolIdentifier::Address(Ustr::from(&pool_address.to_string()));
76        Ok(PoolCreatedEvent::new(
77            block_number,
78            token,
79            token1,
80            pool_address,
81            pool_identifier,
82            None,
83            None,
84        ))
85    } else {
86        anyhow::bail!("Missing data in the pool created event log")
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use rstest::rstest;
93    use serde_json::json;
94
95    use super::*;
96
97    fn camelot_log(data: &str) -> HypersyncLog {
98        let log_json = json!({
99            "block_number": "0x1",
100            "address": "0x1a3c9b1d2f0529d97f2afc5136cc23e58f1fd35b",
101            "data": data,
102            "topics": [
103                "0x91ccaa7a278130b65168c3a0c8d3bcae84cf5e43704342bd3ec0b59e59c036db",
104                "0x00000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1",
105                "0x000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e5831"
106            ]
107        });
108        serde_json::from_value(log_json).expect("Failed to deserialize HyperSync log")
109    }
110
111    #[rstest]
112    fn test_parse_pool_created_hypersync_rejects_short_data() {
113        let valid_log =
114            camelot_log("0x000000000000000000000000f64dfe17c8b87f012fcf50fbda1d62bfa148366a");
115        let short_log =
116            camelot_log("0x000000000000000000000000f64dfe17c8b87f012fcf50fbda1d62bfa14836");
117
118        let error = parse_camelot_v3_pool_created_event_hypersync(short_log).unwrap_err();
119        let event = parse_camelot_v3_pool_created_event_hypersync(valid_log).unwrap();
120
121        assert_eq!(
122            error.to_string(),
123            "Pool created event data too short: expected at least 32 bytes, was 31"
124        );
125        assert_eq!(event.block_number, 1);
126        assert_eq!(
127            event.pool_identifier.to_string().to_lowercase(),
128            "0xf64dfe17c8b87f012fcf50fbda1d62bfa148366a"
129        );
130    }
131}