Javascript Code For Working With Hive, Steem And Blurt

in javascript •  last year 

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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!