1. Home
  2. HOWTO
  3. How to build and submit transactions using btsdex nodejs library

How to build and submit transactions using btsdex nodejs library

Interaction with BitShares blockchain happens by broadcasting transactions to the nodes. A transaction is usually built and signed locally and then broadcast. It is considered accepted after being included in a block and finalized after its block becomes irreversible. A transaction can contain several operations (transfer, limit order, etc..), this allows to execute several actions on the blockchain in an atomic fashion (one fails all fail) and without the need to sign each one separately (beneficial for performance). For this you may use the transaction builder.

Installing NodeJS for Ubuntu or OSX (Only in case you don’t have it):

The easiest way to install NodeJS is to use Node Version Manager.

To install nvm for Ubuntu or OSX, use following commands in terminal using a non-root user:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
nvm install v10
nvm use v10

Installing btsdex NodeJS BitShares Library:

git clone -b master https://github.com/bitshares/btsdex.git
cd btsdex
npm install

Creating btsdex NodeJS script file:

The below example can be used to broadcast any transaction to the BitShares Blockchain using API node hence you can use any BitShares Public API node, you will just need to replace parameters under params and the operation name asset_update in below example with your desired transaction parameters and its operation name.

Use the below links to determine your desired operation name and needed parameters along with their required order. Below transaction example serialization can be found here asset_update as you will need to honor the order of operation parameters along with their sub parameters which are present in below example asset_options note that some of parameters or sub parameters are optional:

bitshares-core available operation names

btsdex library parameters serialization and order

Create new file and call it transaction.js under main btsdex directory, copy paste the below example then read the notes next to hash for what to change:

const BitShares = require("btsdex");
BitShares.connect("wss://dex.iobanker.com/ws"); # replace wss://dex.iobanker.com/ws with API node if you want to use another BitShares API node
BitShares.subscribe('connected', startAfterConnected);

async function startAfterConnected() {
let acc = await new BitShares("username", "password"); # replace with your username and password

# Below are required parameters and structure for the example of operation *asset_update*; every different operation would required different parameters and structure

# Finding what data to use in these parameters would require understanding of how BitShares Blockchain works, for example *issuer* is referred to the ID *1.2.1787259* of owner account of an Asset, use telegram [BitShares Development](https://t.me/BitSharesDEV) group to ask about required parameters.

let params = {
    fee: {amount: 0, asset_id: "1.3.0"},
    "issuer":"1.2.1787259",
    "asset_to_update":"1.3.5537", 
    "new_options": {
     "max_supply": "1000000000000000", 
     "market_fee_percent": 0, 
     "max_market_fee": "0", 
     "min_market_fee": 0, 
     "issuer_permissions": 79, 
     "flags": 6, 
     "core_exchange_rate": {
      "base": {"amount": 500000, "asset_id": "1.3.0"}, 
      "quote": {"amount": 10000, "asset_id": "1.3.5537"}
      }, 
    "whitelist_authorities": [], 
    "blacklist_authorities": [], 
    "whitelist_markets": [], 
    "blacklist_markets": [],
    "description": "{\"main\":\"Your Asset Info\",\"market\":\"Your Market info\"}", 
    "extensions": {"taker_fee_percent": 10}
    }
}

let tx = acc.newTx();
tx.asset_update(params); # Replace asset_update with your desired operation name
await tx.broadcast();
console.log(tx);
}
Save and close the file.

Execute the script using node:

node transaction.js