@crypdex/hydro-sdk
Version:
Javascript SDK for the Hydro API
488 lines (487 loc) • 21.2 kB
JavaScript
"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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, 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 path_1 = require("path");
var ethereumjs_util_1 = require("ethereumjs-util");
var RequestHandler_1 = require("./RequestHandler");
var errors_1 = require("../errors/errors");
var Candle_1 = require("../models/Candle");
var Fee_1 = require("../models/Fee");
var LockedBalance_1 = require("../models/LockedBalance");
var Market_1 = require("../models/Market");
var Order_1 = require("../models/Order");
var Orderbook_1 = require("../models/Orderbook");
var OrderList_1 = require("../models/OrderList");
var Ticker_1 = require("../models/Ticker");
var TradeList_1 = require("../models/TradeList");
var HydroClient = /** @class */ (function () {
function HydroClient(handler, sign) {
this.handler = handler;
this.sign = sign;
}
HydroClient.create = function (_a) {
var sign = _a.sign, account = _a.account, baseUrl = _a.baseUrl, privateKey = _a.privateKey;
if (sign) {
if (!account) {
throw new Error('an account is required');
}
return HydroClient.withCustomAuth(sign, account, baseUrl);
}
if (privateKey) {
return HydroClient.withPrivateKey(privateKey, baseUrl);
}
return HydroClient.withoutAuth(baseUrl);
};
/**
* If you only want to make public API calls, no authentication is needed
*/
HydroClient.withoutAuth = function (baseUrl) {
var sign = function (message) {
throw new errors_1.AuthError('Cannot authenticate without a private key!');
};
var handler = new RequestHandler_1.RequestHandler(sign, undefined, baseUrl);
return new HydroClient(handler, sign);
};
/**
* Provide a private key for authentication purposes
* @param privateKey A private key in hex format with the form "0x..."
*/
HydroClient.withPrivateKey = function (privateKey, baseUrl) {
var privateKeyBuffer = ethereumjs_util_1.toBuffer(privateKey);
var sign = function (message) {
// @ts-ignore: Error in the ethereumjs-util ts definition
var shaMessage = ethereumjs_util_1.hashPersonalMessage(ethereumjs_util_1.toBuffer(message));
var ecdsaSignature = ethereumjs_util_1.ecsign(shaMessage, privateKeyBuffer);
return ethereumjs_util_1.toRpcSig(ecdsaSignature.v, ecdsaSignature.r, ecdsaSignature.s);
};
var account = '0x' + ethereumjs_util_1.privateToAddress(privateKeyBuffer).toString('hex');
var handler = new RequestHandler_1.RequestHandler(sign, account, baseUrl);
return new HydroClient(handler, sign);
};
/**
* If you don't want to supply your private key, or want to integrate with a wallet, provide
* your own function to sign messages and the account you will be using.
* @param sign A function that takes the input message and signs it with the private key of the account
* @param account The account that will be doing the signing
*/
HydroClient.withCustomAuth = function (sign, account, baseUrl) {
var handler = new RequestHandler_1.RequestHandler(sign, account, baseUrl);
return new HydroClient(handler, sign);
};
HydroClient.prototype.setBaseUrl = function (baseUrl) {
this.handler.baseUrl = baseUrl;
return this;
};
/**
* Public API Calls
*
* These calls do not require any authentication to complete, and will generally give you
* public state about the Hydro API
*
* See https://docs.ddex.io/#public-rest-api
*/
/**
* Returns all active markets
*
* See https://docs.ddex.io/#list-markets
*/
HydroClient.prototype.listMarkets = function () {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets'))];
case 1:
data = _a.sent();
return [2 /*return*/, data.markets.map(function (market) { return new Market_1.Market(market); })];
}
});
});
};
/**
* Returns a specific market
*
* See https://docs.ddex.io/#get-a-market
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
*/
HydroClient.prototype.getMarket = function (marketId) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', marketId))];
case 1:
data = _a.sent();
return [2 /*return*/, new Market_1.Market(data.market)];
}
});
});
};
/**
* Returns tickers for all active markets
*
* See https://docs.ddex.io/#list-tickers
*/
HydroClient.prototype.listTickers = function () {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', 'tickers'))];
case 1:
data = _a.sent();
return [2 /*return*/, data.tickers.map(function (ticker) { return new Ticker_1.Ticker(ticker); })];
}
});
});
};
/**
* Returns ticker for a specific market
*
* See https://docs.ddex.io/#get-a-ticker
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
*/
HydroClient.prototype.getTicker = function (marketId) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', marketId, 'ticker'))];
case 1:
data = _a.sent();
return [2 /*return*/, new Ticker_1.Ticker(data.ticker)];
}
});
});
};
/**
* Returns the orderbook for a specific market
*
* See https://docs.ddex.io/#get-orderbook
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param level (Optional) The amount of detail returned in the orderbook. Default is level ONE.
*/
HydroClient.prototype.getOrderbook = function (marketId, level) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', marketId, 'orderbook'), { level: level })];
case 1:
data = _a.sent();
return [2 /*return*/, new Orderbook_1.Orderbook(data.orderBook, level)];
}
});
});
};
/**
* Returns paginated trades for a specific market
*
* See https://docs.ddex.io/#get-trades
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param page (Optional) Which page to return. Default is page 1.
* @param perPage (Optional) How many results per page. Default is 20.
*/
HydroClient.prototype.listTrades = function (marketId, page, perPage) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', marketId, 'trades'), { page: page, perPage: perPage })];
case 1:
data = _a.sent();
return [2 /*return*/, new TradeList_1.TradeList(data)];
}
});
});
};
/**
* Returns "candles" for building a trading chart for a specific market
*
* See https://docs.ddex.io/#get-candles
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param from The beginning of the time range as a UNIX timestamp
* @param to The end of the time range as a UNIX timestamp
* @param granularity The width of each candle in seconds
*/
HydroClient.prototype.listCandles = function (marketId, from, to, granularity) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', marketId, 'candles'), {
from: from,
to: to,
granularity: granularity
})];
case 1:
data = _a.sent();
return [2 /*return*/, data.candles.map(function (candle) { return new Candle_1.Candle(candle); })];
}
});
});
};
/**
* Calculate an estimated fee taken by the exchange given a price and amount for an order
*
* See https://docs.ddex.io/#calculate-fees
*
* @param price The price of the order
* @param amount The amount of token in the order
*/
HydroClient.prototype.calculateFees = function (price, amount, quoteSymbol) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('fees'), { price: price, amount: amount, quoteSymbol: quoteSymbol })];
case 1:
data = _a.sent();
return [2 /*return*/, new Fee_1.Fee(data)];
}
});
});
};
/**
* Private API Calls
*
* These calls require authentication, meaning you must have a valid trading address
* and the ability to sign requests with that address' private key.
*
* See https://docs.ddex.io/#private-rest-api
*/
/**
* Build a new order to submit to the exchange
*
* See https://docs.ddex.io/#build-unsigned-order
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param side Whether this is a "buy" or "sell" order
* @param price The price of the order
* @param amount The amount of token in the order
*/
HydroClient.prototype.buildOrder = function (marketId, side, price, amount) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.post(path_1.join('orders', 'build'), { marketId: marketId, side: side, price: price, amount: amount })];
case 1:
data = _a.sent();
return [2 /*return*/, new Order_1.Order(data.order)];
}
});
});
};
/**
* Submit a signed order to the exchange
*
* See https://docs.ddex.io/#place-order
*
* @param orderId The id of a built order
* @param signature String created by signing the orderId
*/
HydroClient.prototype.placeOrder = function (orderId, signature) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.post(path_1.join('orders'), { orderId: orderId, signature: signature })];
case 1:
data = _a.sent();
return [2 /*return*/, new Order_1.Order(data.order)];
}
});
});
};
/**
* A convenience function that will build an order, sign the order, and then
* immediately place the order on the system using the signing method passed
* in.
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param side Whether this is a "buy" or "sell" order
* @param price The price of the order
* @param amount The amount of token in the order
*/
HydroClient.prototype.createOrder = function (marketId, side, price, amount) {
return __awaiter(this, void 0, void 0, function () {
var order, signature;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.buildOrder(marketId, side, price, amount)];
case 1:
order = _a.sent();
return [4 /*yield*/, this.sign(order.id)];
case 2:
signature = _a.sent();
return [4 /*yield*/, this.placeOrder(order.id, signature)];
case 3: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Cancel an order you have submitted to the exchange
*
* See https://docs.ddex.io/#cancel-order
*
* @param orderId The id of the order you wish to cancel
*/
HydroClient.prototype.cancelOrder = function (orderId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.delete(path_1.join('orders', orderId))];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Return paginated orders you have submitted to the exchange
*
* See https://docs.ddex.io/#list-orders
*
* @param marketId (Optional) The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param status (Optional) Choose between "pending" or "all" orders
* @param page (Optional) Which page to return. Default is page 1.
* @param perPage (Optional) How many results per page. Default is 20.
*/
HydroClient.prototype.listOrders = function (marketId, status, page, perPage) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('orders'), { marketId: marketId, status: status, page: page, perPage: perPage }, true)];
case 1:
data = _a.sent();
return [2 /*return*/, new OrderList_1.OrderList(data)];
}
});
});
};
/**
* Return a specific order you have submitted to the exchange
*
* See https://docs.ddex.io/#get-order
*
* @param orderId The id of the order
*/
HydroClient.prototype.getOrder = function (orderId) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('orders', orderId), {}, true)];
case 1:
data = _a.sent();
return [2 /*return*/, new Order_1.Order(data.order)];
}
});
});
};
/**
* Return paginated list of all trades you have made
*
* See https://docs.ddex.io/#list-account-trades
*
* @param marketId The id of the market, specified as a trading pair, e.g. "ZRX-ETH"
* @param page (Optional) Which page to return. Default is page 1.
* @param perPage (Optional) How many results per page. Default is 20.
*/
HydroClient.prototype.listAccountTrades = function (marketId, page, perPage) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('markets', marketId, 'trades', 'mine'), { page: page, perPage: perPage }, true)];
case 1:
data = _a.sent();
return [2 /*return*/, new TradeList_1.TradeList(data)];
}
});
});
};
/**
* Return locked balances for each active token
*
* See https://docs.ddex.io/#list-locked-balances
*/
HydroClient.prototype.listLockedBalances = function () {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('account', 'lockedBalances'), {}, true)];
case 1:
data = _a.sent();
return [2 /*return*/, data.lockedBalances.map(function (lockedBalance) { return new LockedBalance_1.LockedBalance(lockedBalance); })];
}
});
});
};
/**
* Return a specific locked balance
*
* See https://docs.ddex.io/#get-locked-balance
*
* @param symbol The symbol for the token you want to see your locked balance
*/
HydroClient.prototype.getLockedBalance = function (symbol) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.handler.get(path_1.join('account', 'lockedBalance'), { symbol: symbol }, true)];
case 1:
data = _a.sent();
return [2 /*return*/, new LockedBalance_1.LockedBalance(data.lockedBalance)];
}
});
});
};
return HydroClient;
}());
exports.HydroClient = HydroClient;