Contract

contract NFTProtocolDEX is ERC1155Holder, ERC721Holder, ReentrancyGuard
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.

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

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

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

event SwapMade(Component[] make, Component[] take, address indexed makerAddress, address[] whitelist, uint256 indexed id)

Event triggered when a swap was opened, see make.

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

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

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

  • id – id of the swap.

event SwapTaken(uint256 swapId, address takerAddress, uint256 fee)

Emitted when a swap was executed, see take.

Parameters
  • swapId – id of the swap that was taken.

  • takerAddress – address of the account that executed the swap.

  • fee – WEI of ETHER that was paid for the swap.

event SwapDropped(uint256 swapId)

Emitted when a swap was dropped, ie. cancelled.

Parameters
  • swapId – id of the dropped swap.

event Vote(uint256 flatFee, uint256 feeBypassLow, uint256 feeBypassHigh)

Emitted when fee parameters have changed, see vote, fees.

Parameters
  • flatFee – fee to be paid by a swap taker in WEI of ETHER.

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

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

address public msig

Multisig address for administrative functions.

address public immutable nftProtocolTokenAddress

Address of the ERC20 NFT Protcol token.

uint256 public flat

Flat fee for all trades in WEI of ETHER, default is 0.001 ETHER. The flat fee can be changed by the multisig account, see vote.

bool public locked

Indicates if the DEX is locked down in case of an emergency. The value is true if the DEX is locked, false otherwise.

uint256 public felo

Low threshold of NFT Protocol token balance where a 10% fee discount is enabled. See fees, vote.

uint256 public fehi

High threshold of NFT Protocol token balance where fees are waived. See fees, vote.

uint8 public constant ERC1155_ASSET

Asset type 0 for ERC1155 swap components.

uint8 public constant ERC721_ASSET

Asset type 1 for ERC721 swap components.

uint8 public constant ERC20_ASSET

Asset type 2 for ERC20 swap components.

uint8 public constant ETHER_ASSET

Asset type 3 for ETHER swap components.

uint8 public constant OPEN_SWAP

Swap status 0 for swaps that are open and active.

uint8 public constant CLOSED_SWAP

Swap status 1 for swaps that are closed.

uint8 public constant DROPPED_SWAP

Swap status 2 for swaps that are dropped, ie. cancelled.

mapping (uint256 => Swap) private swaps

Map holding all swaps (including cancelled and executed swaps).

uint256 public size
mapping (uint256 => mapping (address => bool)) public list

Map from swapId to whitelist of a swap.

constructor(address _nftProtocolToken, address _multisig)

Initializes the contract by setting the address of the NFT Protocol token and multisig (administrator) account.

Parameters
  • _nftProtocolToken – address of the NFT Protocol ERC20 token

  • _multisig – address of the administrator account

function make(Component[] calldata _make, Component[] calldata _take, address[] calldata _whitelist)
external
payable
nonReentrant
unlocked

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.

Parameters
  • _make – array of components for the maker side of the swap.

  • _take – array of components for the taker side of the swap.

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

function take(uint256 _swapId)
external
payable
nonReentrant
unlocked

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), which are transferred to the taker’s account.

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.

Parameters
  • _swapId – id of the swap to be taken.

function drop(uint256 _swapId)
external
nonReentrant
unlocked

Cancel 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. ETH assets are booked to the maker account and can be extracted via pull.

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 pend()
public
view
returns (uint256)

WEI of ETHER that can be withdrawn by a user, see pull.

function pull()
external
nonReentrant

Withdraw ETHER funds from the contract, see pend.

function swap(uint256 _swapId)
public
view
returns (Swap memory)

Get a swap, including cancelled and executed swaps.

Parameters
  • _swapId – id of the swap.

Return

swap struct.

function fees()
public
view
returns (uint256)

Calculate the fee owed for a trade. This function is usually called by the taker to determine the amount of ETH that has to be paid for a trade.

Return

fees in WEI of ETHER to be paid by the caller as a taker.

function vote(uint256 _flatFee, uint256 _feeBypassLow, uint256 _feeBypassHigh)
external
onlyMsig

Governance votes to set fees.

Parameters
  • _flatFee – flat fee in WEI of ETHER that has to be paid for a trade, if the taker has less than _feeBypassLow NFT Protocol tokens in its account.

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

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

function lock(bool _locked)
external
onlyMsig

Shut down the DEX in case of an emergency.

Only the msig will be able to call this function successfully.

Parameters
  • _lockedtrue to lock down the DEX, false to unlock the DEX.

function auth(address _to)
external
onlyMsig

Set multisig ie. administrator account.

Only the msig will be able to call this function successfully.

Parameters
  • _to – address of the new multisig/admin account

function lift()
external
onlyMsig

Rescue ETHER funds from the DEX that do not belong the a user, e.g., fees and ETHER that have been sent to the DEX accidentally.

This function books the contract’s ETHER funds that do not belong to a user, to the msig account and makes them available for withdrawal through pull.

The user funds that were transfered to the DEX through make are protected and cannot be extracted.

Only the msig account will be able to call this function successfully.

function checkAsset(Component memory _comp)
internal
pure
returns (uint256)
function checkAssets(Component[] memory _assets)
internal
pure
returns (uint256)