BTSDEX v0.3.1

in #blockchain6 years ago

BTSDEX.png

Here's the new version

I am glad to inform you that two weeks later a new version of the js-package BTSDEX was released. If you still do not know what it is BTSDEX, then I advise you to read the first post or look at the documentation.

What's New

Transaction Commission

Calculating costs is very important. To make this easy, I added the helper class BitShares.fees:

const BitShares = require("btsdex");

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

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

  let fees = BitShares.fees;
  console.log(fees); // Fees {transfer: 0.1042, limit_order_create: 0.00578, ...}
  console.log(fees.account_update); // 0.00578
}

Commissions are available after connection.
Because Commissions can change, the object of the class Fees has an update() method. This may be necessary if your software should work long enough.

BitShares.fees.update();
console.log(BitShares.fees); // Fees with updated values

Event System

Very often we have to expect, when there will be some action in the blockchain, to which our software should respond. The idea of ​​reading each block and viewing all the operations in it, seemed to me ineffective. Therefore, this update adds an event system.

Event types

At the moment, BTSDEX has three types of 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).

For 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 * /}


The connected event

This event is triggered once, after connecting to the blockchain. Any number of functions can be subscribed to this event and all of them will be called after connection.

BitShares.subscribe('connected', firstFunction);
BitShares.subscribe('connected', secondFunction);

Another feature of the event is that when you first subscription call the method BitShares.connect(), i.e. will be an automatic connection. If by this time the connection to the blockchain has already been connected, then it will simply call the function.

Now it's not necessary to explicitly call BitShares.connect(), it's enough to subscribe to the connected event.

const BitShares = require("btsdex");

BitShares.init("wss://bitshares.openledger.info/ws");
BitShares.subscribe('connected', start);

async function start() {
  // something is happening here
}


The block event

The block event is triggered when a new block is created in the blockchain. The first event subscription automatically creates a subscription to the connected event, and if this is the first subscription, it will cause a connection to the blockchain.

const BitShares = require("btsdex");

BitShares.init("wss://bitshares.openledger.info/ws");
BitShares.subscribe('block', newBlock);

// need to wait ~ 10-15 seconds
async function newBlock(obj) {
  console.log (obj); // [{id: '2.1.0', head_block_number: 17171083, time: ...}]
}

As you can see from the example, an object with block fields is passed to all the signed functions.

The account event

The account event is triggered when certain changes occur (balance changes). These include:

  • If the account sent someone one of their assets
  • If an asset has been sent to an account
  • If the account has created an order
  • If the account order was executed (partially or completely), or was canceled.

The first subscriber to account will call a block subscription, which in the end will cause a connection to the blockchain.

Example code:

const BitShares = require("btsdex");

BitShares.init("wss://bitshares.openledger.info/ws");
BitShares.subscribe('account', changeAccount, 'trade-bot');

async function changeAccount (array) {
  console.log(array); // [{id: '1.11.37843675', block_num: 17171423, op: ...}, {...}]
}

In all the signed functions, an array of account history objects is transferred, which occurred since the last event.

Documentation

I understand that it is very difficult to understand and use it all. So I decided to start collecting all the information in one place. I will try to fill it with large and detailed examples. On the other hand, I expect questions, suggestions and help in writing the code in github.

Good luck and see you again!