Skip to main content

nautilus_dydx/python/
wallet.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//! Python bindings for dYdX wallet.
17
18#![allow(clippy::missing_errors_doc)]
19
20use std::sync::Arc;
21
22use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
23use pyo3::prelude::*;
24
25use crate::execution::wallet::Wallet;
26
27/// Python wrapper for the Wallet.
28#[pyclass(name = "DydxWallet")]
29#[derive(Debug, Clone)]
30pub struct PyDydxWallet {
31    pub(crate) inner: Arc<Wallet>,
32}
33
34#[pymethods]
35impl PyDydxWallet {
36    /// Create a wallet from a hex-encoded private key.
37    ///
38    /// # Errors
39    ///
40    /// Returns an error if the private key is invalid.
41    #[staticmethod]
42    #[pyo3(name = "from_private_key")]
43    pub fn py_from_private_key(private_key: &str) -> PyResult<Self> {
44        let wallet = Wallet::from_private_key(private_key).map_err(to_pyvalue_err)?;
45        Ok(Self {
46            inner: Arc::new(wallet),
47        })
48    }
49
50    /// Get the wallet address.
51    ///
52    /// # Errors
53    ///
54    /// Returns an error if address derivation fails.
55    #[pyo3(name = "address")]
56    pub fn py_address(&self) -> PyResult<String> {
57        let account = self.inner.account_offline().map_err(to_pyruntime_err)?;
58        Ok(account.address)
59    }
60
61    fn __repr__(&self) -> String {
62        "DydxWallet(<redacted>)".to_string()
63    }
64}