OrderController

Git Source

Inherits: IOrderController, Base

The OrderController is for handling deposit and withdrawal orders.

State Variables

portfolio

IPortfolio public portfolio;

token

IERC20 public token;

tranche

ITranchePool public tranche;

dust

uint256 public dust;

NULL

uint256 public constant NULL = 0;
uint256 public constant HEAD = 0;

nextOrderId

uint256 internal nextOrderId = 1;

orders

mapping(uint256 => Order) internal orders;

Functions

constructor

constructor(
    ITranchePool tranche_,
    IProtocolConfig protocolConfig_,
    IPortfolio portfolio_,
    uint256 dust_,
    address manager_
)
    Base(protocolConfig_.protocolAdmin(), protocolConfig_.pauser());

deposit

Allows a user to deposit tokens and create a DEPOSIT order. It doesn't gurantee deposit

The order can be executed if there's a matching withdrawal request. The caller should approve tokens prior to calling this function.

function deposit(uint256 tokenAmount, uint256 iterationLimit) external whenNotPaused;

Parameters

NameTypeDescription
tokenAmountuint256The amount of tokens to deposit.
iterationLimituint256The maximum number of orders to process in a single call.

withdraw

Allows a user to withdraw tranches and create a WITHDRAW order.

The order can be executed if there's a matching deposit request. The caller should approve tranches prior to calling this function.

function withdraw(uint256 trancheAmount, uint256 iterationLimit) external whenNotPaused;

Parameters

NameTypeDescription
trancheAmountuint256The amount of tranches to withdraw.
iterationLimituint256The maximum number of orders to process in a single call.

cancelOrder

Allows a user to cancel their pending order.

This can be called by the user who placed the order only.

function cancelOrder() external;

cancelDustOrder

Allows any users to cancel dust order.

function cancelDustOrder(uint256 orderId) external;

Parameters

NameTypeDescription
orderIduint256The order id to cancel.

setDust

function setDust(uint256 _dust) external;

expectedTokenAmount

Calculate the expected token amount for a given tranche amount.

function expectedTokenAmount(uint256 trancheAmount) public view virtual returns (uint256);

Parameters

NameTypeDescription
trancheAmountuint256The amount of tranches to convert.

Returns

NameTypeDescription
<none>uint256The expected token amount.

expectedTrancheAmount

Calculate the expected tranche amount for a given token amount.

function expectedTrancheAmount(uint256 tokenAmount) public view virtual returns (uint256);

Parameters

NameTypeDescription
tokenAmountuint256The amount of tokens to convert.

Returns

NameTypeDescription
<none>uint256The expected tranche amount.

expectedTokenAmountCeil

function expectedTokenAmountCeil(uint256 trancheAmount) internal view virtual returns (uint256);

expectedTrancheAmountCeil

function expectedTrancheAmountCeil(uint256 tokenAmount) internal view virtual returns (uint256);

getUserOrder

Return the order of the given user.

function getUserOrder(address user) public view returns (Order memory);

Parameters

NameTypeDescription
useraddressThe user address.

Returns

NameTypeDescription
<none>OrderThe order.

getOrders

Return all orders.

function getOrders() public view returns (Order[] memory);

Returns

NameTypeDescription
<none>Order[]The orders.

getValidOrders

Return the valid orders and the current order type.

function getValidOrders() public view returns (Order[] memory, OrderType);

Returns

NameTypeDescription
<none>Order[]orders The valid orders.
<none>OrderTypeorderType The type of the order.

getOrderCount

Return the count of all orders.

function getOrderCount() public view returns (uint256);

Returns

NameTypeDescription
<none>uint256The count of all orders.

getValidOrderCount

Return the count of valid orders and the current order type.

function getValidOrderCount() public view returns (uint256 count, OrderType orderType);

Returns

NameTypeDescription
countuint256The count of valid orders.
orderTypeOrderTypeThe type of the current order.

currentOrderType

Return the type of the current order in the linked list of orders

function currentOrderType() public view returns (OrderType);

_processWithdrawOrder

function _processWithdrawOrder(uint256 tokenAmount, uint256 iterationLimit) internal returns (uint256, uint256);

_processDepositOrder

function _processDepositOrder(uint256 trancheAmount, uint256 iterationLimit) internal returns (uint256, uint256);

_createOrder

function _createOrder(address user, uint256 amount, OrderType orderType) internal;

_removeOrder

function _removeOrder(uint256 orderId) internal;

_cancelOrder

function _cancelOrder(address user) internal;

_executeDepositOrder

function _executeDepositOrder(uint256 tokenAmount, address user) internal returns (uint256 trancheAmount);

_executeDepositOrder

function _executeDepositOrder(uint256 tokenAmount, uint256 trancheAmount, address user) internal;

_executeWithdrawOrder

function _executeWithdrawOrder(uint256 tokenAmount, uint256 trancheAmount, address user) internal;

_executeWithdrawOrder

function _executeWithdrawOrder(uint256 trancheAmount, address user) internal returns (uint256 tokenAmount);

_setToken

function _setToken(IERC20 _token) internal;

_setTranche

function _setTranche(ITranchePool _tranche) internal;

_setPortfolio

function _setPortfolio(IPortfolio _portfolio) internal;

_setDust

function _setDust(uint256 _dust) internal;

_validateOrder

check user token allowance for corresponding order type

function _validateOrder(Order memory order, OrderType orderType) internal view returns (bool);

_validateInputs

function _validateInputs(address user, uint256 amount, OrderType orderType) internal view;

_checkBalance

function _checkBalance(address user, uint256 amount, OrderType orderType) internal view returns (bool);

_checkAllowance

function _checkAllowance(address user, uint256 amount, OrderType orderType) internal view returns (bool);

_isStatus

function _isStatus(Status allowedStatus) internal view virtual returns (bool);