Skip to content

Commit

Permalink
fix order pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
xdev10 committed May 8, 2023
1 parent 5f422e5 commit 3243138
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 332 deletions.
18 changes: 5 additions & 13 deletions contracts/error/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ library Errors {
error InvalidOraclePrice(address token);
error InvalidSignerMinMaxPrice(uint256 minPrice, uint256 maxPrice);
error InvalidMedianMinMaxPrice(uint256 minPrice, uint256 maxPrice);
error DuplicateTokenPrice(address token);
error NonEmptyTokensWithPrices(uint256 tokensWithPricesLength);
error EmptyPriceFeed(address token);
error PriceAlreadySet(address token, uint256 minPrice, uint256 maxPrice);
Expand All @@ -121,7 +122,6 @@ library Errors {

// OracleModule errors
error InvalidPrimaryPricesForSimulation(uint256 primaryTokensLength, uint256 primaryPricesLength);
error InvalidSecondaryPricesForSimulation(uint256 secondaryTokensLength, uint256 secondaryPricesLength);
error EndOfOracleSimulation();

// OracleUtils errors
Expand All @@ -131,9 +131,6 @@ library Errors {
error InvalidSignature(address recoveredSigner, address expectedSigner);

error EmptyPrimaryPrice(address token);
error EmptySecondaryPrice(address token);
error EmptyLatestPrice(address token);
error EmptyCustomPrice(address token);

error OracleBlockNumbersAreNotEqual(uint256[] oracleBlockNumbers, uint256 expectedBlockNumber);
error OracleBlockNumbersAreSmallerThanRequired(uint256[] oracleBlockNumbers, uint256 expectedBlockNumber);
Expand All @@ -146,16 +143,11 @@ library Errors {
// BaseOrderUtils errors
error EmptyOrder();
error UnsupportedOrderType();
error InvalidLimitOrderPrices(
uint256 primaryPrice,
error InvalidOrderPrices(
uint256 primaryPriceMin,
uint256 primaryPriceMax,
uint256 triggerPrice,
bool shouldValidateSmallerPrimaryPrice
);
error InvalidStopLossOrderPrices(
uint256 primaryPrice,
uint256 secondaryPrice,
uint256 triggerPrice,
bool shouldValidateAscendingPrice
uint256 orderType
);
error PriceImpactLargerThanOrderSize(int256 priceImpactUsdForPriceAdjustment, uint256 sizeDeltaUsd);
error OrderNotFulfillableDueToPriceImpact(uint256 price, uint256 acceptablePrice);
Expand Down
6 changes: 2 additions & 4 deletions contracts/exchange/OrderHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,9 @@ contract OrderHandler is BaseOrderHandler {
errorSelector == Errors.DisabledFeature.selector ||
errorSelector == Errors.InvalidKeeperForFrozenOrder.selector ||
errorSelector == Errors.UnsupportedOrderType.selector ||
// the transaction is reverted for InvalidLimitOrderPrices and
// InvalidStopLossOrderPrices errors since since the oracle prices
// the transaction is reverted for InvalidOrderPrices since the oracle prices
// do not fulfill the specified trigger price
errorSelector == Errors.InvalidLimitOrderPrices.selector ||
errorSelector == Errors.InvalidStopLossOrderPrices.selector
errorSelector == Errors.InvalidOrderPrices.selector
) {
ErrorUtils.revertWithCustomError(reasonBytes);
}
Expand Down
13 changes: 0 additions & 13 deletions contracts/market/MarketUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,6 @@ library MarketUtils {
revert Errors.UnableToGetCachedTokenPrice(token, market.marketToken);
}

// @dev return the latest prices for the market tokens
// the secondary price for market.indexToken is overwritten for certain order
// types, use this value instead of the primary price for positions
// @param oracle Oracle
// @param market the market values
function getMarketPricesForPosition(Oracle oracle, Market.Props memory market) internal view returns (MarketPrices memory) {
return MarketPrices(
oracle.getLatestPrice(market.indexToken),
oracle.getLatestPrice(market.longToken),
oracle.getLatestPrice(market.shortToken)
);
}

// @dev return the primary prices for the market tokens
// @param oracle Oracle
// @param market the market values
Expand Down
98 changes: 8 additions & 90 deletions contracts/oracle/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,7 @@ contract Oracle is RoleModule {
// this is used in clearAllPrices to help ensure that all token prices
// set in setPrices are cleared after use
EnumerableSet.AddressSet internal tokensWithPrices;
// prices for the same token can be sent multiple times in one txn
// the prices can be for different block numbers
// the first occurrence of the token's price will be stored in primaryPrices
// the second occurrence will be stored in secondaryPrices
mapping(address => Price.Props) public primaryPrices;
mapping(address => Price.Props) public secondaryPrices;
// customPrices can be used to store custom price values
// these prices will be cleared in clearAllPrices
mapping(address => Price.Props) public customPrices;

constructor(
RoleStore _roleStore,
Expand Down Expand Up @@ -253,28 +245,12 @@ contract Oracle is RoleModule {
primaryPrices[token] = price;
}

// @dev set the secondary price
// @param token the token to set the price for
// @param price the price value to set to
function setSecondaryPrice(address token, Price.Props memory price) external onlyController {
secondaryPrices[token] = price;
}

// @dev set a custom price
// @param token the token to set the price for
// @param price the price value to set to
function setCustomPrice(address token, Price.Props memory price) external onlyController {
customPrices[token] = price;
}

// @dev clear all prices
function clearAllPrices() external onlyController {
uint256 length = tokensWithPrices.length();
for (uint256 i; i < length; i++) {
address token = tokensWithPrices.at(0);
delete primaryPrices[token];
delete secondaryPrices[token];
delete customPrices[token];
tokensWithPrices.remove(token);
}
}
Expand Down Expand Up @@ -307,59 +283,6 @@ contract Oracle is RoleModule {
return price;
}

// @dev get the secondary price of a token
// @param token the token to get the price for
// @return the secondary price of a token
function getSecondaryPrice(address token) external view returns (Price.Props memory) {
if (token == address(0)) { return Price.Props(0, 0); }

Price.Props memory price = secondaryPrices[token];
if (price.isEmpty()) {
revert Errors.EmptySecondaryPrice(token);
}

return price;
}

// @dev get the latest price of a token
// @param token the token to get the price for
// @return the latest price of a token
function getLatestPrice(address token) external view returns (Price.Props memory) {
if (token == address(0)) { return Price.Props(0, 0); }

Price.Props memory customPrice = customPrices[token];

// treat the custom price as a latest price for consistency in cases where
// the trigger price or acceptable price is used as the custom price
if (!customPrice.isEmpty()) {
return customPrice;
}

Price.Props memory secondaryPrice = secondaryPrices[token];

if (!secondaryPrice.isEmpty()) {
return secondaryPrice;
}

Price.Props memory primaryPrice = primaryPrices[token];
if (!primaryPrice.isEmpty()) {
return primaryPrice;
}

revert Errors.EmptyLatestPrice(token);
}

// @dev get the custom price of a token
// @param token the token to get the price for
// @return the custom price of a token
function getCustomPrice(address token) external view returns (Price.Props memory) {
Price.Props memory price = customPrices[token];
if (price.isEmpty()) {
revert Errors.EmptyCustomPrice(token);
}
return price;
}

// @dev get the stable price of a token
// @param dataStore DataStore
// @param token the token to get the price for
Expand Down Expand Up @@ -542,21 +465,16 @@ contract Oracle is RoleModule {
revert Errors.InvalidMedianMinMaxPrice(medianMinPrice, medianMaxPrice);
}

if (primaryPrices[reportInfo.token].isEmpty()) {
emitOraclePriceUpdated(eventEmitter, reportInfo.token, medianMinPrice, medianMaxPrice, true, false);
if (!primaryPrices[reportInfo.token].isEmpty()) {
revert Errors.DuplicateTokenPrice(reportInfo.token);
}

primaryPrices[reportInfo.token] = Price.Props(
medianMinPrice,
medianMaxPrice
);
} else {
emitOraclePriceUpdated(eventEmitter, reportInfo.token, medianMinPrice, medianMaxPrice, false, false);
emitOraclePriceUpdated(eventEmitter, reportInfo.token, medianMinPrice, medianMaxPrice, true, false);

secondaryPrices[reportInfo.token] = Price.Props(
medianMinPrice,
medianMaxPrice
);
}
primaryPrices[reportInfo.token] = Price.Props(
medianMinPrice,
medianMaxPrice
);

tokensWithPrices.add(reportInfo.token);
}
Expand Down
10 changes: 0 additions & 10 deletions contracts/oracle/OracleModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,12 @@ contract OracleModule {
revert Errors.InvalidPrimaryPricesForSimulation(params.primaryTokens.length, params.primaryPrices.length);
}

if (params.secondaryTokens.length != params.secondaryPrices.length) {
revert Errors.InvalidSecondaryPricesForSimulation(params.secondaryTokens.length, params.secondaryPrices.length);
}

for (uint256 i; i < params.primaryTokens.length; i++) {
address token = params.primaryTokens[i];
Price.Props memory price = params.primaryPrices[i];
oracle.setPrimaryPrice(token, price);
}

for (uint256 i; i < params.secondaryTokens.length; i++) {
address token = params.secondaryTokens[i];
Price.Props memory price = params.secondaryPrices[i];
oracle.setSecondaryPrice(token, price);
}

_;

revert Errors.EndOfOracleSimulation();
Expand Down
14 changes: 0 additions & 14 deletions contracts/oracle/OracleUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ library OracleUtils {
struct SimulatePricesParams {
address[] primaryTokens;
Price.Props[] primaryPrices;
address[] secondaryTokens;
Price.Props[] secondaryPrices;
}

struct ReportInfo {
Expand Down Expand Up @@ -283,18 +281,6 @@ library OracleUtils {
return true;
}

if (errorSelector == Errors.EmptySecondaryPrice.selector) {
return true;
}

if (errorSelector == Errors.EmptyLatestPrice.selector) {
return true;
}

if (errorSelector == Errors.EmptyCustomPrice.selector) {
return true;
}

return false;
}

Expand Down
Loading

0 comments on commit 3243138

Please sign in to comment.