tlab-trading-toolkit
Version:
A trading toolkit for building advanced trading bots on the GDAX platform
166 lines (165 loc) • 6.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ProductMap_1 = require("../ProductMap");
const BookBuilder_1 = require("../../lib/BookBuilder");
const PoloniexCommon_1 = require("./PoloniexCommon");
const utils_1 = require("../utils");
const Logger_1 = require("../../utils/Logger");
const types_1 = require("../../lib/types");
const superAgent = require("superagent");
/**
* An adapter class that maps the standardized API calls to Polinex's API interface
*/
class PoloniexExchangeAPI {
constructor(config) {
this.owner = 'Poloniex';
this.auth = config.auth || null;
this.logger = config.logger || Logger_1.ConsoleLoggerFactory();
}
static product(genericProduct) {
return ProductMap_1.ProductMap.ExchangeMap.get('Poloniex').getExchangeProduct(genericProduct) || genericProduct;
}
static genericProduct(exchangeProduct) {
return ProductMap_1.ProductMap.ExchangeMap.get('Poloniex').getGenericProduct(exchangeProduct) || exchangeProduct;
}
static getMarket(genericProduct) {
return ProductMap_1.ProductMap.ExchangeMap.get('Poloniex').getMarket(genericProduct);
}
static getMarketForExchangeProduct(exchangeProduct) {
return ProductMap_1.ProductMap.ExchangeMap.get('Poloniex').getMarket(PoloniexExchangeAPI.genericProduct(exchangeProduct));
}
loadProducts() {
const req = this.publicRequest('returnTicker');
return utils_1.handleResponse(req, null).then((tickers) => {
const products = Object.keys(tickers);
const productList = products.map(PoloniexCommon_1.getGenericProduct);
return Promise.resolve(productList);
});
}
placeOrder(order) {
return undefined;
}
cancelOrder(id) {
return undefined;
}
cancelAllOrders() {
return undefined;
}
loadOrder(id) {
return undefined;
}
loadAllOrders(genericProduct) {
return undefined;
}
loadBalances() {
const req = this.authRequest('returnCompleteBalances', { account: 'all' });
return utils_1.handleResponse(req, {
owner: this.owner,
req: 'returnBalances'
}).then((balances) => {
const result = {
exchange: {}
};
for (const currency in balances) {
const balance = balances[currency];
const available = types_1.Big(balance.available);
const total = available.plus(balance.onOrders);
// TODO include other accounts
result.exchange[currency] = { balance: total, available: available };
}
return result;
});
}
requestCryptoAddress(cur) {
return undefined;
}
requestTransfer(request) {
return undefined;
}
requestWithdrawal(request) {
return undefined;
}
transfer(cur, amount, from, to, options) {
return undefined;
}
loadMidMarketPrice(genericProduct) {
const req = this.publicRequest('returnTicker');
return utils_1.handleResponse(req, {
owner: this.owner,
req: 'returnTicker'
}).then((tickers) => {
const ticker = tickers[PoloniexExchangeAPI.product(genericProduct)];
return types_1.Big(ticker.lowestAsk).plus(ticker.highestBid).times(0.5);
});
}
loadOrderbook(genericProduct) {
const product = PoloniexExchangeAPI.product(genericProduct);
const req = this.publicRequest('returnOrderBook', { currencyPair: product });
return utils_1.handleResponse(req, {
owner: this.owner,
req: 'returnOrderBook',
product: product
}).then((book) => {
const builder = new BookBuilder_1.BookBuilder(this.logger);
let order;
book.asks.forEach((level) => {
order = {
id: level[0].toString(),
side: 'sell',
price: types_1.Big(level[0]),
size: types_1.Big(level[1])
};
builder.add(order);
});
book.bids.forEach((level) => {
order = {
id: level[0].toString(),
side: 'buy',
price: types_1.Big(level[0]),
size: types_1.Big(level[1])
};
builder.add(order);
});
return builder;
});
}
loadTicker(genericProduct) {
const product = PoloniexExchangeAPI.product(genericProduct);
const req = this.publicRequest('returnTicker');
return utils_1.handleResponse(req, {
owner: this.owner,
req: 'returnTicker'
}).then((tickers) => {
const ticker = tickers[product];
return {
productId: genericProduct,
price: types_1.Big(ticker.last),
bid: types_1.Big(ticker.highestBid),
ask: types_1.Big(ticker.lowestAsk),
volume: types_1.Big(ticker.baseVolume),
time: new Date()
};
});
}
publicRequest(command, params) {
const url = `${PoloniexCommon_1.POLONIEX_API_URL}?command=${command}`;
return superAgent.get(url).query(params).accept('json');
}
authRequest(command, params) {
if (!this.auth) {
return null;
}
const nonce = new Date().valueOf();
const url = `${PoloniexCommon_1.POLONIEX_API_URL}?command=${command}&nonce=${nonce}`;
const body = Object.assign({ command: command, nonce: nonce }, params);
const signature = utils_1.getSignature(this.auth, JSON.stringify(body), 'sha512');
return superAgent.post(url)
.set({
Key: this.auth.key,
Sign: signature
})
.send(body)
.accept('json');
}
}
exports.PoloniexExchangeAPI = PoloniexExchangeAPI;