Skip to main content

bybit/models/return_codes/
asset.rs

1use crate::prelude::*;
2
3/// Enum representing Bybit API V5 Asset return error/codes.
4///
5/// See: https://bybit-exchange.github.io/docs/v5/error#asset
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Display, derive_more::Debug, derive_more::From)]
7#[repr(i32)]
8#[display("{} - {}", *self as i32, self.message())]
9pub enum AssetCode {
10    /// Insufficient balance in account.
11    InsufficientBalance = 140001,
12
13    /// Invalid coin.
14    InvalidCoin = 140002,
15
16    /// Invalid amount.
17    InvalidAmount = 140003,
18
19    /// Transfer failed.
20    TransferFailed = 140004,
21
22    /// Withdrawal failed.
23    WithdrawalFailed = 140005,
24
25    /// Deposit failed.
26    DepositFailed = 140006,
27
28    /// Coin does not exist.
29    CoinNotExist = 140007,
30
31    /// Withdrawal address is invalid.
32    InvalidWithdrawalAddress = 140008,
33
34    /// Withdrawal is disabled.
35    WithdrawalDisabled = 140009,
36
37    /// Deposit is disabled.
38    DepositDisabled = 140010,
39
40    /// Transfer is disabled.
41    TransferDisabled = 140011,
42
43    /// Withdrawal amount is too small.
44    WithdrawalAmountTooSmall = 140012,
45
46    /// Withdrawal amount is too large.
47    WithdrawalAmountTooLarge = 140013,
48
49    /// Withdrawal fee is too high.
50    WithdrawalFeeTooHigh = 140014,
51
52    /// Withdrawal address is not in whitelist.
53    WithdrawalAddressNotWhitelisted = 140015,
54
55    /// Withdrawal address is not supported.
56    WithdrawalAddressNotSupported = 140016,
57
58    /// Internal transfer is not allowed for this coin.
59    InternalTransferNotAllowed = 140017,
60
61    /// Withdrawal is not allowed for this account.
62    WithdrawalNotAllowed = 140018,
63
64    /// Deposit is not allowed for this account.
65    DepositNotAllowed = 140019,
66
67    /// Sub-account transfer is disabled.
68    SubAccountTransferDisabled = 140020,
69
70    /// Master account transfer is disabled.
71    MasterAccountTransferDisabled = 140021,
72
73    /// Invalid chain type.
74    InvalidChainType = 140022,
75
76    /// Withdrawal address is not on the selected chain.
77    WithdrawalAddressWrongChain = 140023,
78
79    /// Withdrawal is not supported for this coin.
80    WithdrawalNotSupported = 140024,
81
82    /// Deposit is not supported for this coin.
83    DepositNotSupported = 140025,
84
85    /// Transfer amount is too small.
86    TransferAmountTooSmall = 140026,
87
88    /// Transfer amount is too large.
89    TransferAmountTooLarge = 140027,
90}
91
92impl AssetCode {
93    /// Converts an error code (as i32 or &str) to an AssetError variant.
94    /// Returns None if the code doesn't match any variant.
95    pub fn from_code<T>(code: T) -> Option<Self>
96    where
97        T: Into<i32> + Copy,
98    {
99        let code = code.into();
100        match code {
101            140001 => Some(Self::InsufficientBalance),
102            140002 => Some(Self::InvalidCoin),
103            140003 => Some(Self::InvalidAmount),
104            140004 => Some(Self::TransferFailed),
105            140005 => Some(Self::WithdrawalFailed),
106            140006 => Some(Self::DepositFailed),
107            140007 => Some(Self::CoinNotExist),
108            140008 => Some(Self::InvalidWithdrawalAddress),
109            140009 => Some(Self::WithdrawalDisabled),
110            140010 => Some(Self::DepositDisabled),
111            140011 => Some(Self::TransferDisabled),
112            140012 => Some(Self::WithdrawalAmountTooSmall),
113            140013 => Some(Self::WithdrawalAmountTooLarge),
114            140014 => Some(Self::WithdrawalFeeTooHigh),
115            140015 => Some(Self::WithdrawalAddressNotWhitelisted),
116            140016 => Some(Self::WithdrawalAddressNotSupported),
117            140017 => Some(Self::InternalTransferNotAllowed),
118            140018 => Some(Self::WithdrawalNotAllowed),
119            140019 => Some(Self::DepositNotAllowed),
120            140020 => Some(Self::SubAccountTransferDisabled),
121            140021 => Some(Self::MasterAccountTransferDisabled),
122            140022 => Some(Self::InvalidChainType),
123            140023 => Some(Self::WithdrawalAddressWrongChain),
124            140024 => Some(Self::WithdrawalNotSupported),
125            140025 => Some(Self::DepositNotSupported),
126            140026 => Some(Self::TransferAmountTooSmall),
127            140027 => Some(Self::TransferAmountTooLarge),
128            _ => None,
129        }
130    }
131
132    /// Returns the error message associated with the error code.
133    pub fn message(&self) -> &'static str {
134        match self {
135            Self::InsufficientBalance => "Insufficient balance in account.",
136            Self::InvalidCoin => "Invalid coin.",
137            Self::InvalidAmount => "Invalid amount.",
138            Self::TransferFailed => "Transfer failed.",
139            Self::WithdrawalFailed => "Withdrawal failed.",
140            Self::DepositFailed => "Deposit failed.",
141            Self::CoinNotExist => "Coin does not exist.",
142            Self::InvalidWithdrawalAddress => "Withdrawal address is invalid.",
143            Self::WithdrawalDisabled => "Withdrawal is disabled.",
144            Self::DepositDisabled => "Deposit is disabled.",
145            Self::TransferDisabled => "Transfer is disabled.",
146            Self::WithdrawalAmountTooSmall => "Withdrawal amount is too small.",
147            Self::WithdrawalAmountTooLarge => "Withdrawal amount is too large.",
148            Self::WithdrawalFeeTooHigh => "Withdrawal fee is too high.",
149            Self::WithdrawalAddressNotWhitelisted => "Withdrawal address is not in whitelist.",
150            Self::WithdrawalAddressNotSupported => "Withdrawal address is not supported.",
151            Self::InternalTransferNotAllowed => "Internal transfer is not allowed for this coin.",
152            Self::WithdrawalNotAllowed => "Withdrawal is not allowed for this account.",
153            Self::DepositNotAllowed => "Deposit is not allowed for this account.",
154            Self::SubAccountTransferDisabled => "Sub-account transfer is disabled.",
155            Self::MasterAccountTransferDisabled => "Master account transfer is disabled.",
156            Self::InvalidChainType => "Invalid chain type.",
157            Self::WithdrawalAddressWrongChain => "Withdrawal address is not on the selected chain.",
158            Self::WithdrawalNotSupported => "Withdrawal is not supported for this coin.",
159            Self::DepositNotSupported => "Deposit is not supported for this coin.",
160            Self::TransferAmountTooSmall => "Transfer amount is too small.",
161            Self::TransferAmountTooLarge => "Transfer amount is too large.",
162        }
163    }
164}
165
166#[cfg(test)]
167mod tests {
168    use super::*;
169    type Sut = AssetCode;
170    #[test]
171    fn test_from_code_i32() {
172        assert_eq!(Sut::from_code(140001), Some(Sut::InsufficientBalance));
173        assert_eq!(Sut::from_code(140027), Some(Sut::TransferAmountTooLarge));
174        assert_eq!(Sut::from_code(99999), None);
175    }
176
177    #[test]
178    fn test_display() {
179        let error = Sut::InsufficientBalance;
180        assert_eq!(
181            error.to_string(),
182            "140001 - Insufficient balance in account."
183        );
184        let error = Sut::TransferAmountTooLarge;
185        assert_eq!(error.to_string(), "140027 - Transfer amount is too large.");
186    }
187}