UNPKG

shrimpy-node-temp

Version:
503 lines 24.5 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var rp = require("request-promise-native"); var authentication_provider_1 = require("./authentication-provider"); var dto_converters_1 = require("../dto-converters"); var ShrimpyApiClient = /** @class */ (function () { function ShrimpyApiClient(publicKey, privateKey) { this._accountBalanceDtoConverter = new dto_converters_1.AccountBalanceDtoConverter(); this._decimalDtoConverter = new dto_converters_1.DecimalDtoConverter(); this._userDtoConverter = new dto_converters_1.UserDtoConverter(); this._strategyDtoConverter = new dto_converters_1.StrategyDtoConverter(); this._tickerDtoConverter = new dto_converters_1.TickerDtoConverter(); this._tradeChangesDtoConverter = new dto_converters_1.TradeChangesDtoConverter(); this._tradeDtoConverter = new dto_converters_1.TradeDtoConverter(); this._authenticationProvider = null; if (publicKey && privateKey) { // keys were supplied, use them this._setApiCredentials(publicKey, privateKey); } } ShrimpyApiClient.prototype.getTicker = function (exchange) { return __awaiter(this, void 0, void 0, function () { var endpoint, tickerDtos; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = exchange + "/ticker"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, false)]; case 1: tickerDtos = _a.sent(); return [2 /*return*/, tickerDtos.map(function (tickerDto) { return _this._tickerDtoConverter.convertFromDto(tickerDto); })]; } }); }); }; ShrimpyApiClient.prototype.getUsers = function () { return __awaiter(this, void 0, void 0, function () { var endpoint, userDtos; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = 'users'; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: userDtos = _a.sent(); return [2 /*return*/, userDtos.map(function (userDto) { return _this._userDtoConverter.convertFromDto(userDto); })]; } }); }); }; ShrimpyApiClient.prototype.getUser = function (userId) { return __awaiter(this, void 0, void 0, function () { var endpoint, userDto; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: userDto = _a.sent(); return [2 /*return*/, this._userDtoConverter.convertFromDto(userDto)]; } }); }); }; ShrimpyApiClient.prototype.createUser = function () { return __awaiter(this, void 0, void 0, function () { var endpoint, result; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = 'users'; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', null, true)]; case 1: result = _a.sent(); return [2 /*return*/, result.id]; } }); }); }; ShrimpyApiClient.prototype.enableSubscription = function (userId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/enable"; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', null, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.disableSubscription = function (userId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/disable"; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', null, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.createApiKeys = function (userId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/keys"; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', null, true)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ShrimpyApiClient.prototype.deleteApiKeys = function (userId, publicKey) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/keys/" + publicKey; return [4 /*yield*/, this._callEndpoint(endpoint, 'DELETE', null, true)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ShrimpyApiClient.prototype.getApiKeys = function (userId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/keys"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ShrimpyApiClient.prototype.setPermissions = function (userId, publicKey, account, trade) { return __awaiter(this, void 0, void 0, function () { var endpoint, parameters; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/keys/" + publicKey + "/permissions"; parameters = { account: account, trade: trade }; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', parameters, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.getPermissions = function (userId, publicKey) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/keys/" + publicKey + "/permissions"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ShrimpyApiClient.prototype.getAccounts = function (userId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ShrimpyApiClient.prototype.getAccount = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ShrimpyApiClient.prototype.createAccount = function (userId, exchange, publicKey, privateKey, passphrase) { return __awaiter(this, void 0, void 0, function () { var endpoint, parameters, result; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts"; parameters = { exchange: exchange, publicKey: publicKey, privateKey: privateKey, passphrase: passphrase, }; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', parameters, true)]; case 1: result = _a.sent(); return [2 /*return*/, result.id]; } }); }); }; ShrimpyApiClient.prototype.deleteAccount = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId; return [4 /*yield*/, this._callEndpoint(endpoint, 'DELETE', null, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.rebalance = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/rebalance"; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', null, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.getRebalancePeriod = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint, result; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/rebalance_period"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: result = _a.sent(); return [2 /*return*/, result.rebalancePeriod]; } }); }); }; ShrimpyApiClient.prototype.setRebalancePeriod = function (userId, accountId, rebalancePeriodHours) { return __awaiter(this, void 0, void 0, function () { var endpoint, parameters; return __generator(this, function (_a) { switch (_a.label) { case 0: // verify rebalancePeriodHours is an integer if (!Number.isInteger(rebalancePeriodHours)) { throw new Error("Invalid rebalance period. Rebalance period must be an integer"); } endpoint = "users/" + userId + "/accounts/" + accountId + "/rebalance_period"; parameters = { rebalancePeriod: rebalancePeriodHours }; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', parameters, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.setStrategy = function (userId, accountId, strategy) { return __awaiter(this, void 0, void 0, function () { var endpoint, parameters; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/strategy"; parameters = this._strategyDtoConverter.convertToDto(strategy); return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', parameters, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.getStrategy = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint, strategyDto; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/strategy"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: strategyDto = _a.sent(); return [2 /*return*/, this._strategyDtoConverter.convertFromDto(strategyDto)]; } }); }); }; ShrimpyApiClient.prototype.allocate = function (userId, accountId, strategy) { return __awaiter(this, void 0, void 0, function () { var endpoint, parameters; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/allocate"; parameters = this._strategyDtoConverter.convertToDto(strategy); return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', parameters, true)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; ShrimpyApiClient.prototype.createTrade = function (userId, accountId, fromSymbol, toSymbol, amount) { return __awaiter(this, void 0, void 0, function () { var endpoint, parameters, result; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/trades"; parameters = { fromSymbol: fromSymbol, toSymbol: toSymbol, amount: this._decimalDtoConverter.convertToDto(amount), }; return [4 /*yield*/, this._callEndpoint(endpoint, 'POST', parameters, true)]; case 1: result = _a.sent(); return [2 /*return*/, result.id]; } }); }); }; ShrimpyApiClient.prototype.getActiveTrades = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint, tradeDtos; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/trades"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: tradeDtos = _a.sent(); return [2 /*return*/, tradeDtos.map(function (tradeDto) { return _this._tradeDtoConverter.convertFromDto(tradeDto); })]; } }); }); }; ShrimpyApiClient.prototype.getTrade = function (userId, accountId, tradeId) { return __awaiter(this, void 0, void 0, function () { var endpoint, tradeChangesDto; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/trades/" + tradeId; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: tradeChangesDto = _a.sent(); return [2 /*return*/, this._tradeChangesDtoConverter.convertFromDto(tradeChangesDto)]; } }); }); }; ShrimpyApiClient.prototype.getBalance = function (userId, accountId) { return __awaiter(this, void 0, void 0, function () { var endpoint, accountBalanceDto; return __generator(this, function (_a) { switch (_a.label) { case 0: endpoint = "users/" + userId + "/accounts/" + accountId + "/balance"; return [4 /*yield*/, this._callEndpoint(endpoint, 'GET', null, true)]; case 1: accountBalanceDto = _a.sent(); return [2 /*return*/, this._accountBalanceDtoConverter.convertFromDto(accountBalanceDto)]; } }); }); }; ShrimpyApiClient.prototype._setApiCredentials = function (publicKey, privateKey) { this._authenticationProvider = new authentication_provider_1.AuthenticationProvider(publicKey, privateKey); }; ShrimpyApiClient.prototype._callEndpoint = function (endPoint, method, parameters, signed) { return __awaiter(this, void 0, void 0, function () { var pathname, options, nonce, bodyString, prehashString, signature, response; return __generator(this, function (_a) { switch (_a.label) { case 0: pathname = "/v1/" + endPoint; options = { uri: "https://dev-api.shrimpy.io" + pathname, headers: { 'content-type': 'application/json', }, method: method }; if (method == 'POST') { if (parameters) { // only attach the body if there are parameters // no parameters, send empty body options.body = JSON.stringify(parameters); } else { options.body = ""; } } if (signed) { // this method is signed, create the signature if (!this._authenticationProvider) { throw new Error("Cannot send a request to " + endPoint + " without api keys. Make sure to pass api keys to the ShrimpyApiClient constructor."); } nonce = Date.now(); bodyString = options.body ? options.body : ""; prehashString = pathname + method + nonce + bodyString; signature = this._authenticationProvider.sign(prehashString); options.headers['DEV-SHRIMPY-API-KEY'] = this._authenticationProvider.publicKey; options.headers['DEV-SHRIMPY-API-NONCE'] = nonce; options.headers['DEV-SHRIMPY-API-SIGNATURE'] = signature; } return [4 /*yield*/, rp(options)]; case 1: response = _a.sent(); return [2 /*return*/, JSON.parse(response)]; } }); }); }; return ShrimpyApiClient; }()); exports.ShrimpyApiClient = ShrimpyApiClient; //# sourceMappingURL=shrimpy-api-client.js.map