Swap
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
Name | Type | Description |
---|---|---|
mira_ | address | The address of MIRA contract. |
minCsb_ | uint256 | The minimum amount of CSB to sell. |
minMira_ | uint256 | The minimum amount of MIRA to sell. |
admin | address | The 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
Name | Type | Description |
---|---|---|
minMira_ | uint256 | The 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
Name | Type | Description |
---|---|---|
minCsb_ | uint256 | The 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
Name | Type | Description |
---|---|---|
<none> | address | |
from | address | The address sending the tokens. |
to | address | The address of the recipient. |
amount | uint256 | The amount of tokens being transferred. |
userData | bytes | The data provided by the token holder. |
operatorData | bytes | The 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
Name | Type | Description |
---|---|---|
miraAmount | uint256 | The amount of MIRA to sell. |
expectedCsbAmount | uint256 | The expected amount of CSB to receive. |
Returns
Name | Type | Description |
---|---|---|
orderId | uint256 | The 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
Name | Type | Description |
---|---|---|
expectedMiraAmount | uint256 | The expected amount of MIRA to receive. |
Returns
Name | Type | Description |
---|---|---|
orderId | uint256 | The 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
Name | Type | Description |
---|---|---|
orderId | uint256 | The 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
Name | Type | Description |
---|---|---|
orderId | uint256 | The 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
Name | Type | Description |
---|---|---|
orderId | uint256 | The order id to get. |
Returns
Name | Type | Description |
---|---|---|
<none> | DataTypes.SellOrder | order The SellOrder struct. |
mira
Returns the address of MIRA contract.
function mira() external view override returns (address);
Returns
Name | Type | Description |
---|---|---|
<none> | address | The address of MIRA contract. |
getMinMira
Returns the minimum amount of MIRA to sell.
function getMinMira() external view override returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The minimum amount of MIRA to sell. |
getMinCsb
Returns the minimum amount of CSB to sell.
function getMinCsb() external view override returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The 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;