Port your dapps from Steemit to Hive - Javascript

in hive •  7 days 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.   =]

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 STEEM!
Sort Order:  

This post has received a 40.13 % upvote from @boomerang.