Skip to main content

nautilus_blockchain/exchanges/parsing/uniswap_v3/
fee_protocol_update.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 alloy::{dyn_abi::SolType, primitives::Address, sol};
17use nautilus_model::defi::{PoolIdentifier, SharedDex, rpc::RpcLog};
18use ustr::Ustr;
19
20use crate::{
21    events::fee_protocol_update::FeeProtocolUpdateEvent,
22    hypersync::{
23        HypersyncLog,
24        log::{
25            extract_block_hash, extract_block_number, extract_log_index, extract_transaction_hash,
26            extract_transaction_index, validate_event_signature_hash,
27        },
28    },
29    rpc::log as rpc_log,
30};
31
32const FEE_PROTOCOL_UPDATE_EVENT_SIGNATURE_HASH: &str =
33    "973d8d92bb299f4af6ce49b52a8adb85ae46b9f214c4c4fc06ac77401237b133";
34
35// Define sol macro for easier parsing of SetFeeProtocol event data.
36// All four parameters are non-indexed and live in the log data:
37// feeProtocol0Old (uint8), feeProtocol1Old (uint8), feeProtocol0New (uint8), feeProtocol1New (uint8)
38sol! {
39    struct SetFeeProtocolEventData {
40        uint8 fee_protocol0_old;
41        uint8 fee_protocol1_old;
42        uint8 fee_protocol0_new;
43        uint8 fee_protocol1_new;
44    }
45}
46
47/// Parses a `SetFeeProtocol` event from a Uniswap V3 HyperSync log.
48///
49/// # Errors
50///
51/// Returns an error if the log parsing fails or if the event data is invalid.
52///
53/// # Panics
54///
55/// Panics if the contract address is not set in the log.
56pub fn parse_fee_protocol_update_event_hypersync(
57    dex: SharedDex,
58    log: &HypersyncLog,
59) -> anyhow::Result<FeeProtocolUpdateEvent> {
60    validate_event_signature_hash(
61        "SetFeeProtocolEvent",
62        FEE_PROTOCOL_UPDATE_EVENT_SIGNATURE_HASH,
63        log,
64    )?;
65
66    if let Some(data) = &log.data {
67        let data_bytes = data.as_ref();
68
69        // Validate the data contains 4 parameters of 32 bytes each
70        if data_bytes.len() < 4 * 32 {
71            anyhow::bail!("SetFeeProtocol event data is too short");
72        }
73
74        let decoded = match <SetFeeProtocolEventData as SolType>::abi_decode(data_bytes) {
75            Ok(decoded) => decoded,
76            Err(e) => anyhow::bail!("Failed to decode SetFeeProtocol event data: {e}"),
77        };
78
79        let pool_address = Address::from_slice(
80            log.address
81                .clone()
82                .expect("Contract address should be set in logs")
83                .as_ref(),
84        );
85        let pool_identifier = PoolIdentifier::Address(Ustr::from(&pool_address.to_string()));
86
87        Ok(FeeProtocolUpdateEvent::new(
88            dex,
89            pool_identifier,
90            extract_block_number(log)?,
91            extract_block_hash(log),
92            extract_transaction_hash(log)?,
93            extract_transaction_index(log)?,
94            extract_log_index(log)?,
95            u32::from(decoded.fee_protocol0_new),
96            u32::from(decoded.fee_protocol1_new),
97        ))
98    } else {
99        anyhow::bail!("Missing data in SetFeeProtocol event log");
100    }
101}
102
103/// Parses a `SetFeeProtocol` event from an RPC log.
104///
105/// # Errors
106///
107/// Returns an error if the log parsing fails or if the event data is invalid.
108pub fn parse_fee_protocol_update_event_rpc(
109    dex: SharedDex,
110    log: &RpcLog,
111) -> anyhow::Result<FeeProtocolUpdateEvent> {
112    rpc_log::validate_event_signature(
113        log,
114        FEE_PROTOCOL_UPDATE_EVENT_SIGNATURE_HASH,
115        "SetFeeProtocol",
116    )?;
117
118    let data_bytes = rpc_log::extract_data_bytes(log)?;
119
120    // Validate the data contains 4 parameters of 32 bytes each
121    if data_bytes.len() < 4 * 32 {
122        anyhow::bail!("SetFeeProtocol event data is too short");
123    }
124
125    let decoded = match <SetFeeProtocolEventData as SolType>::abi_decode(&data_bytes) {
126        Ok(decoded) => decoded,
127        Err(e) => anyhow::bail!("Failed to decode SetFeeProtocol event data: {e}"),
128    };
129
130    let pool_address = rpc_log::extract_address(log)?;
131    let pool_identifier = PoolIdentifier::Address(Ustr::from(&pool_address.to_string()));
132    Ok(FeeProtocolUpdateEvent::new(
133        dex,
134        pool_identifier,
135        rpc_log::extract_block_number(log)?,
136        rpc_log::extract_block_hash(log)?,
137        rpc_log::extract_transaction_hash(log)?,
138        rpc_log::extract_transaction_index(log)?,
139        rpc_log::extract_log_index(log)?,
140        u32::from(decoded.fee_protocol0_new),
141        u32::from(decoded.fee_protocol1_new),
142    ))
143}
144
145#[cfg(test)]
146mod tests {
147    use std::{str::FromStr, sync::Arc};
148
149    use alloy::primitives::B256;
150    use rstest::*;
151
152    use super::*;
153    use crate::exchanges::arbitrum;
154
155    /// Real Arbitrum on-chain `SetFeeProtocol` log at block 3,106,049 (Uniswap V3 event ABI).
156    /// Pool: 0x0d500e0f1d159e75f3771fb5e6ab86de19a8abd4
157    /// new protocol fees: feeProtocol0=6, feeProtocol1=6
158    const HYPERSYNC_LOG: &str =
159        include_str!("../../../../test_data/uniswap_v3_set_fee_protocol_hypersync.json");
160    const RPC_LOG: &str =
161        include_str!("../../../../test_data/uniswap_v3_set_fee_protocol_rpc.json");
162
163    #[fixture]
164    fn hypersync_log() -> HypersyncLog {
165        serde_json::from_str(HYPERSYNC_LOG).expect("Failed to deserialize HyperSync log")
166    }
167
168    #[fixture]
169    fn rpc_log() -> RpcLog {
170        serde_json::from_str(RPC_LOG).expect("Failed to deserialize RPC log")
171    }
172
173    #[rstest]
174    fn test_parse_fee_protocol_update_event_hypersync(hypersync_log: HypersyncLog) {
175        let dex = Arc::clone(&arbitrum::UNISWAP_V3.dex);
176        let event = parse_fee_protocol_update_event_hypersync(dex, &hypersync_log).unwrap();
177
178        assert_eq!(event.block_hash, None);
179
180        assert_eq!(
181            event.pool_identifier.to_string(),
182            "0x0d500E0f1d159E75f3771Fb5e6aB86DE19A8abD4"
183        );
184        assert_eq!(event.fee_protocol0_new, 6);
185        assert_eq!(event.fee_protocol1_new, 6);
186        assert_eq!(event.block_number, 3_106_049);
187        assert_eq!(event.transaction_index, 0);
188        assert_eq!(event.log_index, 0);
189    }
190
191    #[rstest]
192    fn test_parse_fee_protocol_update_event_rpc(rpc_log: RpcLog) {
193        let dex = Arc::clone(&arbitrum::UNISWAP_V3.dex);
194        let event = parse_fee_protocol_update_event_rpc(dex, &rpc_log).unwrap();
195
196        let expected =
197            B256::from_str("0x9637a84b493b07d3bc7f452169d8a1c79ba096ba3d24e41c8aa24d349e1526ed")
198                .unwrap();
199        assert_eq!(event.block_hash, Some(expected));
200
201        assert_eq!(
202            event.pool_identifier.to_string(),
203            "0x0d500E0f1d159E75f3771Fb5e6aB86DE19A8abD4"
204        );
205        assert_eq!(event.fee_protocol0_new, 6);
206        assert_eq!(event.fee_protocol1_new, 6);
207        assert_eq!(event.block_number, 3_106_049);
208    }
209
210    #[rstest]
211    fn test_hypersync_rpc_match(hypersync_log: HypersyncLog, rpc_log: RpcLog) {
212        let dex = Arc::clone(&arbitrum::UNISWAP_V3.dex);
213        let event_hypersync =
214            parse_fee_protocol_update_event_hypersync(Arc::clone(&dex), &hypersync_log).unwrap();
215        let event_rpc = parse_fee_protocol_update_event_rpc(dex, &rpc_log).unwrap();
216
217        assert_eq!(event_hypersync.pool_identifier, event_rpc.pool_identifier);
218        assert_eq!(
219            event_hypersync.fee_protocol0_new,
220            event_rpc.fee_protocol0_new
221        );
222        assert_eq!(
223            event_hypersync.fee_protocol1_new,
224            event_rpc.fee_protocol1_new
225        );
226        assert_eq!(event_hypersync.block_number, event_rpc.block_number);
227        assert_eq!(event_hypersync.transaction_hash, event_rpc.transaction_hash);
228        assert_eq!(
229            event_hypersync.transaction_index,
230            event_rpc.transaction_index
231        );
232        assert_eq!(event_hypersync.log_index, event_rpc.log_index);
233    }
234}