LibPriceBrackets

Git Source

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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
price_uint128the price of the order that the market order maker will have to pay
maker_addressthe address of the order maker
amount_uint256the 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
price_uint128the price of the order that the market order maker would have to pay
orderId_uint48the id of the order to remove

Returns

NameTypeDescription
<none>addressmaker_ the address of the order maker
<none>uint256amount_ 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
price_uint128the price of the order that the market order maker will have to pay
orderId_uint48the id of the order to modify
newAmount_uint256the new amount of token that will be sent to the maker

Returns

NameTypeDescription
<none>addressmaker_ the address of the order maker
<none>uint256amount_ 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
targetAmount_uint256the target amount to reach
_ffunction (uint48, uint128, address, uint256) internalthe function to call for each order removed

Returns

NameTypeDescription
accumulatedAmount_uint256the accumulated amount summed by filling orders. Might not reach the target
accumulatedCost_uint256the 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
price_uint128the price of the order that the market order maker will have to pay

Returns

NameTypeDescription
<none>boolexists_ 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
prevs_uint128[]the list of previous prices to find the closest previous price
price_uint128the price of the order that the market order maker will have to pay

Returns

NameTypeDescription
prev_uint128the 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
nexts_uint128[]the list of next prices to find the closest next price
price_uint128the price of the order that the market order maker will have to pay

Returns

NameTypeDescription
next_uint128the 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders

Returns

NameTypeDescription
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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
price_uint128the price of the order that the market order maker will have to pay

Returns

NameTypeDescription
<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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders
price_uint128the price of the order that the market order maker will have to pay
orderId_uint48the id of the order

Returns

NameTypeDescription
<none>LibLinkedOrders.Orderorder_ 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

NameTypeDescription
selfPriceBracketsthe price bracket with the list of orders

Returns

NameTypeDescription
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;
}