Skip to main content

CDAuctioneerLimitOrders

Git Source

Inherits: ILimitOrders, IVersioned, IERC721Receiver, ReentrancyGuardTransient, Ownable, PeripheryEnabler

Title: CDAuctioneer Limit Orders

Author: Zeus

forge-lint: disable-start(mixed-case-function)

Enables limit order functionality for the Convertible Deposit Auctioneer

Users create orders specifying max price, MEV bots fill when price is favorable. User deposits are held in sUSDS to generate yield, which accrues to a configurable recipient.

State Variables

OHM_SCALE

uint256 internal constant OHM_SCALE = 1e9

DEPOSIT_MANAGER

The Deposit Manager contract

This variable is immutable

address public immutable DEPOSIT_MANAGER

CD_AUCTIONEER

The Convertible Deposit Auctioneer contract

This variable is immutable

IConvertibleDepositAuctioneer public immutable CD_AUCTIONEER

USDS

The USDS contract

This variable is immutable

ERC20 public immutable USDS

SUSDS

The sUSDS contract

This variable is immutable

ERC4626 public immutable SUSDS

POSITION_NFT

The Position NFT contract

This variable is immutable

ERC721 public immutable POSITION_NFT

yieldRecipient

Recipient of accrued yield

address public yieldRecipient

receiptTokens

Receipt token address for each deposit period

mapping(uint8 depositPeriod => ERC20 receiptToken) public receiptTokens

_orders

Limit orders by ID

mapping(uint256 orderId => LimitOrder order) internal _orders

_ordersForUser

Limit order IDs by user

mapping(address => uint256[]) internal _ordersForUser

nextOrderId

Next order ID to be assigned

uint256 public nextOrderId

totalUsdsOwed

Total USDS owed to all order owners (principal tracking)

uint256 public totalUsdsOwed

Functions

constructor

constructor(
address owner_,
address depositManager_,
address cdAuctioneer_,
address usds_,
address sUsds_,
address positionNft_,
address yieldRecipient_,
uint8[] memory depositPeriods_,
address[] memory receiptTokens_
) Ownable(owner_);

VERSION

Returns the version of the contract

function VERSION() external pure returns (uint8 major, uint8 minor);

Returns

NameTypeDescription
majoruint8- Major version upgrade indicates breaking change to the interface.
minoruint8- Minor version change retains backward-compatible interface.

setYieldRecipient

Update the yield recipient address

function setYieldRecipient(address newRecipient_) external onlyOwner onlyEnabled;

Parameters

NameTypeDescription
newRecipient_addressThe new yield recipient

addDepositPeriod

Add a new deposit period and associated receipt token

This function will revert if:

  • The contract is not enabled
  • The caller is not the owner
  • The deposit period is 0
  • The receipt token address is 0
  • The deposit period is already configured
  • The deposit period is not enabled in the auctioneer
function addDepositPeriod(uint8 depositPeriod_, address receiptToken_) external onlyOwner onlyEnabled;

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period to add
receiptToken_addressThe receipt token address for the deposit period

_addDepositPeriod

Internal function to add a deposit period and associated receipt token

This function will revert if:

  • The deposit period is 0
  • The receipt token address is 0
  • The deposit period is already configured
  • The deposit period is not enabled in the auctioneer
function _addDepositPeriod(uint8 depositPeriod_, address receiptToken_) internal;

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period to add
receiptToken_addressThe receipt token address for the deposit period

removeDepositPeriod

Remove a deposit period and associated receipt token

This function will revert if:

  • The contract is not enabled
  • The caller is not the owner
  • The deposit period is not configured Note: Active orders for this deposit period will fail to fill until the deposit period is re-added. Users can cancel their orders if needed.
function removeDepositPeriod(uint8 depositPeriod_) external onlyOwner onlyEnabled;

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period to remove

_deposit

Internal function to deposit USDS into sUSDS and adjust the deposit and incentive budgets

function _deposit(uint256 depositBudget_, uint256 incentiveBudget_)
internal
returns (uint256 actualDepositBudget, uint256 actualIncentiveBudget);

Parameters

NameTypeDescription
depositBudget_uint256USDS budget for bids
incentiveBudget_uint256USDS budget for filler incentives (paid proportionally)

Returns

NameTypeDescription
actualDepositBudgetuint256The actual deposit budget (may be less than the input)
actualIncentiveBudgetuint256The actual incentive budget (may be less than the input)

createOrder

Create a new limit order

This function will revert if:

  • The contract is not enabled
  • The deposit budget, max price, or min fill size is invalid
  • The receipt token is not configured in this contract
  • The deposit period is not enabled in the auctioneer
  • The caller cannot receive ERC721 tokens
  • The min fill size is below the auctioneer minimum bid
function createOrder(
uint8 depositPeriod_,
uint256 depositBudget_,
uint256 incentiveBudget_,
uint256 maxPrice_,
uint256 minFillSize_
) external nonReentrant onlyEnabled returns (uint256 orderId);

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period for the CD position
depositBudget_uint256USDS budget for bids
incentiveBudget_uint256USDS budget for filler incentives (paid proportionally)
maxPrice_uint256Maximum execution price (USDS per OHM)
minFillSize_uint256Minimum USDS per fill (except final fill)

Returns

NameTypeDescription
orderIduint256The ID of the created order

_calculateFillAndIncentive

Calculate capped fill amount and incentive for an order

function _calculateFillAndIncentive(LimitOrder storage order_, uint256 fillAmount_)
internal
view
returns (uint256 cappedFill, uint256 incentive, uint256 remainingDeposit);

Parameters

NameTypeDescription
order_LimitOrderThe limit order
fillAmount_uint256The requested fill amount

Returns

NameTypeDescription
cappedFilluint256The fill amount capped to remaining deposit
incentiveuint256The incentive amount (with final fill handling)
remainingDeposituint256The remaining deposit budget

fillOrder

Fill a limit order

This function will revert if:

  • The contract is not enabled
  • The order is not active
  • The order budget has been fully spent
  • The fill amount is below the minimum fill size (unless this is the final fill)
  • The deposit period is not enabled
  • The receipt token is not configured
  • The execution price is above the maximum price
  • The previewBid function returns zero OHM output
function fillOrder(uint256 orderId_, uint256 fillAmount_)
external
nonReentrant
onlyEnabled
returns (uint256, uint256, uint256);

Parameters

NameTypeDescription
orderId_uint256The ID of the order to fill
fillAmount_uint256The amount of USDS to use for the bid

Returns

NameTypeDescription
<none>uint256uint256 The actual fill amount (may be capped to remaining deposit)
<none>uint256uint256 The incentive amount paid to the filler
<none>uint256uint256 The remaining deposit budget after the fill

cancelOrder

Cancel an active order and return remaining funds

This function will revert if:

  • The caller is not the order owner
  • The order is not active
  • The order is fully spent Note that if the contract is disabled, this function will still operate in order to allow users to withdraw their deposited funds.
function cancelOrder(uint256 orderId_) external nonReentrant;

Parameters

NameTypeDescription
orderId_uint256The ID of the order to cancel

getAccruedYield

Calculate current accrued yield in USDS terms

function getAccruedYield() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256uint256 The current accrued yield in USDS terms

getAccruedYieldShares

Calculate accrued yield in sUSDS shares

Uses previewWithdraw to safely account for rounding

function getAccruedYieldShares() public view returns (uint256);

Returns

NameTypeDescription
<none>uint256uint256 The current accrued yield in sUSDS terms

sweepYield

Sweep all accrued yield to the yield recipient as sUSDS

function sweepYield() external nonReentrant onlyEnabled returns (uint256 shares);

Returns

NameTypeDescription
sharesuint256The amount of sUSDS swept to the yield recipient

getOrder

Get a limit order by ID

function getOrder(uint256 orderId_) external view returns (LimitOrder memory);

Parameters

NameTypeDescription
orderId_uint256The ID of the order

Returns

NameTypeDescription
<none>LimitOrderLimitOrder The limit order

getOrdersForUser

Get limit order IDs by user

function getOrdersForUser(address user_) external view returns (uint256[] memory);

Parameters

NameTypeDescription
user_addressThe address of the user

Returns

NameTypeDescription
<none>uint256[]uint256[] The IDs of the user's orders

previewFillOrder

Preview a fill order

function previewFillOrder(uint256 orderId_, uint256 fillAmount_)
external
view
returns (bool canFill, string memory reason, uint256 effectivePrice, uint256 incentive);

Parameters

NameTypeDescription
orderId_uint256The ID of the order
fillAmount_uint256The amount of USDS to use for the bid

Returns

NameTypeDescription
canFillboolWhether the order can be filled
reasonstringThe reason the order cannot be filled
effectivePriceuint256The effective price of the fill
incentiveuint256The incentive amount for the fill

calculateIncentive

Calculate incentive and incentive rate for a given fill amount

function calculateIncentive(uint256 orderId_, uint256 fillAmount_)
public
view
returns (uint256 incentive, uint256 incentiveRate);

Parameters

NameTypeDescription
orderId_uint256The ID of the order
fillAmount_uint256The amount of USDS to use for the bid

Returns

NameTypeDescription
incentiveuint256The incentive amount for the fill
incentiveRateuint256The incentive rate for the fill

canFillOrder

Check if an order can be filled at a given size

function canFillOrder(uint256 orderId_, uint256 fillAmount_)
public
view
returns (bool canFill, string memory reason, uint256 effectivePrice);

Parameters

NameTypeDescription
orderId_uint256The ID of the order
fillAmount_uint256The amount of USDS to use for the bid

Returns

NameTypeDescription
canFillboolWhether the order can be filled
reasonstringThe reason the order cannot be filled
effectivePriceuint256The effective price of the fill

getRemaining

Get remaining deposit and incentive budgets for order

function getRemaining(uint256 orderId_) external view returns (uint256 deposit, uint256 incentive);

Parameters

NameTypeDescription
orderId_uint256The ID of the order

Returns

NameTypeDescription
deposituint256The remaining deposit budget
incentiveuint256The remaining incentive budget

getExecutionPrice

Get current execution price for a fill amount

function getExecutionPrice(uint8 depositPeriod_, uint256 fillAmount_) external view returns (uint256);

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period
fillAmount_uint256The amount of USDS to use for the bid

Returns

NameTypeDescription
<none>uint256effectivePrice The effective price of the fill

getFillableOrders

Find fillable orders for a deposit period

WARNING: Gas-intensive. Intended for off-chain use only.

function getFillableOrders(uint8 depositPeriod_) external view returns (uint256[] memory);

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period

Returns

NameTypeDescription
<none>uint256[]uint256[] The IDs of the fillable orders

getFillableOrders

Find fillable orders for a deposit period between given order IDs

For use if getFillableOrders(uint8 depositPeriod_) exceeds limit

WARNING: Gas-intensive. Intended for off-chain use only.

function getFillableOrders(uint8 depositPeriod_, uint256 index0, uint256 index1)
external
view
returns (uint256[] memory);

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period
index0uint256The starting order ID
index1uint256The ending order ID

Returns

NameTypeDescription
<none>uint256[]uint256[] The IDs of the fillable orders

_getFillableOrders

Find fillable orders for a deposit period between given order IDs

For use if getFillableOrders(uint8 depositPeriod_) exceeds limit

WARNING: Gas-intensive. Intended for off-chain use only.

function _getFillableOrders(uint8 depositPeriod_, uint256 index0, uint256 index1)
internal
view
returns (uint256[] memory);

Parameters

NameTypeDescription
depositPeriod_uint8The deposit period
index0uint256The starting order ID
index1uint256The ending order ID

Returns

NameTypeDescription
<none>uint256[]uint256[] The IDs of the fillable orders

_isOrderFillable

Check if an order is fillable

function _isOrderFillable(uint256 orderId_, uint8 depositPeriod_) internal view returns (bool);

Parameters

NameTypeDescription
orderId_uint256The ID of the order
depositPeriod_uint8The deposit period

Returns

NameTypeDescription
<none>boolbool Whether the order is fillable

_requireEnabledDepositPeriod

function _requireEnabledDepositPeriod(uint8 depositPeriod_) private view;

_onlyOwner

Implementation-specific validation of ownership

Calls Ownable's _checkOwner()

function _onlyOwner() internal view override;

_enable

Implementation-specific enable function

No-op

function _enable(bytes calldata enableData_) internal override;

Parameters

NameTypeDescription
enableData_bytesCustom data that can be used by the implementation. The format of this data is left to the discretion of the implementation.

_disable

Implementation-specific disable function

No-op

function _disable(bytes calldata disableData_) internal override;

Parameters

NameTypeDescription
disableData_bytesCustom data that can be used by the implementation. The format of this data is left to the discretion of the implementation.

onERC721Received

ERC721 receiver function

function onERC721Received(address, address, uint256, bytes calldata) external pure override returns (bytes4);

Returns

NameTypeDescription
<none>bytes4bytes4 The selector of the function

supportsInterface

function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool);