UNPKG

@nodeswork/applet

Version:
297 lines (295 loc) 9.28 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("underscore"); const request = require("request-promise"); const account_1 = require("../../account"); const services_1 = require("../../services"); const def_1 = require("./def"); const defaultRequest = request.defaults({ json: true }); let WEXAccount = class WEXAccount extends def_1.BaseDigitalCurrencyAccount { constructor($request) { super($request); this.$request = $request; } static $acceptCurrencies() { return [ def_1.dc.currencies.BTC, def_1.dc.currencies.LTC, def_1.dc.currencies.ETH, def_1.dc.currencies.USD, ]; } static $acceptTradings() { return [ [def_1.dc.currencies.BTC, def_1.dc.currencies.USD], [def_1.dc.currencies.LTC, def_1.dc.currencies.BTC], [def_1.dc.currencies.LTC, def_1.dc.currencies.USD], [def_1.dc.currencies.ETH, def_1.dc.currencies.BTC], [def_1.dc.currencies.ETH, def_1.dc.currencies.USD], [def_1.dc.currencies.ETH, def_1.dc.currencies.LTC], ]; } static async $getDepths(pairs) { const localPairs = _.map(pairs, (pair) => toLocalPair(pair)); const depths = await this.getDepth(localPairs); const result = {}; _.each(depths, (depth, localPair) => { const globalPair = toGlobalPair(localPair); const globalPairString = def_1.dc.pairToString(globalPair); const asks = _.map(depth.asks, toGlobalDepthInfo); const bids = _.map(depth.bids, toGlobalDepthInfo); result[globalPairString] = { asks, bids }; }); return result; } async $getBalance() { const info = await this.getInfo(); const result = {}; _.each(info.funds, (value, name) => { const dcName = toGlobalName(name); if (dcName != null) { result[dcName] = value; } }); return result; } async getInfo() { return (await this.$operate({ name: 'getInfo', ref: 'getInfo', method: 'GET', })).return; } async trade(options) { return (await this.$operate({ name: 'trade', ref: 'Trade', method: 'POST', body: _.pick(options, 'pair', 'type', 'rate', 'amount'), })).return; } async getActiveOrders(pair) { return (await this.$operate({ name: 'getActiveOrders', ref: 'ActiveOrders', method: 'GET', body: { pair }, })).return; } async getOrderInfo(orderId) { return (await this.$operate({ name: 'getOrderInfo', ref: 'OrderInfo', method: 'GET', body: { order_id: orderId }, })).return; } async cancelOrder(orderId) { return (await this.$operate({ name: 'cancelOrder', ref: 'CancelOrder', method: 'POST', body: { order_id: orderId }, })).return; } async getTradeHistory(options) { return (await this.$operate({ name: 'getTradeHistory', ref: 'TradeHistory', method: 'GET', body: _.pick(options, 'from', 'count', 'from_id', 'end_id', 'order', 'since', 'end', 'pair'), })).return; } async getTransactionHistory(options) { return (await this.$operate({ name: 'getTransactionHistory', ref: 'TransHistory', method: 'GET', body: _.pick(options, 'from', 'count', 'from_id', 'end_id', 'order', 'since', 'end'), })).return; } async getCoinDepositAddress(coinName) { return (await this.$operate({ name: 'getCoinDepositAddress', ref: 'CoinDepositAddress', method: 'GET', body: { coinName }, })).return; } async withdrawCoin(options) { return (await this.$operate({ name: 'withdrawCoin', ref: 'WithdrawCoin', method: 'POST', body: _.pick(options, 'coinName', 'amount', 'address'), })).return; } async createCoupon(options) { return (await this.$operate({ name: 'createCoupon', ref: 'CreateCoupon', method: 'POST', body: _.pick(options, 'currency', 'amount', 'receiver'), })).return; } async redeemCoupon(coupon) { return (await this.$operate({ name: 'redeemCoupon', ref: 'RedeemCoupon', method: 'POST', body: { coupon }, })).return; } static async getPublicInfo() { return await defaultRequest.get(wex.PUBLIC_API_PREFIX + '/info'); } static async getPublicTicker(pairs) { return await defaultRequest.get(wex.PUBLIC_API_PREFIX + `/ticker/${pairs.join('-')}`); } static async getDepth(pairs) { pairs = _.filter(pairs, (x) => !!x); return await defaultRequest.get(wex.PUBLIC_API_PREFIX + `/depth/${pairs.join('-')}`); } static async getTrades(pairs) { return await defaultRequest.get(wex.PUBLIC_API_PREFIX + `/trades/${pairs.join('-')}`); } }; WEXAccount = __decorate([ account_1.Account({ accountType: 'WEXAccount', provider: 'wex', }), __metadata("design:paramtypes", [services_1.RequestService]) ], WEXAccount); exports.WEXAccount = WEXAccount; function toLocalPair(pair) { const s = toLocalName(pair[0]); const t = toLocalName(pair[1]); if (s == null || t == null) { return null; } return s + '_' + t; } function toGlobalPair(pair) { const x = pair.split('_'); return [toGlobalName(x[0]), toGlobalName(x[1])]; } function toLocalName(name) { switch (name) { case def_1.dc.currencies.BTC: return 'btc'; case def_1.dc.currencies.LTC: return 'ltc'; case def_1.dc.currencies.ETH: return 'eth'; case def_1.dc.currencies.USD: return 'usd'; default: return null; } } function toGlobalName(name) { switch (name) { case 'btc': return def_1.dc.currencies.BTC; case 'ltc': return def_1.dc.currencies.LTC; case 'eth': return def_1.dc.currencies.ETH; case 'usd': return def_1.dc.currencies.USD; default: return null; } } function toGlobalDepthInfo([price, volume]) { return { price, volume, }; } var wex; (function (wex) { wex.PUBLIC_API_PREFIX = 'https://wex.nz/api/3'; wex.COINS = { BTC: 'BTC', LTC: 'LTC', NMC: 'NMC', NVC: 'NVC', PPC: 'PPC', DSH: 'DSH', ETH: 'ETH', BCH: 'BCH', ZEC: 'ZEC', }; wex.CURRENCIES = { USD: 'USD', EUR: 'EUR', RUR: 'RUR', }; wex.PAIRS = { BTC_USD: 'btc_usd', BTC_RUR: 'btc_rur', BTC_EUR: 'btc_eur', LTC_BTC: 'ltc_btc', LTC_USD: 'ltc_usd', LTC_RUR: 'ltc_rur', LTC_EUR: 'ltc_eur', NMC_BTC: 'nmc_btc', NMC_USD: 'nmc_usd', NVC_BTC: 'nvc_btc', NVC_USD: 'nvc_usd', USD_RUR: 'usd_rur', EUR_USD: 'eur_usd', EUR_RUR: 'eur_rur', PPC_BTC: 'ppc_btc', PPC_USD: 'ppc_usd', DSH_BTC: 'dsh_btc', DSH_USD: 'dsh_usd', DSH_RUR: 'dsh_rur', DSH_EUR: 'dsh_eur', DSH_LTC: 'dsh_ltc', DSH_ETH: 'dsh_eth', ETH_BTC: 'eth_btc', ETH_USD: 'eth_usd', ETH_EUR: 'eth_eur', ETH_LTC: 'eth_ltc', ETH_RUR: 'eth_rur', BCH_USD: 'bch_usd', BCH_BTC: 'bch_btc', ZEC_BTC: 'zec_btc', ZEC_USD: 'zec_usd', }; wex.ORDER_STATUS = { ACTIVE: 0, EXECUTED_ORDER: 1, CANCELED: 2, CANCELED_WITH_PARTIAL_EXECUTION: 3, }; wex.TRANSACTION_TYPE = { DEPOSIT: 1, WITHDRAWAL: 2, CREDIT: 4, DEBIT: 5, }; wex.TRANSACTION_STATUS = { CANCELED_OR_FAILED: 0, WAITING_FOR_ACCEPTANCE: 1, SUCCESSFUL: 2, NOT_CONFIRMED: 3, }; wex.TRADE_TYPES = { SELL: 'ask', BUY: 'bid', }; })(wex = exports.wex || (exports.wex = {})); //# sourceMappingURL=wex-accounts.js.map