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.
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)
Then install the modules needed by the program using
npm install axios hive-js @hiveio/hive-js
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.
And here is screenshot of it running while I'm writing this tutorial.
Hope you find this instruction useful!