DexBook
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
Name | Type | Description |
---|---|---|
tokenA_ | address | the tokenA |
tokenB_ | address | the tokenB |
placeBuyLimitOrder
function placeBuyLimitOrder(uint256 amount_, uint128 price_, uint128[] calldata prevs_, uint128[] calldata nexts_)
external
returns (uint48 orderId_);
Parameters
Name | Type | Description |
---|---|---|
amount_ | uint256 | tokenA amount to buy |
price_ | uint128 | tokenB/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
Name | Type | Description |
---|---|---|
amount_ | uint256 | tokenB amount to sell |
price_ | uint128 | tokenA/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
Name | Type | Description |
---|---|---|
tokenBamount_ | uint256 | amount of tokenB used to buy tokenA |
placeSellMarketOrder
buys tokenB with tokenA at the best available price
function placeSellMarketOrder(uint256 tokenAamount_) external;
Parameters
Name | Type | Description |
---|---|---|
tokenAamount_ | uint256 | amount of tokenA used to buy tokenB |
removeBuyLimitOrder
removes a buy limit order from the order book
function removeBuyLimitOrder(uint48 orderId_, uint128 price_) external;
Parameters
Name | Type | Description |
---|---|---|
orderId_ | uint48 | id of the order to remove |
price_ | uint128 | tokenB/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
Name | Type | Description |
---|---|---|
orderId_ | uint48 | id of the order to remove |
price_ | uint128 | tokenA/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
Name | Type | Description |
---|---|---|
orderId_ | uint48 | id of the order to modify |
oldPrice_ | uint128 | old tokenB/tokenA, not considering decimals, with PRICE_PRECISION |
newPrice_ | uint128 | new tokenB/tokenA, not considering decimals, with PRICE_PRECISION |
newAmount_ | uint256 | new 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
Name | Type | Description |
---|---|---|
orderId_ | uint48 | id of the order to modify |
oldPrice_ | uint128 | old tokenA/tokenB, not considering decimals, with PRICE_PRECISION |
newPrice_ | uint128 | new tokenA/tokenB, not considering decimals, with PRICE_PRECISION |
newAmount_ | uint256 | new 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
Name | Type | Description |
---|---|---|
amount_ | uint256 | amount of tokenA to convert |
price_ | uint256 | tokenB/tokenA price, not considering decimals, with PRICE_PRECISION |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | amount 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
Name | Type | Description |
---|---|---|
amount_ | uint256 | amount of tokenB to convert |
price_ | uint256 | tokenA/tokenB price, not considering decimals, with PRICE_PRECISION |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | amount of tokenA |
tokenAToTokenBDecimals
converts tokenA decimals to tokenB decimals
function tokenAToTokenBDecimals(uint256 amount_) public view returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
amount_ | uint256 | amount of tokenA to convert |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | amount of tokenB |
tokenBToTokenADecimals
converts tokenB decimals to tokenA decimals
function tokenBToTokenADecimals(uint256 amount_) public view returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
amount_ | uint256 | amount of tokenB to convert |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | amount of tokenA |
pricePrecision
returns the precision used in the prices
function pricePrecision() external pure returns (uint128);
Returns
Name | Type | Description |
---|---|---|
<none> | uint128 | precision |
tokenA
returns tokenA address
function tokenA() external view returns (address);
Returns
Name | Type | Description |
---|---|---|
<none> | address | tokenA address |
tokenB
returns tokenB address
function tokenB() external view returns (address);
Returns
Name | Type | Description |
---|---|---|
<none> | address | tokenB address |
tokenADecimals
returns tokenA decimals
function tokenADecimals() external view returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | tokenA decimals |
tokenBDecimals
returns tokenB decimals
function tokenBDecimals() external view returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | tokenB decimals |
protocolFee
returns the protocol fee
function protocolFee() external pure returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | protocol fee |
feeRecipient
returns the address of the fee recipient
function feeRecipient() external view returns (address);
Returns
Name | Type | Description |
---|---|---|
<none> | address | fee recipient |
sellOrdersPrices
returns the priceBrackets of the sell orders
function sellOrdersPrices() external view returns (uint128[] memory prices_);
Returns
Name | Type | Description |
---|---|---|
prices_ | uint128[] | priceBrackets of the sell orders |
buyOrdersPrices
returns the priceBrackets of the buy orders
function buyOrdersPrices() external view returns (uint128[] memory prices_);
Returns
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
price_ | uint128 | tokenA/tokenB price, not considering decimals, with PRICE_PRECISION |
Returns
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
price_ | uint128 | tokenA/tokenB price, not considering decimals, with PRICE_PRECISION |
Returns
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
price_ | uint128 | tokenA/tokenB price, not considering decimals, with PRICE_PRECISION |
orderId_ | uint48 | order id |
Returns
Name | Type | Description |
---|---|---|
order_ | LibLinkedOrders.Order | sell 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
Name | Type | Description |
---|---|---|
price_ | uint128 | tokenB/tokenA price, not considering decimals, with PRICE_PRECISION |
orderId_ | uint48 | order id |
Returns
Name | Type | Description |
---|---|---|
order_ | LibLinkedOrders.Order | buy 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
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
price_ | uint128 | price with PRICE_PRECISION to invert |
amountPlusFee
returns the amount + protocol fee
function amountPlusFee(uint256 amount_) external pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
amount_ | uint256 | amount to add the protocol fee |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | amount + protocol fee |
basisPoint
returns the basis point - 1/100th percent
function basisPoint() external pure returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | basis 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);