-
Notifications
You must be signed in to change notification settings - Fork 29
Home
The main class in the package is BitShares
. All you need is in it. There are a couple more helper classes, but they are not really designed for use outside of the BitShares
class. Perhaps at some next version this will change.
The BitShares
class consists of static methods intended for working with the BitShares public blockchain API. Using the BitShares class, you can create an object whose methods provide access to the private part of the BitShares blockchain API.
$ npm install btsdex
Requre in your project:
const BitShares = require("btsdex");
To connect to the BitShares network, you must initialize and connect:
BitShares.init("wss://bitshares.openledger.info/ws")
await BitShares.connect();
After the connection, you can use any public method from official documentation (if the method is still relevant!).
To access the Database API, you can use the BitShares.db object.
An example of methods from the Database API:
get_objects (const vector <object_id_type> & ids) const
list_assets (const string & lower_bound_symbol, uint32_t limit) const
To use them:
let obj = await BitShares.db.get_objects(["1.3.0"])
let bts = await BitShares.db.list_assets("BTS", 100)
To access the Account History API, you can use the BitShares.history object.
Example of a method from the Account History API:
To use it:
let ops = await BitShares.history.get_account_history("1.2.849826", "1.11.0", 10, "1.11.0")
If you want to have access to account operations, you need to create a BitShares object:
let bot = new BitShares("accountName", "privateActiveKey")
While this object can not much: buy, sell, transfer, cancel orders.
Signatures of methods:
bot.buy(buySymbol, baseSymbol, amount, price, fill_or_kill = false, expire = "2020-02-02T02: 02: 02")
bot.sell(sellSymbol, baseSymbol, amount, price, fill_or_kill = false, expire = "2020-02-02T02: 02: 02")
bot.cancelOrder(id)
bot.transfer(toName, assetSymbol, amount, memo)
Examples of using:
await bot.buy("OPEN.BTC", "BTS", 0.002, 140000)
await bot.sell("BTS", "USD", 187, 0.24)
await bot.transfer("scientistnik", "BTS", 10)
If you want to send tokens with memo, then before that you need to set a private memo-key:
bot.setMemoKey("privateMemoKey")
await bot.transfer("scientistnik", "USD", 10, "Thank you for BTSDEX!")
There are a couple more helper classes, such as BitShares.assets and BitShares.accounts:
let usd = await BitShares.assets.usd;
let btc = await BitShares.assets["OPEN.BTS"];
let bts = await BitShares.assets["bts"];
let iam = await BitShares.accounts.scientistnik;
let tradebot = await BitShares.accounts["trade-bot"];
The returned objects contain all the fields that blockchain returns when the given asset or account name is requested.