Contract

interface IDEXConstants
function MAKER_SIDE()
external
pure
returns (uint8)

Returns the index of maker side in the swap components array.

function TAKER_SIDE()
external
pure
returns (uint8)

Returns the index of taker side in the swap components array.

function ERC1155_ASSET()
external
pure
returns (uint8)

Returns the asset type for ERC1155 swap components.

function ERC721_ASSET()
external
pure
returns (uint8)

Returns the asset type for ERC721 swap components.

function ERC20_ASSET()
external
pure
returns (uint8)

Returns the asset type for ERC20 swap components.

function ETHER_ASSET()
external
pure
returns (uint8)

Returns to asset type for Ether swap components.

function OPEN_SWAP()
external
pure
returns (uint8)

Returns the swap status for open (i.e. active) swaps.

function CLOSED_SWAP()
external
pure
returns (uint8)

Returns the swap status for closed swaps.

function DROPPED_SWAP()
external
pure
returns (uint8)

Returns the swap status for dropped swaps.

interface IDEXAccessControl
function locked()
external
view
returns (bool)

Return the locked state of the DEX. In locked state, all transactional functions are disabled.

Return

True if the DEX is in locked state, false if the DEX is in unlocked state.

function deprecated()
external
view
returns (bool)

Return the deprecated state of the DEX. In deprecated state, no new swaps can be opened. All other functions remain intact.

Return

True if the DEX is in deprecated state.

function lock(bool lock_)
external

Lock the DEX in case of an emergency.

Parameters
  • lockTrue to lock the DEX, false to unlock the DEX.

function deprecate(bool deprecate_)
external

Deprecate the DEX if a new contract is rolled out.

Parameters
  • deprecateTrue to deprecate the DEX, false to lift DEX deprecation.

event Locked(bool locked_)

Emitted when the DEX locked state changed, see locked.

Parameters
  • lockedTrue if the DEX was locked, false if the DEX was unlocked.

event Deprecated(bool deprecated_)

Emitted when the DEX deprecated state changed, see deprecated.

Parameters
  • deprecatedTrue if the DEX was deprecated, false if DEX deprecation was lifted.

interface INFTProtocolDEX
struct Component

Datastructure of a swap component representing an asset of ERC1155, ERC721, ERC20, or ETH. ERC1155 batches are supported through tokenIds and amounts as arrays.

Members
  • assetType (uint8) – 0 - ERC1155, 1 - ERC721, 2 - ERC20, 3 - Ether.

  • tokenAddress (address) – address of the token.

  • tokenIDs (uint256[]) – array if token ids. Multiple token ids can be specified for ERC1155 batch transfers. For ERC721 transfers, this array holds one value.

  • amounts (uint256[]) – array if amounts. Multiple amounts can be specified for ERC1155 batch transfers. For ERC721 transfers, this arrray holds one value.

struct Swap

Datastructure representing a swap, which allows for exchanging arbitrary lists of ERC1155, ERC721, ERC20, and Ether assets.

Example:

Offer [ERC721, ERC721, ERC1155 Batch, ERC1155 Batch, ERC20, …, ERC20, Ether] for [ERC20, …, ERC20, ERC721, …, ERC721].

Members
  • id (uint256) – id of the swap.

  • status (uint8) – swap status: 0 - open, 1 - closed, 2 - dropped.

  • components (Component[][2]) – Two arrays of Component, one for the maker side and one for the taker side.

  • maker (address) – address of the maker account.

  • taker (address) – address of the taker account.

  • whitelist (bool) – true if whitelist for this swap is enabled, false otherwise.

  • custodial (bool) – true if the swap is custodial, false otherwise.

  • expiration (uint256) – block where the swap expires, 0 for no expiration.

function name()
external
view
returns (string memory)

Returns the name of the DEX contract.

function majorVersion()
external
view
returns (uint16)

Returns the major version of the DEX contract.

function minorVersion()
external
view
returns (uint16)

Returns the minor version of the DEX contract.

function token()
external
view
returns (address)

Returns the address of NFT Protocol Token.

function numSwaps()
external
view
returns (uint256)

The total number of swaps in the contract.

function whitelistedWith(address sender_, uint256 swapID_)
external
view
returns (bool)

Returns True if sender is in the whitelist of a swap.

Parameters
  • sender – Account of the sender.

  • swapID – ID of the swap.

function whitelisted(uint256 swapID_)
external
view
returns (bool)

Same as whitelisted with the sender account.

function requireCanTakeSwapWith(address sender_, uint256 swapID_)
external
view

Checks if a swap can be taken by the caller.

This function reverts with a message if the swap cannot be taken by the caller. Reasons include: - Swap not open. - Swap has a whitelist and caller is not included. - Taker assets are not available. - Swap is non-custodial and maker has not made all assets available (e.g., moved assets or revoked allowances). - Sender is swap maker.

Parameters
  • sender – Address of the hypothetical swap taker.

  • swapID – ID of the swap.

function requireCanTakeSwap(uint256 swapID_)
external
view

Same as requireCanTakeSwapWith with the sender account.

function requireMakerAssets(uint256 swapID_)
external
view

Checks if all maker assets are available for non-custodial swaps, including balances and allowances.

Parameters
  • swapID – ID of the swap.

function requireTakerAssetsWith(address sender_, uint256 swapID_)
external
view

Checks if all taker assets are available.

Parameters
  • sender – Address of the hypothetical swap taker.

  • swapID – ID of the swap.

function requireTakerAssets(uint256 swapID_)
external
view

Same as requireTakerAssetsWith with the sender account.

function tvl()
external
view
returns (uint256)

Returns the total ether value locked (tvl), including all deposited swap ether, excluding the fees collected by the administrator.

function makeSwap(Component[] calldata make_, Component[] calldata take_, bool custodial_, uint256 expiration_, address[] calldata whitelist_)
external
payable

Opens a swap with a list of assets on the maker side (make_) and on the taker side (take_).

All assets listed on the maker side have to be available in the caller’s account. They are transferred to the DEX contract during this contract call.

If the maker list contains Ether assets, then the total Ether funds have to be sent along with the message of this contract call.

Emits a SwapMade event, if successful.

Parameters
  • make – Array of components for the maker side of the swap.

  • take – Array of components for the taker side of the swap.

  • custodial – True if the swap is custodial, e.g., maker assets are transfered into the DEX.

  • expiration – Block number at which the swap expires, 0 for no expiration.

  • whitelist – List of addresses that shall be permitted to take the swap. If empty, then whitelisting will be disabled for this swap.

function takeSwap(uint256 swapID_, uint256 seqNum_)
external
payable

Takes a swap that is currently open.

All assets listed on the taker side have to be available in the caller’s account, see make. They are transferred to the maker’s account in exchange for the maker’s assets that currently reside within the DEX contract for custodial swaps, which are transferred to the taker’s account. For non-custodial swaps, the maker assets are transfered from the maker account. This functions checks allowances, ownerships, and balances of all assets that are involved in this swap.

The fee for this trade has to be sent along with the message of this contract call, see fees.

If the taker list contains ETHER assets, then the total ETHER value also has to be added in WEI to the value that is sent along with the message of this contract call.

This function requires the caller to provide the most recent sequence number of the swap, which only changes when the swap ether component is updated. The sequence number is used to prevent mempool front-running attacks.

Parameters
  • swapID – ID of the swap to be taken.

  • seqNum – Most recent sequence number of the swap.

function dropSwap(uint256 swapID_)
external

Drop a swap and return the assets on the maker side back to the maker.

All ERC1155, ERC721, and ERC20 assets will the transferred back directly to the maker. Ether assets are booked to the maker account and can be extracted via withdraw and withdrawFull.

Only the swap maker will be able to call this function successfully.

Only swaps that are currently open can be dropped.

Parameters
  • swapID – id of the swap to be dropped.

function amendSwapEther(uint256 swapID_, uint8 side_, uint256 value_)
external
payable

Amend ether value of a swap.

Parameters
  • swapID – ID fo the swap to be modified.

  • side – Swap side to modify, see MAKER_SIDE and TAKER_SIDE.

  • value – New Ether value in Wei to be set for the swap side.

function takerSendValueWith(address sender_, uint256 swapID_)
external
view
returns (uint256)

Returns the total Ether value in Wei that is required by the sender to take a swap.

Parameters
  • sender – Address of the sender.

  • swapID – ID of the swap.

function takerSendValue(uint256 swapID_)
external
view
returns (uint256)

Same as takerSendValueWith with the sender account.

function makerSendValueWith(address sender_, Component[] calldata make_)
external
view
returns (uint256)

Returns the total Ether value in Wei that is required by the sender to make a swap.

Parameters
  • sender – Address of the sender.

  • make – Component array for make side of the swap, see makeSwap.

function makerSendValue(Component[] calldata make_)
external
view
returns (uint256)

Same as makerSendValueWith with the sender account.

function amendSwapEtherSendValueWith(address sender_, uint256 swapID_, uint8 side_, uint256 value_)
external
view
returns (uint256)

Returns the total Ether value in Wei that is required by the caller to send in order to adjust the Ether of a swap, see adjustSwapEther.

Parameters
  • sender – Sender account.

  • swapID – ID of the swap to be modified.

  • side – Swap side to modify, see MAKER_SIDE and TAKER_SIDE.

  • value – New Ether value in Wei to be set for the swap side.

function amendSwapEtherSendValue(uint256 swapID_, uint8 side_, uint256 value_)
external
view
returns (uint256)

Same as amendSwapEtherSendValueWith with the sender account.

function balanceOf(address of_)
external
view
returns (uint256)

Returns the Wei of Ether balance of a user, see withdraw and withdrawFull.

Parameters
  • of – Address of the account.

function balance()
external
view
returns (uint256)

Same as balanceOf with the sender account.

function withdraw(uint256 value_)
external

Withdraw funds in Wei of Ether from the contract, see balance.

Parameters
  • value – Wei of Ether to withdraw.

function withdrawFull()
external

Withdraw all Ether funds from the contract that are available to the caller, see withdraw.

function rescue()
external

Rescue funds that are stuck in the DEX, e.g., no user has access to. This function only runs successfully if , which should never happen.

function swap(uint256 swapID_)
external
view
returns (Swap memory)

Get a swap, including closed and dropped swaps.

Parameters
  • swapID – ID of the swap.

Return

Swap data structure.

function flatFee()
external
view
returns (uint256)

The flat fee in Wei of Ether to take a swap, see setFlatFee.

Return

Flat fee in Wei of Ether.

function lowFee()
external
view
returns (uint256)

The threshold of NFT Protocol token holdings for swap takersto get a 10% discount on the flat fee.

Return

Threshold for amounts in smallest unit of NFT Protocol token holdings to get a 10% discount.

function highFee()
external
view
returns (uint256)

The threshold of NFT Protocol token holdings for swap takes to waive the flat fee.

Return

Threshold for amount in smallest unit of NFT Protocol token holdings to waive the flat fee.

function takerFeeWith(address sender_)
external
view
returns (uint256)

Returns the taker fee owed for a swap, taking into account the holdings of NFT Protocol tokens, see flatFee, lowFee, highFee.

Parameters
  • sender – Address of the sender.

function takerFee()
external
view
returns (uint256)

Same as takerFeeOf with the sender account.

function setFees(uint256 flatFee_, uint256 lowFee_, uint256 highFee_)
external

Set the flat fee structure for swaps taking.

Parameters
  • flatFee – Flat fee in Wei of Ether paid by the taker of swap, if they hold less than lowFee_ in smallest units of NFT Protocol token.

  • lowFee – Threshold in smallest unit of NFT Protocol token to be held by the swap taker to get a 10% fee discount.

  • highFee – Threshold in smallest unit of NFT Protocol token to be held by the swap taker to pay no fees.

event SwapMade(uint256 indexed swapID, Component[] make, Component[] take, address indexed maker, bool indexed custodial, uint256 expiration, address[] whitelist)

Emitted when a swap was opened, see makeSwap.

Parameters
  • swapID – ID of the swap.

  • make – Array of swap components on the maker side, see Component.

  • take – Array of swap components on the taker side, see Component.

  • maker – Account of the swap maker.

  • custodial – True if swap is custodial.

  • expiration – Block where the swap expires, 0 for no expiration.

  • whitelist – Array of addresses that are allowed to take the swap.

event SwapTaken(uint256 indexed swapID, uint256 seqNum, address indexed taker, uint256 fee)

Emitted when a swap was executed, see takeSwap.

Parameters
  • swapID – ID of the swap that was taken.

  • seqNum – Sequence number of the swap.

  • taker – Address of the account that executed the swap.

  • fee – Fee value in Wei of Ether paid by the swap taker.

event SwapDropped(uint256 indexed swapID)

Emitted when a swap was dropped, ie. cancelled.

Parameters
  • swapID – ID of the dropped swap.

event SwapEtherAmended(uint256 indexed swapID, uint256 seqNum, uint8 indexed side, uint256 index, uint256 from, uint256 to)

Emitted when a Ether component of a swap was amended, see amendSwapEther.

Parameters
  • swapID – ID of the swap.

  • seqNum – New sequence number of the swap.

  • side – Swap side, either MAKER_SIDE or TAKER_SIDE.

  • index – Index of the amended or added Ether component in the components array.

  • from – Previous amount of Ether in Wei.

  • to – Updated amount of Ether in Wei.

event FeesChanged(uint256 flatFee, uint256 lowFee, uint256 highFee)

Emitted when the flat fee parameters have changed, see setFees.

Parameters
  • flatFee – Fee to be paid by a swap taker in Wei of Ether.

  • lowFee – Threshold of NFT Protocol tokens to be held by a swap taker in order to get a 10% fee discount.

  • highFee – Threshold of NFT Protocol tokens to be held by a swap taker in order to pay no fees.

event Deposited(address indexed account, uint256 value)

Emitted when Ether funds were deposited into the DEX, see balance.

Parameters
  • account – Address of the account.

  • value – Wei of Ether deposited.

event Withdrawn(address indexed account, uint256 value)

Emitted when Ether funds were withdrawn from the DEX, see balance.

Parameters
  • account – Address of the account.

  • value – Wei of Ether withdrawn.

event Spent(address indexed spender, uint256 value, uint256 indexed swapID)

Emitted when Ether funds were spent during a make or take swap operation, see balance.

Parameters
  • spender – Address of the spender.

  • value – Wei of Ether spent.

  • swapID – ID of the swap, the Ether was spent on, see takeSwap, amendSwapEther.

event Rescued(address indexed recipient, uint256 value)

Emitted when funds were rescued, see rescue.

Parameters
  • recipient – Address of the beneficiary, e.g., the administrator account.

  • value – Wei of Ether rescued.

contract DEXConstants
uint8 public constant MAKER_SIDE
uint8 public constant TAKER_SIDE
uint8 public constant ERC1155_ASSET
uint8 public constant ERC721_ASSET
uint8 public constant ERC20_ASSET
uint8 public constant ETHER_ASSET
uint8 public constant OPEN_SWAP
uint8 public constant CLOSED_SWAP
uint8 public constant DROPPED_SWAP
contract DEXAccessControl is IDEXAccessControl, Ownable
bool public locked
Inheritdoc

IDEXAccessControl

bool public deprecated
Inheritdoc

IDEXAccessControl

modifier unlocked()

Unlocked DEX function modifier.

modifier supported()

Supported (not deprecated) function modifier.

modifier notOwner()

Not owner function modifier.

constructor(address owner_)

Initializes access control.

Parameters
  • owner – Address of the administrator account (multisig).

function lock(bool lock_)
external
override
onlyOwner

This function is only accessible by the administrator account.

Inheritdoc

IDEXAccessControl

function deprecate(bool deprecate_)
external
override
onlyOwner

This function is only accessible by the administrator account.

Inheritdoc

IDEXAccessControl

function renounceOwnership()
public
override
onlyOwner

This function is disabled.

contract NFTProtocolDEX is INFTProtocolDEX, DEXAccessControl, DEXConstants, ERC1155Holder, ERC721Holder, ReentrancyGuard
string public constant name
Inheritdoc

INFTProtocolDEX

uint16 public constant majorVersion
Inheritdoc

INFTProtocolDEX

uint16 public constant minorVersion
Inheritdoc

INFTProtocolDEX

address public immutable token
Inheritdoc

INFTProtocolDEX

uint256 public flatFee

Default is 0.001 Ether.

Inheritdoc

INFTProtocolDEX

uint256 public lowFee

Default is 10,000 tokens.

Inheritdoc

INFTProtocolDEX

uint256 public highFee

Default is 100,000 tokens.

Inheritdoc

INFTProtocolDEX

uint256 public numSwaps
Inheritdoc

INFTProtocolDEX

mapping (address => uint256) private _balances

Map of Ether balances.

uint256 public tvl

Total value locked, including all swap ether, excluding the contract owner’s fees.

mapping (uint256 => Swap) private _swaps

Mapping from swapID to swap structures for all swaps, including closed and dropped swaps.

mapping (uint256 => mapping (address => bool)) private _whitelists

Mapping from swapID to swap whitelist.

constructor(address token_, address admin_)
DEXAccessControl(admin_)

Initializes the contract with the address of the NFT Protocol token and the address of the administrator account.

Parameters
  • token – address of the NFT Protocol ERC20 token

  • admin – address of the administrator account (multisig)

function makeSwap(Component[] calldata make_, Component[] calldata take_, bool custodial_, uint256 expiration_, address[] calldata whitelist_)
external
payable
override
supported
unlocked
notOwner
nonReentrant

This function is not available: - in deprecated or locked mode, see :sol:func:deprecated and :sol:func:locked, respectively. - to the contract administrator, see :sol:func:owner.

Inheritdoc

INFTProtocolDEX

function takeSwap(uint256 swapID_, uint256 seqNum_)
external
payable
override
unlocked
notOwner
nonReentrant

This function is not available: - in locked mode, see :sol:func:locked, - to the contract administrator, see :sol:func:owner.

Inheritdoc

INFTProtocolDEX

function dropSwap(uint256 swapID_)
external
override
unlocked
notOwner
nonReentrant

This function is not available: - in locked mode, see :sol:func:locked, - to the contract administrator, see :sol:func:owner.

Inheritdoc

INFTProtocolDEX

function amendSwapEther(uint256 swapID_, uint8 side_, uint256 value_)
external
payable
override
unlocked
notOwner
nonReentrant
validSide(side_)
validSwap(swapID_)

This function is not available: - in locked mode, see :sol:func:locked, - to the contract administrator, see :sol:func:owner.

Inheritdoc

INFTProtocolDEX

function swap(uint256 swapID_)
external
view
override
validSwap(swapID_)
returns (Swap memory)

This function requires the swap to be defined.

Inheritdoc

INFTProtocolDEX

function whitelistedWith(address sender_, uint256 swapID_)
public
view
override
validSwap(swapID_)
returns (bool)

This function requires the swap to be defined.

Inheritdoc

INFTProtocolDEX

function whitelisted(uint256 swapID_)
external
view
override
returns (bool)
Inheritdoc

INFTProtocolDEX

function requireCanTakeSwapWith(address sender_, uint256 swapID_)
public
view
override
unlocked
validSwap(swapID_)

This function requires the swap to be defined and open.

Inheritdoc

INFTProtocolDEX

function requireCanTakeSwap(uint256 swapID_)
external
view
override
Inheritdoc

INFTProtocolDEX

function requireMakerAssets(uint256 swapID)
external
view
override
unlocked
validSwap(swapID)
Inheritdoc

INFTProtocolDEX

function requireTakerAssetsWith(address sender_, uint256 swapID_)
public
view
override
unlocked
validSwap(swapID_)
Inheritdoc

INFTProtocolDEX

function requireTakerAssets(uint256 swapID_)
public
view
override
Inheritdoc

INFTProtocolDEX

function balanceOf(address of_)
public
view
override
returns (uint256)
Inheritdoc

INFTProtocolDEX

function balance()
external
view
override
returns (uint256)
Inheritdoc

INFTProtocolDEX

function withdraw(uint256 value_)
external
override
Inheritdoc

INFTProtocolDEX

function withdrawFull()
external
override
Inheritdoc

INFTProtocolDEX

function makerSendValueWith(address sender_, Component[] calldata make_)
public
view
override
supported
unlocked
returns (uint256)
Inheritdoc

INFTProtocolDEX

function makerSendValue(Component[] calldata make_)
external
view
override
returns (uint256)
Inheritdoc

INFTProtocolDEX

function takerSendValueWith(address sender_, uint256 swapID_)
public
view
override
unlocked
returns (uint256)
Inheritdoc

INFTProtocolDEX

function takerSendValue(uint256 swapID_)
external
view
override
unlocked
returns (uint256)
Inheritdoc

INFTProtocolDEX

function amendSwapEtherSendValueWith(address sender_, uint256 swapID_, uint8 side_, uint256 value_)
public
view
override
unlocked
validSide(side_)
returns (uint256)
Inheritdoc

INFTProtocolDEX

function amendSwapEtherSendValue(uint256 swapID_, uint8 side_, uint256 value_)
external
view
override
returns (uint256)
Inheritdoc

INFTProtocolDEX

function takerFeeWith(address sender_)
public
view
override
unlocked
returns (uint256)
Inheritdoc

INFTProtocolDEX

function takerFee()
external
view
override
returns (uint256)
Inheritdoc

INFTProtocolDEX

function setFees(uint256 flatFee_, uint256 lowFee_, uint256 highFee_)
external
override
supported
onlyOwner

This function can only be called by the contract administrator, see owner.

Inheritdoc

INFTProtocolDEX

function rescue()
external
override
onlyOwner
nonReentrant

This function can only be called by the contract administrator, see owner.

Inheritdoc

INFTProtocolDEX