Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perps orderbook #2356

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions markets/perps-market/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ defaultValue = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
defaultValue = ""

[setting.synthetixPackage]
defaultValue = "synthetix:<%= package.version %>-testable"
defaultValue = "synthetix:3.10.2-testable"
depends = []

[setting.spotMarketPackage]
defaultValue = "synthetix-spot-market:<%= package.version %>-testable"
defaultValue = "synthetix-spot-market:3.10.2-testable"
depends = []

[import.synthetix]
Expand All @@ -33,6 +33,9 @@ artifact = "PerpsMarketFactoryModule"
[contract.AsyncOrderModule]
artifact = "AsyncOrderModule"

[contract.BookOrderModule]
artifact = "BookOrderModule"

[contract.AsyncOrderSettlementPythModule]
artifact = "AsyncOrderSettlementPythModule"

Expand Down Expand Up @@ -79,6 +82,7 @@ contracts = [
"PerpsAccountModule",
"PerpsMarketModule",
"AsyncOrderModule",
"BookOrderModule",
"AsyncOrderSettlementPythModule",
"AsyncOrderCancelModule",
"FeatureFlagModule",
Expand Down
4 changes: 4 additions & 0 deletions markets/perps-market/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ artifact = "PerpsMarketFactoryModule"
[contract.AsyncOrderModule]
artifact = "AsyncOrderModule"

[contract.BookOrderModule]
artifact = "BookOrderModule"

[contract.AsyncOrderSettlementPythModule]
artifact = "AsyncOrderSettlementPythModule"

Expand Down Expand Up @@ -85,6 +88,7 @@ contracts = [
"PerpsAccountModule",
"PerpsMarketModule",
"AsyncOrderModule",
"BookOrderModule",
"AsyncOrderSettlementPythModule",
"AsyncOrderCancelModule",
"FeatureFlagModule",
Expand Down
88 changes: 88 additions & 0 deletions markets/perps-market/contracts/interfaces/IBookOrderModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {AsyncOrder} from "../storage/AsyncOrder.sol";
import {SettlementStrategy} from "../storage/SettlementStrategy.sol";

/**
* @title Module for processing orders from the offchain orderbook
*/
interface IBookOrderModule {
/**
* @notice An order being settled by the orderbook.
*/
struct BookOrder {
/**
* @dev Order account id.
*/
uint128 accountId;
/**
* @dev Order size delta (of asset units expressed in decimal 18 digits). It can be positive or negative.
*/
int128 sizeDelta;
/**
* @dev The price that should be used to fill the order
*/
uint256 orderPrice;
/**
* @dev The price that should be used for this order.
* It should be signed by a trusted price provider for the perps market.
* This field is optional and can be 0x. If this is the case, the next order(s) must be of oposite magnitude to match this order.
*/
bytes signedPriceData;
/**
* @dev An optional code provided by frontends to assist with tracking the source of volume and fees.
*/
bytes32 trackingCode;
}

event BookOrderSettled(
uint128 indexed marketId,
BookOrder[] orders,
uint256 totalCollectedFees
);

/**
* @notice Indicates a summary as to the operation state of a subbmitted order for settlement
*/
enum OrderStatus {
FILLED,
CANCELLED
}

/**
* @notice Returned by `settleBookOrders` to indicate the result of a submitted order for settlement
*/
struct BookOrderSettleStatus {
/**
* @dev The result of the order
*/
OrderStatus status;
}

/**
* @notice Set the current order mode to BOOK
* @param accountId the account id to set to BOOK
* @param useBook whether or not to set hte mode to BOOK. If not BOOK, it will be ONCHAIN
*/
function setBookMode(uint128 accountId, bool useBook) external;

/**
* @notice Get the current order mode
* @param accountId the account id to pull data for
* @return the current order mode
*/
function getOrderMode(uint128 accountId) external view returns (bytes16);

/**
* @notice Called by the offchain orderbook to settle a prevoiusly placed order onchain. Any orders submitted to this function will be processed as if they happened simultaneously, at the prices given by the orderbook.
* If an order is found to be unfillable (ex. insufficient account liquidity), it will be returned in the `statuses` return field.
* @param marketId the market for which all of the following orders should be operated on
* @param orders the list of orders to settle
* @return statuses the result of the `orders` supplied to this function.
*/
function settleBookOrders(
uint128 marketId,
BookOrder[] memory orders
) external returns (BookOrderSettleStatus[] memory statuses);
}
36 changes: 36 additions & 0 deletions markets/perps-market/contracts/interfaces/IPerpsAccountModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ interface IPerpsAccountModule {
uint128 marketId
) external view returns (int128 positionSize);

/**
* @notice Position with additional fields returned by the `getAccountFullPositionInfo` function
*/
struct DetailedPosition {
uint128 marketId;
int256 size;
int256 pnl;
int256 accruedFunding;
uint256 chargedInterest;
uint256 currentPrice;
uint256 entryPrice;
uint256 requiredInitialMargin;
uint256 requiredMaintenanceMargin;
string marketName;
string marketSymbol;
}

/**
* @notice Returns detailed information about all the positions currently opened by an account.
* @param accountId Id of account to get positions for
*/
function getAccountFullPositionInfo(
uint128 accountId
) external view returns (DetailedPosition[] memory);

/**
* @notice Returns detailed information about all the collateral currently allocated for an account. It also returns debt.
* @param accountId Id of account to get collateral information for
*/
function getAccountAllCollateralAmounts(
uint128 accountId
)
external
view
returns (uint256[] memory collateralIds, uint256[] memory collateralAmounts, uint256 debt);

/**
* @notice Gets the available margin of an account. It can be negative due to pnl.
* @param accountId Id of the account.
Expand Down
2 changes: 1 addition & 1 deletion markets/perps-market/contracts/mocks/FeeCollectorMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract FeeCollectorMock is IFeeCollector {
uint128 marketId,
uint256 feeAmount,
address sender
) external override returns (uint256) {
) external view override returns (uint256) {
// mention the variables in the block to prevent unused local variable warning
marketId;
sender;
Expand Down
Loading