Trading Contract
Overview
The Trading
contract is a key component of wmx.fi, enabling users to engage in trading with leverage, manage positions, place and cancel orders and handle various trading operations.
Structs
- Product: Defines trading products with properties like max leverage, liquidation threshold, fee, and interest.
- Position: Represents a trading position with size, margin, timestamp, and price.
- Order: Details an order with flags for closing, size, and margin.
Contracts and Variables
- Owner: Contract owner's address.
- Router: Router contract address.
- Treasury: Treasury contract address.
- Oracle: Oracle contract address.
- Next Position ID: Incremental ID for positions.
- Next Close Order ID: Incremental ID for close orders.
- Products: Mapping of products.
- Positions: Mapping of positions.
- Orders: Mapping of orders.
- Min Margin: Minimum margin requirements.
- Pending Fees: Mapping of pending fees.
- UNIT_DECIMALS: Constant for unit decimals.
- UNIT: Standard unit.
- PRICE_DECIMALS: Constant for price decimals.
Events
- NewOrder: Emitted on new order submission.
- PositionUpdated: Emitted on position update.
- ClosePosition: Emitted on position close.
Constructor
- Initializes the contract with the owner's address.
Governance Methods
- setOwner: Changes the owner.
- setRouter: Sets the router and updates treasury and oracle.
- setMinMargin: Sets the minimum margin requirement.
- addProduct: Adds a new trading product.
- updateProduct: Updates an existing trading product.
Core Methods
- distributeFees: Distributes collected fees.
- submitOrder: Submits a new trading order.
- submitCloseOrder: Submits an order to close a position.
- cancelOrder: Cancels an existing order.
- settleOrder: Settles a submitted order.
- _settleCloseOrder: Internally handles the closing of an order.
- liquidatePosition: Liquidates a position.
- releaseMargin: Releases margin for a position.
Fallback and Receive Functions
- Handles receiving ETH directly to the contract.
Internal Methods
- _getPositionKey: Generates a key for positions.
- _updateOpenInterest: Updates open interest for a currency.
- _transferIn: Handles incoming transfers.
- _transferOut: Manages outgoing transfers.
- _validatePrice: Validates the price of a product.
- _getPnL: Calculates profit and loss.
Getters
- getProduct: Retrieves product details.
- getPosition: Fetches position details.
- getOrder: Obtains order details.
- getOrders: Retrieves multiple orders.
- getPositions: Fetches multiple positions.
- getPendingFee: Gets pending fees for a currency.
Modifiers
- onlyOracle: Ensures that only the oracle can invoke certain functions.
- onlyOwner: Restricts access to the owner.