NewOrderOrderbook
This library implements a linked list data structure for managing orders in an orderbook.
Variables
Order (struct)
Represents an order in the orderbook, containing the owner's address and the deposit amount.
owner
address
The owner of the order.
depositAmount
uint256
The amount of deposit in the order.
OrderStorage (struct)
Represents the order storage for the orderbook. It contains mappings and data structures for managing the order linked list.
list
mapping(uint256 => mapping(uint256 => uint256))
A hashmap-style mapping that stores the order indices linked to each price.
orders
mapping(uint256 => Order)
A mapping that stores the order details based on their indices.
head
mapping(uint256 => uint256)
A mapping that stores the head of the linked list for each price.
count
uint256
The count of orders, used for array allocation.
Please note that the "engine" property mentioned in the original description is not a part of the OrderStorage struct.
Internal Functions
_insertId
_insertId
Inserts an order ID into the order linked list based on the order's price and deposit amount. The order ID is inserted in the correct position, maintaining the order of the linked list.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the order.
id
uint256
The ID of the order.
amount
uint256
The deposit amount of the order.
_fpop
_fpop
Removes and returns the first order ID from the linked list for a given price.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the order.
Returns
first
uint256
The ID of the first order in the linked list.
_createOrder
_createOrder
Creates a new order and adds it to the order storage.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
owner
address
The address of the order owner.
depositAmount
uint256
The deposit amount of the order.
Returns
id
uint256
The ID of the created order.
_decreaseOrder
_decreaseOrder
Decreases the deposit amount of an order by the specified amount. If the decreased amount becomes zero, the order is deleted from the order storage.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
id
uint256
The ID of the order.
amount
uint256
The amount to decrease from the order's deposit amount.
_deleteOrder
_deleteOrder
Deletes an order from the order storage.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
id
uint256
The ID of the order to delete.
_getOrderIds
_getOrderIds
Retrieves an array of order IDs from the linked list for a given price, limited by the specified maximum count.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the orders.
n
uint256
The maximum number of order IDs to retrieve.
Returns
orders
uint256[] memory
An array of order IDs.
_getOrders
_getOrders
Retrieves an array of orders from the linked list for a given price, limited by the specified maximum count.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the orders.
n
uint256
The maximum number of orders to retrieve.
Returns
orders
Order[] memory
An array of orders.
_head
_head
Retrieves the head of the linked list for a given price.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the orders.
Returns
head
uint256
The ID of the first order in the linked list.
_isEmpty
_isEmpty
Checks if the linked list for a given price is empty.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the orders.
Returns
isEmpty
bool
A flag indicating if the linked list is empty (true) or not (false).
_next
_next
Retrieves the next order ID in the linked list for a given price and current order ID.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
price
uint256
The price of the orders.
curr
uint256
The current order ID.
Returns
next
uint256
The ID of the next order in the linked list.
_getOrder
_getOrder
Retrieves the order details for a given order ID.
Parameters
self
OrderStorage storage
The storage reference to the OrderStorage struct.
id
uint256
The ID of the order.
Returns
order
Order memory
The details of the order.
Last updated