Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:coinbase/gdax-tt
Browse files Browse the repository at this point in the history
  • Loading branch information
Cayle Sharrock committed Sep 7, 2017
2 parents 0f94101 + f67c47d commit ba7e487
Show file tree
Hide file tree
Showing 10 changed files with 731 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ build
src/**/*.js
.nyc_output
coverage
.DS_Store
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"crypto": "0.0.3",
"gdax": "0.3.1",
"limiter": "git://github.com/jhurliman/node-rate-limiter.git#58ce2fda6b5c2bc4ccb81ba3768c5b1bc06c91a5",
"node.bittrex.api": "0.5.1",
"pushbullet": "2.0.0",
"querystring": "0.2.0",
"simple-mock": "0.8.0",
Expand Down
159 changes: 159 additions & 0 deletions src/exchanges/bittrex/BittrexAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/***************************************************************************************************************************
* @license *
* Copyright 2017 Coinbase, Inc. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on *
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
* License for the specific language governing permissions and limitations under the License. *
***************************************************************************************************************************/
import { Product, PublicExchangeAPI, Ticker } from '../PublicExchangeAPI';
import * as Bittrex from 'node.bittrex.api';
import { ExchangeAuthConfig } from '../AuthConfig';
import { AuthenticatedExchangeAPI, Balances } from '../AuthenticatedExchangeAPI';
import { Big } from '../../lib/types';
import { BookBuilder } from '../../lib/BookBuilder';
import { Logger } from '../../utils/Logger';
import { PlaceOrderMessage } from '../../core/Messages';
import { LiveOrder } from '../../lib/Orderbook';

export class BittrexAPI implements PublicExchangeAPI, AuthenticatedExchangeAPI {
static normalizeProduct(gdaxProduct: string): string {
const [base, quote] = gdaxProduct.split('-');
return `${quote}-${base}`;
}

readonly owner: string;
readonly logger: Logger;

constructor(auth: ExchangeAuthConfig, logger: Logger) {
this.owner = 'Bittrex';
this.logger = logger;
Bittrex.options({
apikey: auth.key || 'APIKEY',
apisecret: auth.secret || 'APISECRET',
inverse_callback_arguments: true,
stream: false,
cleartext: false,
verbose: false
});
}

loadProducts(): Promise<Product[]> {
return new Promise((resolve, reject) => {
Bittrex.getmarkets((err, data) => {
if (err) {
return reject(err);
}
if (!data.success || !data.result) {
return reject(new Error('Unexpected response from Bittrex: ' + JSON.stringify(data)));
}
const result: Product[] = data.result.map((market: any) => {
return {
id: market.MarketName, // same format as GDAX, so no need to map
baseCurrency: market.BaseCurrency,
quoteCurrency: market.marketCurrency,
baseMinSize: Big(market.MinTradeSize),
baseMaxSize: Big('1e18'),
quoteIncrement: Big(market.MinTradeSize)
};
});
return resolve(result);
});
});
}

loadMidMarketPrice(gdaxProduct: string): Promise<BigNumber.BigNumber> {
return this.loadTicker(gdaxProduct).then((ticker: Ticker) => {
return ticker.bid.plus(ticker.ask).times(0.5);
});
}

loadOrderbook(gdaxProduct: string): Promise<BookBuilder> {
const product = BittrexAPI.normalizeProduct(gdaxProduct);
return new Promise((resolve, reject) => {
Bittrex.getorderbook({
market: product,
type: 'both',
depth: 5000
}, (err, data) => {
if (err) {
return reject(err);
}
if (!data.success || !data.result) {
return reject(new Error('Unexpected response from Bittrex: ' + JSON.stringify(data)));
}
const bids: any = data.result.buy;
const asks: any = data.result.sell;
const book: BookBuilder = new BookBuilder(this.logger);
bids.forEach((order: any) => {
book.add({
id: order.Rate,
price: Big(order.Rate),
size: Big(order.Quantity),
side: 'buy'
});
});
asks.forEach((order: any) => {
book.add({
id: order.Rate,
price: Big(order.Rate),
size: Big(order.Quantity),
side: 'sell'
});
});
return resolve(book);
});
});
}

loadTicker(gdaxProduct: string): Promise<Ticker> {
const product = BittrexAPI.normalizeProduct(gdaxProduct);
return new Promise((resolve, reject) => {
Bittrex.getticker({ market: product }, (err, data) => {
if (err) {
return reject(err);
}
if (!data.success || !data.result) {
return reject(new Error('Unexpected response from Bittrex: ' + JSON.stringify(data)));
}
const result: Ticker = {
productId: gdaxProduct,
ask: Big(data.result.Ask),
bid: Big(data.result.Bid),
price: Big(data.result.Last),
time: new Date()
};
return resolve(result);
});
});
}

placeOrder(order: PlaceOrderMessage): Promise<LiveOrder> {
throw new Error('Method not implemented.');
}

cancelOrder(id: string): Promise<string> {
throw new Error('Method not implemented.');
}

cancelAllOrders(product: string): Promise<string[]> {
throw new Error('Method not implemented.');
}

loadOrder(id: string): Promise<LiveOrder> {
throw new Error('Method not implemented.');
}

loadAllOrders(gdaxProduct: string): Promise<LiveOrder[]> {
throw new Error('Method not implemented.');
}

loadBalances(): Promise<Balances> {
throw new Error('Method not implemented.');
}
}
Loading

0 comments on commit ba7e487

Please sign in to comment.