Port your DAPPS from Steem to Hive - Javascript (nodeJs)

in #hive2 years ago (edited)

source

I already briefly explained here how you can adapt steem.js to work on Hive, and the same here for dsteem.js.

The goal of this post is to give you a way to easily support both chains in your code base.


In this way you will be able to automate your steemit bots to show some "love" to some Steemit users that you don't like and at the same time support your favorite authors on https://hive.blog.   =}


// _libs/index.js

const apis = {
  steemJs: null,
  hiveJs: null,
  dsteem: null,
  dhive: null,
}

// Usage:
// const steem = require('../_libs').lib('steem');
// const hive = require('../_libs').lib('hive');
// hive.broadcast.send(....);
const lib = (chainName) => {
  if (chainName === 'steem') {
    if (!apis.steemJs) {
      apis.steemJs = require('steem');
    }
    return apis.steemJs;
  } else if (chainName === 'hive') {
    if (!apis.hiveJs) {
      apis.hiveJs = require('steem');
      apis.hiveJs.api.setOptions({
        url: 'https://api.hive.blog',
      });
      apis.hiveJs.config.set('address_prefix', 'STM');
      apis.hiveJs.config.set(
        'chain_id',
        '0000000000000000000000000000000000000000000000000000000000000000',
      );
    }
    return apis.hiveJs;
  } else {
    throw new Error(`Unexpected chain ${chainName} for lib. Available: steem, hive`);
  }
};
exports.lib = lib;

// Usage:
// const dsteem = require('../_libs').dlib('steem');
// const dhive = require('../_libs').dlib('hive');
// dhive.database.call(....);
const dlib = (chainName) => {
  const opts = { addressPrefix: 'STM' };
  if (chainName === 'steem') {
    if (apis.dsteem) return apis.dsteem;
    const dsteem = require('dsteem');
    apis.dsteem = new dsteem.Client('https://api.steemit.com');
    return apis.dsteem;
  } else if (chainName === 'hive') {
    if (apis.dhive) return apis.dhive;
    const dhive = require('dsteem');
    apis.dhive = new dhive.Client('https://api.hive.blog');
    return apis.dhive;
  } else {
    throw new Error(`Unexpected chain ${chainName} for dlib. Available: steem, hive`);
  }
}
exports.dlib = dlib;



Enjoy and please let me know if you have any questions.   =]

Sort:  

Win! Definitely gonna hive this a try ASAP, will let you know. Cheers

Awesome, thanks! Reblogged for reference later.

Thx!   =]

This post has been voted on by the HiveSTEM curation team and voting trail. It is elligible for support from @curie and @minnowbooster.

If you appreciate the work we are doing, then consider supporting our witness @stem.witness. Additional witness support to the curie witness would be appreciated as well.

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Please consider using the stem.openhive.network app and including @steemstem as a beneficiary of this post. This could yield a stronger support.