Auto Claim & Stake SPS - Step by Step Instruction

in #splinterlands2 years ago

After reading this SPS auto claim program tutorial in python, I decided to write one for Javascript since I'm not familiar with Python. But before that, like any seasoned (read lazy) programmer would do, I searched if anyone have done it and found this. So this post is for those non techie out there who would like to auto claim/compound their SPS. Without further ado...

### Setup your environment
Head over to https://nodejs.org/en/download/ to download your copy of Node.js.

image.png

In this example, I'll be downloading the 64 bit windows installer (.msi). Once the download is finished, double click the file to run it. Accept the agreement, choose the install location, keep the default setup, basically click next until you reach the end.

After successfully installing Node.js, open a command terminal/powershell and type node -v, you should see the version of node.js installed.

Next create a project directory (using mkdir directoryname), in my case I named it autoclaim then change directory to it (using cd directoryname)

image.png

Then install the modules needed by the program using
npm install axios hive-js @hiveio/hive-js

image.png

Create the program

Then, using notepad or any text editor, create a file (named mine index.js) and copy/paste the below code. Then change the YOUR_USENAME with your splinterlands IGN/Hive username and YOUR_PRIVATE_POSTING_KEY with your posting key. If you are using Hive Keychain (which I suggest you should), you can find it in settings, manage account, posting key. Click on it twice and it willbe copied on your clipboard.

// Modules
const axios = require('axios'); // npm i axios
const hive = require('@hiveio/hive-js'); // https://www.npmjs.com/package/@hiveio/hive-js
const ecc = require('eosjs-ecc') // https://www.npmjs.com/package/eosjs-ecc

// Configurations
let params = {
  username: `YOUR_USERNAME`,
  posting_key: `YOUR_PRIVATE_POSTING_KEY`,
  stake_frequency: 5,   // Minutes
  airdrop_frequency: 3  // Hours
}

auto_claim_airdrop();
auto_stake();


// Airdrop Claim Functions
async function auto_claim_airdrop() {
  setInterval(async () => {
    let url = `https://api2.splinterlands.com/players/sps?username=${params.username}`
    let response = await axios.get(url);
    let airdrops_array = response.data.airdrop;
    let claim_pending = airdrops_array.find(claim => (claim.platform === `game` && claim.claim_tx === null)) !== undefined;
    if (claim_pending) login();
    else console.log(`No aidrops to claim.`)
  }, params.airdrop_frequency * 60 * 60 * 1000)
}

async function login() {
  params.ts = new Date().getTime();
  params.login_signature = ecc.sign(`${params.username}${params.ts}`, params.posting_key);
  let url = `https://api2.splinterlands.com/players/login`
      + `?name=${params.username}`
      + `&ts=${params.ts}`
      + `&sig=${params.login_signature}`;
  let response = await axios.get(url);

  params.token = response.data.token;
  claim_sps();
}

async function claim_sps() {
  params.claim_signature = ecc.sign(`hive${params.username}${params.ts}`, params.posting_key);
  let url = `https://ec-api.splinterlands.com/players/claim_sps_airdrop`
      + `?platform=hive`
      + `&address=${params.username}`
      + `&sig=${params.claim_signature}`
      + `&ts=${params.ts}`
      + `&token=${params.token}`
      + `&username=${params.username}`
  let response = await axios.get(url);

  let data = response.data;
  if (data.success) {
    console.log(`Account ${data.data.to} successfully claimed ${data.data.qty} SPS.`
        + ` See transaction at hiveblocks.com/tx/${data.tx.id}`);
  } else console.log(`Error:`, data.error);
}

// Auto Stake Functions
async function auto_stake() {
  stake_sps(await get_balance());
  setInterval(async () => stake_sps(await get_balance()), params.stake_frequency * 60 * 1000);
}

async function get_balance() {
  let url = `https://api2.splinterlands.com/players/balances?username=${params.username}`;
  let response = await axios.get(url);

  return response.data.find(currency => currency.token === `SPS`).balance;
}

function stake_sps(qty) {
  let json = {"token": "SPS", "qty": qty}
  hive.broadcast.customJson(
      params.posting_key, [], [params.username], `sm_stake_tokens`, JSON.stringify(json), function (err, res) {
        if (res) console.log(`Staked ${qty} tokens.`)
        else console.log(`Error Staking: ${err}`)
      });
}

Run the Program

Then lastly, you need to run it using node filename. Before that, here is a screenshot of my SPS balance.

image.png

And here is screenshot of it running while I'm writing this tutorial.

image.png

Hope you find this instruction useful!

Sort:  

Hmmm. I might have to get my son to take a look at this although he'd probably be happier with the python version. Thanks for the instructions.

!LUV !PIZZA !LOLZ

Posted Using LeoFinance Beta

Be there or be square!
If you're not there, then I assume you're not a round.

Credit: reddit
@maakue, I sent you an $LOLZ on behalf of @gillianpearce
Use the !LOL or !LOLZ command to share a joke and an $LOLZ. (2/6)

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

You published more than 20 posts.
Your next target is to reach 30 posts.

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

To support your work, I also upvoted your post!

Check out the last post from @hivebuzz:

Introducing NFT for Peace
Support the HiveBuzz project. Vote for our proposal!

I'll check it out :P

!PIZZA

PIZZA! PIZZA!

PIZZA Holders sent $PIZZA tips in this post's comments:
@floki-skarsgaard(1/5) tipped @maakue (x1)
gillianpearce tipped maakue (x1)

Join us in Discord!