Programming Tutorial: How to Auto claim SPS (Part 1)

@bauloewe · 2021-12-16 20:13 · splinterlands

Some of you might have noticed that splinterlands is handing out a token called Splintershards (SPS) based on your splinterland assets.

The token is going to be used in the "governance" of the game. Meaning that you get essentially a share in the game. Right now the majority comes from your assets, so big whales get tons of it. But you can also stake SPS. Meaning you'll get pretty much "dividends" by the second on your SPS.

How do you do that ? Go to the website and push a couple buttons on the SPS Management page and voila your SPS start turning into more SPS. One downside: by doing that you lock them, and it takes 4 weeks to unlock them again.

But needing to think about doing that every day is cumbersome. Wouldn't it be nice if you could do that automatically ? Use a robot to do all that manual labor?

Fret not, I'm going to teach you exactly how to do that with the programming language python.

Lets get started

What do you need for it ?

Dependency Version Source
python 3.x https://www.python.org/downloads/
Beem 0.24.26(or latest) https://pypi.org/project/beem/

For claiming the airdrop you only need two things a) your private key b) valid login token

a) is simple b) might be a bit more tricky.

To get that you need to login to splinterlands with your script first, you do that by using the following api endpoint: https://api2.splinterlands.com/players/login, but how do you login exactly ? How about we write a little function for that:

Logging in
import requests

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

The login takes the name of your account, a current timestamp in milliseconds and a parameter called sig. How do we compute the sig ?

Signatures

Well lets write another function to help us with that, we're going to need it later too.

from binascii import hexlify
from beemgraphenebase.ecdsasig import sign_message

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


What did we do here ? We used the help of beem to compute a signature using the famous "bitcoin" elliptic curve. Looks simpel, but trust me, it isn't. (and shout out to @bubke for posting the easy way to sign transactions with beem ;) )

Lets get back to our login function:

import time

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)

It's important that you the same timestamp and username for the signature as well as the query parameters.

Now the login returns a response object. Currently we are only interested in the session token. It's stored in a field called "token".

Claiming your airdrop

So now that we have the groundwork covered, lets get to the meat and bones of the tutorial: claiming your airdrop!

First lets log into splinterlands

posting_key = "YOUR_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"

airdrop_url += ?platform=hive&address=address&sig=XXXXXXXX&ts=1639589244825&token=XXXXXXX&username=user

As you've noticed the airdrop url is using a lot of parameters. There's a better way in python to handle this, using a query parameter dictionary.

posting_key = "YOUR_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_player_address"
airdrop_params["sig"] = compute_sig("hive" + "your_player_address" + str(ts), posting_key)
airdrop_params["ts"] = ts
airdrop_params["token"] = token
airdrop_params["username"] = username

print(requests.get(airdrop_url, params=airdrop_params).json())

As you can see, it's more lines of code, but a lot more readable. But aside from that, you might have noticed, that the signature this time consists of the platform, the address and the current timestamp.

A quick note on platform and address: I haven't tried claiming the airdrop for other platforms than hive. But from the website I'd assume the values for the other platforms besides hive are: binance, wax, eth, steem and tron. The address should be your address on the given platform.

And voila, you've just claimed your airdrop reward for the day.

Stay tuned for part two, which will follow soon. In part two we will broadcast a transaction to the hive blockchain, stake our liquid sps and claim our stake reward.

If you find any mistakes in the code or somewhere else, please tell me. I'll try to fix it asap.

Giveaway

Besides, where's the fun in a tutorial if there are no prizes to win:

If you comment below there's a change to win

1 GF Fire Elemental G4-161-AOADVCNCXC
1 GF Kobold Miner   G1-3-63S0FR68MO

Winners will be chosen at random seven days after this post goes live :)

PS: For those who know my old bot framework on github: I'm currently overhauling the whole codebase. Should be a lot more robust, maintainable and more "feature" rich afterwards.

And the whole code, for those of you who just want a working example.

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


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)


posting_key = "YOUR_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" #binance, wax, eth, steem and tron
airdrop_params["address"] = "YOUR_ADDRESS" #on hive usually your username
airdrop_params["sig"] = compute_sig("hive" + "your_wallet_address" + str(ts), posting_key)
airdrop_params["ts"] = ts
airdrop_params["token"] = token
airdrop_params["username"] = username

print(requests.get(airdrop_url, params=airdrop_params).json())

Part 2

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

#splinterlands #python #bot #programming #sps
Payout: 0.000 HBD
Votes: 48
More interactions (upvote, reblog, reply) coming soon.