Javascript Code For Working With Hive, Steem And Blurt

in #javascript7 months ago (edited)

Previous Version

Before HF24, Hive property names were the same as those from Steem, and the library that I used was this one: @droida/3-blockchains-1-program.
After that, symbols and property names changed, and some API calls were not working using the same require instruction. So I had to adapt my scripts.

Because I wanted to make the minimal amount of change in my existing scripts, I use newhive as argument for operations not working with the Steem library, eg transfer. Here is my library for working with the 3 blockchains.

chainlib.js

download

var currentChainName="steem";
const chain=require("steem");
const hive=require("@hiveio/hive-js");

module.exports = {
    /**
     * @argument {String} name the blockchain name (newhive for transfer and claim rewards)
     */
    getChain: function(name) {
        if (name && name!=currentChainName) {
            currentChainName=name;
            if (name=='steem') {
                chain.api.setOptions({ url: 'https://api.steemit.com' });
                chain.config.set('address_prefix','STM');
                chain.config.set('chain_id','0000000000000000000000000000000000000000000000000000000000000000');
                chain.config.set('alternative_api_endpoints', ['https://api.steemit.com', 'https://api.steemit.com']);
                return chain;
                
            } else if (name=='hive') {
                chain.api.setOptions({ url: 'https://api.hive.blog' });
                chain.config.set('address_prefix','STM');
                chain.config.set('chain_id','beeab0de00000000000000000000000000000000000000000000000000000000');
                chain.config.set('alternative_api_endpoints', ['https://api.openhive.network', 'https://rpc.esteem.app']);
                chain.config.set('rebranded_api', true);
                //chain.broadcast.updateOperations() // not needed anymore
                return chain;
                
            } else if (name=='newhive') {
                hive.api.setOptions({ url: 'https://api.hive.blog' });
                hive.config.set('address_prefix','STM');
                hive.config.set('chain_id','beeab0de00000000000000000000000000000000000000000000000000000000');
                hive.config.set('alternative_api_endpoints', ['https://api.openhive.network', 'https://rpc.esteem.app']);
                hive.config.set('rebranded_api', true);
                return hive;
                
            } else if (name=='blurt') {
                chain.api.setOptions({ url: 'https://rpc.blurt.world', useAppbaseApi: true });
                chain.config.set('address_prefix','BLT');
                chain.config.set('chain_id','cd8d90f29ae273abec3eaa7731e25934c63eb654d55080caff2ebb7f5df6381f');
                chain.config.set('alternative_api_endpoints', ['https://blurtd.privex.io', 'https://api.blurt.blog']);
                return chain;
            }
        }
        return chain;
    },
    
    getChainName: function() {
        if (currentChainName=="newhive") {
            return "hive";
        }
        return currentChainName;
    },
    
    getSymbol: function() {
        switch (currentChainName) {
            case 'steem':
                return "STEEM";
            case 'hive':
            case 'newhive':
                return "HIVE";
            case 'blurt':
                return "BLURT";
        }
    },
    
    getStableSymbol: function() {
        switch (currentChainName) {
            case 'steem':
                return "SBD";
            case 'hive':
            case 'newhive':
                return "HBD";
        }
    },
    
    getPowerSymbol: function() {
        switch (currentChainName) {
            case 'steem':
                return "SP";
            case 'hive':
            case 'newhive':
                return "HP";
            case 'blurt':
                return "BP";
        }
    },
    
    getPrefix: function() {
        return module.exports.getChainName()+"_";
    },
    
    getStablePrefix: function() {
        var stableSymbol=module.exports.getStableSymbol();
        if (stableSymbol) {
            return stableSymbol.toLowerCase()+"_";
        }
    }
};

Example

The blockchain name is passed as a command line argument.

#!/usr/bin/node
const chainlib=require("./chainlib.js");

const chain=chainlib.getChain(process.argv[2]);

chain.api.getDynamicGlobalProperties(function(err, result) {
  console.log(err, result);
});

Quicktrades

I published this small library mainly because @quicktrades now uses it for trading in Hive and Steem internal markets. The release will be out in the coming days.

Sort:  

Excellent @droida √ Enjoy some Vibes √
and !wine in celebration 🍷


Congratulations, @andy-vibes You Successfully Shared 0.100 WINE With @droida.
You Earned 0.100 WINE As Curation Reward.
You Utilized 1/1 Successful Calls.

wine-greeting
Total Purchase : 24845.318 WINE & Last Price : 0.290 HIVE
HURRY UP & GET YOUR SPOT IN WINE INITIAL TOKEN OFFERING -ITO-


WINE Current Market Price : 0.270 HIVE

Great script with useful application for the users of both chain.
I would like to ask you @droida if you can help me create an account on BLURTTER.
I will return the 10BLURT as soon as I claim my post rewards.

OK. Just write the desired nickname and the email address to send the password to, or write to droida#3806 on Discord.

Thank you very much.
I have succeeded to recover the account created after the fork.

I'm glad you got your account back

Congratulations @droida! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You distributed more than 18000 upvotes.
Your next target is to reach 19000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Feedback from the April 1st Hive Power Up Day
Support the HiveBuzz project. Vote for our proposal!