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

[WIP] Sip 395 - orderbooks #2226

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions markets/perps-market/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ artifact = "LiquidationModule"
[contract.CollateralConfigurationModule]
artifact = "CollateralConfigurationModule"

[contract.LimitOrderModule]
artifact = "LimitOrderModule"

[contract.MarketConfigurationModule]
artifact = "MarketConfigurationModule"

Expand Down Expand Up @@ -83,6 +86,7 @@ contracts = [
"MarketConfigurationModule",
"CollateralConfigurationModule",
"GlobalPerpsMarketModule",
"LimitOrderModule",
]

[invoke.upgrade_proxy]
Expand Down Expand Up @@ -132,6 +136,12 @@ func = "setFeatureFlagAllowAll"
from = "<%= settings.owner %>"
args = ["<%= formatBytes32String('perpsSystem') %>", true]

[invoke.addLimitOrderToFeatureFlag]
target = ["PerpsMarketProxy"]
func = "setFeatureFlagAllowAll"
from = "<%= settings.owner %>"
args = ["<%= formatBytes32String('limitOrder') %>", true]

[contract.MockPythERC7412Wrapper]
artifact = "contracts/mocks/MockPythERC7412Wrapper.sol:MockPythERC7412Wrapper"

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 @@ -60,6 +60,9 @@ artifact = "CollateralConfigurationModule"
[contract.MarketConfigurationModule]
artifact = "MarketConfigurationModule"

[contract.LimitOrderModule]
artifact = "LimitOrderModule"

[contract.FeatureFlagModule]
artifact = "contracts/modules/FeatureFlagModule.sol:FeatureFlagModule"

Expand Down Expand Up @@ -92,6 +95,7 @@ contracts = [
"MarketConfigurationModule",
"CollateralConfigurationModule",
"GlobalPerpsMarketModule",
"LimitOrderModule",
]

[invoke.upgrade_proxy]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ interface IGlobalPerpsMarketModule {
*/
event ReferrerShareUpdated(address referrer, uint256 shareRatioD18);

/**
* @notice Emitted when the share percentage for a relayer address has been updated.
* @param relayer The address of the relayer
* @param shareRatioD18 The new share ratio for the relayer
*/
event RelayerShareUpdated(address relayer, uint256 shareRatioD18);

/**
* @notice Emitted when interest rate parameters are set
* @param lowUtilizationInterestRateGradient interest rate gradient applied to utilization prior to hitting the gradient breakpoint
Expand Down Expand Up @@ -74,6 +81,11 @@ interface IGlobalPerpsMarketModule {
*/
error InvalidReferrerShareRatio(uint256 shareRatioD18);

/**
* @notice Thrown when a relayer share gets set to larger than 100%
*/
error InvalidRelayerShareRatio(uint256 shareRatioD18);

/**
* @notice Thrown when gradient breakpoint is lower than low gradient or higher than high gradient
*/
Expand Down Expand Up @@ -236,4 +248,18 @@ interface IGlobalPerpsMarketModule {
* @dev InterestRateUpdated event is emitted
*/
function updateInterestRate() external;

/**
* @notice Update the referral share percentage for a relayer
* @param relayer The address of the relayer
* @param shareRatioD18 The new share percentage for the relayer
*/
function updateRelayerShare(address relayer, uint256 shareRatioD18) external;

/**
* @notice get the referral share percentage for the specified relayer
* @param relayer The address of the relayer
* @return shareRatioD18 The configured share percentage for the relayer
*/
function getRelayerShare(address relayer) external view returns (uint256 shareRatioD18);
}
145 changes: 145 additions & 0 deletions markets/perps-market/contracts/interfaces/ILimitOrderModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {LimitOrder} from "../storage/LimitOrder.sol";

/**
* @title limit order module
*/
interface ILimitOrderModule {
/**
* @notice cancels a limit order nonce for an account and prevents it from being called
* @param accountId id of the account used for the limit order
* @param limitOrderNonce limit order nonce to cancel
* @param price limit order nonce to cancel
* @param amount limit order nonce to cancel
*/
event LimitOrderCancelled(
uint128 indexed accountId,
uint256 limitOrderNonce,
uint256 price,
int256 amount
);

/**
* @notice Gets fired when a new limit order is settled.
* @param marketId Id of the market used for the trade.
* @param accountId Id of the account used for the trade.
* @param price Price at which the limit order was settled.
* @param pnl Pnl of the previous closed position.
* @param accruedFunding Accrued funding of the previous closed position.
* @param amount directional size of the limit order.
* @param newSize New size of the position after settlement.
* @param limitOrderFees Amount of fees collected by the protocol and relayer combined.
* @param relayerFees Amount of fees collected by the relayer.
* @param collectedFees Amount of fees collected by fee collector.
* @param trackingCode Optional code for integrator tracking purposes.
* @param interest interest charges
*/
event LimitOrderSettled(
uint128 indexed marketId,
uint128 indexed accountId,
uint256 price,
int256 pnl,
int256 accruedFunding,
int128 amount,
int128 newSize,
uint256 limitOrderFees,
uint256 relayerFees,
uint256 collectedFees,
bytes32 indexed trackingCode,
uint256 interest
);

/**
* @notice thrown when a limit order that is attempted to be cancelled has already been used
* @param accountId id of the account used for the limit order
* @param limitOrderNonce limit order nonce to cancel
* @param price limit order nonce to cancel
* @param amount limit order nonce to cancel
*/
error LimitOrderAlreadyUsed(
uint128 accountId,
uint256 limitOrderNonce,
uint256 price,
int256 amount
);

/**
* @notice Thrown when attempting to use two makers or two takers
* @param shortIsMaker is the short a maker?
* @param longIsMaker is the long a maker?
*/
error MismatchingMakerTakerLimitOrder(bool shortIsMaker, bool longIsMaker);

/**
* @notice Thrown when attempting to use an invalid relayer
* @param relayer address of the relayer submitted with a limit order
*/
error LimitOrderRelayerInvalid(address relayer);

/**
* @notice Thrown when attempting to use two different relayers
*/
error LimitOrderDifferentRelayer(address shortRelayer, address longRelayer);

/**
* @notice Thrown when attempting to use two different markets
*/
error LimitOrderMarketMismatch(uint256 shortMarketId, uint256 longMarketId);

/**
* @notice Thrown when attempting to use an expired limit order on either side
*/
error LimitOrderExpired(
uint128 shortAccountId,
uint256 shortExpiration,
uint128 longAccountId,
uint256 longExpiration,
uint256 blockTimetamp
);

/**
* @notice Thrown when attempting to use two different amounts
*/
error LimitOrderAmountError(int256 shortAmount, int256 longAmount);

/**
* @notice cancels a limit order with a nonce from being called for an account
* @param order the order to cancel
* @param sig the order signature
*/
function cancelLimitOrder(
LimitOrder.SignedOrderRequest calldata order,
LimitOrder.Signature calldata sig
) external;

/**
* @notice gets the fees for a limit order
* @param marketId the id for the market
* @param amount the amount for the order
* @param price the price of the order
* @param isMaker a boolean to get the fee for a taker vs maker
* @return limitOrderFees the fees for the limit order
*/
function getLimitOrderFees(
uint128 marketId,
int128 amount,
uint256 price,
bool isMaker
) external view returns (uint256);

/**
* @notice Settles long and short limit orders of matching amounts submitted by a valid relayer
* @param longOrder a limit order going long on a given market
* @param longSignature a signature used to validate the long market ordert
* @param shortOrder a limit order going short on a given market
* @param shortSignature a signature used to validate the short market ordert
*/
function settleLimitOrder(
LimitOrder.SignedOrderRequest calldata shortOrder,
LimitOrder.Signature calldata shortSignature,
LimitOrder.SignedOrderRequest calldata longOrder,
LimitOrder.Signature calldata longSignature
) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ interface IMarketConfigurationModule {
*/
event OrderFeesSet(uint128 indexed marketId, uint256 makerFeeRatio, uint256 takerFeeRatio);

/**
* @notice Gets fired when limit order fees are updated.
* @param marketId udpates fees to this specific market.
* @param limitOrderMakerFeeRatio the limit order maker fee ratio.
* @param limitOrderTakerFeeRatio the limit order taker fee ratio.
*/
event LimitOrderFeesSet(
uint128 indexed marketId,
uint256 limitOrderMakerFeeRatio,
uint256 limitOrderTakerFeeRatio
);

/**
* @notice Gets fired when funding parameters are updated.
* @param marketId udpates funding parameters to this specific market.
Expand Down Expand Up @@ -151,6 +163,18 @@ interface IMarketConfigurationModule {
*/
function setOrderFees(uint128 marketId, uint256 makerFeeRatio, uint256 takerFeeRatio) external;

/**
* @notice Set limit order fees for a market with this function.
* @param marketId id of the market to set limit order fees.
* @param limitOrderMakerFeeRatio the limit order maker fee ratio.
* @param limitOrderTakerFeeRatio the limit order taker fee ratio.
*/
function setLimitOrderFees(
uint128 marketId,
uint256 limitOrderMakerFeeRatio,
uint256 limitOrderTakerFeeRatio
) external;

/**
* @notice Set node id for perps market
* @param perpsMarketId id of the market to set price feed.
Expand Down Expand Up @@ -330,6 +354,16 @@ interface IMarketConfigurationModule {
uint128 marketId
) external view returns (uint256 makerFeeRatio, uint256 takerFeeRatio);

/**
* @notice Gets the limit order fees of a market.
* @param marketId id of the market.
* @return limitOrderMakerFeeRatio the limit order maker fee ratio.
* @return limitOrderTakerFeeRatio the limit order taker fee ratio.
*/
function getLimitOrderFees(
uint128 marketId
) external view returns (uint256 limitOrderMakerFeeRatio, uint256 limitOrderTakerFeeRatio);

/**
* @notice Gets the locked OI ratio of a market.
* @param marketId id of the market.
Expand Down
28 changes: 28 additions & 0 deletions markets/perps-market/contracts/modules/GlobalPerpsMarketModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,32 @@ contract GlobalPerpsMarketModule is IGlobalPerpsMarketModule {

emit InterestRateUpdated(PerpsMarketFactory.load().perpsMarketId, interestRate);
}

/**
* @inheritdoc IGlobalPerpsMarketModule
*/
function updateRelayerShare(address relayer, uint256 shareRatioD18) external override {
OwnableStorage.onlyOwner();

if (shareRatioD18 > DecimalMath.UNIT) {
revert InvalidRelayerShareRatio(shareRatioD18);
}

if (relayer == address(0)) {
revert AddressError.ZeroAddress();
}

GlobalPerpsMarketConfiguration.load().relayerShare[relayer] = shareRatioD18;

emit RelayerShareUpdated(relayer, shareRatioD18);
}

/**
* @inheritdoc IGlobalPerpsMarketModule
*/
function getRelayerShare(
address relayer
) external view override returns (uint256 shareRatioD18) {
return GlobalPerpsMarketConfiguration.load().relayerShare[relayer];
}
}
Loading