Skip to main content

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