bullet_exchange_interface/message/user/mod.rs
1//! User operations.
2
3use crate::decimals::PositiveDecimal;
4use crate::define_enum;
5use crate::string::CustomString;
6use crate::time::UnixTimestampMicros;
7use crate::types::{
8 AssetId, MarketId, SpotCollateralTransferDirection, TradingMode, TriggerOrderId, TwapId,
9};
10mod args;
11pub use args::*;
12
13define_enum! {
14 /// User operations requiring account ownership or delegation.
15 ///
16 /// These operations are authorized via `context.sender()` with optional delegate resolution.
17 /// Operations include account management, trading, vault deposits, and user-initiated liquidations.
18 enum UserAction<Address> {
19 // =========================================================================
20 // Account Operations (0-19)
21 // =========================================================================
22 /// Deposit assets to perp margin account.
23 Deposit {
24 asset_id: AssetId,
25 amount: PositiveDecimal,
26 } = 0,
27
28 /// Withdraw assets from perp margin account.
29 Withdraw {
30 asset_id: AssetId,
31 amount: PositiveDecimal,
32 } = 1,
33
34 /// Deposit assets to spot collateral.
35 DepositSpotCollateral {
36 asset_id: AssetId,
37 amount: PositiveDecimal,
38 } = 2,
39
40 /// Withdraw assets from spot collateral.
41 WithdrawSpotCollateral {
42 asset_id: AssetId,
43 amount: PositiveDecimal,
44 } = 3,
45
46 /// Transfer assets between perp margin and spot collateral.
47 TransferSpotCollateral {
48 asset_id: AssetId,
49 amount: PositiveDecimal,
50 direction: SpotCollateralTransferDirection,
51 sub_account_index: Option<u8>,
52 } = 4,
53
54 /// Borrow assets from spot pool.
55 BorrowSpot {
56 asset_id: AssetId,
57 amount: PositiveDecimal,
58 sub_account_index: Option<u8>,
59 } = 5,
60
61 /// Create a new sub-account.
62 CreateSubAccount { index: u8 } = 6,
63
64 /// Transfer assets between main account and sub-account.
65 TransferToSubAccount {
66 asset_id: AssetId,
67 amount: PositiveDecimal,
68 sub_account_index: u8,
69 to_sub_account: bool,
70 } = 7,
71
72 /// Delegate trading permissions to another address.
73 DelegateUser { delegate: Address, name: CustomString } = 8,
74
75 /// Revoke delegation from an address.
76 RevokeDelegation { delegate: Address } = 9,
77
78 /// Update maximum leverage for a market.
79 UpdateMaxLeverage {
80 market_id: MarketId,
81 max_leverage: u16,
82 sub_account_index: Option<u8>,
83 } = 10,
84
85 /// Claim your own referral rewards.
86 ClaimReferralRewards { asset_id: AssetId } = 11,
87
88 /// Deposit USDC into an isolated market / isolated margin position.
89 DepositIso {
90 market_id: MarketId,
91 amount: PositiveDecimal,
92 } = 12,
93
94 /// Withdraw USDC from an isolated market / isolated margin position.
95 WithdrawIso {
96 market_id: MarketId,
97 amount: PositiveDecimal,
98 } = 13,
99
100 /// Set the trading mode of a perp ledger to cross or iso.
101 SetPerpLedgerTradingMode {
102 market_id: MarketId,
103 trading_mode: TradingMode,
104 sub_account_index: Option<u8>,
105 } = 14,
106
107 /// Delegate trading permissions to another address allowing for sub accounts to be delegated.
108 DelegateUserV1 {
109 delegate: Address,
110 name: CustomString,
111 sub_account_index: Option<u8>,
112 } = 15,
113
114 /// Revoke delegation from an address allowing for sub accounts to revoke delegation.
115 RevokeDelegationV1 {
116 delegate: Address,
117 sub_account_index: Option<u8>,
118 } = 16,
119
120 /// Delegate trading permissions with optional expiry and flags
121 DelegateUserV2 {
122 delegate: Address,
123 name: CustomString,
124 sub_account_index: Option<u8>,
125 expires_at: Option<UnixTimestampMicros>,
126 flags: u32,
127 } = 17,
128
129 // Reserved: 18-19
130
131 // =========================================================================
132 // Order Operations (20-39)
133 // =========================================================================
134 /// Place new orders on a market.
135 PlaceOrders {
136 market_id: MarketId,
137 orders: Vec<NewOrderArgs>,
138 replace: bool,
139 sub_account_index: Option<u8>,
140 } = 20,
141
142 /// Amend existing orders (cancel + place).
143 AmendOrders {
144 market_id: MarketId,
145 orders: Vec<AmendOrderArgs>,
146 sub_account_index: Option<u8>,
147 } = 21,
148
149 /// Cancel specific orders.
150 CancelOrders {
151 market_id: MarketId,
152 orders: Vec<CancelOrderArgs>,
153 sub_account_index: Option<u8>,
154 } = 22,
155
156 /// Cancel all orders on a market.
157 CancelMarketOrders {
158 market_id: MarketId,
159 sub_account_index: Option<u8>,
160 } = 23,
161
162 /// Create trigger orders for spot markets.
163 CreateTriggerOrders {
164 market_id: MarketId,
165 trigger_orders: Vec<NewTriggerOrderArgs>,
166 sub_account_index: Option<u8>,
167 } = 24,
168
169 /// Create take-profit/stop-loss for a perp position.
170 CreatePositionTpsl {
171 market_id: MarketId,
172 tpsl_pair: TpslPair,
173 size: Option<PositiveDecimal>,
174 sub_account_index: Option<u8>,
175 } = 25,
176
177 /// Cancel trigger orders.
178 CancelTriggerOrders {
179 market_id: MarketId,
180 trigger_order_ids: Vec<TriggerOrderId>,
181 sub_account_index: Option<u8>,
182 } = 26,
183
184 /// Create TWAP orders
185 CreateTwapOrder {
186 market_id: MarketId,
187 twap_order_args: NewTwapOrderArgs,
188 sub_account_index: Option<u8>,
189 } = 27,
190
191 /// Cancel a TWAP order
192 CancelTwapOrder {
193 market_id: MarketId,
194 twap_id: TwapId,
195 sub_account_index: Option<u8>,
196 } = 28,
197
198 /// Cancel all orders for perp or spot
199 CancelAllOrders { sub_account_index: Option<u8> } = 29,
200 // Reserved: 30-39
201
202 // =========================================================================
203 // Pool Operations (40-49)
204 // =========================================================================
205 /// Deposit USDC to the PnL pool.
206 DepositToPnlPool { usdc_amount: PositiveDecimal } = 40,
207
208 /// Settle user's PnL from the pool.
209 SettleFromPnlPool { sub_account_index: Option<u8> } = 41,
210
211 /// Deposit to the insurance fund.
212 DepositToInsuranceFund { usdc_amount: PositiveDecimal } = 42,
213
214 /// Deposit to protocol treasury.
215 DepositToTreasury {
216 asset_id: AssetId,
217 amount: PositiveDecimal,
218 } = 43,
219
220 /// Claim accumulated borrow/lend protocol fees.
221 ClaimBorrowLendFees {} = 44,
222
223 /// Deposit to the iso insurance fund.
224 DepositToIsoInsuranceFund { market_id: MarketId, amount: PositiveDecimal } = 45,
225 // Reserved: 46-49
226
227 // =========================================================================
228 // Vault User Operations (50-59)
229 // =========================================================================
230 /// Create a new vault (caller becomes the vault leader).
231 CreateVault { args: CreateVaultArgs<Address> } = 50,
232
233 /// Deposit assets to a vault.
234 DepositToVault {
235 vault_address: Address,
236 asset_id: AssetId,
237 amount: PositiveDecimal,
238 } = 51,
239
240 /// Queue a withdrawal from a vault.
241 QueueWithdrawal {
242 vault_address: Address,
243 shares: PositiveDecimal,
244 } = 52,
245
246 /// Cancel a queued withdrawal.
247 CancelQueuedWithdrawal { vault_address: Address } = 53,
248
249 /// Force withdraw from a vault (bypasses queue).
250 ForceWithdrawVault {
251 vault_address: Address,
252 shares: PositiveDecimal,
253 } = 54,
254
255 // Reserved: 55-59
256
257 // =========================================================================
258 // User-Initiated Liquidation Operations (60-69)
259 // =========================================================================
260 /// Backstop liquidation for perp positions (user provides capital).
261 BackstopLiquidatePerpPositions {
262 address: Address,
263 positions: Option<Vec<BackstopLiquidatePerpPositionArgs>>,
264 sub_account_index: Option<u8>,
265 } = 60,
266
267 /// Liquidate borrow/lend liability (user provides capital).
268 LiquidateBorrowLendLiability {
269 liquidatee_address: Address,
270 liability_asset_id: AssetId,
271 collateral_asset_id: AssetId,
272 liability_amount: PositiveDecimal,
273 sub_account_index: Option<u8>,
274 } = 61,
275
276 BackstopLiquidateIsoPerpPosition {
277 address: Address,
278 position: BackstopLiquidatePerpPositionArgs,
279 sub_account_index: Option<u8>,
280 } = 62,
281 // Reserved: 63-255
282 }
283}