Swap

Git Source

Inherits: ISwap, Context, IERC777Recipient, Initializable, ReentrancyGuard, Pausable, AccessControlEnumerable

State Variables

_mira

address internal _mira;

_minCsb

uint256 internal _minCsb;

_minMira

uint256 internal _minMira;

_orders

mapping(uint256 orderId => DataTypes.SellOrder sellOrder) internal _orders;

_orderCount

uint256 internal _orderCount;

SELL_MIRA

uint8 public constant SELL_MIRA = 1;

SELL_CSB

uint8 public constant SELL_CSB = 2;

OPERATION_TYPE_ACCEPT_ORDER

uint256 public constant OPERATION_TYPE_ACCEPT_ORDER = 1;

OPERATION_TYPE_SELL_MIRA

uint256 public constant OPERATION_TYPE_SELL_MIRA = 2;

ERC1820_REGISTRY

IERC1820Registry public constant ERC1820_REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);

TOKENS_RECIPIENT_INTERFACE_HASH

bytes32 public constant TOKENS_RECIPIENT_INTERFACE_HASH = keccak256("ERC777TokensRecipient");

ADMIN_ROLE

bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");

Functions

constructor

constructor();

initialize

Initializes the MarketPlace, setting the WCSB contract address.

function initialize(address mira_, uint256 minCsb_, uint256 minMira_, address admin) external override initializer;

Parameters

NameTypeDescription
mira_addressThe address of MIRA contract.
minCsb_uint256The minimum amount of CSB to sell.
minMira_uint256The minimum amount of MIRA to sell.
adminaddressThe address of the contract admin.

pause

Pauses interaction with the contract. Requirements:

  • The caller must have the ADMIN_ROLE.
function pause() external override whenNotPaused onlyRole(ADMIN_ROLE);

unpause

Resumes interaction with the contract. Requirements:

  • The caller must have the ADMIN_ROLE.
function unpause() external override whenPaused onlyRole(ADMIN_ROLE);

setMinMira

Sets the minimum amount of MIRA to sell.

function setMinMira(uint256 minMira_) external override onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
minMira_uint256The minimum amount of MIRA to sell.

setMinCsb

Sets the minimum amount of CSB to sell.

function setMinCsb(uint256 minCsb_) external override onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
minCsb_uint256The minimum amount of CSB to sell.

tokensReceived

Called by an {IERC777} token contract whenever tokens are being moved or created into a registered account to (this contract).
The userData/operatorData should be an abi encoded bytes of two uint256, the first uint256 represents operation type.
opType = 1: accept an order.
opType = 2: sell MIRA for CSB.

function tokensReceived(
    address,
    address from,
    address to,
    uint256 amount,
    bytes calldata userData,
    bytes calldata operatorData
) external override(IERC777Recipient);

Parameters

NameTypeDescription
<none>address
fromaddressThe address sending the tokens.
toaddressThe address of the recipient.
amountuint256The amount of tokens being transferred.
userDatabytesThe data provided by the token holder.
operatorDatabytesThe data provided by the operator (if any).

sellMIRA

Sells MIRA for CSB. Creates a SellOrder and emits the SellMIRA event.

function sellMIRA(uint256 miraAmount, uint256 expectedCsbAmount) external override returns (uint256 orderId);

Parameters

NameTypeDescription
miraAmountuint256The amount of MIRA to sell.
expectedCsbAmountuint256The expected amount of CSB to receive.

Returns

NameTypeDescription
orderIduint256The new created order id.

sellCSB

Sells CSB for MIRA. Creates a SellOrder and emits the SellCSB event.

The amount of CSB to sell must be specified in the msg.value.

function sellCSB(uint256 expectedMiraAmount) external payable override whenNotPaused returns (uint256 orderId);

Parameters

NameTypeDescription
expectedMiraAmountuint256The expected amount of MIRA to receive.

Returns

NameTypeDescription
orderIduint256The new created order id.

cancelOrder

Cancels a sell order and refunds to the seller. Deletes a given SellOrder and emits the SellOrderCanceled event.

function cancelOrder(uint256 orderId) external override whenNotPaused nonReentrant;

Parameters

NameTypeDescription
orderIduint256The order id to cancel.

acceptOrder

Accepts a sell order and transfers the tokens to the traders. Deletes a given SellOrder and emits the SellOrderMatched event.

function acceptOrder(uint256 orderId) external payable override;

Parameters

NameTypeDescription
orderIduint256The order id to accept.

getOrder

Returns the SellOrder struct of a given order id.

function getOrder(uint256 orderId) external view override returns (DataTypes.SellOrder memory);

Parameters

NameTypeDescription
orderIduint256The order id to get.

Returns

NameTypeDescription
<none>DataTypes.SellOrderorder The SellOrder struct.

mira

Returns the address of MIRA contract.

function mira() external view override returns (address);

Returns

NameTypeDescription
<none>addressThe address of MIRA contract.

getMinMira

Returns the minimum amount of MIRA to sell.

function getMinMira() external view override returns (uint256);

Returns

NameTypeDescription
<none>uint256The minimum amount of MIRA to sell.

getMinCsb

Returns the minimum amount of CSB to sell.

function getMinCsb() external view override returns (uint256);

Returns

NameTypeDescription
<none>uint256The minimum amount of CSB to sell.

_sellMIRA

function _sellMIRA(address owner, uint256 miraAmount, uint256 expectedCsbAmount, bool onTokensReceived)
    internal
    nonReentrant
    whenNotPaused
    returns (uint256 orderId);

_acceptOrder

function _acceptOrder(uint256 orderId, address buyer, uint256 erc777Amount) internal nonReentrant whenNotPaused;