Programming Tutorial: How to auto claim SPS (Part 2)

in #splinterlands3 years ago (edited)

After the first part yesterday you already know how to claim your daily SPS airdrop.

Auto Staking SPS

Now let's look at how you automatically stake that reward. Let's start with a little helper function which will do all the heavy lifting for us:

from beem import Hive

def stake_sps(hive: Hive, user: str, qty: int):
    hive_id: str = "sm_stake_tokens"
    request = {"token": "SPS", "qty": qty}
    hive.custom_json(hive_id, json_data=request, required_posting_auths=[user])


And that's all the heavy lifting done. If you call that function you'll broadcast a transaction to the hive blockchain, which will stake those sweet SPS.

Onto the next part, how do we know, how much SPS we can actually stake ?

If you took a closer look at the login response from Part 1 you noticed, that it contains all the player balances. So if we just login again, it will tell us how much SPS we have. Let's extract that info from the response:

def get_balance(login_resp: dict, currency_token: str):
    balance: float = 0
    for entry in login_resp.get("balances"):
        if currency_token == entry.get("token"):
            balance = entry.get("balance")
            break
    return balance

Now we're almost finished. But first we need to connect to the hive blockchain, we're going to use the hive class from beem for that.
There are many ways to get your private keys into beem, the easiest is by putting it in a keys array. That way beem will store it temporarily for this execution.

login_response = login(posting_key, username).json()

sps_balance = get_balance(login_response, "SPS")

hive: Hive = Hive(keys=[posting_key])

stake_sps(hive, username, sps_balance)

And now we just logged into splinterlands again, determined our current SPS balance and told the hive blockchain that we want to stake some sps.

Claiming your Stake

But how do we claim our stake reward ?
Well, it's simple, ever time you stake a transaction you automatically claim the reward.
If you just want to claim the stake reward, just stake 0.

stake_sps(hive, username, 0)

And we're done with part 2. Now you know how to automatically claim your airdrop, stake your sps and claim your stake reward.

And now the whole working code from part 1 and part 2:

import time
import requests
from binascii import hexlify
from beemgraphenebase.ecdsasig import sign_message
from beem import Hive


def compute_sig(string_to_sign: str, priv_key: str):
    bytestring_signature = sign_message(string_to_sign, priv_key)
    sig = hexlify(bytestring_signature).decode("ascii")
    return sig


def login(private_key: str, username: str):
    login_endpoint = "https://api2.splinterlands.com/players/login"
    ts = int(time.time() * 1000)
    sig = compute_sig(username + str(ts), private_key)
    login_endpoint += "?name=" + username + "&ts=" + str(ts) + "&sig=" + sig
    return requests.get(login_endpoint)


def stake_sps(hive: Hive, user: str, qty: float):
    hive_id: str = "sm_stake_tokens"
    request = {"token": "SPS", "qty": qty}
    hive.custom_json(hive_id, json_data=request, required_posting_auths=[user])


def get_balance(login_resp: dict, currency_token: str):
    balance: float = 0
    for entry in login_resp.get("balances"):
        if currency_token == entry.get("token"):
            balance = entry.get("balance")
            break
    return balance


posting_key = "PRIVATE_POSTING_KEY"
username = "YOUR_USERNAME"
login_response = login(posting_key, username).json()

token = login_response.get("token")

airdrop_url = "https://ec-api.splinterlands.com/players/claim_sps_airdrop"
ts = int(time.time() * 1000)

airdrop_params = {}

airdrop_params["platform"] = "hive"
airdrop_params["address"] = "YOUR_ADDRESS"
airdrop_params["sig"] = compute_sig("hive" + "YOUR_ADDRESS" + str(ts), posting_key)
airdrop_params["ts"] = ts
airdrop_params["token"] = token
airdrop_params["username"] = username

hive: Hive = Hive(keys=[posting_key])

print(requests.get(airdrop_url, params=airdrop_params).json())
stake_sps(hive, username, 0) # 
time.sleep(60) #it takes some time for the transaction to register
login_response = login(posting_key, username).json()

sps_balance = get_balance(login_response, "SPS")
print(sps_balance)
stake_sps(hive, username, sps_balance)

Note: you might have noticed that I did things in this order:

  1. claim airdrop
  2. claim stake reward
  3. wait 60 seconds
  4. get balance
  5. stake sps

The reason i called them in that order is, that it takes some time for the airdrop to process. Since the request gets send to the splinterlands server I assume that they themselves broadcast a transaction serverside. Probably via a queue. So things take a bit longer than when we broadcast it ourselves. Afterwards I claim the stake reward. Then wait 60 seconds to make sure the reward and airdrop transactions both register on the hive blockchain as well as the splinterland server.

Then we can get the sps balance and stake all of it. Airdrop and stake reward. And we're done.
If you don't see the changes reflected immediately on the splinterlands website don't worry. Sometimes it takes a few minutes for it to notice.

If you now wan't to know how to unstake: I guess you just head over to the SPS Management page and press a button. That part doesn't need much automation imo ;)

Giveaway

This time I'm also going to do a small giveaway, conditions are : comment below with your splinterlands username included in the comment.

The prizes will be:

1 GF NAGA FIRE WIZARD G3-94-6VWH3UDZTC
1 Normal Talia Firestorm lvl 1 C1-70-97HJJKZWM8

Drawing will be at random after 7 days

Sort:  

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

You distributed more than 600 upvotes.
Your next target is to reach 700 upvotes.

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

Support the HiveBuzz project. Vote for our proposal!

Thanks for the giveaway!
username: cs50x

Looks like you're getting both cards.

I'll send them out now.

Wow, how lucky I am!
Thank you for the wonderful Christmas present.
I'm really happy :)

how can i check dec balance

hi, what do you mean ? you log into the website and the login response stores the balances:)