Library for work with BitShares DEX: BTSDEX.js v.0.2.0

in #blockchain6 years ago (edited)

BTSDEX.png

What is this and why?!

I want to show you my library for work with BitShares blockchain write on JavaScript. Exactly, this wrapper for official bitshares.js.

Why I decided write this, if have bitshares.js?!
It is not so usable for my projects, as I would like. Solving their private problems, after some time, it became clear that the code is growing and not very convenient. Decided to unify several repetitive parts in different projects to bring everything into one package. And as this package was being written, there was a desire to make it more universal and convenient. So BTSDEX appeared.

Implementation

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.

The package is distributed under the MIT license. Link to github!

How to install:

$ npm install btsdex

Requre in your project:

const BitShares = require("btsdex");

Some examples

To connect to the BitShares network, you must initialize and connect:

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


Public API

After the connection, you can use any public method from official documentation (if the method is still relevant!).

Database API

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)


History API

To access the Account History API, you can use the BitShares.history object.

Example of a method from the Account History API:
get_account_history (account_id_type account, operation_history_id_type stop = operation_history_id_type (), unsigned limit = 100, operation_history_id_type start = operation_history_id_type ()) const

To use it:

let ops = await BitShares.history.get_account_history("1.2.849826", "1.11.0", 10, "1.11.0")


Private API

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, 140,000)
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!")


Helper classes

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.

What's Next

In the current version, I personally do not have enough convenient mechanism for requesting the cost of various transactions and subscribing to various changes in the blockchain. In the official bitshares.js there is an implementation of this, but it is not very convenient as for me. Most likely I will use it, but writing some wrapper on top.


If you have any other suggestions, you can write me! I always open to dialogue!
Thank you for your attention and see you soon!

Sort:  

Great work, are you already in the community groups (Telegram/Discord)? Perhaps you'll find good suggestions there also :)

Which groups did you mean?

Hey. Very nice wrapper.
Can you add some little examples to your library that just work out of the box. E.g. I'm interested in doing transfers.
Thanks J

For example:

const BitShares = require("btsdex");

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

  let bot = new BitShares("jjb777", "yourPrivateActiveKey")

  await bot.transfer("scientistnik","BTS", 10)
  console.log("Transfer sent")
}

start()

Hey.
I had nearly the same code but it failed due to an old npm version dependency problems. But I got it to work.
Thanks J

Hey .... I did not get 10 BTS!!!

I'm kidding))

Great work, keep it up! :)

I took some programming classes a long time ago but Im not much of a programmee. Will this information help bitshare grow and the coin appreciate?

I would like to believe)

you excellent devloper ! !! i hav followed you !!

Excellent Work! Very convenient library!
As possible memoDecode after tx history? Then the library will be indispensable in use.

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 67284.54
ETH 3306.88
USDT 1.00
SBD 2.71