LibPriceBrackets
State Variables
PRICE_PRECISION
price precision is required because solidity does not support decimal values
uint128 internal constant PRICE_PRECISION = 10 ** 18;
NULL
NULL price
uint128 internal constant NULL = uint128(0);
Functions
insertOrder
function insertOrder(
PriceBrackets storage self,
uint128 price_,
address maker_,
uint256 amount_,
uint128[] calldata prevs_,
uint128[] calldata nexts_
) internal returns (uint48 orderId_);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
price_ | uint128 | the price of the order that the market order maker will have to pay |
maker_ | address | the address of the order maker |
amount_ | uint256 | the amount of token that will be sent to the maker |
prevs_ | uint128[] | hints for the closest existing previous prices |
nexts_ | uint128[] | hints for the closest existing next prices |
removeOrder
removes an order from the price bracket
function removeOrder(PriceBrackets storage self, uint128 price_, uint48 orderId_) internal returns (address, uint256);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
price_ | uint128 | the price of the order that the market order maker would have to pay |
orderId_ | uint48 | the id of the order to remove |
Returns
Name | Type | Description |
---|---|---|
<none> | address | maker_ the address of the order maker |
<none> | uint256 | amount_ the amount of token that will be sent to the maker |
modifyOrder
modifies an order from the price bracket
function modifyOrder(PriceBrackets storage self, uint128 price_, uint48 orderId_, uint256 newAmount_)
internal
returns (address, uint256);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
price_ | uint128 | the price of the order that the market order maker will have to pay |
orderId_ | uint48 | the id of the order to modify |
newAmount_ | uint256 | the new amount of token that will be sent to the maker |
Returns
Name | Type | Description |
---|---|---|
<none> | address | maker_ the address of the order maker |
<none> | uint256 | amount_ the amount of token that will be sent to the maker |
removeOrdersUntilTarget
removes orders from the price bracket until the target amount is reached
when a market order is placed, it fills limit orders until the target
function removeOrdersUntilTarget(
PriceBrackets storage self,
uint256 targetAmount_,
function(uint48,uint128,address,uint256) internal _f
) internal returns (uint256 accumulatedAmount_, uint256 accumulatedCost_);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
targetAmount_ | uint256 | the target amount to reach |
_f | function (uint48, uint128, address, uint256) internal | the function to call for each order removed |
Returns
Name | Type | Description |
---|---|---|
accumulatedAmount_ | uint256 | the accumulated amount summed by filling orders. Might not reach the target |
accumulatedCost_ | uint256 | the accumulated cost for the market order maker to pay |
exists
checks if a price bracket exists
function exists(PriceBrackets storage self, uint128 price_) internal view returns (bool);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
price_ | uint128 | the price of the order that the market order maker will have to pay |
Returns
Name | Type | Description |
---|---|---|
<none> | bool | exists_ true if the price bracket exists |
_findClosestPrev
finds the closest previous price bracket to the price
function _findClosestPrev(PriceBrackets storage self, uint128[] calldata prevs_, uint128 price_)
internal
view
returns (uint128 prev_);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
prevs_ | uint128[] | the list of previous prices to find the closest previous price |
price_ | uint128 | the price of the order that the market order maker will have to pay |
Returns
Name | Type | Description |
---|---|---|
prev_ | uint128 | the closest previous price bracket |
_findClosestNext
finds the closest next price bracket to the price
function _findClosestNext(PriceBrackets storage self, uint128[] calldata nexts_, uint128 price_)
internal
view
returns (uint128 next_);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
nexts_ | uint128[] | the list of next prices to find the closest next price |
price_ | uint128 | the price of the order that the market order maker will have to pay |
Returns
Name | Type | Description |
---|---|---|
next_ | uint128 | the closest next price bracket |
getPrices
used to fetch the price brackets off chain. Converts the linked list to an array
function getPrices(PriceBrackets storage self) internal view returns (uint128[] memory prices_);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
Returns
Name | Type | Description |
---|---|---|
prices_ | uint128[] | an array with all the price brackets |
getOrdersAtPrice
gets the orders at a specific price bracket
function getOrdersAtPrice(PriceBrackets storage self, uint128 price_)
internal
view
returns (LibLinkedOrders.ViewOrder[] memory);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
price_ | uint128 | the price of the order that the market order maker will have to pay |
Returns
Name | Type | Description |
---|---|---|
<none> | LibLinkedOrders.ViewOrder[] | orders_ an array with all the orders at the price bracket |
getOrderAtPrice
gets the order at a specific price bracket and order id
function getOrderAtPrice(PriceBrackets storage self, uint128 price_, uint48 orderId_)
internal
view
returns (LibLinkedOrders.Order memory);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
price_ | uint128 | the price of the order that the market order maker will have to pay |
orderId_ | uint48 | the id of the order |
Returns
Name | Type | Description |
---|---|---|
<none> | LibLinkedOrders.Order | order_ the order at the price bracket and order id |
getOrdersAndPrices
gets all the orders in every price bracket
used to fetch all the orders off chain. Converts the linked lists to an array of OrdersByPrice
function getOrdersAndPrices(PriceBrackets storage self)
internal
view
returns (OrdersByPrice[] memory correctOrdersByPrices_);
Parameters
Name | Type | Description |
---|---|---|
self | PriceBrackets | the price bracket with the list of orders |
Returns
Name | Type | Description |
---|---|---|
correctOrdersByPrices_ | OrdersByPrice[] | an array with all the orders at the price bracket |
Errors
PriceSmallerThanPrevError
emitted when the hints are wrong and the current price is bigger than the previous hinted price
error PriceSmallerThanPrevError();
PriceBiggerThanNextError
emitted when the hints are wrong and the current price is smaller than the next hinted price
error PriceBiggerThanNextError();
PriceBracketDoesNotExistError
emitted when the price bracket does not exist
error PriceBracketDoesNotExistError();
Structs
OrdersByPrice
OrdersByPrice is a struct that contains the price and an array of orders at that price
useful to fetch off chain all orders at a given price
struct OrdersByPrice {
uint128 price;
LibLinkedOrders.ViewOrder[] orders;
}
PriceBracket
PriceBracket is a struct that contains the previous and next price brackets and a linked list of orders
a linked list is used to allow insertion and removal of orders in constant time (given hints)
struct PriceBracket {
uint128 prev;
uint128 next;
LibLinkedOrders.LinkedOrders linkedOrders;
}
PriceBrackets
PriceBrackets is a struct that contains the lowest and highest price and a mapping of price to price bracket
contains the head (lowest price) and tail (highest price) of the linked list of price brackets
struct PriceBrackets {
uint128 lowestPrice;
uint128 highestPrice;
mapping(uint128 => PriceBracket) priceBrackets;
}