Skip to main content

nautilus_derive/common/
consts.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
16//! Static constants for the Derive adapter.
17//!
18//! Protocol constants (`DOMAIN_SEPARATOR`, `ACTION_TYPEHASH`, per-action module
19//! addresses) are sourced from the "Protocol Constants" reference at
20//! <https://docs.derive.xyz/reference/protocol-constants>. Both mainnet and
21//! testnet values are populated; per-instance overrides on
22//! [`crate::config::DeriveExecClientConfig`] take precedence.
23
24use std::{sync::LazyLock, time::Duration};
25
26use nautilus_model::identifiers::{ClientId, Venue};
27use ustr::Ustr;
28
29use crate::common::enums::DeriveEnvironment;
30
31/// Venue identifier string.
32pub const DERIVE: &str = "DERIVE";
33
34/// Static venue instance.
35pub static DERIVE_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(Ustr::from(DERIVE)));
36
37/// Static client ID instance.
38pub static DERIVE_CLIENT_ID: LazyLock<ClientId> =
39    LazyLock::new(|| ClientId::new(Ustr::from(DERIVE)));
40
41/// Referral code for order attribution (zero additional fees),
42/// see <https://docs.derive.xyz/reference/api-broker> for further details.
43pub const DERIVE_NAUTILUS_REFERRAL_CODE: &str = "nautilus";
44
45pub const REST_URL_MAINNET: &str = "https://api.lyra.finance";
46pub const REST_URL_TESTNET: &str = "https://api-demo.lyra.finance";
47pub const WS_URL_MAINNET: &str = "wss://api.lyra.finance/ws";
48pub const WS_URL_TESTNET: &str = "wss://api-demo.lyra.finance/ws";
49
50pub const DERIVE_TRADES_PAGE_SIZE: u32 = 1000;
51pub const DERIVE_CANDLES_DEFAULT_LIMIT: usize = 1000;
52pub const DERIVE_CANDLES_MAX_PAGES: usize = 100;
53
54/// EIP-712 domain separator (mainnet).
55pub const DOMAIN_SEPARATOR_MAINNET: &str =
56    "0xd96e5f90797da7ec8dc4e276260c7f3f87fedf68775fbe1ef116e996fc60441b";
57
58/// EIP-712 domain separator (testnet).
59pub const DOMAIN_SEPARATOR_TESTNET: &str =
60    "0x9bcf4dc06df5d8bf23af818d5716491b995020f377d3b7b64c29ed14e3dd1105";
61
62/// EIP-712 action typehash. Identical across networks per Derive's published
63/// Protocol Constants.
64pub const ACTION_TYPEHASH: &str =
65    "0x4d7a9f27c403ff9c0f19bce61d76d82f9aa29f8d6d4b0c5474607d9770d1af17";
66
67/// Trade module contract address (mainnet).
68pub const TRADE_MODULE_ADDRESS_MAINNET: &str = "0xB8D20c2B7a1Ad2EE33Bc50eF10876eD3035b5e7b";
69
70/// Trade module contract address (testnet).
71pub const TRADE_MODULE_ADDRESS_TESTNET: &str = "0x87F2863866D85E3192a35A73b388BD625D83f2be";
72
73/// Withdrawal module contract address (mainnet).
74pub const WITHDRAW_MODULE_ADDRESS_MAINNET: &str = "0x9d0E8f5b25384C7310CB8C6aE32C8fbeb645d083";
75
76/// Withdrawal module contract address (testnet).
77pub const WITHDRAW_MODULE_ADDRESS_TESTNET: &str = "0xe850641C5207dc5E9423fB15f89ae6031A05fd92";
78
79/// Transfer module contract address (mainnet).
80pub const TRANSFER_MODULE_ADDRESS_MAINNET: &str = "0x01259207A40925b794C8ac320456F7F6c8FE2636";
81
82/// Transfer module contract address (testnet).
83pub const TRANSFER_MODULE_ADDRESS_TESTNET: &str = "0x0CFC1a4a90741aB242cAfaCD798b409E12e68926";
84
85/// Deposit module contract address (mainnet).
86pub const DEPOSIT_MODULE_ADDRESS_MAINNET: &str = "0x9B3FE5E5a3bcEa5df4E08c41Ce89C4e3Ff01Ace3";
87
88/// Deposit module contract address (testnet).
89pub const DEPOSIT_MODULE_ADDRESS_TESTNET: &str = "0x43223Db33AdA0575D2E100829543f8B04A37a1ec";
90
91/// Returns the EIP-712 domain separator constant for the configured environment.
92#[must_use]
93pub const fn domain_separator_for(environment: DeriveEnvironment) -> &'static str {
94    match environment {
95        DeriveEnvironment::Mainnet => DOMAIN_SEPARATOR_MAINNET,
96        DeriveEnvironment::Testnet => DOMAIN_SEPARATOR_TESTNET,
97    }
98}
99
100/// Returns the Trade module contract address constant for the configured environment.
101#[must_use]
102pub const fn trade_module_address_for(environment: DeriveEnvironment) -> &'static str {
103    match environment {
104        DeriveEnvironment::Mainnet => TRADE_MODULE_ADDRESS_MAINNET,
105        DeriveEnvironment::Testnet => TRADE_MODULE_ADDRESS_TESTNET,
106    }
107}
108
109/// REST authentication header carrying the Derive Chain smart-contract wallet.
110pub const HEADER_LYRA_WALLET: &str = "X-LYRAWALLET";
111
112/// REST authentication header carrying the signed timestamp.
113pub const HEADER_LYRA_TIMESTAMP: &str = "X-LYRATIMESTAMP";
114
115/// REST authentication header carrying the session-key signature.
116pub const HEADER_LYRA_SIGNATURE: &str = "X-LYRASIGNATURE";
117
118/// Minimum signature TTL the venue accepts for self-custodial actions.
119///
120/// Per the v2 onboarding docs, `signature_expiry_sec` must be at least five
121/// minutes in the future of `now`.
122pub const MIN_SIGNATURE_TTL: Duration = Duration::from_secs(5 * 60);
123
124/// Signature TTL used for Derive trigger orders.
125///
126/// Mainnet rejects trigger orders unless `signature_expiry_sec` is 30 to 90
127/// days from venue time. The one-day buffer avoids edge failures from local
128/// clock drift and request latency.
129pub const TRIGGER_ORDER_SIGNATURE_TTL: Duration = Duration::from_secs(31 * 24 * 60 * 60);
130
131/// Fixed-point scale used by all on-chain decimal fields (1e18).
132pub const DECIMAL_SCALE: u128 = 1_000_000_000_000_000_000;
133
134pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
135
136/// Default timeout for blocking on account registration during execution
137/// client `connect()`. Matches the BitMEX / Bybit / OKX adapters' 30-second
138/// budget.
139pub const DERIVE_ACCOUNT_REGISTRATION_TIMEOUT_SECS: f64 = 30.0;
140
141pub const WS_HEARTBEAT_SECS: u64 = 30;
142
143pub const RECONNECT_BASE_BACKOFF: Duration = Duration::from_millis(250);
144pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
145pub const RECONNECT_JITTER_MS: u64 = 200;
146pub const RECONNECT_BACKOFF_FACTOR: f64 = 2.0;
147pub const RECONNECT_TIMEOUT: Duration = Duration::from_secs(15);
148
149pub const WS_DISCONNECT_TIMEOUT: Duration = Duration::from_secs(5);
150
151/// Maximum wait for a JSON-RPC response on the WebSocket transport before the
152/// request outcome is treated as unknown, so a silent venue cannot hang
153/// `connect()` or an order write. Matches [`HTTP_TIMEOUT`].
154pub const WS_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
155
156#[cfg(test)]
157mod tests {
158    use rstest::rstest;
159
160    use super::*;
161
162    #[rstest]
163    fn test_venue_constant() {
164        assert_eq!(DERIVE_VENUE.as_str(), DERIVE);
165    }
166
167    #[rstest]
168    fn test_url_constants_have_expected_schemes() {
169        assert!(REST_URL_MAINNET.starts_with("https://"));
170        assert!(REST_URL_TESTNET.starts_with("https://"));
171        assert!(WS_URL_MAINNET.starts_with("wss://"));
172        assert!(WS_URL_TESTNET.starts_with("wss://"));
173    }
174
175    #[rstest]
176    fn test_decimal_scale_is_1e18() {
177        assert_eq!(DECIMAL_SCALE, 10u128.pow(18));
178    }
179
180    #[rstest]
181    fn test_min_signature_ttl_is_five_minutes() {
182        assert_eq!(MIN_SIGNATURE_TTL, Duration::from_secs(300));
183    }
184
185    #[rstest]
186    fn test_trigger_order_signature_ttl_is_thirty_one_days() {
187        assert_eq!(
188            TRIGGER_ORDER_SIGNATURE_TTL,
189            Duration::from_secs(31 * 24 * 60 * 60),
190        );
191    }
192
193    #[rstest]
194    fn test_domain_separator_for_routes_per_environment() {
195        assert_eq!(
196            domain_separator_for(DeriveEnvironment::Mainnet),
197            DOMAIN_SEPARATOR_MAINNET,
198        );
199        assert_eq!(
200            domain_separator_for(DeriveEnvironment::Testnet),
201            DOMAIN_SEPARATOR_TESTNET,
202        );
203    }
204
205    #[rstest]
206    fn test_trade_module_address_for_routes_per_environment() {
207        assert_eq!(
208            trade_module_address_for(DeriveEnvironment::Mainnet),
209            TRADE_MODULE_ADDRESS_MAINNET,
210        );
211        assert_eq!(
212            trade_module_address_for(DeriveEnvironment::Testnet),
213            TRADE_MODULE_ADDRESS_TESTNET,
214        );
215    }
216}