Skip to main content

nautilus_dydx/grpc/
types.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//! Type definitions for dYdX v4 gRPC operations.
17
18use std::str::FromStr;
19
20use cosmrs::tendermint::{Error, chain::Id};
21use serde::{Deserialize, Serialize};
22use strum::{AsRefStr, Display};
23
24/// [Chain ID](https://docs.dydx.xyz/nodes/network-constants#chain-id)
25/// serves as a unique chain identifier to prevent replay attacks.
26///
27/// See also [Cosmos ecosystem](https://cosmos.directory/).
28#[derive(Debug, Eq, PartialEq, Clone, Display, AsRefStr, Deserialize, Serialize)]
29pub enum ChainId {
30    /// Testnet.
31    #[strum(serialize = "dydx-testnet-4")]
32    #[serde(rename = "dydx-testnet-4")]
33    Testnet4,
34    /// Mainnet.
35    #[strum(serialize = "dydx-mainnet-1")]
36    #[serde(rename = "dydx-mainnet-1")]
37    Mainnet1,
38}
39
40impl FromStr for ChainId {
41    type Err = anyhow::Error;
42
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        match s {
45            "dydx-testnet-4" | "testnet" => Ok(Self::Testnet4),
46            "dydx-mainnet-1" | "mainnet" => Ok(Self::Mainnet1),
47            _ => anyhow::bail!("Invalid chain ID: {s}"),
48        }
49    }
50}
51
52impl TryFrom<ChainId> for Id {
53    type Error = Error;
54
55    fn try_from(chain_id: ChainId) -> Result<Self, Self::Error> {
56        chain_id.as_ref().parse()
57    }
58}