# PermissionedExchange

For now PermissionedExchange contract allows traders trade their SQTs on admin sent orders, later on we may allow others to send their orders. Controllers may set the trade quota for trader, and trader cannot trade over the their quota. It provides a way for indexers to swap their rewards(SQT) to stable token with a fixed exchange rate.

# ExchangeOrder

struct ExchangeOrder {
  address tokenGive;
  address tokenGet;
  uint256 amountGive;
  uint256 amountGet;
  address sender;
  uint256 expireDate;
  uint256 pairOrderId;
  uint256 tokenGiveBalance;
}

# STATES

# settings

ISettings contract which stores SubQuery network contracts address

contract ISettings settings

# nextOrderId

The next order Id

uint256 nextOrderId

# tradeQuota

Trade quota for traders for specific token: address => trader address => trade Quota

mapping(address => mapping(address => uint256)) tradeQuota

# exchangeController

Account address is controller or not

mapping(address => bool) exchangeController

# orders

Orders: orderId => ExchangeOrder

mapping(uint256 => struct PermissionedExchange.ExchangeOrder) orders

# tradeLimitation

stable coin trade limitation

uint256 tradeLimitation

# accumulatedTrades

accumulated stable coin trades per account

mapping(address => uint256) accumulatedTrades

# tradeLimitationPerAccount

stable coin trade limitation per account

uint256 tradeLimitationPerAccount

# EVENTS

# ExchangeOrderSent

Emitted when exchange order sent.

event ExchangeOrderSent(uint256 orderId, address sender, address tokenGive, address tokenGet, uint256 amountGive, uint256 amountGet, uint256 expireDate)

# Trade

Emitted when trader trade on exist orders.

event Trade(uint256 orderId, address tokenGive, uint256 amountGive, address tokenGet, uint256 amountGet)

# OrderSettled

Emitted when expired exchange order settled.

event OrderSettled(uint256 orderId, address tokenGive, uint256 amountGive, address tokenGet, uint256 amountGet)

# QuotaAdded

Emitted when controller add trade quota to trader.

event QuotaAdded(address token, address account, uint256 amount)

# ExchangeOrderChanged

Emitted when addLiquidity by owner.

event ExchangeOrderChanged(uint256 orderId, uint256 tokenGiveBalance)

# FUNCTIONS

# initialize

Initialize the contract make order start from 1 and set controller account.

function initialize(contract ISettings _settings, address[] _controllers) external
Name Type Description
_settings contract ISettings ISettings contract
_controllers address[] List of addresses to set as controller account

# setSettings

Update setting state.

function setSettings(contract ISettings _settings) external
Name Type Description
_settings contract ISettings ISettings contract

# setController

Set controller role for this contract, controller have the permission to addQuota for trader.

function setController(address _controller, bool _isController) external
Name Type Description
_controller address The account address to set.
_isController bool Set to controller or not.

# setTradeLimitation

Set the stable coin trading limitation in single transaction.

function setTradeLimitation(uint256 _limit) external
Name Type Description
_limit uint256 New limitation.

# setTradeLimitationPerAccount

Set the stable coin trading limitation in single transaction.

function setTradeLimitationPerAccount(uint256 _limit) external
Name Type Description
_limit uint256 New limitation.

# setAccumulatedTrades

Override accumulatedTrades for given user

function setAccumulatedTrades(address user, uint256 newValue) external
Name Type Description
user address user address
newValue uint256 new accumulatedTrades value

# addLiquidity

Add liquidity to a exist order.

function addLiquidity(uint256 _orderId, uint256 _amount) external
Name Type Description
_orderId uint256 order id
_amount uint256 amount to add

# addQuota

allow controllers to add the trade quota to traders on specific token.

function addQuota(address _token, address _account, uint256 _amount) external
Name Type Description
_token address Token address to add quota.
_account address Trader address to add quota.
_amount uint256

# sendOrder

only onwer have the permission to send the order for now, and traders can do exchanges on these exist orders.

function sendOrder(address _tokenGive, address _tokenGet, uint256 _amountGive, uint256 _amountGet, uint256 _expireDate, uint256 _pairId, uint256 _tokenGiveBalance) public
Name Type Description
_tokenGive address The token address order want give.
_tokenGet address The token address order want get.
_amountGive uint256 Amount of give token to calculate exchange rate.
_amountGet uint256 Amount of get token to calculate exchange rate.
_expireDate uint256 Exchange order expire date in uinx timestamp.
_pairId uint256 The order id of its pair order. 0 means no pair order.
_tokenGiveBalance uint256 The balance of order give token amount.

# createPairOrders

admin have the permission to create pair orders, traders tarde on one of the pair orders, the token get will transfer to the token give of its pair order.

function createPairOrders(address _tokenGive, address _tokenGet, uint256 _amountGive, uint256 _amountGet, uint256 _expireDate, uint256 _tokenGiveBalance) public
Name Type Description
_tokenGive address The token address order want give.
_tokenGet address The token address order want get.
_amountGive uint256 Amount of give token to calculate exchange rate.
_amountGet uint256 Amount of get token to calculate exchange rate.
_expireDate uint256 Exchange order expire date in uinx timestamp.
_tokenGiveBalance uint256 The balance of order give token amount.

# trade

Traders do exchange on exchange orders, but need to trade under the trade quota. If the order has no pair order, the token get will transfer to order sender, otherwise will transfer to the token give of its pair order.

function trade(uint256 _orderId, uint256 _amount) public
Name Type Description
_orderId uint256 The order id to trade.
_amount uint256 The amount to trade.

# settleExpiredOrder

Everyone allowed to call settleExpiredOrder to settled expired order this will return left given token back to order sender.

function settleExpiredOrder(uint256 _orderId) public
Name Type Description
_orderId uint256 The order id to settle.

# cancelOrder

Order sender can cancel the sent order anytime, and this will return leftgiven token back to order sender.

function cancelOrder(uint256 _orderId) public
Name Type Description
_orderId uint256 The order id to cancel.