Skip to content

SIP-80 Futures#1450

Merged
jacko125 merged 263 commits intodevelopfrom
futures-implementation
Mar 9, 2022
Merged

SIP-80 Futures#1450
jacko125 merged 263 commits intodevelopfrom
futures-implementation

Conversation

@zyzek
Copy link
Copy Markdown
Contributor

@zyzek zyzek commented Aug 6, 2021

Implements the futures contracts.

There are several major new smart contracts to consider:

  • FuturesMarket.sol: the actual futures market itself. One of these will exist per asset, and each operates behind a proxy so that they are upgradeable.
  • FuturesMarketSettings.sol: an interface to the same FlexibleStorage contract as SystemSettings. This provides setters and getters for all the specific settings for each market, such as fee rates and capital controls, along with for cross-market settings such as the liquidation fee and minimum initial margin. This is accompanied by MixinFuturesMarketSettings.sol, which allows futures markets themselves to access the flexible storage.
  • FuturesMarketManager.sol: This is responsible for managing which markets exist, as well as issuing and burning sUSD on behalf of the markets (including paying fees to the pool). It is also responsible for aggregating the debt of all futures markets, and reporting this aggregate to the debt cache.
  • FuturesMarketData.sol: A data contract which accumulates all relevant data from individual markets or futures positions, so that they can be reported to the front-end in a single transaction.

The futures markets make extensive use of signed arithmetic, which represent long and short positions with positive and negative values respectively. To facilitate this, the SafeMath and SafeDecimalMath libraries have been extended to support signed values, in the form of SignedSafeMath.sol and SignedSafeDecimalMath.sol. Although OpenZeppelin provides a signed safe math library, it does so only in versions later than that which Synthetix actually imports.

A user wishing to interact with a futures market follows these steps:

  1. Deposits margin with transferMargin. This function is used for both deposits and withdrawals, depending on the sign of the argument.
  2. Submits an order with modifyPosition, providing the desired change in their position size. The sign of the argument determines whether they are going more long or more short. Positive inputs buy long or sell short, while negative inputs buy short or sell long. To close an existing position, a user must bring their position size to zero by providing -position.size as the argument, or using the closePosition function.

Orders are immediately confirmed at the spot price. A position is subject to profit and funding as per the calculations in SIP-80. Liquidation are performed by keepers, though they could potentially be handled by any user. If an account a has exhausted its margin, the position can be liquidated, and the predicate canLiquidate(a) returns true. When a keeper detects this, it executes liquidatePosition(a).

@artdgn artdgn force-pushed the futures-implementation branch 2 times, most recently from 837ca7a to 82e22cd Compare March 3, 2022 05:07
@artdgn artdgn force-pushed the futures-implementation branch from 82e22cd to 7caa059 Compare March 3, 2022 05:30
@artdgn artdgn force-pushed the futures-implementation branch from ae33449 to 527ef56 Compare March 9, 2022 02:43
@artdgn artdgn force-pushed the futures-implementation branch from f6275e9 to c7157b1 Compare March 9, 2022 02:48
@artdgn artdgn requested a review from jacko125 March 9, 2022 02:51
@jacko125 jacko125 merged commit f50d15a into develop Mar 9, 2022
@artdgn artdgn deleted the futures-implementation branch August 31, 2022 02:34
@artdgn artdgn restored the futures-implementation branch August 31, 2022 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants