My Scripts 3- Auto Sending From One Account To Multiple Others

in HiveDevs4 years ago

This script is one that saves a lot of time for me, and I wrote it just yesterday. I usually don't keep funds in my account, I send them out to different accounts each with its own purpose. Some of them get powered up. Now I can do that by just typing in 4 letters into the terminal and hitting enter.

#! /usr/bin/env node
//^ We need that to have this on CLI applications
let hive = require("@hiveio/hive-js")

let account = "rishi556" //Who to do this from
let privateActiveKey = "" //Private active key
let toKeep = 0.005 //How much to keep in the account
let hiveAccounts = {"rishi556.cold" : 0.25, "giftgiver.wallet" : 0.25} //account:% to send as a decimal
let powerUpAccounts = {"giftgiver" : 0.25, "rishi556.lambo" : 0.25} //account:% to send as a decimal

hive.api.getAccounts([account], (err, result) => { // Array of accounts. We only have one
  let balance = parseFloat(result[0].balance) - toKeep
  if (balance < 0){
    console.log(`Not enough of a balance.`)
    return
  }
  for (i in hiveAccounts){
    send(i, (balance * hiveAccounts[i]).toFixedDown(3) + " HIVE")
  }
  for (i in powerUpAccounts){
    powerUp(i, (balance * powerUpAccounts[i]).toFixedDown(3) + " HIVE")
  }

})

function send(reciever, amount){
  hive.broadcast.transfer(privateActiveKey, account, reciever, amount, "memo", (err, result) => { //WIF, from, destination, amount, memo
    if (!err){
      console.log(`Sent ${amount} to ${reciever}.`)
    }
  })
}

function powerUp(reciever, amount){
  hive.broadcast.transferToVesting(privateActiveKey, account, reciever, amount, (err, result) => { //wif, from, to, amount. This powers up accounts
    if (!err){
      console.log(`Powered Up ${amount} to ${reciever}.`)
    }
  })
}

//This does the job of toFixed, but rounds down always
Number.prototype.toFixedDown = function(digits) {
  if(this == 0) {
    return 0;
  }
  var n = this - Math.pow(10, -digits)/2;
  n += n / Math.pow(2, 53);
  return n.toFixed(digits)
}

To make this work as a CLI application, we need to add this into the package.json file(index.js is the name of the file the code's in, and send is the command we want to use) :

"bin": {
    "send": "index.js"
  }

There's a lot of ways to improve this, including checking to ensure that the numbers add up to 1 or below and such, but this much works. hiveAccounts is accounts to send liquid hive to and powerUpAccounts is accounts to power up. I decided not to include HBD in this as I normally just covert HBD to HIVE. As always, if you have any questions, the comments are open and I'll try and help you out.


History Of Scripts

Auto Send From Multiple To One Destination

Witness Rank And Amount Needed To Rank Up

Sort:  

Your current Rank (139) in the battle Arena of Holybread has granted you an Upvote of 14%

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

You published more than 400 posts. Your next target is to reach 450 posts.

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

Do not miss the last post from @hivebuzz:

Hive Whale - Make it spray and get your badge!
Support the HiveBuzz project. Vote for our proposal!