crypto-exchange-client
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

Typescript Crypto API Clients w/ WebSockets

Similar to ccxt, this project aims to provide a common interface for interacting with exchange apis. The difference is that this projcet provides a common websocket interface, is implemented in only a single language, and enforces types (so that different implementations can be uniform, and not cause errors when switching between exchanges)

Cobinhood is the only completed implementation, but this project is setup to allow for any number of implementations.

Installation

Coming soon

Cobinhood Usage

API Documentation

Authentication

In the root of your project folder, create a .env file

COBINHOOD_API_KEY="your-cobinhood-api-key"

this will be loaded on boot of your application, and will throw an exception if process.env.COBINHOOD_API_KEY is an empty value when trying to access an authenticated endpoint (meaning, you don't need an .env file, but it's handy for development).

Examples

See ./examples/cobinhood
run with

yarn example ./examples/cobinhood/watch-market-depth.ts

Creating Clients

import { CobinhoodRestClient, CobinhoodFeed } from 'crypto-exchange-client';
 
const client = new CobinhoodRestClient();
const socket = new client.SocketClient();
// or, specifically:
const socket = new CobinhoodFeed();

Using the WebSocket Client

socket.onOpen(handleOpen(socket));
 
socket.onReceiveTicker(handleReceivedTicker);
socket.onReceiveOrderBookUpdate(handleReceivedOrderBookUpdate);
 
socket.onSubscribe(handleSubscribe);
socket.connect();
 
const handleOpen = (socket) => () => {
  socket.subscribeTo('ticker', { trading_pair_id: 'ETH-BTC' });
};
 
// do something with ticker data
const handleReceivedTicker = (data: TickerUpdate) => {};
 
// do something with order book data
const handleReceivedOrderBookUpdate = (data: OrderBookUpdateSummary) => {};
 
// json here is a subscription response, which mostly contains
// the subscription request data
const handleSubscribe = (json: any) => {}

REST Client Overview

NOTE: this documentation is a work in progress. PRs to expand it will be more than welcome.

// getting all your balances
const balances = await client.getBalances();
 
// buying
const order = await client.createLimitBuyOrder('ETH-BTC', '2', '0.098');
 

Full Cobinhood Documentation

Cobinhood Rest Client

Cobinhood Rest Client

Given that you have a CobinhoodRestClient,

const client = new CobinhoodRestClient();

The following mirrors the cobinhood api documentation

Markets

Markets

None of the market apis require auth.

client.getCurrencies()
client.getMarkets()
client.getOrderBook(marketstring, limit = 50)
client.getMarketStats()
client.getTicker();
client.getRecentTrades(marketstring);

Interacting with the market data:

// { [symbol: string]: MarketPair }
client.marketPairsBySymbol

All market data from the api requests are stored on an object of type MarketPair.

Note that if a list of currency symbols is needed, but a separate request is not desired, that can be done with Object.keys on the client.marketPairsBySymbol data, and splitting on the separator, -.

const currencies = _.uniq(_.flatten(
    Object.keys(marketPairsBySymbol).map(symbol => symbol.split('-'))
))

Getting Cached Market

client.marketForSymbol('ETH-BTC');

Cobinhood Web Socket Client

Orders

Orders

All of these apis require auth

client.getOrder(idstring);
client.getOpenOrders();
 
client.createOrder(marketstring, amountstring, pricestring, typestring, isBuySideboolean);
// shorthands for createOrder
client.createLimitOrder(marketstring, amountstring, pricestring, isBuySideboolean);
client.createLimitBuyOrder(marketstring, amountstring, pricestring);
client.createLimitSellOrder(marketstring, amountstring, pricestring);
 
client.createMarketBuyOrder(marketstring, amountstring);
client.createMarketSellOrder(marketstring, amountstring);
client.createMarketOrder(marketstring, amountstring, isBuySideboolean);
 
 
client.cancelOrder(idstring);

Cobinhood Web Socket Client

Cobinhood Web Socket Client

Cobinhood Web Socket Client

Cobinhood Web Socket Client

const socket = new CobinhoodFeed();

Support

Programming takes time, so if there is no time to submit a PR or a bugfix, donations are welcome at:

ETH (or any ERC20 that Cobinhood supports):
0xF6C2769a30647f6B7a412E446cD56650fAC64205

BTC:
1MfGcwcUPHADTumrxCponqv7eAxfZMyha8

Readme

Keywords

none

Package Sidebar

Install

npm i crypto-exchange-client

Weekly Downloads

1

Version

0.0.6

License

MIT

Unpacked Size

124 kB

Total Files

144

Last publish

Collaborators

  • nullvoxpopuli