Create a currency calculator Bot in Byteball

Digging a bit deeper now, Let’s create a bot that will convert a number of bytes to your desired currency using API from Coingecko.

Part 1 explains hello world:-

Now use this repo to start with the new project.

  1. Clone the repo.
  2. Open the project directory and start editing start.js because that’s the entry point of the bot.

Now moving along copy this code and paste it into start.js by moving previous code.

'use strict';
// const async = require('async');
const conf = require('byteballcore/conf.js');
// const db = require('byteballcore/db.js');
const eventBus = require('byteballcore/event_bus.js');
const headlessWallet = require('headless-byteball');
// const validationUtils = require('byteballcore/validation_utils.js');
const wallet = require('byteballcore/wallet.js');
const device = require('byteballcore/device.js');
var Request = require("request");
eventBus.on('text', function(from_addr, text) {
let arr = new Array();
arr = text.split(" ");
// 10 to btc = 10 btc worth of bytes
// 200 to steem = 200 steem worth of bytes by getting steem to btc and and then result btc*200 and then getting 200 steem worth of bytes from that result
if (arr.length === 3) {
Request.get(`${arr[2]}&include_24hr_vol=false&include_last_updated_at=false`, (err, res, body) => {
if (!err) {
let pbody = JSON.parse(body);
let curren = arr[2].toLowerCase();
let valknow = parseFloat(arr[0]);
let val = String(valknow * parseFloat(pbody.byteball[`${curren}`]));
if (val === "NaN") {
// e.g. 10 to Steem => 10 gbytes = ?steem
Request.get(`${arr[2]}&vs_currencies=btc&include_24hr_vol=false&include_last_updated_at=false`, (err, res, body) => {
let pbody_2 = JSON.parse(body);
let curren = arr[2].toLowerCase();
let newval = 1.0 / parseFloat(pbody_2[`${curren}`].btc); // e.g. btc to steem
Request.get(``, (err, res, body2) => {
let pbody_3 = JSON.parse(body2);
let curren = arr[2].toLowerCase();
let newval_2 = parseFloat(pbody_3.byteball.btc); // byteball to Btc
// 1 GB worth of btc * 1 Btc worth of Steem = 1 GB worth of Steem (Formula concluded so far)
device.sendMessageToDevice(from_addr, 'text', `${arr[0]} Gbytes = ` + String(valknow * (newval * newval_2)) + " " + curren);
} else if (val !== "NaN") {
device.sendMessageToDevice(from_addr, 'text', `${arr[0]} Gbytes = ` + val + " " + curren);
} else {
device.sendMessageToDevice(from_addr, 'text', 'There is a problem getting your request done, Make sure your command is right, send `nob` to know commands');
} else {
device.sendMessageToDevice(from_addr, 'text', 'Please enter correct command or type `nob` to get help');
//device.sendMessageToDevice(from_addr, 'text', text.split("").reverse().join(""));

Explaining the code… The code explanation contains parts that I think might be hard for you without knowing what they do, So I am explaining that here.

  1. This line…

Request.get(`${arr[2]}&include_24hr_vol=false&include_last_updated_at=false`, (err, res, body)

getting Byteball to input currency exchange in the input you provided with bot.

The API link refers to CoingGecko and arr[2] refers to 2nd argument of your input to bot.

For e.g. you write 10 to btc and send that to bot now 10 is arr[0], to is arr[1] and btc is arr[2]. So arr[0] and arr[2] we only need for code. Other if-else are conditions to get right input parameter from the user.

Now interesting part…

Coin Gecko only supports currency conversion from any conversion to main currencies like btc, ltc, etc. So if you say 10 to steem then you will get NaN. Because that is not supported.

To overcome it let’s dive deeper, That is under if condition where the value is NaN

We do it now:- We will get 10 to steem or anything that exists on coin gecko with this method.

  1. Convert Byteball to main currency like btc
  2. Now take that currency like steem and get it’s BTC value too.
  3. Now keep in mind that….

`1 GB worth of btc * 1 Btc worth of Steem = 1 GB worth of Steem`

4. That way you can get arr[0]*1GB to steem value. Get it? Take your time if it’s new for you.

5. So after adding that formula we have 10 to steem value or any other currency we want exists on CoinGecko and the coin will be recognized by the id like to call Lite coin, You use ltc e.g. 11 to ltc.

The output will be like.

You can easily convert vice versa by doing division instead of multiplication.


That’s all folks.