Skip to content

exchange-gate/eg-sdk-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExchangeGate Javascript SDK

This ExchangeGate SDK library supports web browsers and NodeJS.

Installation

NPM (or Yarn)

You can use any NPM-compatible package manager, including NPM itself and Yarn.

npm i @exchange-gate.io/eg-sdk-js

Then:

import { ExchangeGate } from '@exchange-gate.io/eg-sdk-js';

Or, if you're not using ES6 modules:

const ExchangeGate = require('@exchange-gate.io/eg-sdk-js').ExchangeGate;

REST API

Initialize Rest Client

const egRest = new ExchangeGate.Rest(API_KEY);

Public trades snapshot

Fetch latest x100 public trades

const publicTrades = await egRest.fetchPublicTradesSnapshot('binance', 'btc-usdt');

Order Book snapshot

Fetch OrderBook snapshot x100 Bids/Asks

const orderBook = await egRest.fetchOrderBookSnapshot('binance', 'btc-usdt');

Exchanger markets

Fetch available Exchanger Markets

const exchangerMarkets = await egRest.fetchExchangerMarkets();

Price ticker

Fetch price ticker for selected markets based from last trade price

const priceTicker = await egRest.fetchPriceTicker('binance', ['btc-usdt', 'btc-eth']);

OrderBook ticker

Fetch order book ticker for selected markets based from best bid/ask

const orderBookTicker = await egRest.fetchOrderBookTicker('binance', ['btc-usdt', 'btc-eth']);

Wallet balance

Fetch Exchanger account wallet balance

const walletBalance = await egRest.fetchWalletBalance('binance', 'btc-usdt');

Open orders

Fetch Exchanger account open orders

const openOrders = await egRest.fetchOpenOrders('binance', 'btc-usdt');

My trades

Fetch Exchanger account trades

const myTrades = await egRest.fetchMyTrades('binance', 'btc-usdt');

Market order

Create Market order

const side = 'buy'; // sell
const marketOrder = await egRest.createMarketOrder('binance', 'btc-usdt', side, amount);

Limit order

Create Limit order

const side = 'buy'; // sell
const limitOrder = await egRest.createLimitOrder('binance', 'btc-usdt', side, amount, limitPrice);

Cancel order

Cancel open order

const order = await egRest.cancelOrder('binance', 'btc-usdt', uuid);

REALTIME

Initialize Realtime Client

const egRealtime = new ExchangeGate.Realtime(API_KEY);

Socket events

Listen for socket events

const socketEvents = ['connect', 'connecting', 'error', 'disconnect', 'close'];
for (const socketEvent of socketEvents) {
    egRealtime.onSocketEvent(socketEvent, (e) => {
        console.log('onSocketEvent', socketEvent, e);
    });
}

Remote Procedure Call's (RPC) over WebSocket's

Any of REST API methods also available over WS as RPC

const publicTrades = await egRealtime.rpc.fetchPublicTradesSnapshot('binance', 'btc-usdt');

STREAMS

Public trades

Subscribe public trades stream

const streamSubscription = egRealtime.stream.publicTrades('binance', 'btc-usdt');
const streamConsumerA = streamSubscription.consumer();
const streamConsumerB = streamSubscription.consumer();

(async () => {
    for await (const event of streamConsumerA) {
        console.log('event-A', event.name, event.data);
    }
    console.log('stream-A closed');
})();

(async () => {
    for await (const event of streamConsumerB) {
        console.log('event-B', event.name, event.data);
    }
    console.log('stream-B closed');
})();

Kill consumer-A but preserve subscription and any other consumers

streamConsumerA.kill();

UnSubscribe stream and close all stream consumers

streamSubscription.close();
// OR
egRealtime.stream.publicTrades('binance', 'btc-usdt').close();

!NOTE! It is safe to issue stream subscription multiple times within single connection

egRealtime.stream.publicTrades('binance', 'btc-usdt');
// this will be same subscription - no double spend
egRealtime.stream.publicTrades('binance', 'btc-usdt');

Disconnect from socket server

egRealtime.disconnect();

Order book

Subscribe order book stream.
Always verified and synchronized with server snapshot, even after dropped connection silently sync's back to guarantee bid/ask precision;
emitTimeoutMs - default 500, set to 0 for realtime.

const streamSubscription = egRealtime.stream.orderBook('binance', 'btc-usdt', emitTimeoutMs);
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

Price ticker

Subscribe market price ticker stream.
Price ticker for selected markets based from last trade price, published once per second

const streamSubscription = egRealtime.stream.priceTicker('binance', ['btc-usdt', 'btc-eth']);
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

OrderBook ticker

Subscribe market order book ticker stream.
Order book ticker for selected markets based from best bid/ask, published once per second

const streamSubscription = egRealtime.stream.orderBookTicker('binance', ['btc-usdt', 'btc-eth']);
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

My trades

Subscribe my trades stream

const streamSubscription = egRealtime.stream.myTrades('binance', 'btc-usdt');
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

Open orders

Subscribe Open orders stream.
New orders or any existing order changes

const streamSubscription = egRealtime.stream.openOrders('binance', 'btc-usdt');
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

Wallet balance

Subscribe Wallet balance stream.

const streamSubscription = egRealtime.stream.walletBalance('binance', 'btc-usdt');
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

Subscription details

Subscribe Subscription details stream.
Remaining credits, available/used socket connections

const streamSubscription = egRealtime.stream.subscriptionDetails();
(async () => {
    for await (const event of streamSubscription.consumer()) {
        console.log('event', event.name, event.data);
    }
})();

About

Exchange-Gate.IO javascript browser/nodeJS SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published