UNPKG

minter-js-sdk

Version:
94 lines (88 loc) 4.8 kB
'use strict'; var _defineProperty = require('@babel/runtime/helpers/defineProperty'); require('qs'); var minterjsUtil = require('minterjs-util'); var utils = require('../utils.js'); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /** * @typedef {object} EstimateSellResult * @property {number|string} will_get - amount of coinToBuy * @property {number|string} commission - amount of coinToSell to pay fee * @property {"pool"|"bancor"} [swap_from] */ /** * @param {MinterApiInstance} apiInstance * @param {import('axios').AxiosRequestConfig} [factoryAxiosOptions] * @return {EstimateCoinSellInstance} */ function EstimateCoinSell(apiInstance, factoryAxiosOptions) { /** * @typedef {Function} EstimateCoinSellInstance * @param {object} params * @param {string|number} params.coinToSell - ID or symbol of the coin to sell * @param {string|number} params.valueToSell * @param {string|number} params.coinToBuy - ID or symbol of the coin to buy * @param {ESTIMATE_SWAP_TYPE} [params.swapFrom] - estimate pool swap * @param {Array<string|number>} [params.route] - IDs of intermediate coins for pool swaps * @param {string|number} [params.gasCoin] * @param {string|number} [params.coinCommission] - gasCoin alias * @param {import('axios').AxiosRequestConfig} [axiosOptions] * @return {Promise<EstimateSellResult>} */ return function estimateCoinSell(params, axiosOptions) { if (params.coinIdToSell || params.coinIdToSell === 0) { params.coinToSell = params.coinIdToSell; // eslint-disable-next-line no-console console.warn('coinIdToSell is deprecated, use coinToSell instead'); } if (params.coinIdToBuy || params.coinIdToBuy === 0) { params.coinToBuy = params.coinIdToBuy; // eslint-disable-next-line no-console console.warn('coinIdToSell is deprecated, use coinToSell instead'); } if (!params.coinToBuy && params.coinToBuy !== 0) { return Promise.reject(new Error('Coin to buy not specified')); } if (!params.valueToSell) { return Promise.reject(new Error('Value to sell not specified')); } if (!params.coinToSell && params.coinToSell !== 0) { return Promise.reject(new Error('Coin to sell not specified')); } var gasCoin = params.gasCoin || params.gasCoin === 0 ? params.gasCoin : params.coinCommission; params = { coin_id_to_sell: utils.isCoinId(params.coinToSell) ? params.coinToSell : undefined, coin_to_sell: !utils.isCoinId(params.coinToSell) ? params.coinToSell : undefined, value_to_sell: minterjsUtil.convertToPip(params.valueToSell), coin_id_to_buy: utils.isCoinId(params.coinToBuy) ? params.coinToBuy : undefined, coin_to_buy: !utils.isCoinId(params.coinToBuy) ? params.coinToBuy : undefined, swap_from: params.swapFrom, route: params.route, coin_id_commission: utils.isCoinId(gasCoin) ? gasCoin : undefined, coin_commission: !utils.isCoinId(gasCoin) ? gasCoin : undefined }; return apiInstance.get('estimate_coin_sell', _objectSpread(_objectSpread(_objectSpread({}, factoryAxiosOptions), axiosOptions), {}, { params: params, // @see https://github.com/axios/axios/issues/5058#issuecomment-1272107602 paramsSerializer: { // eslint-disable-next-line unicorn/no-null indexes: null } })).then(function (response) { var resData = response.data; if (!utils.isValidNumber(resData.will_get)) { throw new Error('Invalid estimation data, `will_get` not specified'); } if (!utils.isValidNumber(resData.commission)) { throw new Error('Invalid estimation data, `commission` not specified'); } return _objectSpread(_objectSpread({}, resData), {}, { // receive pips from node and convert them will_get: minterjsUtil.convertFromPip(resData.will_get), commission: minterjsUtil.convertFromPip(resData.commission) }); }); }; } module.exports = EstimateCoinSell;