<?php

namespace ccxt\async;

// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

use Exception; // a common import
use ccxt\async\abstract\mercado as Exchange;
use ccxt\ExchangeError;
use ccxt\ArgumentsRequired;
use ccxt\InvalidOrder;
use ccxt\Precise;
use \React\Async;
use \React\Promise\PromiseInterface;

class mercado extends Exchange {

    public function describe(): mixed {
        return $this->deep_extend(parent::describe(), array(
            'id' => 'mercado',
            'name' => 'Mercado Bitcoin',
            'countries' => array( 'BR' ), // Brazil
            'rateLimit' => 1000,
            'version' => 'v3',
            'has' => array(
                'CORS' => true,
                'spot' => true,
                'margin' => false,
                'swap' => false,
                'future' => false,
                'option' => false,
                'addMargin' => false,
                'borrowCrossMargin' => false,
                'borrowIsolatedMargin' => false,
                'borrowMargin' => false,
                'cancelOrder' => true,
                'closeAllPositions' => false,
                'closePosition' => false,
                'createMarketOrder' => true,
                'createOrder' => true,
                'createReduceOnlyOrder' => false,
                'createStopLimitOrder' => false,
                'createStopMarketOrder' => false,
                'createStopOrder' => false,
                'fetchAllGreeks' => false,
                'fetchBalance' => true,
                'fetchBorrowInterest' => false,
                'fetchBorrowRate' => false,
                'fetchBorrowRateHistories' => false,
                'fetchBorrowRateHistory' => false,
                'fetchBorrowRates' => false,
                'fetchBorrowRatesPerSymbol' => false,
                'fetchCrossBorrowRate' => false,
                'fetchCrossBorrowRates' => false,
                'fetchCurrencies' => false,
                'fetchDepositAddress' => false,
                'fetchDepositAddresses' => false,
                'fetchDepositAddressesByNetwork' => false,
                'fetchFundingHistory' => false,
                'fetchFundingInterval' => false,
                'fetchFundingIntervals' => false,
                'fetchFundingRate' => false,
                'fetchFundingRateHistory' => false,
                'fetchFundingRates' => false,
                'fetchGreeks' => false,
                'fetchIndexOHLCV' => false,
                'fetchIsolatedBorrowRate' => false,
                'fetchIsolatedBorrowRates' => false,
                'fetchIsolatedPositions' => false,
                'fetchLeverage' => false,
                'fetchLeverages' => false,
                'fetchLeverageTiers' => false,
                'fetchLiquidations' => false,
                'fetchLongShortRatio' => false,
                'fetchLongShortRatioHistory' => false,
                'fetchMarginAdjustmentHistory' => false,
                'fetchMarginMode' => false,
                'fetchMarginModes' => false,
                'fetchMarketLeverageTiers' => false,
                'fetchMarkets' => true,
                'fetchMarkOHLCV' => false,
                'fetchMarkPrice' => false,
                'fetchMarkPrices' => false,
                'fetchMyLiquidations' => false,
                'fetchMySettlementHistory' => false,
                'fetchMyTrades' => 'emulated',
                'fetchOHLCV' => true,
                'fetchOpenInterest' => false,
                'fetchOpenInterestHistory' => false,
                'fetchOpenInterests' => false,
                'fetchOpenOrders' => true,
                'fetchOption' => false,
                'fetchOptionChain' => false,
                'fetchOrder' => true,
                'fetchOrderBook' => true,
                'fetchOrders' => true,
                'fetchPosition' => false,
                'fetchPositionHistory' => false,
                'fetchPositionMode' => false,
                'fetchPositions' => false,
                'fetchPositionsForSymbol' => false,
                'fetchPositionsHistory' => false,
                'fetchPositionsRisk' => false,
                'fetchPremiumIndexOHLCV' => false,
                'fetchSettlementHistory' => false,
                'fetchTicker' => true,
                'fetchTickers' => false,
                'fetchTrades' => true,
                'fetchTradingFee' => false,
                'fetchTradingFees' => false,
                'fetchUnderlyingAssets' => false,
                'fetchVolatilityHistory' => false,
                'reduceMargin' => false,
                'repayCrossMargin' => false,
                'repayIsolatedMargin' => false,
                'setLeverage' => false,
                'setMargin' => false,
                'setMarginMode' => false,
                'setPositionMode' => false,
                'withdraw' => true,
            ),
            'timeframes' => array(
                '15m' => '15m',
                '1h' => '1h',
                '3h' => '3h',
                '1d' => '1d',
                '1w' => '1w',
                '1M' => '1M',
            ),
            'urls' => array(
                'logo' => 'https://user-images.githubusercontent.com/1294454/27837060-e7c58714-60ea-11e7-9192-f05e86adb83f.jpg',
                'api' => array(
                    'public' => 'https://www.mercadobitcoin.net/api',
                    'private' => 'https://www.mercadobitcoin.net/tapi',
                    'v4Public' => 'https://www.mercadobitcoin.com.br/v4',
                    'v4PublicNet' => 'https://api.mercadobitcoin.net/api/v4',
                ),
                'www' => 'https://www.mercadobitcoin.com.br',
                'doc' => array(
                    'https://www.mercadobitcoin.com.br/api-doc',
                    'https://www.mercadobitcoin.com.br/trade-api',
                ),
            ),
            'api' => array(
                'public' => array(
                    'get' => array(
                        'coins',
                        '{coin}/orderbook/', // last slash critical
                        '{coin}/ticker/',
                        '{coin}/trades/',
                        '{coin}/trades/{from}/',
                        '{coin}/trades/{from}/{to}',
                        '{coin}/day-summary/{year}/{month}/{day}/',
                    ),
                ),
                'private' => array(
                    'post' => array(
                        'cancel_order',
                        'get_account_info',
                        'get_order',
                        'get_withdrawal',
                        'list_system_messages',
                        'list_orders',
                        'list_orderbook',
                        'place_buy_order',
                        'place_sell_order',
                        'place_market_buy_order',
                        'place_market_sell_order',
                        'withdraw_coin',
                    ),
                ),
                'v4Public' => array(
                    'get' => array(
                        '{coin}/candle/',
                    ),
                ),
                'v4PublicNet' => array(
                    'get' => array(
                        'candles',
                    ),
                ),
            ),
            'fees' => array(
                'trading' => array(
                    'maker' => 0.003,
                    'taker' => 0.007,
                ),
            ),
            'options' => array(
                'limits' => array(
                    'BTC' => 0.001,
                    'BCH' => 0.001,
                    'ETH' => 0.01,
                    'LTC' => 0.01,
                    'XRP' => 0.1,
                ),
            ),
            'features' => array(
                'spot' => array(
                    'sandbox' => false,
                    'createOrder' => array(
                        'marginMode' => false,
                        'triggerPrice' => false,
                        'triggerPriceType' => null,
                        'triggerDirection' => false,
                        'stopLossPrice' => false,
                        'takeProfitPrice' => false,
                        'attachedStopLossTakeProfit' => null,
                        'timeInForce' => array(
                            'IOC' => false,
                            'FOK' => false,
                            'PO' => true, // todo
                            'GTD' => false,
                        ),
                        'hedged' => false,
                        'trailing' => false,
                        'leverage' => false,
                        'marketBuyByCost' => true, // todo
                        'marketBuyRequiresPrice' => true,
                        'selfTradePrevention' => false,
                        'iceberg' => false,
                    ),
                    'createOrders' => null,
                    'fetchMyTrades' => array(
                        'marginMode' => false,
                        'limit' => null, // todo
                        'daysBack' => 100000, // todo
                        'untilDays' => 100000, // todo
                        'symbolRequired' => true,
                    ),
                    'fetchOrder' => array(
                        'marginMode' => false,
                        'trigger' => false,
                        'trailing' => false,
                        'symbolRequired' => true,
                    ),
                    'fetchOpenOrders' => array(
                        'marginMode' => false,
                        'limit' => null,
                        'trigger' => false,
                        'trailing' => false,
                        'symbolRequired' => true,
                    ),
                    'fetchOrders' => array(
                        'marginMode' => false,
                        'limit' => 500,
                        'daysBack' => 100000,
                        'untilDays' => 100000,
                        'trigger' => false,
                        'trailing' => false,
                        'symbolRequired' => true,
                    ),
                    'fetchClosedOrders' => null,
                    'fetchOHLCV' => array(
                        'limit' => 1000,
                    ),
                ),
                'swap' => array(
                    'linear' => null,
                    'inverse' => null,
                ),
                'future' => array(
                    'linear' => null,
                    'inverse' => null,
                ),
            ),
            'precisionMode' => TICK_SIZE,
        ));
    }

    public function fetch_markets($params = array ()): PromiseInterface {
        return Async\async(function () use ($params) {
            /**
             * retrieves data on all markets for mercado
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array[]} an array of objects representing market data
             */
            $response = Async\await($this->publicGetCoins ($params));
            //
            //     array(
            //         "BCH",
            //         "BTC",
            //         "ETH",
            //         "LTC",
            //         "XRP",
            //         "MBPRK01",
            //         "MBPRK02",
            //         "MBPRK03",
            //         "MBPRK04",
            //         "MBCONS01",
            //         "USDC",
            //         "WBX",
            //         "CHZ",
            //         "MBCONS02",
            //         "PAXG",
            //         "MBVASCO01",
            //         "LINK"
            //     )
            //
            $result = array();
            $amountLimits = $this->safe_value($this->options, 'limits', array());
            for ($i = 0; $i < count($response); $i++) {
                $coin = $response[$i];
                $baseId = $coin;
                $quoteId = 'BRL';
                $base = $this->safe_currency_code($baseId);
                $quote = $this->safe_currency_code($quoteId);
                $id = $quote . $base;
                $result[] = array(
                    'id' => $id,
                    'symbol' => $base . '/' . $quote,
                    'base' => $base,
                    'quote' => $quote,
                    'settle' => null,
                    'baseId' => $baseId,
                    'quoteId' => $quoteId,
                    'settleId' => null,
                    'type' => 'spot',
                    'spot' => true,
                    'margin' => false,
                    'swap' => false,
                    'future' => false,
                    'option' => false,
                    'active' => null,
                    'contract' => false,
                    'linear' => null,
                    'inverse' => null,
                    'contractSize' => null,
                    'expiry' => null,
                    'expiryDatetime' => null,
                    'strike' => null,
                    'optionType' => null,
                    'precision' => array(
                        'amount' => $this->parse_number('1e-8'),
                        'price' => $this->parse_number('1e-5'),
                    ),
                    'limits' => array(
                        'leverage' => array(
                            'min' => null,
                            'max' => null,
                        ),
                        'amount' => array(
                            'min' => $this->safe_number($amountLimits, $baseId),
                            'max' => null,
                        ),
                        'price' => array(
                            'min' => $this->parse_number('1e-5'),
                            'max' => null,
                        ),
                        'cost' => array(
                            'min' => null,
                            'max' => null,
                        ),
                    ),
                    'created' => null,
                    'info' => $coin,
                );
            }
            return $result;
        }) ();
    }

    public function fetch_order_book(string $symbol, ?int $limit = null, $params = array ()): PromiseInterface {
        return Async\async(function () use ($symbol, $limit, $params) {
            /**
             * fetches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
             * @param {string} $symbol unified $symbol of the $market to fetch the order book for
             * @param {int} [$limit] the maximum amount of order book entries to return
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} A dictionary of ~@link https://docs.ccxt.com/#/?id=order-book-structure order book structures~ indexed by $market symbols
             */
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin' => $market['base'],
            );
            $response = Async\await($this->publicGetCoinOrderbook ($this->extend($request, $params)));
            return $this->parse_order_book($response, $market['symbol']);
        }) ();
    }

    public function parse_ticker(array $ticker, ?array $market = null): array {
        //
        //     {
        //         "high":"103.96000000",
        //         "low":"95.00000000",
        //         "vol":"2227.67806598",
        //         "last":"97.91591000",
        //         "buy":"95.52760000",
        //         "sell":"97.91475000",
        //         "open":"99.79955000",
        //         "date":1643382606
        //     }
        //
        $symbol = $this->safe_symbol(null, $market);
        $timestamp = $this->safe_timestamp($ticker, 'date');
        $last = $this->safe_string($ticker, 'last');
        return $this->safe_ticker(array(
            'symbol' => $symbol,
            'timestamp' => $timestamp,
            'datetime' => $this->iso8601($timestamp),
            'high' => $this->safe_string($ticker, 'high'),
            'low' => $this->safe_string($ticker, 'low'),
            'bid' => $this->safe_string($ticker, 'buy'),
            'bidVolume' => null,
            'ask' => $this->safe_string($ticker, 'sell'),
            'askVolume' => null,
            'vwap' => null,
            'open' => null,
            'close' => $last,
            'last' => $last,
            'previousClose' => null,
            'change' => null,
            'percentage' => null,
            'average' => null,
            'baseVolume' => $this->safe_string($ticker, 'vol'),
            'quoteVolume' => null,
            'info' => $ticker,
        ), $market);
    }

    public function fetch_ticker(string $symbol, $params = array ()): PromiseInterface {
        return Async\async(function () use ($symbol, $params) {
            /**
             * fetches a price $ticker, a statistical calculation with the information calculated over the past 24 hours for a specific $market
             * @param {string} $symbol unified $symbol of the $market to fetch the $ticker for
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} a ~@link https://docs.ccxt.com/#/?id=$ticker-structure $ticker structure~
             */
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin' => $market['base'],
            );
            $response = Async\await($this->publicGetCoinTicker ($this->extend($request, $params)));
            $ticker = $this->safe_value($response, 'ticker', array());
            //
            //     {
            //         "ticker" => {
            //             "high":"1549.82293000",
            //             "low":"1503.00011000",
            //             "vol":"81.82827101",
            //             "last":"1533.15000000",
            //             "buy":"1533.21018000",
            //             "sell":"1540.09000000",
            //             "open":"1524.71089000",
            //             "date":1643691671
            //         }
            //     }
            //
            return $this->parse_ticker($ticker, $market);
        }) ();
    }

    public function parse_trade(array $trade, ?array $market = null): array {
        $timestamp = $this->safe_timestamp_2($trade, 'date', 'executed_timestamp');
        $market = $this->safe_market(null, $market);
        $id = $this->safe_string_2($trade, 'tid', 'operation_id');
        $type = null;
        $side = $this->safe_string($trade, 'type');
        $price = $this->safe_string($trade, 'price');
        $amount = $this->safe_string_2($trade, 'amount', 'quantity');
        $feeCost = $this->safe_string($trade, 'fee_rate');
        $fee = null;
        if ($feeCost !== null) {
            $fee = array(
                'cost' => $feeCost,
                'currency' => null,
            );
        }
        return $this->safe_trade(array(
            'id' => $id,
            'info' => $trade,
            'timestamp' => $timestamp,
            'datetime' => $this->iso8601($timestamp),
            'symbol' => $market['symbol'],
            'order' => null,
            'type' => $type,
            'side' => $side,
            'takerOrMaker' => null,
            'price' => $price,
            'amount' => $amount,
            'cost' => null,
            'fee' => $fee,
        ), $market);
    }

    public function fetch_trades(string $symbol, ?int $since = null, ?int $limit = null, $params = array ()): PromiseInterface {
        return Async\async(function () use ($symbol, $since, $limit, $params) {
            /**
             * get the list of most recent trades for a particular $symbol
             * @param {string} $symbol unified $symbol of the $market $to fetch trades for
             * @param {int} [$since] timestamp in ms of the earliest trade $to fetch
             * @param {int} [$limit] the maximum amount of trades $to fetch
             * @param {array} [$params] extra parameters specific $to the exchange API endpoint
             * @return {Trade[]} a list of ~@link https://docs.ccxt.com/#/?id=public-trades trade structures~
             */
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $method = 'publicGetCoinTrades';
            $request = array(
                'coin' => $market['base'],
            );
            if ($since !== null) {
                $method .= 'From';
                $request['from'] = $this->parse_to_int($since / 1000);
            }
            $to = $this->safe_integer($params, 'to');
            if ($to !== null) {
                $method .= 'To';
            }
            $response = Async\await($this->$method ($this->extend($request, $params)));
            return $this->parse_trades($response, $market, $since, $limit);
        }) ();
    }

    public function parse_balance($response): array {
        $data = $this->safe_value($response, 'response_data', array());
        $balances = $this->safe_value($data, 'balance', array());
        $result = array( 'info' => $response );
        $currencyIds = is_array($balances) ? array_keys($balances) : array();
        for ($i = 0; $i < count($currencyIds); $i++) {
            $currencyId = $currencyIds[$i];
            $code = $this->safe_currency_code($currencyId);
            if (is_array($balances) && array_key_exists($currencyId, $balances)) {
                $balance = $this->safe_value($balances, $currencyId, array());
                $account = $this->account();
                $account['free'] = $this->safe_string($balance, 'available');
                $account['total'] = $this->safe_string($balance, 'total');
                $result[$code] = $account;
            }
        }
        return $this->safe_balance($result);
    }

    public function fetch_balance($params = array ()): PromiseInterface {
        return Async\async(function () use ($params) {
            /**
             * query for balance and get the amount of funds available for trading or funds locked in orders
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} a ~@link https://docs.ccxt.com/#/?id=balance-structure balance structure~
             */
            Async\await($this->load_markets());
            $response = Async\await($this->privatePostGetAccountInfo ($params));
            return $this->parse_balance($response);
        }) ();
    }

    public function create_order(string $symbol, string $type, string $side, float $amount, ?float $price = null, $params = array ()) {
        return Async\async(function () use ($symbol, $type, $side, $amount, $price, $params) {
            /**
             * create a trade order
             * @param {string} $symbol unified $symbol of the $market to create an order in
             * @param {string} $type 'market' or 'limit'
             * @param {string} $side 'buy' or 'sell'
             * @param {float} $amount how much of currency you want to trade in units of base currency
             * @param {float} [$price] the $price at which the order is to be fulfilled, in units of the quote currency, ignored in $market orders
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} an ~@link https://docs.ccxt.com/#/?id=order-structure order structure~
             */
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin_pair' => $market['id'],
            );
            $method = $this->capitalize($side) . 'Order';
            if ($type === 'limit') {
                $method = 'privatePostPlace' . $method;
                $request['limit_price'] = $this->price_to_precision($market['symbol'], $price);
                $request['quantity'] = $this->amount_to_precision($market['symbol'], $amount);
            } else {
                $method = 'privatePostPlaceMarket' . $method;
                if ($side === 'buy') {
                    if ($price === null) {
                        throw new InvalidOrder($this->id . ' createOrder() requires the $price argument with $market buy orders to calculate total order $cost ($amount to spend), where $cost = $amount * $price-> Supply a $price argument to createOrder() call if you want the $cost to be calculated for you from $price and amount');
                    }
                    $amountString = $this->number_to_string($amount);
                    $priceString = $this->number_to_string($price);
                    $cost = $this->parse_to_numeric(Precise::string_mul($amountString, $priceString));
                    $request['cost'] = $this->price_to_precision($market['symbol'], $cost);
                } else {
                    $request['quantity'] = $this->amount_to_precision($market['symbol'], $amount);
                }
            }
            $response = Async\await($this->$method ($this->extend($request, $params)));
            // TODO => replace this with a call to parseOrder for unification
            return $this->safe_order(array(
                'info' => $response,
                'id' => (string) $response['response_data']['order']['order_id'],
            ), $market);
        }) ();
    }

    public function cancel_order(string $id, ?string $symbol = null, $params = array ()) {
        return Async\async(function () use ($id, $symbol, $params) {
            /**
             * cancels an open $order
             * @param {string} $id $order $id
             * @param {string} $symbol unified $symbol of the $market the $order was made in
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} An ~@link https://docs.ccxt.com/#/?$id=$order-structure $order structure~
             */
            if ($symbol === null) {
                throw new ArgumentsRequired($this->id . ' cancelOrder() requires a $symbol argument');
            }
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin_pair' => $market['id'],
                'order_id' => $id,
            );
            $response = Async\await($this->privatePostCancelOrder ($this->extend($request, $params)));
            //
            //     {
            //         "response_data" => {
            //             "order" => array(
            //                 "order_id" => 2176769,
            //                 "coin_pair" => "BRLBCH",
            //                 "order_type" => 2,
            //                 "status" => 3,
            //                 "has_fills" => false,
            //                 "quantity" => "0.10000000",
            //                 "limit_price" => "1996.15999",
            //                 "executed_quantity" => "0.00000000",
            //                 "executed_price_avg" => "0.00000",
            //                 "fee" => "0.00000000",
            //                 "created_timestamp" => "1536956488",
            //                 "updated_timestamp" => "1536956499",
            //                 "operations" => array()
            //             }
            //         ),
            //         "status_code" => 100,
            //         "server_unix_timestamp" => "1536956499"
            //     }
            //
            $responseData = $this->safe_value($response, 'response_data', array());
            $order = $this->safe_dict($responseData, 'order', array());
            return $this->parse_order($order, $market);
        }) ();
    }

    public function parse_order_status(?string $status) {
        $statuses = array(
            '2' => 'open',
            '3' => 'canceled',
            '4' => 'closed',
        );
        return $this->safe_string($statuses, $status, $status);
    }

    public function parse_order(array $order, ?array $market = null): array {
        //
        //     {
        //         "order_id" => 4,
        //         "coin_pair" => "BRLBTC",
        //         "order_type" => 1,
        //         "status" => 2,
        //         "has_fills" => true,
        //         "quantity" => "2.00000000",
        //         "limit_price" => "900.00000",
        //         "executed_quantity" => "1.00000000",
        //         "executed_price_avg" => "900.00000",
        //         "fee" => "0.00300000",
        //         "created_timestamp" => "1453838494",
        //         "updated_timestamp" => "1453838494",
        //         "operations" => array(
        //             array(
        //                 "operation_id" => 1,
        //                 "quantity" => "1.00000000",
        //                 "price" => "900.00000",
        //                 "fee_rate" => "0.30",
        //                 "executed_timestamp" => "1453838494",
        //             ),
        //         ),
        //     }
        //
        $id = $this->safe_string($order, 'order_id');
        $order_type = $this->safe_string($order, 'order_type');
        $side = null;
        if (is_array($order) && array_key_exists('order_type', $order)) {
            $side = ($order_type === '1') ? 'buy' : 'sell';
        }
        $status = $this->parse_order_status($this->safe_string($order, 'status'));
        $marketId = $this->safe_string($order, 'coin_pair');
        $market = $this->safe_market($marketId, $market);
        $timestamp = $this->safe_timestamp($order, 'created_timestamp');
        $fee = array(
            'cost' => $this->safe_string($order, 'fee'),
            'currency' => $market['quote'],
        );
        $price = $this->safe_string($order, 'limit_price');
        // $price = $this->safe_number($order, 'executed_price_avg', $price);
        $average = $this->safe_string($order, 'executed_price_avg');
        $amount = $this->safe_string($order, 'quantity');
        $filled = $this->safe_string($order, 'executed_quantity');
        $lastTradeTimestamp = $this->safe_timestamp($order, 'updated_timestamp');
        $rawTrades = $this->safe_value($order, 'operations', array());
        return $this->safe_order(array(
            'info' => $order,
            'id' => $id,
            'clientOrderId' => null,
            'timestamp' => $timestamp,
            'datetime' => $this->iso8601($timestamp),
            'lastTradeTimestamp' => $lastTradeTimestamp,
            'symbol' => $market['symbol'],
            'type' => 'limit',
            'timeInForce' => null,
            'postOnly' => null,
            'side' => $side,
            'price' => $price,
            'triggerPrice' => null,
            'cost' => null,
            'average' => $average,
            'amount' => $amount,
            'filled' => $filled,
            'remaining' => null,
            'status' => $status,
            'fee' => $fee,
            'trades' => $rawTrades,
        ), $market);
    }

    public function fetch_order(string $id, ?string $symbol = null, $params = array ()) {
        return Async\async(function () use ($id, $symbol, $params) {
            /**
             * fetches information on an $order made by the user
             * @param {string} $id $order $id
             * @param {string} $symbol unified $symbol of the $market the $order was made in
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} An ~@link https://docs.ccxt.com/#/?$id=$order-structure $order structure~
             */
            if ($symbol === null) {
                throw new ArgumentsRequired($this->id . ' fetchOrder() requires a $symbol argument');
            }
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin_pair' => $market['id'],
                'order_id' => intval($id),
            );
            $response = Async\await($this->privatePostGetOrder ($this->extend($request, $params)));
            $responseData = $this->safe_value($response, 'response_data', array());
            $order = $this->safe_dict($responseData, 'order');
            return $this->parse_order($order, $market);
        }) ();
    }

    public function withdraw(string $code, float $amount, string $address, ?string $tag = null, $params = array ()): PromiseInterface {
        return Async\async(function () use ($code, $amount, $address, $tag, $params) {
            /**
             * make a $withdrawal
             * @param {string} $code unified $currency $code
             * @param {float} $amount the $amount to withdraw
             * @param {string} $address the $address to withdraw to
             * @param {string} $tag
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {array} a ~@link https://docs.ccxt.com/#/?id=transaction-structure transaction structure~
             */
            list($tag, $params) = $this->handle_withdraw_tag_and_params($tag, $params);
            $this->check_address($address);
            Async\await($this->load_markets());
            $currency = $this->currency($code);
            $request = array(
                'coin' => $currency['id'],
                'quantity' => sprintf('%.10f', $amount),
                'address' => $address,
            );
            if ($code === 'BRL') {
                $account_ref = (is_array($params) && array_key_exists('account_ref', $params));
                if (!$account_ref) {
                    throw new ArgumentsRequired($this->id . ' withdraw() requires $account_ref parameter to withdraw ' . $code);
                }
            } elseif ($code !== 'LTC') {
                $tx_fee = (is_array($params) && array_key_exists('tx_fee', $params));
                if (!$tx_fee) {
                    throw new ArgumentsRequired($this->id . ' withdraw() requires $tx_fee parameter to withdraw ' . $code);
                }
                if ($code === 'XRP') {
                    if ($tag === null) {
                        if (!(is_array($params) && array_key_exists('destination_tag', $params))) {
                            throw new ArgumentsRequired($this->id . ' withdraw() requires a $tag argument or destination_tag parameter to withdraw ' . $code);
                        }
                    } else {
                        $request['destination_tag'] = $tag;
                    }
                }
            }
            $response = Async\await($this->privatePostWithdrawCoin ($this->extend($request, $params)));
            //
            //     {
            //         "response_data" => {
            //             "withdrawal" => array(
            //                 "id" => 1,
            //                 "coin" => "BRL",
            //                 "quantity" => "300.56",
            //                 "net_quantity" => "291.68",
            //                 "fee" => "8.88",
            //                 "account" => "bco => 341, ag => 1111, cta => 23456-X",
            //                 "status" => 1,
            //                 "created_timestamp" => "1453912088",
            //                 "updated_timestamp" => "1453912088"
            //             }
            //         ),
            //         "status_code" => 100,
            //         "server_unix_timestamp" => "1453912088"
            //     }
            //
            $responseData = $this->safe_value($response, 'response_data', array());
            $withdrawal = $this->safe_dict($responseData, 'withdrawal');
            return $this->parse_transaction($withdrawal, $currency);
        }) ();
    }

    public function parse_transaction(array $transaction, ?array $currency = null): array {
        //
        //     {
        //         "id" => 1,
        //         "coin" => "BRL",
        //         "quantity" => "300.56",
        //         "net_quantity" => "291.68",
        //         "fee" => "8.88",
        //         "account" => "bco => 341, ag => 1111, cta => 23456-X",
        //         "status" => 1,
        //         "created_timestamp" => "1453912088",
        //         "updated_timestamp" => "1453912088"
        //     }
        //
        $currency = $this->safe_currency(null, $currency);
        return array(
            'id' => $this->safe_string($transaction, 'id'),
            'txid' => null,
            'timestamp' => null,
            'datetime' => null,
            'network' => null,
            'addressFrom' => null,
            'address' => null,
            'addressTo' => null,
            'amount' => null,
            'type' => null,
            'currency' => $currency['code'],
            'status' => null,
            'updated' => null,
            'tagFrom' => null,
            'tag' => null,
            'tagTo' => null,
            'comment' => null,
            'internal' => null,
            'fee' => null,
            'info' => $transaction,
        );
    }

    public function parse_ohlcv($ohlcv, ?array $market = null): array {
        return array(
            $this->safe_integer($ohlcv, 0),
            $this->safe_number($ohlcv, 1),
            $this->safe_number($ohlcv, 2),
            $this->safe_number($ohlcv, 3),
            $this->safe_number($ohlcv, 4),
            $this->safe_number($ohlcv, 5),
        );
    }

    public function fetch_ohlcv(string $symbol, $timeframe = '15m', ?int $since = null, ?int $limit = null, $params = array ()): PromiseInterface {
        return Async\async(function () use ($symbol, $timeframe, $since, $limit, $params) {
            /**
             * fetches historical candlestick data containing the open, high, low, and close price, and the volume of a $market
             * @param {string} $symbol unified $symbol of the $market to fetch OHLCV data for
             * @param {string} $timeframe the length of time each candle represents
             * @param {int} [$since] timestamp in ms of the earliest candle to fetch
             * @param {int} [$limit] the maximum amount of $candles to fetch
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {int[][]} A list of $candles ordered, open, high, low, close, volume
             */
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'resolution' => $this->safe_string($this->timeframes, $timeframe, $timeframe),
                'symbol' => $market['base'] . '-' . $market['quote'], // exceptional endpoint, that needs custom $symbol syntax
            );
            if ($limit === null) {
                $limit = 100; // set some default $limit,'s required if user doesn't provide it
            }
            if ($since !== null) {
                $request['from'] = $this->parse_to_int($since / 1000);
                $request['to'] = $this->sum($request['from'], $limit * $this->parse_timeframe($timeframe));
            } else {
                $request['to'] = $this->seconds();
                $request['from'] = $request['to'] - ($limit * $this->parse_timeframe($timeframe));
            }
            $response = Async\await($this->v4PublicNetGetCandles ($this->extend($request, $params)));
            $candles = $this->convert_trading_view_to_ohlcv($response, 't', 'o', 'h', 'l', 'c', 'v');
            return $this->parse_ohlcvs($candles, $market, $timeframe, $since, $limit);
        }) ();
    }

    public function fetch_orders(?string $symbol = null, ?int $since = null, ?int $limit = null, $params = array ()): PromiseInterface {
        return Async\async(function () use ($symbol, $since, $limit, $params) {
            /**
             * fetches information on multiple $orders made by the user
             * @param {string} $symbol unified $market $symbol of the $market $orders were made in
             * @param {int} [$since] the earliest time in ms to fetch $orders for
             * @param {int} [$limit] the maximum number of order structures to retrieve
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {Order[]} a list of ~@link https://docs.ccxt.com/#/?id=order-structure order structures~
             */
            if ($symbol === null) {
                throw new ArgumentsRequired($this->id . ' fetchOrders() requires a $symbol argument');
            }
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin_pair' => $market['id'],
            );
            $response = Async\await($this->privatePostListOrders ($this->extend($request, $params)));
            $responseData = $this->safe_value($response, 'response_data', array());
            $orders = $this->safe_list($responseData, 'orders', array());
            return $this->parse_orders($orders, $market, $since, $limit);
        }) ();
    }

    public function fetch_open_orders(?string $symbol = null, ?int $since = null, ?int $limit = null, $params = array ()): PromiseInterface {
        return Async\async(function () use ($symbol, $since, $limit, $params) {
            /**
             * fetch all unfilled currently open $orders
             * @param {string} $symbol unified $market $symbol
             * @param {int} [$since] the earliest time in ms to fetch open $orders for
             * @param {int} [$limit] the maximum number of open order structures to retrieve
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {Order[]} a list of ~@link https://docs.ccxt.com/#/?id=order-structure order structures~
             */
            if ($symbol === null) {
                throw new ArgumentsRequired($this->id . ' fetchOpenOrders() requires a $symbol argument');
            }
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin_pair' => $market['id'],
                'status_list' => '[2]', // open only
            );
            $response = Async\await($this->privatePostListOrders ($this->extend($request, $params)));
            $responseData = $this->safe_value($response, 'response_data', array());
            $orders = $this->safe_list($responseData, 'orders', array());
            return $this->parse_orders($orders, $market, $since, $limit);
        }) ();
    }

    public function fetch_my_trades(?string $symbol = null, ?int $since = null, ?int $limit = null, $params = array ()) {
        return Async\async(function () use ($symbol, $since, $limit, $params) {
            /**
             * fetch all $trades made by the user
             * @param {string} $symbol unified $market $symbol
             * @param {int} [$since] the earliest time in ms to fetch $trades for
             * @param {int} [$limit] the maximum number of $trades structures to retrieve
             * @param {array} [$params] extra parameters specific to the exchange API endpoint
             * @return {Trade[]} a list of ~@link https://docs.ccxt.com/#/?id=trade-structure trade structures~
             */
            if ($symbol === null) {
                throw new ArgumentsRequired($this->id . ' fetchMyTrades() requires a $symbol argument');
            }
            Async\await($this->load_markets());
            $market = $this->market($symbol);
            $request = array(
                'coin_pair' => $market['id'],
                'has_fills' => true,
            );
            $response = Async\await($this->privatePostListOrders ($this->extend($request, $params)));
            $responseData = $this->safe_value($response, 'response_data', array());
            $ordersRaw = $this->safe_value($responseData, 'orders', array());
            $orders = $this->parse_orders($ordersRaw, $market, $since, $limit);
            $trades = $this->orders_to_trades($orders);
            return $this->filter_by_symbol_since_limit($trades, $market['symbol'], $since, $limit);
        }) ();
    }

    public function orders_to_trades($orders) {
        $result = array();
        for ($i = 0; $i < count($orders); $i++) {
            $trades = $this->safe_value($orders[$i], 'trades', array());
            for ($y = 0; $y < count($trades); $y++) {
                $result[] = $trades[$y];
            }
        }
        return $result;
    }

    public function sign($path, $api = 'public', $method = 'GET', $params = array (), $headers = null, $body = null) {
        $url = $this->urls['api'][$api] . '/';
        $query = $this->omit($params, $this->extract_params($path));
        if (($api === 'public') || ($api === 'v4Public') || ($api === 'v4PublicNet')) {
            $url .= $this->implode_params($path, $params);
            if ($query) {
                $url .= '?' . $this->urlencode($query);
            }
        } else {
            $this->check_required_credentials();
            $url .= $this->version . '/';
            $nonce = $this->nonce();
            $body = $this->urlencode($this->extend(array(
                'tapi_method' => $path,
                'tapi_nonce' => $nonce,
            ), $params));
            $auth = '/tapi/' . $this->version . '/' . '?' . $body;
            $headers = array(
                'Content-Type' => 'application/x-www-form-urlencoded',
                'TAPI-ID' => $this->apiKey,
                'TAPI-MAC' => $this->hmac($this->encode($auth), $this->encode($this->secret), 'sha512'),
            );
        }
        return array( 'url' => $url, 'method' => $method, 'body' => $body, 'headers' => $headers );
    }

    public function handle_errors(int $httpCode, string $reason, string $url, string $method, array $headers, string $body, $response, $requestHeaders, $requestBody) {
        if ($response === null) {
            return null;
        }
        //
        // todo add a unified standard handleErrors with $this->exceptions in describe()
        //
        //     array("status":503,"message":"Maintenancing, try again later","result":null)
        //
        $errorMessage = $this->safe_value($response, 'error_message');
        if ($errorMessage !== null) {
            throw new ExchangeError($this->id . ' ' . $this->json($response));
        }
        return null;
    }
}
