Skip to content

class BitShares

Nikolay Nozdrin-Plotnitsky edited this page May 10, 2018 · 3 revisions

Index

  • Static methods
    • init()
    • connect()
    • subscribe()
    • generateKeys()
    • ticker()
    • tradeHistory()
  • Object methods
    • constructor()
    • setFeeAsset()
    • setMemoKey()
    • balances()
    • buy()
    • sell()
    • transfer()
    • cancelOrder()
    • orders()

Static methods

init()

Signature:

static init(node = "wss://bitshares.openledger.info/ws", autoconnect = false)

connect()

Signature:

static async connect()

Example

const BitShares = require('btsdex');

async function start() {
  BitShares.init();
  await BitShares.connect();

  // Do something...
}

start()

subscribe()

Signature:

static subscribe(event, callback)

Now have events:

  • connected - works once after connecting to the blockchain;
  • block - it works when a new block is created in the blockchain;
  • account - occurs when the specified account is changed (balance change).

Example

const BitShares = require("btsdex");

BitShares.init("wss://bitshares.openledger.info/ws");

BitShares.subscribe('connected', startAfterConnected);
BitShares.subscribe('block', callEachBlock);
BitShares.subscribe('account', changeAccount, 'trade-bot');

async function startAfterConnected() {/* is called once after connecting to the blockchain */}
async function callEachBlock(obj) {/* is called with each block created */}
async function changeAccount(array) {/* is called when you change the 'trade-bot' account */}

generateKeys()

Signature:

static generateKeys(name, password, arrKeysName)

This method need if you know only login and password. And you need active and memo private keys. This method will be change!

Example

const BitShares = require("btsdex");

BitShares.init("wss://bitshares.openledger.info/ws");

keys = BitShares.generateKeys('trade-bot', 'password', ['owner','active','memo']);
console.log(keys); //{ privKeys:{ owner:..., active:..., memo:...}, pubKeys:{ owner:..., active:..., memo:...}}

let acc = new BitShares('trade-bot', keys.privKeys.active.toWif());

ticker()

Signature:

static async ticker(baseSymbol, quoteSymbol)

Example

const BitShares = require('btsdex');

async function start() {
  BitShares.init();
  await BitShares.connect();

  let ticker = await BitShares.ticker('usd', 'bts');
  console.log(ticker); // { latest: '0.3857908',lowest_ask: ... }
}

start()

tradeHistory()

Signature:

static async tradeHistory(quoteSymbol, baseSymbol, startDate, stopDate, bucketSeconds)

Example

const BitShares = require('btsdex');

async function start() {
  BitShares.init();
  await BitShares.connect();

  let start = new Date();
  start.setMonth(start.getMonth() - 1); // Month back
  let stop = new Date();

  let data = await BitShares.tradeHistory("usd","bts", start, stop, 60 * 60 * 24)); 
  console.log(data); // [{high_base:..., low_quote:...}, {...}]
}

start()

Object methods

constructor()

Signature:

constructor(accountName, activeKey, feeSymbol = 'bts')

setFeeAsset()

Signature:

async setFeeAsset(feeSymbol)

setMemoKey()

Signature:

setMemoKey(memoKey)

balances()

Signature:

async balances()

Example

const BitShares = require('btsdex');

BitShares.init();
BitShares.subscribe('connected', start);

async function start() {
  let bot = new BitShares('trade-bot', 'privateActiveKey');
  console.log(await bot.balances()); // [{ amount: 117669, asset: Asset {id: '1.3.0',...} },{...}]
}

buy()

Signature:

async buy(buySymbol, baseSymbol, amount, price, fill_or_kill = false, expire = "2020-02-02T02:02:02")

Example

const BitShares = require('btsdex');

BitShares.init();
BitShares.subscribe('connected', start);

async function start() {
  let bot = new BitShares('trade-bot', 'privateActiveKey');
  await bot.buy('open.btc', 'bts', 0.0043, 31000);
}

sell()

Signature:

async sell(sellSymbol, baseSymbol, amount, price, fill_or_kill = false, expire = "2020-02-02T02:02:02")

Example

const BitShares = require('btsdex');

BitShares.init();
BitShares.subscribe('connected', start);

async function start() {
  let bot = new BitShares('trade-bot', 'privateActiveKey');
  await bot.sell('open.btc', 'bts', 0.0043, 31000);
}

transfer()

Signature:

async transfer(toName, assetSymbol, amount, memo)

Example

const BitShares = require("btsdex");

async function start() {
  BitShares.init("wss://bitshares.openledger.info/ws");
  await BitShares.connect();

  let bot = new BitShares("name-account", "yourPrivateActiveKey");

  await bot.transfer("scientistnik","BTS", 10);
}

cancelOrder()

Signature:

async cancelOrder(id)

orders()

Signature:

async orders()

Example

const BitShares = require('btsdex');

BitShares.init();
BitShares.subscribe('connected', start);

async function start() {
  let bot = new BitShares('trade-bot', 'privateActiveKey');
  console.log(await bot.orders()); // [{ id: '1.7.49552602', seller: ..., ... },{...}]
}