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.

PropertyTypeDescription

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.

PropertyTypeDescription

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

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

NameTypeDescription

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

Removes and returns the first order ID from the linked list for a given price.

Parameters

NameTypeDescription

self

OrderStorage storage

The storage reference to the OrderStorage struct.

price

uint256

The price of the order.

Returns

NameTypeDescription

first

uint256

The ID of the first order in the linked list.

_createOrder

Creates a new order and adds it to the order storage.

Parameters

NameTypeDescription

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

NameTypeDescription

id

uint256

The ID of the created order.

_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

NameTypeDescription

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

Deletes an order from the order storage.

Parameters

VariableTypeDescription

self

OrderStorage storage

The storage reference to the OrderStorage struct.

id

uint256

The ID of the order to delete.

_getOrderIds

Retrieves an array of order IDs from the linked list for a given price, limited by the specified maximum count.

Parameters

NameTypeDescription

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

NameTypeDescription

orders

uint256[] memory

An array of order IDs.

_getOrders

Retrieves an array of orders from the linked list for a given price, limited by the specified maximum count.

Parameters

NameTypeDescription

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

NameTypeDescription

orders

Order[] memory

An array of orders.

Retrieves the head of the linked list for a given price.

Parameters

NameTypeDescription

self

OrderStorage storage

The storage reference to the OrderStorage struct.

price

uint256

The price of the orders.

Returns

NameTypeDescription

head

uint256

The ID of the first order in the linked list.

_isEmpty

Checks if the linked list for a given price is empty.

Parameters

NameTypeDescription

self

OrderStorage storage

The storage reference to the OrderStorage struct.

price

uint256

The price of the orders.

Returns

NameTypeDescription

isEmpty

bool

A flag indicating if the linked list is empty (true) or not (false).

_next

Retrieves the next order ID in the linked list for a given price and current order ID.

Parameters

NameTypeDescription

self

OrderStorage storage

The storage reference to the OrderStorage struct.

price

uint256

The price of the orders.

curr

uint256

The current order ID.

Returns

NameTypeDescription

next

uint256

The ID of the next order in the linked list.

_getOrder

Retrieves the order details for a given order ID.

Parameters

NameTypeDescription

self

OrderStorage storage

The storage reference to the OrderStorage struct.

id

uint256

The ID of the order.

Returns

NameTypeDescription

order

Order memory

The details of the order.

Last updated