UNPKG

poloniex-exchange-api

Version:

Simple typescript interface to the Poloniex cryptoexchange.

376 lines 17.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const axiosDefault = require("axios"); const crypto = require("crypto"); const qs = require("qs"); const axios = axiosDefault.default; const defaultConfig = { rootUrl: `https://poloniex.com`, timeout: 5000, }; const defaultAgentConfig = { baseURL: defaultConfig.rootUrl, headers: { 'User-Agent': `Poloniex API Client (poloniex-exchange-api node package)`, }, method: 'GET', timeout: defaultConfig.timeout, validateStatus: () => true, }; const publicAgentConfig = Object.assign({}, defaultAgentConfig); const privateAgentConfig = Object.assign({}, defaultAgentConfig, { method: 'POST' }); exports.signMessage = (postBody, privateKey) => { const message = qs.stringify(postBody); return crypto.createHmac('sha512', privateKey) .update(message) .digest('hex'); }; exports.generateNonce = () => Date.now() * 1000; const getRawAgent = (auth) => ({ auth, getPublicEndpoint(queryParams, configOverride) { return __awaiter(this, void 0, void 0, function* () { const config = Object.assign({}, defaultConfig, configOverride); const uri = `/public?${qs.stringify(queryParams)}`; const agentConfig = Object.assign({}, publicAgentConfig, { url: uri }, config); try { return Promise.resolve(yield axios(agentConfig)); } catch (err) { return Promise.reject(err); } }); }, isUpgraded() { return this.auth; }, postToPrivateEndpoint(data, configOverride) { return __awaiter(this, void 0, void 0, function* () { if (!this.isUpgraded()) return Promise.reject(`not authenticated`); const config = Object.assign({}, defaultConfig, configOverride); const uri = `/tradingApi`; const headersOverride = config ? config.headers : null; const headers = Object.assign({}, privateAgentConfig.headers, { Key: this.auth.publicKey, Sign: this.signMessage(data, this.auth.privateKey) }, headersOverride); const agentConfig = Object.assign({}, privateAgentConfig, { headers, url: uri, data: qs.stringify(data) }, config); try { const response = yield axios(agentConfig); return Promise.resolve(response); } catch (err) { return Promise.reject(err); } }); }, signMessage: exports.signMessage, upgrade(newAuth) { this.auth = newAuth; }, }); exports.getClient = (auth, requestConfig = null) => ({ rawAgent: getRawAgent(auth), isUpgraded() { return this.rawAgent.isUpgraded(); }, upgrade(newAuth) { this.rawAgent.upgrade(newAuth); }, returnTicker() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnTicker'; const params = { command }; return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, return24Volume() { return __awaiter(this, void 0, void 0, function* () { const command = 'return24hVolume'; const params = { command }; return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, returnOrderBook(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnOrderBook'; const required = { command }; const optional = queryParams ? (({ currencyPair, depth }) => ({ currencyPair, depth }))(queryParams) : { currencyPair: 'all' }; const params = Object.assign({}, required, optional); return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, returnPublicTradeHistory(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnTradeHistory'; const params = (({ currencyPair, start, end }) => ({ command, currencyPair, start, end }))(queryParams); return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, returnChartData(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnChartData'; const params = (({ currencyPair, start, end, period }) => ({ command, currencyPair, start, end, period }))(queryParams); return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, returnCurrencies() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnCurrencies'; const params = { command }; return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, returnLoanOrders(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnLoanOrders'; const params = (({ currency }) => ({ command, currency }))(queryParams); return this.rawAgent.getPublicEndpoint(params, requestConfig); }); }, returnBalances() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnBalances'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnCompleteBalances(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnCompleteBalances'; const nonce = exports.generateNonce(); const required = { command, nonce }; const optional = queryParams ? (({ account }) => ({ account }))(queryParams) : { account: 'all' }; const params = Object.assign({}, required, optional); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnDepositAddress() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnDepositAddress'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, generateNewAddress(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'generateNewAddress'; const nonce = exports.generateNonce(); const params = (({ currency }) => ({ command, nonce, currency }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnDepositsWithdrawals(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnDepositsWithdrawals'; const nonce = exports.generateNonce(); const params = (({ start, end }) => ({ command, nonce, start, end }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnOpenOrders(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnOpenOrders'; const nonce = exports.generateNonce(); const required = { command, nonce }; const optional = queryParams ? (({ currencyPair }) => ({ currencyPair }))(queryParams) : { currencyPair: 'all' }; const params = Object.assign({}, required, optional); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnPrivateTradeHistory(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnTradeHistory'; const nonce = exports.generateNonce(); const required = { command, nonce }; const optional = queryParams ? (({ currencyPair, start, end, limit }) => ({ currencyPair, start, end, limit }))(queryParams) : { currencyPair: 'all' }; const params = Object.assign({}, required, optional); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnOrderTrades(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnOrderTrades'; const nonce = exports.generateNonce(); const params = (({ orderNumber }) => ({ command, nonce, orderNumber }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, buy(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'buy'; const nonce = exports.generateNonce(); const params = (({ currencyPair, rate, amount, fillOrKill, immediateOrCancel, postOnly }) => ({ command, nonce, currencyPair, rate, amount, fillOrKill, immediateOrCancel, postOnly }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, sell(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'sell'; const nonce = exports.generateNonce(); const params = (({ currencyPair, rate, amount, fillOrKill, immediateOrCancel, postOnly }) => ({ command, nonce, currencyPair, rate, amount, fillOrKill, immediateOrCancel, postOnly }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, cancelOrder(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'cancelOrder'; const nonce = exports.generateNonce(); const params = (({ orderNumber }) => ({ command, nonce, orderNumber }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, moveOrder(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'moveOrder'; const nonce = exports.generateNonce(); const params = (({ orderNumber, rate, amount, immediateOrCancel, postOnly }) => ({ command, nonce, orderNumber, rate, amount, immediateOrCancel, postOnly }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, withdraw(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'withdraw'; const nonce = exports.generateNonce(); const params = (({ currency, amount, address, paymentId }) => ({ command, nonce, currency, amount, address, paymentId }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnFeeInfo() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnFeeInfo'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnAvailableAccountBalances(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnAvailableAccountBalances'; const nonce = exports.generateNonce(); const required = { command, nonce }; const optional = queryParams ? (({ account }) => ({ account }))(queryParams) : null; const params = Object.assign({}, required, optional); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnTradableBalances() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnTradableBalances'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, transferBalance(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'transferBalance'; const nonce = exports.generateNonce(); const params = (({ currency, amount, fromAddress, toAddress }) => ({ command, nonce, currency, amount, fromAddress, toAddress }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnMarginAccountSummary() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnMarginAccountSummary'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, marginBuy(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'marginBuy'; const nonce = exports.generateNonce(); const params = (({ currencyPair, rate, amount, lendingRate }) => ({ command, nonce, currencyPair, rate, amount, lendingRate }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, marginSell(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'marginSell'; const nonce = exports.generateNonce(); const params = (({ currencyPair, rate, amount, lendingRate }) => ({ command, nonce, currencyPair, rate, amount, lendingRate }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, getMarginPosition(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'getMarginPosition'; const nonce = exports.generateNonce(); const required = { command, nonce }; const optional = queryParams ? (({ currencyPair }) => ({ currencyPair }))(queryParams) : { currencyPair: 'all' }; const params = Object.assign({}, required, optional); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, closeMarginPosition(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'closeMarginPosition'; const nonce = exports.generateNonce(); const params = (({ currencyPair }) => ({ command, nonce, currencyPair }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, createLoanOffer(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'createLoanOffer'; const nonce = exports.generateNonce(); const params = (({ currency, amount, duration, autoRenew, lendingRate }) => ({ command, nonce, currency, amount, duration, autoRenew, lendingRate }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, cancelLoanOffer(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'cancelLoanOffer'; const nonce = exports.generateNonce(); const params = (({ orderNumber }) => ({ command, nonce, orderNumber }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnOpenLoanOffers() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnOpenLoanOffers'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnActiveLoans() { return __awaiter(this, void 0, void 0, function* () { const command = 'returnActiveLoans'; const nonce = exports.generateNonce(); const params = { command, nonce }; return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, returnLendingHistory(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'returnLendingHistory'; const nonce = exports.generateNonce(); const params = (({ start, end, limit }) => ({ command, nonce, start, end, limit }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, toggleAutoRenew(queryParams) { return __awaiter(this, void 0, void 0, function* () { const command = 'toggleAutoRenew'; const nonce = exports.generateNonce(); const params = (({ orderNumber }) => ({ command, nonce, orderNumber }))(queryParams); return this.rawAgent.postToPrivateEndpoint(params, requestConfig); }); }, }); //# sourceMappingURL=index.js.map