af_iperps/
lib.rs

1#![cfg_attr(all(doc, not(doctest)), feature(doc_cfg))]
2
3//! Move types for Aftermath's `Perpetuals` package
4
5use af_move_type::otw::Otw;
6use af_sui_pkg_sdk::sui_pkg_sdk;
7use af_sui_types::{Address, IdentStr, SUI_FRAMEWORK_ADDRESS};
8use af_utilities::types::ifixed::IFixed;
9use sui_framework_sdk::balance::Balance;
10use sui_framework_sdk::dynamic_object_field::Wrapper;
11use sui_framework_sdk::object::{ID, UID};
12use sui_framework_sdk::sui::SUI;
13use sui_framework_sdk::{Field, FieldTypeTag};
14
15pub mod errors;
16pub mod event_ext;
17pub mod event_instance;
18#[cfg(feature = "graphql")]
19pub mod graphql;
20pub mod math;
21pub mod order_helpers;
22pub mod order_id;
23#[cfg(feature = "stop-orders")]
24pub mod stop_order_helpers;
25
26pub use self::market::{MarketParams, MarketState};
27pub use self::orderbook::Order;
28pub use self::position::Position;
29use crate::authority::{ADMIN, ASSISTANT};
30
31/// Package IDs of the perpetuals contract versions published on testnet, in order of its versions.
32pub const TESTNET_PACKAGE_VERSIONS: &[Address] = &[Address::from_static(
33    "0x1fc71972750d0d81567183a8500befef94d7699aac76edffcca253fe541367fd",
34)];
35
36// Convenient aliases since these types will never exist onchain with a type argument other than an
37// OTW.
38pub type AdminCapability = self::authority::Capability<ADMIN>;
39pub type AssistantCapability = self::authority::Capability<ASSISTANT>;
40pub type AccountCap = self::account::AccountCap<Otw>;
41pub type AccountCapTypeTag = self::account::AccountCapTypeTag<Otw>;
42pub type Account = self::account::Account<Otw>;
43pub type AccountTypeTag = self::account::AccountTypeTag<Otw>;
44pub type StopOrderTicket = self::stop_orders::StopOrderTicket<Otw>;
45pub type StopOrderTicketTypetag = self::stop_orders::StopOrderTicketTypeTag<Otw>;
46pub type ClearingHouse = self::clearing_house::ClearingHouse<Otw>;
47pub type ClearingHouseTypeTag = self::clearing_house::ClearingHouseTypeTag<Otw>;
48pub type Vault = self::clearing_house::Vault<Otw>;
49pub type VaultTypeTag = self::clearing_house::VaultTypeTag<Otw>;
50pub type MarketInfo = self::registry::MarketInfo<Otw>;
51pub type CollateralInfo = self::registry::CollateralInfo<Otw>;
52
53/// Dynamic field storing a [`Vault`].
54pub type VaultDf = Field<keys::MarketVault, Vault>;
55/// Dynamic field storing a [`Position`].
56pub type PositionDf = Field<keys::Position, Position>;
57/// Dynamic field storing a leaf in a [`Map`] of [`Order`]s.
58///
59/// [`Map`]: self::ordered_map::Map
60pub type OrderLeafDf = Field<u64, ordered_map::Leaf<Order>>;
61/// Dynamic object field wrapper for the [`Orderbook`](orderbook::Orderbook) ID.
62pub type OrderbookDofWrapper = Field<Wrapper<keys::Orderbook>, ID>;
63/// Dynamic object field wrapper for the asks [`Map`](ordered_map::Map) ID.
64pub type AsksMapDofWrapper = Field<Wrapper<keys::AsksMap>, ID>;
65/// Dynamic object field wrapper for the bids [`Map`](ordered_map::Map) ID.
66pub type BidsMapDofWrapper = Field<Wrapper<keys::BidsMap>, ID>;
67/// Dynamic field storing a [`MarketInfo`].
68pub type MarketInfoDf = Field<keys::RegistryMarketInfo, MarketInfo>;
69/// Dynamic field storing a [`CollateralInfo`].
70pub type CollateralInfoDf = Field<keys::RegistryCollateralInfo<Otw>, CollateralInfo>;
71
72sui_pkg_sdk!(perpetuals {
73    module account {
74        /// The AccountCap is used to check ownership of `Account` with the same `account_id`.
75        struct AccountCap<!phantom T> has key, store {
76            id: UID,
77            // Account object id
78            account_obj_id: ID,
79            /// Numerical value associated to the account
80            account_id: u64,
81        }
82
83        /// The Account object saves the collateral available to be used in clearing houses.
84        struct Account<!phantom T> has key, store {
85            id: UID,
86            /// Numerical value associated to the account
87            account_id: u64,
88            /// Balance available to be allocated to markets.
89            collateral: Balance<T>,
90        }
91
92        struct IntegratorConfig has store {
93            /// Max **additional** taker fee the user is willing
94            /// to pay for integrator-submitted orders.
95            max_taker_fee: IFixed
96        }
97    }
98
99    module authority {
100        /// Capability object required to perform admin functions.
101        ///
102        /// Minted once when the module is published and transfered to its creator.
103        struct Capability<!phantom Role> has key, store {
104            id: UID
105        }
106
107        /// Admin Role
108        struct ADMIN();
109
110        /// Assistant Role
111        struct ASSISTANT();
112    }
113
114    module clearing_house {
115        /// The central object that owns the market state.
116        ///
117        /// Dynamic fields:
118        /// - [`position::Position`]
119        /// - [`Vault`]
120        ///
121        /// Dynamic objects:
122        /// - [`orderbook::Orderbook`]
123        struct ClearingHouse<!phantom T> has key {
124            id: UID,
125            version: u64,
126            paused: bool,
127            market_params: market::MarketParams,
128            market_state: market::MarketState
129        }
130
131        /// Stores all deposits from traders for collateral T.
132        /// Stores the funds reserved for covering bad debt from untimely
133        /// liquidations.
134        ///
135        /// The Clearing House keeps track of who owns each share of the vault.
136        struct Vault<!phantom T> has store {
137            collateral_balance: Balance<T>,
138            insurance_fund_balance: Balance<T>,
139        }
140
141        /// Stores the proposed parameters for updating margin ratios
142        struct MarginRatioProposal has store {
143            /// Target timestamp at which to apply the proposed updates
144            maturity: u64,
145            /// Proposed IMR
146            margin_ratio_initial: IFixed,
147            /// Proposed MMR
148            margin_ratio_maintenance: IFixed,
149        }
150
151        /// Stores the proposed parameters for a position's custom fees
152        struct PositionFeesProposal has store {
153            /// Proposed IMR
154            maker_fee: IFixed,
155            /// Proposed MMR
156            taker_fee: IFixed,
157        }
158
159        /// Structure that stores the amount of fees collected by an integrator
160        struct IntegratorVault has store {
161            /// Amount of fees collected by this integrator, in collateral units
162            fees: IFixed,
163        }
164
165        struct IntegratorInfo has copy, drop {
166            integrator_address: address,
167            taker_fee: IFixed,
168        }
169
170        /// Stores the proposed parameters for a position's custom fees
171        struct SettlementPrices has store {
172            /// Base asset's settlement price
173            base_price: IFixed,
174            /// Collateral asset's settlement price
175            collateral_price: IFixed,
176        }
177        /// Used by clearing house to check margin when placing an order
178        struct SessionHotPotato<!phantom T> {
179            clearing_house: ClearingHouse<T>,
180            orderbook: orderbook::Orderbook,
181            account_id: u64,
182            timestamp_ms: u64,
183            collateral_price: IFixed,
184            index_price: IFixed,
185            gas_price: u64,
186            margin_before: IFixed,
187            min_margin_before: IFixed,
188            position_base_before: IFixed,
189            total_open_interest: IFixed,
190            total_fees: IFixed,
191            maker_events: vector<events::FilledMakerOrder>,
192            liqee_account_id: Option<u64>,
193            liquidator_fees: IFixed,
194            session_summary: SessionSummary
195        }
196
197        struct SessionSummary has drop {
198            base_filled_ask: IFixed,
199            base_filled_bid: IFixed,
200            quote_filled_ask: IFixed,
201            quote_filled_bid: IFixed,
202            base_posted_ask: IFixed,
203            base_posted_bid: IFixed,
204            posted_orders: u64,
205            base_liquidated: IFixed,
206            quote_liquidated: IFixed,
207            is_liqee_long: bool,
208            bad_debt: IFixed
209        }
210    }
211
212    module stop_orders {
213        /// Object that allows to place one order on behalf of the user, used to
214        /// offer stop limit or market orders. A stop order is an order that is placed
215        /// only if the index price respects certain conditions, like being above or
216        /// below a certain price.
217        ///
218        /// Only the `AccountCap` owner can mint this object and can decide who can be
219        /// the executor of the ticket. This allows users to run their
220        /// own stop orders bots eventually, but it's mainly used to allow 3rd parties
221        /// to offer such a service (the user is required to trust such 3rd party).
222        /// The object is shared and the 3rd party is set as `executors`. The ticket
223        /// can be destroyed in any moment only by the user or by the executor.
224        /// The user needs to trust the 3rd party for liveness of the service offered.
225        ///
226        /// The order details are encrypted offchain and the result is stored in the ticket.
227        /// The user needs to share such details with the 3rd party only to allow for execution.
228        ///
229        /// The ticket can be either a shared, owned or party object.
230        /// The permission to execute or cancel it is controlled exclusively through `executors`,
231        /// which can be modified only by the `AccountCap` owner associated with the ticket
232        /// using the function `edit_stop_order_ticket_executors`.
233        struct StopOrderTicket<!phantom T> has key, store {
234            id: UID,
235            /// Addresses allowed to execute the order on behalf of the user.
236            executors: vector<address>,
237            /// The executor collects the gas in case the order is placed or canceled for any reason.
238            /// The user gets back the gas in case he manually cancels the order.
239            gas: Balance<SUI>,
240            /// User account id
241            account_id: u64,
242            /// Value to indentify the stop order type. Available values can be found in the
243            /// constants module.
244            stop_order_type: u64,
245            /// Vector containing the blake2b hash obtained from offchain on the stop order parameters.
246            /// Depending on the stop order type value, a different set of parameters is expected to be used.
247            ///
248            /// Parameters encoded for a SLTP stop order (stop_order_type code 0):
249            /// - clearing_house_id: ID
250            /// - expire_timestamp: Option<u64>
251            /// - is_limit_order: `true` if limit order, `false` if market order
252            /// - stop_index_price: u256
253            /// - is_stop_loss: `true` if stop loss order, `false` if take profit order
254            /// - position_is_ask: `true` if position is short, `false` if position is long
255            /// - size: u64
256            /// - price: u64 (can be set at random value if `is_limit_order` is false)
257            /// - order_type: u64 (can be set at random value if `is_limit_order` is false)
258            /// - salt: vector<u8>
259            ///
260            /// Parameters encoded for a Standalone stop order (stop_order_type code 1):
261            /// - clearing_house_id: ID
262            /// - expire_timestamp: Option<u64>
263            /// - is_limit_order: `true` if limit order, `false` if market order
264            /// - stop_index_price: u256
265            /// - ge_stop_index_price: `true` means the order can be placed when
266            /// oracle index price is >= than chosen `stop_index_price`
267            /// - side: bool
268            /// - size: u64
269            /// - price: u64 (can be set at random value if `is_limit_order` is false)
270            /// - order_type: u64 (can be set at random value if `is_limit_order` is false)
271            /// - reduce_only: bool
272            /// - salt: vector<u8>
273            encrypted_details: vector<u8>
274        }
275    }
276
277    module events {
278        struct CreatedAccount<!phantom T> has copy, drop {
279            account_obj_id: ID,
280            user: address,
281            account_id: u64
282        }
283
284        struct DepositedCollateral<!phantom T> has copy, drop {
285            account_id: u64,
286            collateral: u64,
287        }
288
289        struct AllocatedCollateral has copy, drop {
290            ch_id: ID,
291            account_id: u64,
292            collateral: u64,
293        }
294
295        struct WithdrewCollateral<!phantom T> has copy, drop {
296            account_id: u64,
297            collateral: u64,
298        }
299
300        struct DeallocatedCollateral has copy, drop {
301            ch_id: ID,
302            account_id: u64,
303            collateral: u64,
304        }
305
306        struct CreatedOrderbook has copy, drop {
307            branch_min: u64,
308            branches_merge_max: u64,
309            branch_max: u64,
310            leaf_min: u64,
311            leaves_merge_max: u64,
312            leaf_max: u64
313        }
314
315        struct CreatedClearingHouse has copy, drop {
316            ch_id: ID,
317            collateral: String,
318            coin_decimals: u64,
319            margin_ratio_initial: IFixed,
320            margin_ratio_maintenance: IFixed,
321            base_oracle_id: ID,
322            collateral_oracle_id: ID,
323            funding_frequency_ms: u64,
324            funding_period_ms: u64,
325            premium_twap_frequency_ms: u64,
326            premium_twap_period_ms: u64,
327            spread_twap_frequency_ms: u64,
328            spread_twap_period_ms: u64,
329            maker_fee: IFixed,
330            taker_fee: IFixed,
331            liquidation_fee: IFixed,
332            force_cancel_fee: IFixed,
333            insurance_fund_fee: IFixed,
334            lot_size: u64,
335            tick_size: u64,
336        }
337
338        struct RegisteredMarketInfo<!phantom T> has copy, drop {
339            ch_id: ID,
340            base_pfs_id: ID,
341            base_pfs_source_id: ID,
342            collateral_pfs_id: ID,
343            collateral_pfs_source_id: ID,
344            scaling_factor: IFixed
345        }
346
347        struct RemovedRegisteredMarketInfo<!phantom T> has copy, drop {
348            ch_id: ID,
349        }
350
351        struct RegisteredCollateralInfo<!phantom T> has copy, drop {
352            ch_id: ID,
353            collateral_pfs_id: ID,
354            collateral_pfs_source_id: ID,
355            scaling_factor: IFixed
356        }
357
358        struct AddedIntegratorConfig<!phantom T> has copy, drop {
359            account_id: u64,
360            integrator_address: address,
361            max_taker_fee: IFixed
362        }
363
364        struct RemovedIntegratorConfig<!phantom T> has copy, drop {
365            account_id: u64,
366            integrator_address: address,
367        }
368
369        struct PaidIntegratorFees<!phantom T> has copy, drop {
370            account_id: u64,
371            integrator_address: address,
372            fees: IFixed
373        }
374
375        struct CreatedIntegratorVault has copy, drop {
376            ch_id: ID,
377            integrator_address: address,
378        }
379
380        struct WithdrewFromIntegratorVault has copy, drop {
381            ch_id: ID,
382            integrator_address: address,
383            fees: u64
384        }
385
386        struct UpdatedClearingHouseVersion has copy, drop {
387            ch_id: ID,
388            version: u64
389        }
390
391        struct PausedMarket has copy, drop {
392            ch_id: ID,
393        }
394
395        struct ResumedMarket has copy, drop {
396            ch_id: ID,
397        }
398
399        struct ClosedMarket has copy, drop {
400            ch_id: ID,
401            base_settlement_price: IFixed,
402            collateral_settlement_price: IFixed
403        }
404
405        struct ClosedPositionAtSettlementPrices has copy, drop {
406            ch_id: ID,
407            account_id: u64,
408            pnl: IFixed,
409            base_asset_amount: IFixed,
410            quote_asset_amount: IFixed,
411            deallocated_collateral: u64,
412            bad_debt: IFixed
413        }
414
415        struct UpdatedPremiumTwap has copy, drop {
416            ch_id: ID,
417            book_price: IFixed,
418            index_price: IFixed,
419            premium_twap: IFixed,
420            premium_twap_last_upd_ms: u64,
421        }
422
423        struct UpdatedSpreadTwap has copy, drop {
424            ch_id: ID,
425            book_price: IFixed,
426            index_price: IFixed,
427            spread_twap: IFixed,
428            spread_twap_last_upd_ms: u64,
429        }
430
431        struct UpdatedGasPriceTwap has copy, drop {
432            ch_id: ID,
433            gas_price: IFixed,
434            mean: IFixed,
435            variance: IFixed,
436            gas_price_last_upd_ms: u64
437        }
438
439        struct UpdatedGasPriceTwapParameters has copy, drop {
440            ch_id: ID,
441            gas_price_twap_period_ms: u64,
442            gas_price_taker_fee: IFixed,
443            z_score_threshold: IFixed
444        }
445
446        struct UpdatedMarketLotAndTick has copy, drop {
447            ch_id: ID,
448            lot_size: u64,
449            tick_size: u64
450        }
451
452        struct UpdatedFunding has copy, drop {
453            ch_id: ID,
454            cum_funding_rate_long: IFixed,
455            cum_funding_rate_short: IFixed,
456            funding_last_upd_ms: u64,
457        }
458
459        struct SettledFunding has copy, drop {
460            ch_id: ID,
461            account_id: u64,
462            collateral_change_usd: IFixed,
463            collateral_after: IFixed,
464            mkt_funding_rate_long: IFixed,
465            mkt_funding_rate_short: IFixed
466        }
467
468        struct FilledMakerOrders has copy, drop {
469            events: vector<FilledMakerOrder>
470        }
471
472        struct FilledMakerOrder has copy, drop {
473            ch_id: ID,
474            maker_account_id: u64,
475            taker_account_id: u64,
476            order_id: u128,
477            filled_size: u64,
478            remaining_size: u64,
479            canceled_size: u64,
480            pnl: IFixed,
481            fees: IFixed,
482        }
483
484        struct FilledTakerOrder has copy, drop {
485            ch_id: ID,
486            taker_account_id: u64,
487            taker_pnl: IFixed,
488            taker_fees: IFixed,
489            integrator_taker_fees: IFixed,
490            integrator_address: Option<address>,
491            base_asset_delta_ask: IFixed,
492            quote_asset_delta_ask: IFixed,
493            base_asset_delta_bid: IFixed,
494            quote_asset_delta_bid: IFixed,
495        }
496
497        struct PostedOrder has copy, drop {
498            ch_id: ID,
499            account_id: u64,
500            order_id: u128,
501            order_size: u64,
502            reduce_only: bool,
503            expiration_timestamp_ms: Option<u64>
504        }
505
506        struct CanceledOrder has copy, drop {
507            ch_id: ID,
508            account_id: u64,
509            size: u64,
510            order_id: u128,
511        }
512
513        struct LiquidatedPosition has copy, drop {
514            ch_id: ID,
515            liqee_account_id: u64,
516            liqor_account_id: u64,
517            is_liqee_long: bool,
518            base_liquidated: IFixed,
519            quote_liquidated: IFixed,
520            liqee_pnl: IFixed,
521            liquidation_fees: IFixed,
522            force_cancel_fees: IFixed,
523            insurance_fund_fees: IFixed,
524            bad_debt: IFixed
525        }
526
527        struct PerformedLiquidation has copy, drop {
528            ch_id: ID,
529            liqee_account_id: u64,
530            liqor_account_id: u64,
531            is_liqee_long: bool,
532            base_liquidated: IFixed,
533            quote_liquidated: IFixed,
534            liqor_pnl: IFixed,
535            liqor_fees: IFixed,
536        }
537
538        struct SocializedBadDebt has copy, drop {
539            ch_id: ID,
540            bad_debt_usd: IFixed,
541            socialized_fundings: IFixed,
542            added_to_long: bool,
543            cum_funding_rate_long: IFixed,
544            cum_funding_rate_short: IFixed,
545        }
546
547        struct PerformedADL has copy, drop {
548            ch_id: ID,
549            bad_debt_account_id: u64,
550            size_reduced: u64,
551            adl_price: u64,
552            counterparty_account_id: u64,
553            bad_debt_is_long: bool,
554        }
555
556        struct CreatedPosition has copy, drop {
557            ch_id: ID,
558            account_id: u64,
559            mkt_funding_rate_long: IFixed,
560            mkt_funding_rate_short: IFixed,
561        }
562
563        struct SetPositionInitialMarginRatio has copy, drop {
564            ch_id: ID,
565            account_id: u64,
566            initial_margin_ratio: IFixed,
567        }
568
569        struct CreatedStopOrderTicket<!phantom T> has copy, drop {
570            ticket_id: ID,
571            account_id: u64,
572            executors: vector<address>,
573            gas: u64,
574            stop_order_type: u64,
575            encrypted_details: vector<u8>
576        }
577
578        struct ExecutedStopOrderTicket<!phantom T> has copy, drop {
579            ticket_id: ID,
580            account_id: u64,
581            executor: address
582        }
583
584        struct DeletedStopOrderTicket<!phantom T> has copy, drop {
585            ticket_id: ID,
586            account_id: u64,
587            executor: address
588        }
589
590        struct EditedStopOrderTicketDetails<!phantom T> has copy, drop {
591            ticket_id: ID,
592            account_id: u64,
593            encrypted_details: vector<u8>
594        }
595
596        struct EditedStopOrderTicketExecutors<!phantom T> has copy, drop {
597            ticket_id: ID,
598            account_id: u64,
599            executors: vector<address>
600        }
601
602        struct CreatedMarginRatiosProposal has copy, drop {
603            ch_id: ID,
604            margin_ratio_initial: IFixed,
605            margin_ratio_maintenance: IFixed,
606        }
607
608        struct UpdatedMarginRatios has copy, drop {
609            ch_id: ID,
610            margin_ratio_initial: IFixed,
611            margin_ratio_maintenance: IFixed,
612        }
613
614        struct DeletedMarginRatiosProposal has copy, drop {
615            ch_id: ID,
616            margin_ratio_initial: IFixed,
617            margin_ratio_maintenance: IFixed,
618        }
619
620        struct CreatedPositionFeesProposal has copy, drop {
621            ch_id: ID,
622            account_id: u64,
623            maker_fee: IFixed,
624            taker_fee: IFixed,
625        }
626
627        struct DeletedPositionFeesProposal has copy, drop {
628            ch_id: ID,
629            account_id: u64,
630            maker_fee: IFixed,
631            taker_fee: IFixed,
632        }
633
634        struct AcceptedPositionFeesProposal has copy, drop {
635            ch_id: ID,
636            account_id: u64,
637            maker_fee: IFixed,
638            taker_fee: IFixed,
639        }
640
641        struct RejectedPositionFeesProposal has copy, drop {
642            ch_id: ID,
643            account_id: u64,
644            maker_fee: IFixed,
645            taker_fee: IFixed,
646        }
647
648        struct ResettedPositionFees has copy, drop {
649            ch_id: ID,
650            account_id: u64,
651        }
652
653        struct UpdatedFees has copy, drop {
654            ch_id: ID,
655            maker_fee: IFixed,
656            taker_fee: IFixed,
657            liquidation_fee: IFixed,
658            force_cancel_fee: IFixed,
659            insurance_fund_fee: IFixed,
660        }
661
662        struct UpdatedFundingParameters has copy, drop {
663            ch_id: ID,
664            funding_frequency_ms: u64,
665            funding_period_ms: u64,
666            premium_twap_frequency_ms: u64,
667            premium_twap_period_ms: u64,
668        }
669
670        struct UpdatedSpreadTwapParameters has copy, drop {
671            ch_id: ID,
672            spread_twap_frequency_ms: u64,
673            spread_twap_period_ms: u64
674        }
675
676        struct UpdatedMinOrderUsdValue has copy, drop {
677            ch_id: ID,
678            min_order_usd_value: IFixed,
679        }
680
681        struct UpdatedBasePfsID has copy, drop {
682            ch_id: ID,
683            pfs_id: ID,
684        }
685
686        struct UpdatedCollateralPfsID has copy, drop {
687            ch_id: ID,
688            pfs_id: ID,
689        }
690
691        struct UpdatedBasePfsSourceID has copy, drop {
692            ch_id: ID,
693            source_id: ID,
694        }
695
696        struct UpdatedCollateralPfsSourceID has copy, drop {
697            ch_id: ID,
698            source_id: ID,
699        }
700
701        struct UpdatedBasePfsTolerance has copy, drop {
702            ch_id: ID,
703            pfs_tolerance: u64,
704        }
705
706        struct UpdatedCollateralPfsTolerance has copy, drop {
707            ch_id: ID,
708            pfs_tolerance: u64,
709        }
710
711        struct UpdatedMaxSocializeLossesMrDecrease has copy, drop {
712            ch_id: ID,
713            max_socialize_losses_mr_decrease: IFixed,
714        }
715        struct UpdatedMaxBadDebt has copy, drop {
716            ch_id: ID,
717            max_bad_debt: IFixed,
718        }
719
720        struct UpdatedCollateralHaircut has copy, drop {
721            ch_id: ID,
722            collateral_haircut: IFixed,
723        }
724
725        struct UpdatedMaxOpenInterest has copy, drop {
726            ch_id: ID,
727            max_open_interest: IFixed,
728        }
729
730        struct UpdatedMaxOpenInterestPositionParams has copy, drop {
731            ch_id: ID,
732            max_open_interest_threshold: IFixed,
733            max_open_interest_position_percent: IFixed,
734        }
735
736        struct UpdatedMaxPendingOrders has copy, drop {
737            ch_id: ID,
738            max_pending_orders: u64
739        }
740
741        struct UpdatedStopOrderMistCost has copy, drop {
742            stop_order_mist_cost: u64
743        }
744
745        struct DonatedToInsuranceFund has copy, drop {
746            sender: address,
747            ch_id: ID,
748            new_balance: u64,
749        }
750
751        struct WithdrewFees has copy, drop {
752            sender: address,
753            ch_id: ID,
754            amount: u64,
755            vault_balance_after: u64,
756        }
757
758        struct WithdrewInsuranceFund has copy, drop {
759            sender: address,
760            ch_id: ID,
761            amount: u64,
762            insurance_fund_balance_after: u64,
763        }
764
765        struct UpdatedOpenInterestAndFeesAccrued has copy, drop {
766            ch_id: ID,
767            open_interest: IFixed,
768            fees_accrued: IFixed
769        }
770    }
771
772    module keys {
773        /// Key type for accessing a `MarketInfo` saved in registry.
774        struct RegistryMarketInfo has copy, drop, store {
775            ch_id: ID
776        }
777
778        /// Key type for accessing a `CollateralInfo` saved in registry.
779        struct RegistryCollateralInfo<!phantom T> has copy, drop, store {}
780
781        /// Key type for accessing a `Config` saved in registry.
782        struct RegistryConfig has copy, drop, store {}
783
784        /// Key type for accessing integrator configs for an account.
785        struct IntegratorConfig has copy, drop, store {
786            integrator_address: address,
787        }
788
789        /// Key type for accessing integrator's collected fees.
790        struct IntegratorVault has copy, drop, store {
791            integrator_address: address,
792        }
793
794        /// Key type for accessing  in clearing house.
795        struct SettlementPrices has copy, drop, store {}
796
797        /// Key type for accessing market params in clearing house.
798        struct Orderbook has copy, drop, store {}
799
800        /// Key type for accessing vault in clearing house.
801        struct MarketVault has copy, drop, store {}
802
803        /// Key type for accessing trader position in clearing house.
804        struct Position has copy, drop, store {
805            account_id: u64,
806        }
807
808        /// Key type for accessing market margin parameters change proposal in clearing house.
809        struct MarginRatioProposal has copy, drop, store {}
810
811        /// Key type for accessing custom fees parameters change proposal for an account
812        struct PositionFeesProposal has copy, drop, store {
813            account_id: u64
814        }
815
816        /// Key type for accessing asks map in the orderbook
817        struct AsksMap has copy, drop, store {}
818
819        /// Key type for accessing asks map in the orderbook
820        struct BidsMap has copy, drop, store {}
821    }
822
823    module market {
824        /// Static attributes of a perpetuals market.
825        struct MarketParams has copy, drop, store {
826            /// Set of parameters governing market's core behaviors
827            core_params: CoreParams,
828            /// Set of parameters related to market's fees
829            fees_params: FeesParams,
830            /// Set of parameters governing fundings and twap updates
831            twap_params: TwapParams,
832            /// Set of parameters defining market's limits
833            limits_params: LimitsParams
834        }
835
836        struct CoreParams has copy, drop, store {
837            /// Identifier of the base asset's price feed storage.
838            base_pfs_id: ID,
839            /// Identifier of the collateral asset's price feed storage.
840            collateral_pfs_id: ID,
841            /// Identifier of the base asset's price feed storage source id (pyth, stork, etc...).
842            base_pfs_source_id: ID,
843            /// Identifier of the collateral asset's price feed storage source id (pyth, stork, etc...).
844            collateral_pfs_source_id: ID,
845            /// Timestamp tolerance for base oracle price
846            base_pfs_tolerance: u64,
847            /// Timestamp tolerance for collateral oracle price
848            collateral_pfs_tolerance: u64,
849            /// Number of base units exchanged per lot
850            lot_size: u64,
851            /// Number of quote units exchanged per tick
852            tick_size: u64,
853            /// Scaling factor to use to convert collateral units to ifixed values and viceversa
854            scaling_factor: IFixed,
855            /// Value haircut applied to collateral allocated in the position.
856            /// Example: 98%
857            collateral_haircut: IFixed,
858            /// Minimum margin ratio for opening a new position.
859            margin_ratio_initial: IFixed,
860            /// Margin ratio below which full liquidations can occur.
861            margin_ratio_maintenance: IFixed,
862        }
863
864        struct FeesParams has copy, drop, store {
865            /// Proportion of volume charged as fees from makers upon processing
866            /// fill events.
867            maker_fee: IFixed,
868            /// Proportion of volume charged as fees from takers after processing
869            /// fill events.
870            taker_fee: IFixed,
871            /// Proportion of volume charged as fees from liquidatees
872            liquidation_fee: IFixed,
873            /// Proportion of volume charged as fees from liquidatees after forced cancelling
874            /// of pending orders during liquidation.
875            force_cancel_fee: IFixed,
876            /// Proportion of volume charged as fees from liquidatees to deposit into insurance fund
877            insurance_fund_fee: IFixed,
878            /// Additional taker fee to apply in case the gas price set for the transaction violates
879            /// the z-score constraint
880            gas_price_taker_fee: IFixed,
881        }
882
883        struct TwapParams has copy, drop, store {
884            /// The time span between each funding rate update.
885            funding_frequency_ms: u64,
886            /// Period of time over which funding (the difference between book and
887            /// index prices) gets paid.
888            ///
889            /// Setting the funding period too long may cause the perpetual to start
890            /// trading at a very dislocated price to the index because there's less
891            /// of an incentive for basis arbitrageurs to push the prices back in
892            /// line since they would have to carry the basis risk for a longer
893            /// period of time.
894            ///
895            /// Setting the funding period too short may cause nobody to trade the
896            /// perpetual because there's too punitive of a price to pay in the case
897            /// the funding rate flips sign.
898            funding_period_ms: u64,
899            /// The time span between each funding TWAP (both index price and orderbook price) update.
900            premium_twap_frequency_ms: u64,
901            /// The reference time span used for weighting the TWAP (both index price and orderbook price)
902            /// updates for funding rates estimation
903            premium_twap_period_ms: u64,
904            /// The time span between each spread TWAP updates (used for liquidations).
905            spread_twap_frequency_ms: u64,
906            /// The reference time span used for weighting the TWAP updates for spread.
907            spread_twap_period_ms: u64,
908            /// The reference time span used for weighting the TWAP updates for gas price.
909            gas_price_twap_period_ms: u64,
910        }
911
912        struct LimitsParams has copy, drop, store {
913            /// Minimum USD value an order is required to be worth to be placed
914            min_order_usd_value: IFixed,
915            /// Maximum number of pending orders that a position can have.
916            max_pending_orders: u64,
917            /// Max open interest (in base tokens) available for this market
918            max_open_interest: IFixed,
919            /// The check on `max_open_interest_position_percent` is not performed if
920            /// the market's open interest is below this threshold.
921            max_open_interest_threshold: IFixed,
922            /// Max open interest percentage a position can have relative to total market's open interest
923            max_open_interest_position_percent: IFixed,
924            /// Max amount of bad debt that can be socialized in nominal value.
925            /// Positions that violate this check should be ADL'd.
926            max_bad_debt: IFixed,
927            /// Max amount of bad debt that can be socialized relative to total market's open interest.
928            /// Positions that violate this check should be ADL'd.
929            max_socialize_losses_mr_decrease: IFixed,
930            /// Z-Score threshold level used to determine if to apply `gas_price_taker_fee` to the
931            /// executed order
932            z_score_threshold: IFixed,
933        }
934        /// The state of a perpetuals market.
935        struct MarketState has store {
936            /// The latest cumulative funding premium in this market for longs. Must be updated
937            /// periodically.
938            cum_funding_rate_long: IFixed,
939            /// The latest cumulative funding premium in this market for shorts. Must be updated
940            /// periodically.
941            cum_funding_rate_short: IFixed,
942            /// The timestamp (millisec) of the latest cumulative funding premium update
943            /// (both longs and shorts).
944            funding_last_upd_ms: u64,
945            /// The last calculated funding premium TWAP (used for funding settlement).
946            premium_twap: IFixed,
947            /// The timestamp (millisec) of the last update of `premium_twap`.
948            premium_twap_last_upd_ms: u64,
949            /// The last calculated spread TWAP (used for liquidations).
950            /// Spread is (book - index).
951            spread_twap: IFixed,
952            /// The timestamp (millisec) of `spread_twap` last update.
953            spread_twap_last_upd_ms: u64,
954            /// Gas price TWAP mean.
955            /// It is used to calculate the penalty to add to taker fees based on the Z-score of the current gas price
956            /// relative to the smoothed mean and variance.
957            gas_price_mean: IFixed,
958            /// Gas price TWAP variance.
959            /// It is used to calculate the penalty to add to taker fees based on the Z-score of the current gas price
960            /// relative to the smoothed mean and variance.
961            gas_price_variance: IFixed,
962            /// The timestamp (millisec) of the last update of `gas_price_mean` and `gas_price_variance`.
963            gas_price_last_upd_ms: u64,
964            /// Open interest (in base tokens) as a fixed-point number. Counts the
965            /// total size of contracts as the sum of all long positions.
966            open_interest: IFixed,
967            /// Total amount of fees accrued by this market (in T's units)
968            /// Only admin can withdraw these fees.
969            fees_accrued: IFixed,
970        }
971    }
972
973    module orderbook {
974        /// An order on the orderbook
975        struct Order has copy, drop, store {
976            /// User's account id
977            account_id: u64,
978            /// Amount of lots to be filled
979            size: u64,
980            /// Optional reduce-only requirement for this order.
981            reduce_only: bool,
982            /// Optional expiration time for the order
983            expiration_timestamp_ms: Option<u64>
984        }
985
986        /// The orderbook doesn't know the types of tokens traded, it assumes a correct
987        /// management by the clearing house
988        struct Orderbook has key, store {
989            id: UID,
990            /// Number of limit orders placed on book, monotonically increases
991            counter: u64,
992        }
993    }
994
995    module ordered_map {
996        /// Ordered map with `u128` type as a key and `V` type as a value.
997        struct Map<!phantom V: copy + drop + store> has key, store {
998            /// Object UID for adding dynamic fields that are used as pointers to nodes.
999            id: UID,
1000            /// Number of key-value pairs in the map.
1001            size: u64,
1002            /// Counter for creating another node as a dynamic field.
1003            counter: u64,
1004            /// Pointer to the root node, which is a branch or a leaf.
1005            root: u64,
1006            /// Pointer to first leaf.
1007            first: u64,
1008            /// Minimal number of kids in a non-root branch;
1009            /// must satisfy 2 <= branch_min <= branch_max / 2.
1010            branch_min: u64,
1011            /// Maximal number of kids in a branch, which is merge of two branches;
1012            /// must satisfy 2 * branch_min <= branches_merge_max <= branch_max.
1013            branches_merge_max: u64,
1014            /// Maximal number of kids in a branch.
1015            branch_max: u64,
1016            /// Minimal number of elements in a non-root leaf;
1017            /// must satisfy 2 <= leaf_min <= (leaf_max + 1) / 2.
1018            leaf_min: u64,
1019            /// Maximal number of elements in a leaf, which is merge of two leaves;
1020            /// must satisfy 2 * leaf_min - 1 <= leaves_merge_max <= leaf_max.
1021            leaves_merge_max: u64,
1022            /// Maximal number of elements in a leaf.
1023            leaf_max: u64,
1024        }
1025
1026        /// Branch node with kids and ordered separating keys.
1027        struct Branch has drop, store {
1028            /// Separating keys for kids sorted in ascending order.
1029            keys: vector<u128>,
1030            /// Kids of the node.
1031            kids: vector<u64>,
1032        }
1033
1034        /// Key-value pair.
1035        struct Pair<V: copy + drop + store> has copy, drop, store {
1036            key: u128,
1037            val: V,
1038        }
1039
1040        /// Leaf node with ordered key-value pairs.
1041        struct Leaf<V: copy + drop + store> has drop, store {
1042            /// Keys sorted in ascending order together with values.
1043            keys_vals: vector<Pair<V>>,
1044            /// Pointer to next leaf.
1045            next: u64,
1046        }
1047    }
1048
1049    module position {
1050        /// Stores information about an open position
1051        struct Position has store {
1052            /// Amount of allocated tokens (e.g., USD stables) backing this account's position.
1053            collateral: IFixed,
1054            /// The perpetual contract size, controlling the amount of exposure to
1055            /// the underlying asset. Positive implies long position and negative,
1056            /// short. Represented as a signed fixed-point number.
1057            base_asset_amount: IFixed,
1058            /// The entry value for this position, including leverage. Represented
1059            /// as a signed fixed-point number.
1060            quote_asset_notional_amount: IFixed,
1061            /// Last long cumulative funding rate used to update this position. The
1062            /// market's latest long cumulative funding rate minus this gives the funding
1063            /// rate this position must pay. This rate multiplied by this position's
1064            /// value (base asset amount * market price) gives the total funding
1065            /// owed, which is deducted from the trader account's margin. This debt
1066            /// is accounted for in margin ratio calculations, which may lead to
1067            /// liquidation. Represented as a signed fixed-point number.
1068            cum_funding_rate_long: IFixed,
1069            /// Last short cumulative funding rate used to update this position. The
1070            /// market's latest short cumulative funding rate minus this gives the funding
1071            /// rate this position must pay. This rate multiplied by this position's
1072            /// value (base asset amount * market price) gives the total funding
1073            /// owed, which is deducted from the trader account's margin. This debt
1074            /// is accounted for in margin ratio calculations, which may lead to
1075            /// liquidation. Represented as a signed fixed-point number.
1076            cum_funding_rate_short: IFixed,
1077            /// Base asset amount resting in ask orders in the orderbook.
1078            /// Represented as a signed fixed-point number.
1079            asks_quantity: IFixed,
1080            /// Base asset amount resting in bid orders in the orderbook.
1081            /// Represented as a signed fixed-point number.
1082            bids_quantity: IFixed,
1083            /// Number of pending orders in this position.
1084            pending_orders: u64,
1085            /// Custom maker fee for this position, set at default value of 100%
1086            maker_fee: IFixed,
1087            /// Custom taker fee for this position, set at default value of 100%
1088            taker_fee: IFixed,
1089            /// Initial Margin Ratio set by user for the position. Must always be less
1090            /// or equal than market's IMR. Used as a desired reference margin ratio when
1091            /// managing collateral in the position during all the actions. Can be changed
1092            /// by the user at any moment (between the allowed limits).
1093            initial_margin_ratio: IFixed
1094        }
1095    }
1096
1097    module registry {
1098        /// Registry object that maintains:
1099        /// - A mapping between a clearing house id and `MarketInfo`
1100        /// - A mapping between a collateral type `T` and `CollateralInfo`
1101        /// It also maintains the global counter for account creation.
1102        /// Minted and shared when the module is published.
1103        struct Registry has key {
1104            id: UID,
1105            next_account_id: u64
1106        }
1107
1108        /// Struct containing all the immutable info about a registered market
1109        struct MarketInfo<!phantom T> has store {
1110            base_pfs_id: ID,
1111            base_pfs_source_id: ID,
1112            collateral_pfs_id: ID,
1113            collateral_pfs_source_id: ID,
1114            scaling_factor: IFixed
1115        }
1116
1117        /// Struct containing all the immutable info about the collateral
1118        /// used in one or more markets
1119        struct CollateralInfo<!phantom T> has store {
1120            collateral_pfs_id: ID,
1121            collateral_pfs_source_id: ID,
1122            scaling_factor: IFixed
1123        }
1124
1125        /// Config that stores useful info for the protocol
1126        struct Config has store {
1127            stop_order_mist_cost: u64,
1128        }
1129    }
1130});
1131
1132impl<T: af_move_type::MoveType> clearing_house::ClearingHouse<T> {
1133    /// Convenience function to build the type of a [`PositionDf`].
1134    pub fn position_df_type(package: Address) -> FieldTypeTag<self::keys::Position, Position> {
1135        Field::type_(
1136            self::keys::Position::type_(package),
1137            Position::type_(package),
1138        )
1139    }
1140
1141    /// Convenience function to build the type of an [`OrderbookDofWrapper`].
1142    pub fn orderbook_dof_wrapper_type(
1143        package: Address,
1144    ) -> FieldTypeTag<Wrapper<keys::Orderbook>, ID> {
1145        Field::type_(
1146            Wrapper::type_(keys::Orderbook::type_(package)),
1147            ID::type_(SUI_FRAMEWORK_ADDRESS, IdentStr::cast("object").to_owned()),
1148        )
1149    }
1150
1151    /// The ID of the package that governs this clearing house's logic.
1152    ///
1153    /// This may be different than the package defining the clearing house's type because a package
1154    /// upgrade + `interface::upgrade_clearing_house_version` call can change
1155    /// [`ClearingHouse::version`] so that the upgraded package is the one that is allowed to make
1156    /// changes to it.
1157    ///
1158    /// Attempting to make a PTB Move call that mutates this clearing house but is not defined in
1159    /// this package version will fail.
1160    pub const fn governing_package_testnet(&self) -> Address {
1161        // NOTE: we published the most recent testnet contracts starting with `VERSION = 1`
1162        TESTNET_PACKAGE_VERSIONS[self.version as usize - 1]
1163    }
1164}
1165
1166impl self::orderbook::Orderbook {
1167    /// Convenience function to build the type of an [`AsksMapDofWrapper`].
1168    pub fn asks_dof_wrapper_type(package: Address) -> FieldTypeTag<Wrapper<keys::AsksMap>, ID> {
1169        Field::type_(
1170            Wrapper::type_(keys::AsksMap::type_(package)),
1171            ID::type_(SUI_FRAMEWORK_ADDRESS, IdentStr::cast("object").to_owned()),
1172        )
1173    }
1174
1175    /// Convenience function to build the type of an [`BidsMapDofWrapper`].
1176    pub fn bids_dof_wrapper_type(package: Address) -> FieldTypeTag<Wrapper<keys::BidsMap>, ID> {
1177        Field::type_(
1178            Wrapper::type_(keys::BidsMap::type_(package)),
1179            ID::type_(SUI_FRAMEWORK_ADDRESS, IdentStr::cast("object").to_owned()),
1180        )
1181    }
1182}
1183
1184impl self::ordered_map::Map<Order> {
1185    /// Convenience function to build the type of an [`OrderLeafDf`].
1186    pub fn leaf_df_type(package: Address) -> FieldTypeTag<u64, self::ordered_map::Leaf<Order>> {
1187        Field::type_(
1188            af_move_type::U64TypeTag,
1189            self::ordered_map::Leaf::type_(package, Order::type_(package)),
1190        )
1191    }
1192}
1193
1194#[cfg(test)]
1195mod tests {
1196    /// Taken from
1197    /// <https://github.com/cargo-public-api/cargo-public-api?tab=readme-ov-file#-as-a-ci-check>
1198    #[test]
1199    fn public_api() {
1200        // Install a compatible nightly toolchain if it is missing
1201        rustup_toolchain::install(public_api::MINIMUM_NIGHTLY_RUST_VERSION).unwrap();
1202
1203        // Build rustdoc JSON
1204        let rustdoc_json = rustdoc_json::Builder::default()
1205            .toolchain(public_api::MINIMUM_NIGHTLY_RUST_VERSION)
1206            .build()
1207            .unwrap();
1208
1209        // Derive the public API from the rustdoc JSON
1210        let public_api = public_api::Builder::from_rustdoc_json(rustdoc_json)
1211            .omit_blanket_impls(true)
1212            .omit_auto_trait_impls(true)
1213            .omit_auto_derived_impls(true)
1214            .build()
1215            .unwrap();
1216
1217        // Assert that the public API looks correct
1218        insta::assert_snapshot!(public_api);
1219    }
1220}