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