Quick and dirty, claim and stake for all Steem Engine tokens (multiple accounts)

in #stem2 years ago (edited)

image.png
Source

I wrote about my scripts that are automatically claiming and staking Steem Engine tokens about a couple of weeks ago. However, I came across a post today by @drakos "SteemJS Tools - Claim all Steem Engine tokens with multiple accounts", that prompted me to reshare my automation in a much more concise manner. I realise it's not the elegant solution, but it does the job for me :)

The premise for doing this is that you want to be able to claim and stake your tokens as soon as possible—automatically. It also supports multiple Steem accounts. A higher stake means greater future rewards. It's automatically reinvesting your tokens (like compound interest!).

What you'll need

  1. A computer that can run NodeJS. I'm using MacOSX and it works out of the box. Sorry Windows users...but I'm definitely sure there is guide out on the Internet somewhere.
  2. The steem-js Javascript library. Installation instructions are in the README.
  3. The sscjs Javascript library. Installation instructions are in the README.
  4. Your Steem account(s) and their respective posting and active keys.

The scripts

claim-steem-engine-tokens.js

#! /usr/bin/env node

var token = process.argv[2];
var user  = process.argv[3];
if( process.argv.length < 4 )
{
  console.log( "Usage: %s <token> <username>", process.argv[1] );
  process.exit( 1 );
}

const Steem = require( 'steem' );

var users = {  // Steem accounts that can use this script
  "username": "postingkey",
  "username": "postingkey",
};

claimTokens( user, token );


function claimTokens( username, token )
{
  console.log( "Claiming %s tokens for %s", token, username );
  var json = JSON.stringify( { "symbol": `${token}` } );
  Steem.broadcast.customJson( users[username], [], [username], 'scot_claim_token', json,
    ( err, result ) => {
      if( err )
      {
        console.log( err, result );
      }
    }
  );
}

stake-steem-engine-tokens.js

#! /usr/bin/env node
  
const SSC = require( 'sscjs' );
const ssc = new SSC( 'https://api.steem-engine.com/rpc/' );

var token = process.argv[2];
var user  = process.argv[3];
var keep  = process.argv[4];
if( process.argv.length < 4 )
{
  console.log( "Usage: %s <token> <username> [keep balance]", process.argv[1] );
  process.exit( 1 );
}
if( !keep )
  keep = 0;

const Steem = require( 'steem' );

var users = {  // Steem accounts that can use this script
  "username": "activekey",
  "username": "activekey",
};

function run()
{ 
  ssc.findOne( 'tokens', 'balances', { account: user, symbol: token }, function( err, result ) {
    if( err )
    { 
      console.log( err, result );
    }
    else    
    {       
      try { if( result.balance == null ) return }
      catch( err ) {  return  }  // user doesn't have any tokens
      
      var balance = parseFloat( result.balance );
      keep = parseFloat( keep ); 
      balance = balance.toFixed( 3 );
      
      if( balance > keep )
      { 
        var amount = balance - keep;
        amount = amount.toFixed( 3 );
        if( amount > 0 )
          stakeTokens( user, token, amount, result );
      }
    }
  } );
}

run();

function stakeTokens( username, token, amount, data )
{
  console.log( "Staking %s tokens for %s: %s", token, username, amount );

  var json = JSON.stringify(
    { "contractName"    : "tokens",
      "contractAction"  : "stake",
      "contractPayload" : { "symbol": `${token}`, "to": `${username}`, "quantity" : `${amount}` } }
  );

  Steem.broadcast.customJson( users[username], [username], [], 'ssc-mainnet1', json,
    ( err, result ) => {
      if( err )
      {
        console.log( err, result );
      }
  } );
}

The crontab

(sorry Windows users!)
(yes, I'm using this on a raspberrypi!)

# every 6 hours
00 */6 * * * /home/pi/Steem/claim-steem-engine-tokens.js PAL account1
01 */6 * * * /home/pi/Steem/claim-steem-engine-tokens.js PAL account2
02 */6 * * * /home/pi/Steem/stake-steem-engine-tokens.js PAL account1 50
03 */6 * * * /home/pi/Steem/stake-steem-engine-tokens.js PAL account2

04 */6 * * * /home/pi/Steem/claim-steem-engine-tokens.js LEO account1
05 */6 * * * /home/pi/Steem/claim-steem-engine-tokens.js LEO account2
06 */6 * * * /home/pi/Steem/stake-steem-engine-tokens.js LEO account1 50
07 */6 * * * /home/pi/Steem/stake-steem-engine-tokens.js LEO account2

Note: You'll notice that the stake-steem-engine-tokens.js script takes an optional numerical parameter. This value is the amount you'd like to keep in liquid form. For example: If I have just claimed my tokens and I have a balance of 60 PAL, the stake script above will only stake 10 PAL. If no value is given, it will stake everything.

I hope you find this useful. If a new tribe comes along, I simply add a couple of lines to my cronjob and I don't have to worry about claiming and staking anymore :)

PS. Use these scripts at your own risk. I will not be held responsible for any inadvertent exposure of your private keys.

You can support me using Steem Basic Income

Sort:  

Instead of running multiple crons, put them in a bash script and cron the script.

Good idea. Though I staggered run times because my raspberrypi struggles 😄

Posted using Partiko iOS

Great tool! Once I learn how to code in JavaScript I will start using it as well.

Posted using Partiko Android

Yes you can use this code! Test on a dev account first to be sure it works as intended as keys are very important to keep safe. Use the steemd.com website to make sure no private data has been made visible on the blockchain.

Posted using Partiko iOS

Thanks! I'll see what I can do with it...

Posted using Partiko Android