text: 'use strict';
  
  //  ---------------------------------------------------------------------------
  
  const Exchange = require ('./base/Exchange');
  const { ExchangeError } = require ('./base/errors');
  
  //  ---------------------------------------------------------------------------
  
  module.exports = class mixcoins extends Exchange {
      describe () {
          return this.deepExtend (super.describe (), {
              id: 'mixcoins',
              name: 'MixCoins',
              countries: ['GB', 'HK'],
              rateLimit: 1500,
              version: 'v1',
              has: {
                  CORS: false,
              },
              urls: {
                  logo: 'https://user-images.githubusercontent.com/1294454/30237212-ed29303c-9535-11e7-8af8-fcd381cfa20c.jpg',
                  api: 'https://mixcoins.com/api',
                  www: 'https://mixcoins.com',
                  doc: 'https://mixcoins.com/help/api/',
              },
              api: {
                  public: {
                      get: [
                          'ticker',
                          'trades',
                          'depth',
                     ],
                  },
                  private: {
                      post: [
                          'cancel',
                          'info',
                          'orders',
                          'order',
                          'transactions',
                          'trade',
                     ],
                  },
              },
              markets: {
                  'BTC/USD': { id: 'btc_usd', symbol: 'BTC/USD', base: 'BTC', quote: 'USD', maker: 0.0015, taker: 0.0025 },
                  'ETH/BTC': { id: 'eth_btc', symbol: 'ETH/BTC', base: 'ETH', quote: 'BTC', maker: 0.001, taker: 0.0015 },
                  'BCH/BTC': { id: 'bch_btc', symbol: 'BCH/BTC', base: 'BCH', quote: 'BTC', maker: 0.001, taker: 0.0015 },
                  'LSK/BTC': { id: 'lsk_btc', symbol: 'LSK/BTC', base: 'LSK', quote: 'BTC', maker: 0.0015, taker: 0.0025 },
                  'BCH/USD': { id: 'bch_usd', symbol: 'BCH/USD', base: 'BCH', quote: 'USD', maker: 0.001, taker: 0.0015 },
                  'ETH/USD': { id: 'eth_usd', symbol: 'ETH/USD', base: 'ETH', quote: 'USD', maker: 0.001, taker: 0.0015 },
              },
          });
      }
  
      async fetchBalance (params = {}) {
          let response = await this.privatePostInfo ();
          let balance = response['result']['wallet'];
          let result = { info: balance };
          let currencies = Object.keys (this.currencies);
          for (let i = 0; i < currencies.length; i++) {
              let currency = currencies[i];
              let lowercase = currency.toLowerCase ();
              let account = this.account ();
              if (lowercase in balance) {
                  account['free'] = parseFloat (balance[lowercase]['avail']);
                  account['used'] = parseFloat (balance[lowercase]['lock']);
                  account['total'] = this.sum (account['free'], account['used']);
              }
              result[currency] = account;
          }
          return this.parseBalance (result);
      }
  
      async fetchOrderBook (symbol, limit = undefined, params = {}) {
          let response = await this.publicGetDepth (this.extend ({
              market: this.marketId (symbol),
          }, params));
          return this.parseOrderBook (response['result']);
      }
  
      async fetchTicker (symbol, params = {}) {
          let response = await this.publicGetTicker (this.extend ({
              market: this.marketId (symbol),
          }, params));
          let ticker = response['result'];
          let timestamp = this.milliseconds ();
          let last = this.safeFloat (ticker, 'last');
          return {
              symbol,
              timestamp,
              datetime: this.iso8601 (timestamp),
              high: this.safeFloat (ticker, 'high'),
              low: this.safeFloat (ticker, 'low'),
              bid: this.safeFloat (ticker, 'buy'),
              bidVolume: undefined,
              ask: this.safeFloat (ticker, 'sell'),
              askVolume: undefined,
              vwap: undefined,
              open: undefined,
              close: last,
              last,
              previousClose: undefined,
              change: undefined,
              percentage: undefined,
              average: undefined,
              baseVolume: this.safeFloat (ticker, 'vol'),
              quoteVolume: undefined,
              info: ticker,
          };
      }
  
      parseTrade (trade, market) {
          let timestamp = parseInt (trade['date']) * 1000;
          return {
              id: trade['id'].toString (),
              info: trade,
              timestamp,
              datetime: this.iso8601 (timestamp),
              symbol: market['symbol'],
              type: undefined,
              side: undefined,
              price: this.safeFloat (trade, 'price'),
              amount: this.safeFloat (trade, 'amount'),
          };
      }
  
      async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
          let market = this.market (symbol);
          let response = await this.publicGetTrades (this.extend ({
              market: market['id'],
          }, params));
          return this.parseTrades (response['result'], market, since, limit);
      }
  
      async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
          let order = {
              market: this.marketId (symbol),
              op: side,
              amount,
          };
          if (type === 'market') {
              order['order_type'] = 1;
              order['price'] = price;
          } else {
              order['order_type'] = 0;
          }
          let response = await this.privatePostTrade (this.extend (order, params));
          return {
              info: response,
              id: response['result']['id'].toString (),
          };
      }
  
      async cancelOrder (id, symbol = undefined, params = {}) {
          return await this.privatePostCancel ({ id: id });
      }
  
      sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
          let url = this.urls['api'] + '/' + this.version + '/' + path;
          if (api === 'public') {
              if (Object.keys (params).length)
                  url += '?' + this.urlencode (params);
          } else {
              this.checkRequiredCredentials ();
              let nonce = this.nonce ();
              body = this.urlencode (this.extend ({
                  nonce,
              }, params));
              headers = {
                  'Content-Type': 'application/x-www-form-urlencoded',
                  Key: this.apiKey,
                  Sign: this.hmac (this.encode (body), this.secret, 'sha512'),
              };
          }
          return { url, method, body, headers: headers };
      }
  
      async request (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
          let response = await this.fetch2 (path, api, method, params, headers, body);
          if ('status' in response)
              if (response['status'] === 200)
                  return response;
          throw new ExchangeError (this.id + ' ' + this.json (response));
      }
  };
  