DexBook

Git Source

State Variables

PRICE_PRECISION

price precision is required to represent prices below 0

uint128 internal constant PRICE_PRECISION = 10 ** 18;

BASIS_POINT

basis point is 1/100 of a percent

uint256 internal constant BASIS_POINT = 10 ** 4;

_protocolFee

protocol fee is 10 basis points or 0.1%

uint256 internal constant _protocolFee = 10;

_tokenADecimals

caches tokenA decimals to save on gas costs

uint256 internal immutable _tokenADecimals;

_tokenBDecimals

caches tokenB decimals to save on gas costs

uint256 internal immutable _tokenBDecimals;

_tokenA

token A of this address

address internal immutable _tokenA;

_tokenB

token B of this address

address internal immutable _tokenB;

_feeRecipient

address that receives protocol fees

address internal immutable _feeRecipient;

_buyOrders

stores all buy orders

LibPriceBrackets.PriceBrackets internal _buyOrders;

_sellOrders

stores all sell orders

LibPriceBrackets.PriceBrackets internal _sellOrders;

Functions

constructor

constructor of the contract. Sets the tokenA and tokenB

constructor(address tokenA_, address tokenB_);

Parameters

NameTypeDescription
tokenA_addressthe tokenA
tokenB_addressthe tokenB

placeBuyLimitOrder

function placeBuyLimitOrder(uint256 amount_, uint128 price_, uint128[] calldata prevs_, uint128[] calldata nexts_)
    external
    returns (uint48 orderId_);

Parameters

NameTypeDescription
amount_uint256tokenA amount to buy
price_uint128tokenB/tokenA, not considering decimals, with PRICE_PRECISION
prevs_uint128[]hints for the previous price bracket
nexts_uint128[]hints for the next price bracket

placeSellLimitOrder

function placeSellLimitOrder(uint256 amount_, uint128 price_, uint128[] calldata prevs_, uint128[] calldata nexts_)
    external
    returns (uint48 orderId_);

Parameters

NameTypeDescription
amount_uint256tokenB amount to sell
price_uint128tokenA/tokenB, not considering decimals, with PRICE_PRECISION
prevs_uint128[]hints for the previous price bracket
nexts_uint128[]hints for the next price bracket

placeBuyMarketOrder

buys tokenA with tokenB at the best available price

function placeBuyMarketOrder(uint256 tokenBamount_) external;

Parameters

NameTypeDescription
tokenBamount_uint256amount of tokenB used to buy tokenA

placeSellMarketOrder

buys tokenB with tokenA at the best available price

function placeSellMarketOrder(uint256 tokenAamount_) external;

Parameters

NameTypeDescription
tokenAamount_uint256amount of tokenA used to buy tokenB

removeBuyLimitOrder

removes a buy limit order from the order book

function removeBuyLimitOrder(uint48 orderId_, uint128 price_) external;

Parameters

NameTypeDescription
orderId_uint48id of the order to remove
price_uint128tokenB/tokenA, not considering decimals, with PRICE_PRECISION

removeSellLimitOrder

removes a sell limit order from the order book

function removeSellLimitOrder(uint48 orderId_, uint128 price_) external;

Parameters

NameTypeDescription
orderId_uint48id of the order to remove
price_uint128tokenA/tokenB, not considering decimals, with PRICE_PRECISION

modifyBuyLimitOrder

modifies a buy limit order

reverts if the new price and amount are the same as the old ones

function modifyBuyLimitOrder(
    uint48 orderId_,
    uint128 oldPrice_,
    uint128 newPrice_,
    uint256 newAmount_,
    uint128[] calldata prevs_,
    uint128[] calldata nexts_
) external returns (uint48);

Parameters

NameTypeDescription
orderId_uint48id of the order to modify
oldPrice_uint128old tokenB/tokenA, not considering decimals, with PRICE_PRECISION
newPrice_uint128new tokenB/tokenA, not considering decimals, with PRICE_PRECISION
newAmount_uint256new amount of tokenA to buy
prevs_uint128[]hints for the previous price bracket
nexts_uint128[]hints for the next price bracket

modifySellLimitOrder

modifies a sell limit order

reverts if the new price and amount are the same as the old ones

function modifySellLimitOrder(
    uint48 orderId_,
    uint128 oldPrice_,
    uint128 newPrice_,
    uint256 newAmount_,
    uint128[] calldata prevs_,
    uint128[] calldata nexts_
) external returns (uint48);

Parameters

NameTypeDescription
orderId_uint48id of the order to modify
oldPrice_uint128old tokenA/tokenB, not considering decimals, with PRICE_PRECISION
newPrice_uint128new tokenA/tokenB, not considering decimals, with PRICE_PRECISION
newAmount_uint256new amount of tokenA to buy
prevs_uint128[]hints for the previous price bracket
nexts_uint128[]hints for the next price bracket

tokenAToTokenB

returns tokenB from a tokenA argument and a tokenB/tokenA price

function tokenAToTokenB(uint256 amount_, uint256 price_) public view returns (uint256);

Parameters

NameTypeDescription
amount_uint256amount of tokenA to convert
price_uint256tokenB/tokenA price, not considering decimals, with PRICE_PRECISION

Returns

NameTypeDescription
<none>uint256amount of tokenB

tokenBToTokenA

returns tokenA from a tokenB argument and a tokenA/tokenB price

function tokenBToTokenA(uint256 amount_, uint256 price_) public view returns (uint256);

Parameters

NameTypeDescription
amount_uint256amount of tokenB to convert
price_uint256tokenA/tokenB price, not considering decimals, with PRICE_PRECISION

Returns

NameTypeDescription
<none>uint256amount of tokenA

tokenAToTokenBDecimals

converts tokenA decimals to tokenB decimals

function tokenAToTokenBDecimals(uint256 amount_) public view returns (uint256);

Parameters

NameTypeDescription
amount_uint256amount of tokenA to convert

Returns

NameTypeDescription
<none>uint256amount of tokenB

tokenBToTokenADecimals

converts tokenB decimals to tokenA decimals

function tokenBToTokenADecimals(uint256 amount_) public view returns (uint256);

Parameters

NameTypeDescription
amount_uint256amount of tokenB to convert

Returns

NameTypeDescription
<none>uint256amount of tokenA

pricePrecision

returns the precision used in the prices

function pricePrecision() external pure returns (uint128);

Returns

NameTypeDescription
<none>uint128precision

tokenA

returns tokenA address

function tokenA() external view returns (address);

Returns

NameTypeDescription
<none>addresstokenA address

tokenB

returns tokenB address

function tokenB() external view returns (address);

Returns

NameTypeDescription
<none>addresstokenB address

tokenADecimals

returns tokenA decimals

function tokenADecimals() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256tokenA decimals

tokenBDecimals

returns tokenB decimals

function tokenBDecimals() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256tokenB decimals

protocolFee

returns the protocol fee

function protocolFee() external pure returns (uint256);

Returns

NameTypeDescription
<none>uint256protocol fee

feeRecipient

returns the address of the fee recipient

function feeRecipient() external view returns (address);

Returns

NameTypeDescription
<none>addressfee recipient

sellOrdersPrices

returns the priceBrackets of the sell orders

function sellOrdersPrices() external view returns (uint128[] memory prices_);

Returns

NameTypeDescription
prices_uint128[]priceBrackets of the sell orders

buyOrdersPrices

returns the priceBrackets of the buy orders

function buyOrdersPrices() external view returns (uint128[] memory prices_);

Returns

NameTypeDescription
prices_uint128[]priceBrackets of the buy orders

sellOrdersAtPrice

returns the sell orders at a given price

function sellOrdersAtPrice(uint128 price_) external view returns (LibLinkedOrders.ViewOrder[] memory orders_);

Parameters

NameTypeDescription
price_uint128tokenA/tokenB price, not considering decimals, with PRICE_PRECISION

Returns

NameTypeDescription
orders_LibLinkedOrders.ViewOrder[]ordered sell orders array at a given price

buyOrdersAtPrice

returns the buy orders at a given price

function buyOrdersAtPrice(uint128 price_) external view returns (LibLinkedOrders.ViewOrder[] memory orders_);

Parameters

NameTypeDescription
price_uint128tokenA/tokenB price, not considering decimals, with PRICE_PRECISION

Returns

NameTypeDescription
orders_LibLinkedOrders.ViewOrder[]ordered buy orders array at a given price

sellOrderAtPrice

returns the sell order at a given price and order id

function sellOrderAtPrice(uint128 price_, uint48 orderId_)
    external
    view
    returns (LibLinkedOrders.Order memory order_);

Parameters

NameTypeDescription
price_uint128tokenA/tokenB price, not considering decimals, with PRICE_PRECISION
orderId_uint48order id

Returns

NameTypeDescription
order_LibLinkedOrders.Ordersell order at a given price and order id

buyOrderAtPrice

returns the buy order at a given price and order id

function buyOrderAtPrice(uint128 price_, uint48 orderId_) external view returns (LibLinkedOrders.Order memory order_);

Parameters

NameTypeDescription
price_uint128tokenB/tokenA price, not considering decimals, with PRICE_PRECISION
orderId_uint48order id

Returns

NameTypeDescription
order_LibLinkedOrders.Orderbuy order at a given price and order id

sellOrdersAndPrices

returns the sell orders and corresponding prices

function sellOrdersAndPrices() external view returns (LibPriceBrackets.OrdersByPrice[] memory orders_);

Returns

NameTypeDescription
orders_LibPriceBrackets.OrdersByPrice[]sell orders and corresponding prices

buyOrdersAndPrices

returns the buy orders and corresponding prices

function buyOrdersAndPrices() external view returns (LibPriceBrackets.OrdersByPrice[] memory orders_);

Returns

NameTypeDescription
orders_LibPriceBrackets.OrdersByPrice[]buy orders and corresponding prices

invertPrice

inverts a price, taking into account PRICE_PRECISION

function invertPrice(uint128 price_) external pure returns (uint128);

Parameters

NameTypeDescription
price_uint128price with PRICE_PRECISION to invert

amountPlusFee

returns the amount + protocol fee

function amountPlusFee(uint256 amount_) external pure returns (uint256);

Parameters

NameTypeDescription
amount_uint256amount to add the protocol fee

Returns

NameTypeDescription
<none>uint256amount + protocol fee

basisPoint

returns the basis point - 1/100th percent

function basisPoint() external pure returns (uint256);

Returns

NameTypeDescription
<none>uint256basis point

_fulfillBuyLimitOrder

function _fulfillBuyLimitOrder(uint48 orderId_, uint128 price_, address maker_, uint256 amount_) internal;

_fulfillSellLimitOrder

function _fulfillSellLimitOrder(uint48 orderId_, uint128 price_, address maker_, uint256 amount_) internal;

Events

BuyLimitOrderPlaced

emitted when a buy limit order is placed

event BuyLimitOrderPlaced(uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 amount);

SellLimitOrderPlaced

emitted when a sell limit order is placed

event SellLimitOrderPlaced(uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 amount);

BuyLimitOrderCancelled

emitted when a buy limit order is cancelled

event BuyLimitOrderCancelled(uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 amount);

SellLimitOrderCancelled

emitted when a sell limit order is cancelled

event SellLimitOrderCancelled(uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 amount);

BuyLimitOrderFilled

emitted when a buy limit order is filled

event BuyLimitOrderFilled(uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 amount);

SellLimitOrderFilled

emitted when a sell limit order is filled

event SellLimitOrderFilled(uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 amount);

BuyLimitOrderModified

emitted when a buy limit order is modified

event BuyLimitOrderModified(
    uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 oldAmount, uint256 amount
);

SellLimitOrderModified

emitted when a sell limit order is modified

event SellLimitOrderModified(
    uint48 indexed orderId, uint128 indexed price, address indexed maker, uint256 oldAmount, uint256 amount
);

BuyMarketOrderFilled

emitted when a buy market order is filled

event BuyMarketOrderFilled(uint256 timestamp, uint256 indexed price, address indexed maker, uint256 amount);

SellMarketOrderFilled

emitted when a sell market order is filled

event SellMarketOrderFilled(uint256 timestamp, uint256 indexed price, address indexed maker, uint256 amount);

Errors

SameAmountError

emitted when modifying an order with stale new same price and amount

error SameAmountError(uint256 amount);

OnlyMakerError

emitted when someone other than the maker tries to remove or modify an order

error OnlyMakerError(address maker);