UNPKG

btcturk-js

Version:
142 lines (141 loc) 7.73 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Btcturk = void 0; var crypto = __importStar(require("crypto-js")); var axios_1 = __importDefault(require("axios")); var endpoints_1 = require("./endpoints"); var Btcturk = /** @class */ (function () { function Btcturk(apiKey, apiSecretKey) { this.apiKey = apiKey; this.apiSecretKey = apiSecretKey; } // Prepare and return mandatory header for requests; Btcturk.prototype.getHeaderCredentials = function () { var b64_api_secret = crypto.enc.Base64.parse(this.apiSecretKey); var stamp = +new Date(); var message = Buffer.from("" + this.apiKey + stamp, "utf8").toString(); var signature = crypto.enc.Base64.stringify(crypto.HmacSHA256(message, b64_api_secret)); signature = Buffer.from(signature, "utf8").toString(); var headers = { "X-PCK": this.apiKey, "X-Stamp": stamp.toString(), "X-Signature": signature, "Content-Type": "application/json", }; return headers; }; // Accepted pair symbols are; BTC_USDT, BTC_TRY, XRP_TRY... Btcturk.prototype.getTicker = function (symbol) { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.TICKER + "/" + (symbol ? "?pairSymbol=" + symbol : "")); }; Btcturk.prototype.getOrderBook = function (symbol, limit) { if (limit === void 0) { limit = 100; } return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.ORDER_BOOK + "?pairSymbol=" + symbol + "&limit=" + limit); }; Btcturk.prototype.getTrades = function (symbol, last) { if (last === void 0) { last = 50; } return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.TRADES + "?pairSymbol=" + symbol + "&last=" + last); }; // from and to params must be in seconds format (in timestamps) // Math.round(new Date().getTime()/1000) -> current timestamp in seconds Btcturk.prototype.getOhlcData = function (symbol, from, to) { return axios_1.default.get(endpoints_1.Endpoint.OHLC_DATA + "?pair=" + symbol + (from ? "&from=" + from : "") + (to ? "&to=" + to : "")); }; Btcturk.prototype.getExchangeInfo = function () { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.EXCHANGE_INFO); }; Btcturk.prototype.getAccountBalance = function () { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.ACCOUNT_BALANCE, { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.getUserTransactions = function (type, symbol, startDate, endDate) { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.USER_TRANSACTIONS + "?" + type .map(function (n) { return "type=" + n; }) .join("&") + "&" + symbol.map(function (n) { return "symbol=" + n; }).join("&") + (startDate ? "&startDate=" + startDate : "") + (endDate ? "&endDate=" + endDate : ""), { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.getUserTransactionByID = function (orderID) { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.USER_TRANSACTIONS + "?orderID=" + orderID, { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.getCryptoTransactions = function (type, symbol, startDate, endDate) { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.CRYPTO_TRANSACTIONS + "?" + type .map(function (n) { return "type=" + n; }) .join("&") + "&" + symbol.map(function (n) { return "symbol=" + n; }).join("&") + (startDate ? "&startDate=" + startDate : "") + (endDate ? "&endDate=" + endDate : ""), { headers: this.getHeaderCredentials(), }); }; // Bank transactions Btcturk.prototype.getFiatTransactions = function (type, symbol, startDate, endDate) { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.FIAT_TRANSACTIONS + "?" + type .map(function (n) { return "type=" + n; }) .join("&") + "&" + symbol.map(function (n) { return "symbol=" + n; }).join("&") + (startDate ? "&startDate=" + startDate : "") + (endDate ? "&endDate=" + endDate : ""), { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.getOpenOrders = function (symbol) { return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.OPEN_ORDERS + "?pairSymbol=" + symbol, { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.getAllOrders = function (symbol, startTime, endTime, page, limit) { if (page === void 0) { page = 1; } if (limit === void 0) { limit = 100; } return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.ALL_ORDERS + "?pairSymbol=" + symbol + (startTime ? "&startDate=" + startTime : "") + (endTime ? "&endTime=" + endTime : "") + "&page=" + page + "&limit=" + limit, { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.getAllOrdersByID = function (orderID, symbol, startTime, endTime, page, limit) { if (page === void 0) { page = 1; } if (limit === void 0) { limit = 100; } return axios_1.default.get(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.ALL_ORDERS + "?orderId=" + orderID + "&pairSymbol=" + symbol + (startTime ? "&startTime=" + startTime : "") + (endTime ? "&endTime=" + endTime : "") + "&page=" + page + "&limit=" + limit, { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.submitOrder = function (quantity, price, stopPrice, orderMethod, orderType, pairSymbol, newOrderClientId) { var data = { quantity: quantity, price: price, stopPrice: stopPrice, orderMethod: orderMethod, orderType: orderType, pairSymbol: pairSymbol, newOrderClientId: newOrderClientId ? newOrderClientId : null, }; return axios_1.default.post(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.SUBMIT_ORDER, data, { headers: this.getHeaderCredentials(), }); }; Btcturk.prototype.cancelOrder = function (id) { return axios_1.default.delete(endpoints_1.Endpoint.BASE + "/" + endpoints_1.Endpoint.SUBMIT_ORDER + "?id=" + id, { headers: this.getHeaderCredentials(), }); }; return Btcturk; }()); exports.Btcturk = Btcturk;