@bsx-exchange/client
TypeScript icon, indicating that this package has built-in type declarations

0.0.18 • Public • Published

BSX JavaScript SDK

The BSX JS SDK is a lightweight library that allows you to interact with BSX API.

Table Of Contents

Installation

With Yarn:

$ yarn add @bsx-exchange/client

With NPM:

$ npm install @bsx-exchange/client

Examples

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

const main = async () => {
  const bsxInstance = await BsxInstance.createWithApiKey(
    '<api-key>', // 9c77801a61fe23cebc574524b2b875e7
    '<api-secret>', // d6217f927d24a9f40b668f94f153b97254ab230df92770f3e2367855fffd0b9f
    '<signer-private-key>', // 0x5ef68ecef054da6b13cdf79f2f78ca362ebffa68b19e4b5b1a3bd78df53e585c
    ENV_NAME.TESTNET,
  );

  // More action
}

Usage

Create instance with API key - Primary method

Please notice that with this method, you cannot perform request withdraw action.

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = await BsxInstance.createWithApiKey(
      '<api-key>',
      '<api-secret>',
      '<signer-private-key>',
      ENV_NAME.TESTNET,
    );

    // Create order
    const resCreateOrder = await bsxInstance.createOrder({
      side: 'BUY',
      type: 'LIMIT',
      product_index: 1, // 1 for BTC-PERP, 2 for ETH_PERP ... (check Product Index section)
      price: '1000',
      size: '0.01',
      post_only: false,
      reduce_only: false,
    });
    console.log('createOrder', resCreateOrder.result, resCreateOrder.error);

    // Get all open orders
    const resOpenOrder = await bsxInstance.getAllOpenOrders();
    console.log('getAllOpenOrders', resOpenOrder.result, resOpenOrder.error);

    // Get order history
    const resOrderHistory = await bsxInstance.getOrderHistory('BTC-PERP');
    console.log('getOrderHistory', resOrderHistory.result, resOrderHistory.error);

    // Cancel order
    const resCancelOrder = await bsxInstance.cancelOrder(resCreateOrder.result.id);
    console.log('cancelOrder', resCancelOrder.result, resCancelOrder.error);

  } catch (error) {
    console.log('Error', error);
  }
}

main();

Initializing the Instance with user wallet and signer (not recommended)

Input private key of your wallet and signer to create SDK Instance for later use. With this method, you can perform request withdraw action. Register action is required before performing any other actions.

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

// Initializing a client to return content in the store's primary language
const bsxInstance = new BsxInstance('<user-private-key>', '<signer-private-key>', ENV_NAME.TESTNET);

Register account (only for initializing with user wallet and signer)

Create order with signature create from signer and user wallet

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = new BsxInstance('<user-private-key>', '<signer-private-key>', ENV_NAME.TESTNET);

    // Register account
    // MUST do if you initialize with user wallet and signer
    const { result, error: registerError, curl } = await bsxInstance.register();
    if (!registerError) {
      console.log('register success', result, curl);
    } else {
      console.log('register error', registerError, curl);
    }

    // Create order
    const resCreateOrder = await bsxInstance.createOrder({
      side: 'BUY',
      type: 'LIMIT',
      product_index: 1, // 1 for BTC-PERP, 2 for ETH_PERP ... (check Product Index section)
      price: '1000',
      size: '0.01',
      post_only: false,
      reduce_only: false,
    });
    console.log('createOrder', resCreateOrder.result, resCreateOrder.error);

    // Get all open orders
    const resOpenOrder = await bsxInstance.getAllOpenOrders();
    console.log('getAllOpenOrders', resOpenOrder.result, resOpenOrder.error);

    // Get order history
    const resOrderHistory = await bsxInstance.getOrderHistory('BTC-PERP');
    console.log('getOrderHistory', resOrderHistory.result, resOrderHistory.error);

    // Cancel order
    const resCancelOrder = await bsxInstance.cancelOrder(resCreateOrder.result.id);
    console.log('cancelOrder', resCancelOrder.result, resCancelOrder.error);
  } catch (error) {
    console.log('Error', error);
  }
}

main();

Using other api base url

You can use other api base url by passing the url to the BsxInstance.createWithApiKey method

import { BsxInstance } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = await BsxInstance.createWithApiKey(
      '<api-key>',
      '<api-secret>',
      '<signer-private-key>',
      '<base-url>', // Example: https://api.bsx.exchange
    );

    // More action

  } catch (error) {
    console.log('Error', error);
  }
}

main();

With user wallet and signer

import { BsxInstance } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = new BsxInstance(
      '<user-private-key>',
      '<signer-private-key>',
      '<base-url>',
    );

    // Register account
    // MUST do if you initialize with user wallet and signer
    const { result, error: registerError, curl } = await bsxInstance.register();
    if (!registerError) {
      console.log('register success', result, curl);
    } else {
      console.log('register error', registerError, curl);
    }

    // More action

  } catch (error) {
    console.log('Error', error);
  }
}

main();

Create order

Create order with signature create from signer and user wallet

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = await BsxInstance.createWithApiKey(
      '<api-key>',
      '<api-secret>',
      '<signer-private-key>',
      ENV_NAME.TESTNET,
    );
    const resCreateOrder = await bsxInstance.createOrder({
      side: 'BUY',
      type: 'LIMIT',
      product_index: 1,
      price: '1000',
      size: '0.01',
      post_only: false,
      reduce_only: false,
    })
  } catch (error) {
    console.log('Error', error);
  }
}

main();

Example with user wallet and signer

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = new BsxInstance('<user-private-key>', '<signer-private-key>', ENV_NAME.TESTNET);

    // Register account
    // MUST do if you initialize with user wallet and signer
    const { result, error: registerError, curl } = await bsxInstance.register();
    if (!registerError) {
      console.log('register success', result, curl);
    } else {
      console.log('register error', registerError, curl);
    }

    const resCreateOrder = await bsxInstance.createOrder({
      side: 'BUY',
      type: 'LIMIT',
      product_index: 1,
      price: '1000',
      size: '0.01',
      post_only: false,
      reduce_only: false,
    })
  } catch (error) {
    console.log('Error', error);
  }
}

main();

Batch update orders

Create and cancel orders in batch

import { BsxInstance, ENV_NAME } from '@bsx-exchange/client';

const main = async () => {
  try {
    const bsxInstance = await BsxInstance.createWithApiKey(
      '<api-key>',
      '<api-secret>',
      '<signer-private-key>',
      ENV_NAME.TESTNET,
    );
    const resBatchUpdateOrders = await bsxInstance.batchUpdateOrders([
      {
        op_type: 'CREATE',
        create_order_request: {
          side: 'BUY',
          type: 'LIMIT',
          product_index: 1,
          price: '1000',
          size: '0.01',
          post_only: false,
          reduce_only: false,
        }
      },
      {
        op_type: 'CANCEL',
        cancel_request: {
          orderId: 'order_id'
        }
      }
      {
        op_type: "CANCEL_BULK",
        cancel_orders_request: {
          "order_ids": ['order_id_1', 'order_id_2'],
        }
      },
      {
        op_type: "CANCEL_ALL",
        cancel_all_orders_request: {
          "product_id": "BTC-PERP"
        }
      }
    ]);
  } catch (error) {
    console.log('Error', error);
  }
}

main();

Submit withdraw request

Submit withdraw request

bsxInstance
  .submitWithdrawalRequest('100.1')
  .then(({ result, error, curl }) => {
    console.log('submitWithdrawalRequest', result, error, curl);
  })
  .catch((error) => {
    console.log('submitWithdrawalRequest catch error', error);
  });

Get all open orders

Get all current open order of user

bsxInstance
  .getAllOpenOrders()
  .then(({ result, error, curl }) => {
    console.log('getAllOpenOrders', result, error, curl);
  })
  .catch((error) => {
    console.log('getAllOpenOrders catch error', error);
  });

Get orders history

Get order history

bsxInstance
  .getOrderHistory('BTC-PERP') // pass '' to get all
  .then(({ result, error, curl }) => {
    console.log('getOrderHistory', result, error, curl);
  })
  .catch((error) => {
    console.log('getOrderHistory catch error', error);
  });

Cancel order

Cancel order by order id

bsxInstance
  .cancelOrder('order_id')
  .then(({ result, error, curl }) => {
    console.log('cancelOrder', result, error, curl);
  })
  .catch((error) => {
    console.log('cancelOrder catch error', error);
  });

Cancel all orders

Cancel all open orders

bsxInstance
  .cancelAllOrders()
  .then(({ result, error, curl }) => {
    console.log('cancelAllOrders', result, error, curl);
  })
  .catch((error) => {
    console.log('cancelAllOrders catch error', error);
  });

Cancel all open orders of a product

bsxInstance
  .cancelAllOrders('BTC-PERP')
  .then(({ result, error, curl }) => {
    console.log('cancelAllOrders', result, error, curl);
  })
  .catch((error) => {
    console.log('cancelAllOrders catch error', error);
  });

Cancel bulk orders

Cancel bulk orders by order ids

bsxInstance
  .cancelBulkOrders(['order_id_1', 'order_id_1'])
  .then(({ result, error, curl }) => {
    console.log('cancelBulkOrders', result, error, curl);
  })
  .catch((error) => {
    console.log('cancelBulkOrders catch error', error);
  });

Get portfolio detail

Get portfolio detail of user

bsxInstance
  .getPortfolioDetail()
  .then(({ result, error, curl }) => {
    console.log('getPortfolioDetail', result, error, curl);
  })
  .catch((error) => {
    console.log('getPortfolioDetail catch error', error);
  });

Get user trade history

Get user trade history

bsxInstance
  .getUserTradeHistory({
    product_id: 'ETH_PERP',
    limit: 50,
    page: 0,
  }) // pass '' to get all
  .then(({ result, error, curl }) => {
    console.log('getUserTradeHistory', result, error, curl);
  })
  .catch((error) => {
    console.log('getUserTradeHistory catch error', error);
  });

Get products

Get all products

bsxInstance
  .getProducts()
  .then(({ result, error, curl }) => {
    console.log('getProducts', result, error, curl);
  })
  .catch((error) => {
    console.log('getProducts catch error', error);
  });

Get funding history

Get funding history

bsxInstance
  .getFundingHistory('BTC-PERP') // pass '' to get all
  .then(({ result, error, curl }) => {
    console.log('getFundingHistory', result, error, curl);
  })
  .catch((error) => {
    console.log('getFundingHistory catch error', error);
  });

Get api key list

Get api key list

bsxInstance
  .getApiKeyList()
  .then(({ result, error, curl }) => {
    console.log('getApiKeyList', result, error, curl);
  })
  .catch((error) => {
    console.log('getApiKeyList catch error', error);
  });

Delete user api key

Delete user api key by key id

bsxInstance
  .deleteUserApiKey('api_key_id')
  .then(({ result, error, curl }) => {
    console.log('deleteUserApiKey', result, error, curl);
  })
  .catch((error) => {
    console.log('deleteUserApiKey catch error', error);
  });

Create user api key

Create user api key

bsxInstance
  .createUserApiKey('Api Name')
  .then(({ result, error, curl }) => {
    console.log('createUserApiKey', result, error, curl);
  })
  .catch((error) => {
    console.log('createUserApiKey catch error', error);
  });

Get transfer history

Get transfer history

bsxInstance
  .getTransferHistory({
    page: 0,
    limit: 100,
  })
  .then(({ result, error, curl }) => {
    console.log('getTransferHistory', result, error, curl);
  })
  .catch((error) => {
    console.log('getTransferHistory catch error', error);
  });

Product Index

Product index is used to identify the product when creating order. The index is as follows:

  • 1: BTC-PERP
  • 2: ETH_PERP
  • 3: SOL_PERP
  • 4: WIF_PERP

License

MIT

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.181latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.181
0.0.170
0.0.160
0.0.150
0.0.140
0.0.130
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i @bsx-exchange/client

Weekly Downloads

1

Version

0.0.18

License

none

Unpacked Size

7.21 MB

Total Files

25

Last publish

Collaborators

  • bsx-engineer