What is BitShares (BTS) Blockchain?
BitShares (BTS) is a high-performance blockchain platform designed to serve as a decentralized exchange (DEX). It was introduced in 2014 as one of the earliest examples of Delegated Proof of Stake (DPoS) blockchain technology, pioneered by developer Daniel Larimer, who later co-founded blockchain projects such as Steem and EOS. BitShares provides a high-speed, highly efficient platform for trading cryptocurrencies without the need for a centralized authority, offering superior performance with a capacity of handling up to 100,000 transactions per second.
In addition to its decentralized exchange, BitShares also enables the creation of SmartCoins, a type of digital asset that is pegged to the value of real-world assets, such as the US dollar, gold, or other cryptocurrencies. These SmartCoins help to provide stability in the often volatile cryptocurrency markets. Furthermore, BitShares offers a robust suite of financial services like banking or brokerages, and boasts features like recurring and scheduled payments, referral rewards programs, and user-issued assets.
One of BitShares’ defining characteristics is its commitment to decentralization and democratic governance. Stakeholders in the BitShares ecosystem can participate in decision-making processes about the platform’s future, including modifications to transaction fees, block intervals, and the election of witnesses (trusted parties responsible for validating transactions).
BitShares is a versatile blockchain platform offering a high-speed decentralized exchange and a host of other financial services, while emphasizing stakeholder governance and the stability offered by SmartCoins.
How to Check the Balance of BTS for a Bitshares Account using Node.Js?
This following Node (Javascript) connects to the Bitshares node, fetches the full account data for the specified account, then for each balance it fetches the asset data to get the symbol and precision.
Please replace my-bitshares-account with the actual account name whose balance you want to check. Also, ensure you have the correct npm package bitsharesjs-ws installed. You can install it via:
1 | npm install -g bitsharesjs-ws |
npm install -g bitsharesjs-ws
You can run this script with Node.js by saving it to a .js file and running node your_file.js in your terminal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | const {Apis} = require("bitsharesjs-ws"); function getBalanceBTS(accountName, apiNode = "wss://bts.mypi.win") { return new Promise((resolve, reject) => { Apis.instance(apiNode, true) .init_promise.then(() => { console.log(`connected to: ${apiNode}`); return Apis.instance().db_api().exec("get_full_accounts", [[accountName], false]); }) .then(response => { let accountData = response[0][1]; let accountBalances = accountData.balances; for (let balance of accountBalances) { let amount = balance.balance; let assetId = balance.asset_type; Apis.instance().db_api().exec("get_objects", [[assetId]]).then(assetResponse => { let asset = assetResponse[0]; let precision = Math.pow(10, asset.precision); let symbol = asset.symbol; console.log(`Balance of asset ${symbol}: ${amount / precision}`); resolve(amount / precision); }); } }) .catch(error => { console.error("Error fetching account:", error); reject(null); }); }); } (async function() { const accountName = "my-bitshares-account"; const balanceOfBTS = await getBalanceBTS(accountName); console.log(`BTS Balance of ${accountName} is ${balanceOfBTS} BTS.`); })(); |
const {Apis} = require("bitsharesjs-ws"); function getBalanceBTS(accountName, apiNode = "wss://bts.mypi.win") { return new Promise((resolve, reject) => { Apis.instance(apiNode, true) .init_promise.then(() => { console.log(`connected to: ${apiNode}`); return Apis.instance().db_api().exec("get_full_accounts", [[accountName], false]); }) .then(response => { let accountData = response[0][1]; let accountBalances = accountData.balances; for (let balance of accountBalances) { let amount = balance.balance; let assetId = balance.asset_type; Apis.instance().db_api().exec("get_objects", [[assetId]]).then(assetResponse => { let asset = assetResponse[0]; let precision = Math.pow(10, asset.precision); let symbol = asset.symbol; console.log(`Balance of asset ${symbol}: ${amount / precision}`); resolve(amount / precision); }); } }) .catch(error => { console.error("Error fetching account:", error); reject(null); }); }); } (async function() { const accountName = "my-bitshares-account"; const balanceOfBTS = await getBalanceBTS(accountName); console.log(`BTS Balance of ${accountName} is ${balanceOfBTS} BTS.`); })();
Again, please verify the accuracy of this information with the most recent Bitshares API documentation to ensure the accuracy of your project. Also, you can pass the BitShares Node (second parameter) when calling the getBalanceBTS function.
Failed to initialize with error self-signed certificate
If you see this message: Error fetching account: Error: self-signed certificate, this issue often comes up when you’re connecting to a server that uses a self-signed SSL certificate. Node.js rejects self-signed certificates by default because they could be a security risk. But if you trust the server, there are a couple of ways you could work around this.
One way is to allow self-signed certificates globally for the entire Node.js process:
1 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
You would put this line at the very beginning of your script. However, keep in mind that this disables the SSL certificate check for all requests made by this process, not just your connection to the BitShares node. This could be a potential security risk if your script makes other HTTPS requests.
Another way is to create a custom HTTPS agent that allows self-signed certificates, and use this agent for your connection to the BitShares node. This method allows you to limit the exception to just one specific connection.
The bitsharesjs-ws library uses the ws library for WebSocket connections, and ws in turn uses the built-in https module for connections over TLS. You can create an agent with the https module like this:
1 2 3 4 | const https = require("https"); const agent = new https.Agent({ rejectUnauthorized: false }); |
const https = require("https"); const agent = new https.Agent({ rejectUnauthorized: false });
Then pass this agent to the Apis.instance() call:
1 2 3 4 5 6 7 | const { Apis } = require("bitsharesjs-ws"); const apiNode = "wss://node.bitshares.eu"; const accountName = "my-bitshares-account"; Apis.instance(apiNode, true, 4000, undefined, agent) // rest of your code |
const { Apis } = require("bitsharesjs-ws"); const apiNode = "wss://node.bitshares.eu"; const accountName = "my-bitshares-account"; Apis.instance(apiNode, true, 4000, undefined, agent) // rest of your code
This should solve the self-signed certificate issue. However, as mentioned before, allowing self-signed certificates can be a security risk if you’re not sure whether you can trust the server. It’s recommended to use a connection over a trusted certificate whenever possible.
Return Balance of a Given Symbol
We can modify the above code to let it return the balance for a given symbol (default to “BTS” token) on the BitShares blockchain:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | function getBalanceBTS(accountName, symbol = "BTS", apiNode = "wss://bts.mypi.win") { return new Promise((resolve, reject) => { Apis.instance(apiNode, true, 4000, undefined, agent) .init_promise .then(() => { return Apis.instance().db_api().exec("get_full_accounts", [[accountName], false]); }) .then(response => { if (response.length == 0) { resolve(0); return; } let accountData = response[0][1]; let accountBalances = accountData.balances; // Use Promise.all to handle multiple asynchronous requests let balancePromises = accountBalances.map(balance => { let amount = balance.balance; let assetId = balance.asset_type; return Apis.instance().db_api().exec("get_objects", [[assetId]]).then(assetResponse => { let asset = assetResponse[0]; let precision = Math.pow(10, asset.precision); if (symbol == asset.symbol.toString()) { return amount / precision; } return null; }); }); return Promise.all(balancePromises); }) .then(balances => { if (!balances) { resolve(0); return; } for (let balance of balances) { if (balance !== null) { resolve(balance); return; } } resolve(0); }) .catch(error => { console.error("Error fetching account:", error); resolve(null); }); }); } |
function getBalanceBTS(accountName, symbol = "BTS", apiNode = "wss://bts.mypi.win") { return new Promise((resolve, reject) => { Apis.instance(apiNode, true, 4000, undefined, agent) .init_promise .then(() => { return Apis.instance().db_api().exec("get_full_accounts", [[accountName], false]); }) .then(response => { if (response.length == 0) { resolve(0); return; } let accountData = response[0][1]; let accountBalances = accountData.balances; // Use Promise.all to handle multiple asynchronous requests let balancePromises = accountBalances.map(balance => { let amount = balance.balance; let assetId = balance.asset_type; return Apis.instance().db_api().exec("get_objects", [[assetId]]).then(assetResponse => { let asset = assetResponse[0]; let precision = Math.pow(10, asset.precision); if (symbol == asset.symbol.toString()) { return amount / precision; } return null; }); }); return Promise.all(balancePromises); }) .then(balances => { if (!balances) { resolve(0); return; } for (let balance of balances) { if (balance !== null) { resolve(balance); return; } } resolve(0); }) .catch(error => { console.error("Error fetching account:", error); resolve(null); }); }); }
To Transfer the BTS Assets on BitShares Blockchain: Use this: How to Transfer BTS Assets on BitShares Blockchain using Node.Js?
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Offline Sign Transactions using C# for TRON blockchain?
Next Post: PHP Code to Redirect with 302 and a Referer