Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into misc-sips
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeal-eth committed Aug 7, 2023
2 parents afc116e + 7e175fe commit 4a02874
Show file tree
Hide file tree
Showing 85 changed files with 5,232 additions and 1,410 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions markets/perps-market/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ depends = ['invoke.upgrade_proxy']
[contract.MockPyth]
artifact = "contracts/mocks/MockPyth.sol:MockPyth"
args = [100, 1]

[contract.FeeCollectorMock]
artifact = "contracts/mocks/FeeCollectorMock.sol:FeeCollectorMock"
3 changes: 3 additions & 0 deletions markets/perps-market/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,6 @@ target = ["PerpsMarketProxy"]
func = "initializeFactory"
from = "<%= settings.owner %>"
depends = ['invoke.setSynthetix']

extra.superMarketId.event = "FactoryInitialized"
extra.superMarketId.arg = 0
38 changes: 12 additions & 26 deletions markets/perps-market/contracts/interfaces/IAsyncOrderModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ interface IAsyncOrderModule {
address sender
);

/**
* @notice Gets fired when a new order is canceled.
* @param marketId Id of the market used for the trade.
* @param accountId Id of the account used for the trade.
* @param acceptablePrice maximum or minimum, depending on the sizeDelta direction, accepted price to settle the order, set by the user.
* @param settlementTime Time at which the order can be settled.
*/

event OrderCanceled(
uint128 indexed marketId,
uint128 indexed accountId,
uint256 settlementTime,
uint256 acceptablePrice
);

/**
* @notice Commit an async order via this function
* @param commitment Order commitment data (see AsyncOrder.OrderCommitmentRequest struct).
Expand All @@ -57,21 +42,22 @@ interface IAsyncOrderModule {
AsyncOrder.OrderCommitmentRequest memory commitment
) external returns (AsyncOrder.Data memory retOrder, uint fees);

/**
* @notice Cancel an expired order via this function
* @param marketId Id of the market used for the trade.
* @param accountId Id of the account used for the trade.
*/
function cancelOrder(uint128 marketId, uint128 accountId) external;

/**
* @notice Get async order claim details
* @param accountId id of the account.
* @param marketId Id of the market used for the trade.
* @return order async order claim details (see AsyncOrder.Data struct).
*/
function getOrder(
function getOrder(uint128 accountId) external returns (AsyncOrder.Data memory order);

/**
* @notice Simulates what the order fee would be for the given market with the specified size.
* @dev Note that this does not include the settlement reward fee, which is based on the strategy type used
* @param marketId id of the market.
* @param sizeDelta size of position.
* @return orderFees incurred fees.
*/
function computeOrderFees(
uint128 marketId,
uint128 accountId
) external returns (AsyncOrder.Data memory order);
int128 sizeDelta
) external view returns (uint256 orderFees);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;
import {SettlementStrategy} from "../storage/SettlementStrategy.sol";
import {PerpsMarketFactory} from "../storage/PerpsMarketFactory.sol";
import {Position} from "../storage/Position.sol";
import {PerpsMarket} from "../storage/PerpsMarket.sol";

interface IAsyncOrderSettlementModule {
/**
Expand All @@ -26,7 +27,9 @@ interface IAsyncOrderSettlementModule {
* @param fillPrice Price at which the order was settled.
* @param sizeDelta Size delta from order.
* @param newSize New size of the position after settlement.
* @param collectedFees Amount of fees collected by the protocol.
* @param totalFees Amount of fees collected by the protocol.
* @param referralFees Amount of fees collected by the referrer.
* @param collectedFees Amount of fees collected by fee collector.
* @param settlementReward Amount of fees collected by the settler.
* @param trackingCode Optional code for integrator tracking purposes.
* @param settler address of the settler of the order.
Expand All @@ -38,6 +41,8 @@ interface IAsyncOrderSettlementModule {
int256 pnl,
int128 sizeDelta,
int128 newSize,
uint256 totalFees,
uint256 referralFees,
uint256 collectedFees,
uint256 settlementReward,
bytes32 indexed trackingCode,
Expand All @@ -54,15 +59,19 @@ interface IAsyncOrderSettlementModule {
uint256 pnlUint;
uint256 amountToDeduct;
uint256 settlementReward;
PerpsMarketFactory.Data factory;
uint256 fillPrice;
uint256 totalFees;
uint256 referralFees;
uint256 feeCollectorFees;
Position.Data newPosition;
PerpsMarket.MarketUpdateData updateData;
}

/**
* @notice Settles an offchain order. It's expected to revert with the OffchainLookup error with the data needed to perform the offchain lookup.
* @param marketId Id of the market used for the trade.
* @param accountId Id of the account used for the trade.
*/
function settle(uint128 marketId, uint128 accountId) external view;
function settle(uint128 accountId) external view;

/**
* @notice Settles an offchain order using the offchain retrieved data from pyth.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ interface IGlobalPerpsMarketModule {
uint256 indexed maxLiquidationRewardUsd
);

/**
* @notice emitted when custom fee collector is set
* @param feeCollector the address of the fee collector to set.
*/
event FeeCollectorSet(address feeCollector);

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

/**
* @notice Thrown when the fee collector does not implement the IFeeCollector interface
*/
error InvalidFeeCollectorInterface(address invalidFeeCollector);

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

/**
* @notice Sets the max collateral amount for a specific synth market.
* @param synthMarketId Synth market id, 0 for snxUSD.
Expand Down Expand Up @@ -75,4 +98,37 @@ interface IGlobalPerpsMarketModule {
external
view
returns (uint256 minLiquidationRewardUsd, uint256 maxLiquidationRewardUsd);

/**
* @notice Gets the total collateral value of all deposited collateral from all traders.
* @return totalCollateralValue value of all collateral
*/
function totalGlobalCollateralValue() external view returns (uint256 totalCollateralValue);

/**
* @notice Sets the fee collector contract.
* @dev must conform to the IFeeCollector interface
* @param feeCollector address of the fee collector contract
*/
function setFeeCollector(address feeCollector) external;

/**
* @notice Gets the configured feeCollector contract
* @return feeCollector address of the fee collector contract
*/
function getFeeCollector() external view returns (address feeCollector);

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

/**
* @notice get the referral share percentage for the specified referrer
* @param referrer The address of the referrer
* @return shareRatioD18 The configured share percentage for the referrer
*/
function getReferrerShare(address referrer) external returns (uint256 shareRatioD18);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ pragma solidity >=0.8.11 <0.9.0;
interface ILiquidationModule {
error NotEligibleForLiquidation(uint128 accountId);

event PositionLiquidated(
uint128 indexed accountId,
uint128 indexed marketId,
uint256 amountLiquidated,
int128 currentPositionSize
);

event AccountLiquidated(uint128 indexed accountId, uint256 reward, bool fullLiquidation);

function liquidate(uint128 accountId) external;

function liquidateFlagged() external;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pragma solidity >=0.8.11 <0.9.0;

import {SettlementStrategy} from "../storage/SettlementStrategy.sol";
import {PerpsMarketConfiguration} from "../storage/PerpsMarketConfiguration.sol";
import {OrderFee} from "../storage/OrderFee.sol";

/**
* @title Module for updating configuration in relation to async order modules.
Expand Down Expand Up @@ -62,6 +60,7 @@ interface IMarketConfigurationModule {
uint128 indexed marketId,
uint256 initialMarginRatioD18,
uint256 maintenanceMarginRatioD18,
uint256 minimumInitialMarginRatioD18,
uint256 liquidationRewardRatioD18,
uint256 maxLiquidationLimitAccumulationMultiplier,
uint256 maxSecondsInLiquidationWindow,
Expand All @@ -80,7 +79,7 @@ interface IMarketConfigurationModule {
* @param marketId udpates funding parameters to this specific market.
* @param lockedOiRatioD18 the locked OI ratio skew scale (as decimal with 18 digits precision).
*/
event LockedOiRatioD18Set(uint128 indexed marketId, uint256 lockedOiRatioD18);
event LockedOiRatioSet(uint128 indexed marketId, uint256 lockedOiRatioD18);

/**
* @notice Gets fired when a settlement strategy is enabled or disabled.
Expand Down Expand Up @@ -132,7 +131,8 @@ interface IMarketConfigurationModule {
* @notice Set liquidation parameters for a market with this function.
* @param marketId id of the market to set liquidation parameters.
* @param initialMarginRatioD18 the initial margin ratio (as decimal with 18 digits precision).
* @param maintenanceMarginRatioD18 the maintenance margin ratio (as decimal with 18 digits precision).
* @param minimumInitialMarginRatioD18 the minimum initial margin ratio (as decimal with 18 digits precision).
* @param maintenanceMarginScalarD18 the maintenance margin scalar relative to the initial margin ratio (as decimal with 18 digits precision).
* @param liquidationRewardRatioD18 the liquidation reward ratio (as decimal with 18 digits precision).
* @param maxLiquidationLimitAccumulationMultiplier the max liquidation limit accumulation multiplier.
* @param maxSecondsInLiquidationWindow the max seconds in liquidation window (used together with the acc multiplier to get max liquidation per window).
Expand All @@ -141,7 +141,8 @@ interface IMarketConfigurationModule {
function setLiquidationParameters(
uint128 marketId,
uint256 initialMarginRatioD18,
uint256 maintenanceMarginRatioD18,
uint256 minimumInitialMarginRatioD18,
uint256 maintenanceMarginScalarD18,
uint256 liquidationRewardRatioD18,
uint256 maxLiquidationLimitAccumulationMultiplier,
uint256 maxSecondsInLiquidationWindow,
Expand Down Expand Up @@ -190,10 +191,12 @@ interface IMarketConfigurationModule {
* @notice Gets liquidation parameters details of a market.
* @param marketId id of the market.
* @return initialMarginRatioD18 the initial margin ratio (as decimal with 18 digits precision).
* @return maintenanceMarginRatioD18 the maintenance margin ratio (as decimal with 18 digits precision).
* @return minimumInitialMarginRatioD18 the minimum initial margin ratio (as decimal with 18 digits precision).
* @return maintenanceMarginScalarD18 the maintenance margin scalar relative to the initial margin ratio (as decimal with 18 digits precision).
* @return liquidationRewardRatioD18 the liquidation reward ratio (as decimal with 18 digits precision).
* @return maxLiquidationLimitAccumulationMultiplier the max liquidation limit accumulation multiplier.
* @return maxSecondsInLiquidationWindow the max seconds in liquidation window (used together with the acc multiplier to get max liquidation per window).
* @return minimumPositionMargin the minimum position margin.
*/
function getLiquidationParameters(
uint128 marketId
Expand All @@ -202,10 +205,12 @@ interface IMarketConfigurationModule {
view
returns (
uint256 initialMarginRatioD18,
uint256 maintenanceMarginRatioD18,
uint256 minimumInitialMarginRatioD18,
uint256 maintenanceMarginScalarD18,
uint256 liquidationRewardRatioD18,
uint256 maxLiquidationLimitAccumulationMultiplier,
uint256 maxSecondsInLiquidationWindow
uint256 maxSecondsInLiquidationWindow,
uint256 minimumPositionMargin
);

/**
Expand Down Expand Up @@ -240,5 +245,5 @@ interface IMarketConfigurationModule {
* @param marketId id of the market.
* @return lockedOiRatioD18 the locked OI ratio skew scale (as decimal with 18 digits precision).
*/
function getLockedOiRatioD18(uint128 marketId) external view returns (uint256 lockedOiRatioD18);
function getLockedOiRatio(uint128 marketId) external view returns (uint256 lockedOiRatioD18);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

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

/**
* @title Account module
*/
interface IAccountModule {
interface IPerpsAccountModule {
/**
* @notice Gets fired when an account colateral is modified.
* @param accountId Id of the account.
Expand Down Expand Up @@ -63,19 +61,38 @@ interface IAccountModule {
* @notice Gets the details of an open position.
* @param accountId Id of the account.
* @param marketId Id of the position market.
* @return pnl pnl of the position.
* @return totalPnl pnl of the entire position including funding.
* @return accruedFunding accrued funding of the position.
* @return size size of the position.
* @return positionSize size of the position.
*/
function getOpenPosition(
uint128 accountId,
uint128 marketId
) external view returns (int pnl, int accruedFunding, int size);
) external view returns (int256 totalPnl, int256 accruedFunding, int128 positionSize);

/**
* @notice Gets the available margin of an account. It can be negative due to pnl.
* @param accountId Id of the account.
* @return availableMargin available margin of the position.
*/
function getAvailableMargin(uint128 accountId) external view returns (int);
function getAvailableMargin(uint128 accountId) external view returns (int256 availableMargin);

/**
* @notice Gets the exact withdrawable amount a trader has available from this account while holding the account's current positions.
* @param accountId Id of the account.
* @return withdrawableMargin available margin to withdraw.
*/
function getWithdrawableMargin(
uint128 accountId
) external view returns (int256 withdrawableMargin);

/**
* @notice Gets the initial/maintenance margins across all positions that an account has open.
* @param accountId Id of the account.
* @return requiredInitialMargin initial margin req (used when withdrawing collateral).
* @return requiredMaintenanceMargin maintenance margin req (used to determine liquidation threshold).
*/
function getRequiredMargins(
uint128 accountId
) external view returns (uint256 requiredInitialMargin, uint256 requiredMaintenanceMargin);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

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

/**
* @title Perps market module
*/
Expand Down Expand Up @@ -32,7 +30,7 @@ interface IPerpsMarketModule {

function indexPrice(uint128 marketId) external view returns (uint);

function fillPrice(uint128 marketId, int orderSize, uint price) external returns (uint);
function fillPrice(uint128 marketId, int128 orderSize, uint price) external returns (uint);

/**
* @dev Given a marketId return a market's summary details in one call.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import "@synthetixio/core-contracts/contracts/interfaces/IERC165.sol";

interface IFeeCollector is IERC165 {
/**
* @notice .This function is called by the spot market proxy to get the fee amount to be collected.
* @dev .The quoted fee amount is then transferred directly to the fee collector.
* @param marketId .synth market id value
* @param feeAmount .max fee amount that can be collected
* @param transactor .the trader the fee was collected from
* @return feeAmountToCollect .quoted fee amount
*/
function quoteFees(
uint128 marketId,
uint256 feeAmount,
address transactor
) external returns (uint256 feeAmountToCollect);
}
Loading

0 comments on commit 4a02874

Please sign in to comment.