Dump All Your Hive-Engine Tokens Easily

in Programming & Dev3 years ago (edited)

Another week, another script that I wrote and am sharing. If only utopian was around again, I could be raking it in(I joke). I did have a conversation with 2 other folks about the days of utopian and how glorious it was. The programming community is serving as a smaller version of that and I've been making posts to it and sharing stuff that I've made to it. Some of which someone has decided to use surprising. I guess I'm not the worst after all.

#! /usr/bin/env node
let axios = require("axios")
let hive = require("@hiveio/hive-js")

let rpcAPI = "https://api.hive-engine.com/rpc/contracts"
let notToDump = ["GIFT", "STARBITS", "LEO"] //This is an array of tokens that you don't want to dump
let username = "" //your username
let privateActiveKey = "" //your private active key


/**
 * Gets balances
 */
async function getBalances() {
  let getBalancesQuery = { id: 0, jsonrpc: "2.0", method: "find", params: { contract: "tokens", table: "balances", query: { account: username, $expr: { $gt: [{ $toDouble: "$balance" }, 0] } }, limit: 1000, offset: 0, indexes: [] } } 
  let result = await axios.post(rpcAPI, getBalancesQuery)
  let balances = result.data.result
  let validBalances = {}
  for (let i in balances) {
    let balance = balances[i]
    if (parseFloat(balance.balance) !== 0 && !notToDump.includes(balance.symbol)) {
      validBalances[balance.symbol] = parseFloat(balance.balance)
    }
  }
  delete validBalances["SWAP.HIVE"]
  return validBalances //These are all the balances above 0 and not swap.hive
}

/**
 * Places orders
 */
async function placeOrders() {
  let balances = await getBalances()
  if (balances.length === 0) {
    console.log(`Nothing to sell. Ending`)
    return
  }
  let sellJSON = []
  for (let i in balances) {
    sellJSON.push({ "contractName": "market", "contractAction": "marketSell", "contractPayload": { "symbol": i, "quantity": balances[i].toString() } })
    console.log(`Dumping ${balances[i]} of ${i}`)
    if (sellJSON.length >= 50) {
      break
    }
  }
  hive.broadcast.customJson(privateActiveKey, [username], null, "ssc-mainnet-hive", JSON.stringify(sellJSON), (err, result) => {
    if (err) {
      console.log(`Error with reason : ${err}`)
    } else {
      console.log(`Dumped. If there is more going for a second round in 10 seconds, otherwise ending. TRX id : ${result.id}`)
      if (sellJSON.length === 50) {
        setTimeout(() => {
          placeOrders()
        }, 1000 * 10)
      }
    }
  })
}

placeOrders()

We start off by getting the balances of the account. Since anything with a 0 balance is worthless to us, we can query for only things greater than that. Since hive-engine is just running a mongo query, we can simply add $expr: { $gt: [{ $toDouble: "$balance" }, 0] to our query. Since they are storing the balances as strings, we need to first convert them to a double, then get the ones above 0 and that query will do that for us. We then sort through it, making sure on javascript that nothing is 0 and that the tokens aren't on the notToDump list. We also can't sell SWAP.HIVE so we remove it from the list.

Then we need to place the order. I don't know the max that we can place on hive-engine at once so I stuck to 50 since thats what we did over on NFTMart and I'm sure CAD had his reasons for that so I'll stick to that. We make the json for that then broadcast it. Once we broadcast it, if the sellJSON that we made had a length of 50 we go back and do it from the start, with a 10 second break to let hive-engine to catch up. We keep repeating till we aren't hitting a length of 50 which means we have dumped everything off our accounts. And we get a nice clean 0's on our account.

Screen Shot 20201026 at 11.44.57 PM.png

Look at these nice clean 0's.

If you do decide to use this, please be careful. I'm not going to be responsible for any mistake(mine or yours) that caused you to accidentally dump all of your valuable tokens.

Sort:  

Good stuff @rishi556! 💪
Not many examples around.


Small fix:

balances.length === 0
..should be:
Object.keys(balances).length === 0

Nice, I should do this as I have so many odd tokens lying about, don't know if I can be bothered though. Saying that I might go and run it just now for that Minty Fresh Feeling

Thanks for sharing to Shartopian