My Scripts 2- Auto Send From Multi To One Destination

in HiveDevs4 years ago

I hope that someone ends up finding some use out of the first post and hope that others enjoy its use in the future as well. I hope that some people use them as examples as I do some old posts from the utopian days made by people way smarter than me.

Now let's go onto the second script. I have a lot of accounts and need to move funds to my central account from time to time(some are delegating to bots, other are used as storage for funds for something, whatever the case, they end up having funds) and doing it manually does take quite some time. Enter this script.

let hive = require("@hiveio/hive-js")

let accounts = {} // account : activeKey
let destination = "" //recieving account
let hiveBalances = {} //hive balances of account, auto pulled
let hbdBalances = {} //hbd balances of account, auto pulled

hive.api.getAccounts(Object.keys(accounts), (err, response) => { //Array of accounts with a limit
  for (i in response) { //Response is an array with data for each user individually
    let data = response[i] 
    if (data.balance != "0.000 HIVE") { //balance is HVIE balance 
      hiveBalances[data.name] = data.balance
    }
    if (data.sbd_balance != "0.000 HBD") { //I expect sbd_balances to have a name change soon 
      hbdBalances[data.name] = data.sbd_balance
    }
  }
  send(hiveBalances)
  send(hbdBalances)
})

function send(balances) {
  for (i in hiveBalances) {
    hive.broadcast.transfer(accounts[i], i, destination, balances[i], "memo", (err, result) => { //WIF, from, destination, amount, memo

    })
  }
}

What it does is gets the balances of all the accounts in accounts and then sends them all to the destination. Because multiple transfers from one account are allowed in the same block, it shouldn't have a problem running both the hive send and hbd send at the exact same time. As always this does simplify my life a lot. I did have to do some work and get the private active key of all accounts in order to put them into the accounts variable(remember to be careful with private active keys, they can move your balance) but after that, its so much easier to pull them to my central account by just launching script up, and running it once.

As always, if you have any questions about this, just leave a comment and I'll try my best to answer.


History Of Scripts

Witness Rank And Amount Needed To Rank Up

Sort:  

Is there a way to check whether the hive account exists or not before proceeding to send the balance?

getAccount will help you with that l, it returns and id of 0 for the account if it doesn’t exist, but it won’t send to the account if it doesn’t exist(blockchain level). For sending from the accounts, getAccount is already called on it so the check already occurs.

Awesome, thank you very much :)

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