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

in #splinterlands3 years ago (edited)

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 ?

DependencyVersionSource
python3.xhttps://www.python.org/downloads/
Beem0.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)

Sort:  

Pretty neat, I didn't expect that they are exposing the claimdrop through their API but very nice to see they are. I like this easy-to-follow tutorial style with code sections explained, thanks. And the giveaway does help increase engagement, doesn't it? :)

Hi @borislavzlatanov,

well thank you. I'm glad you like the tutorial.
Sectioning off the code was an attempt to make it easier to follow, didn't do any programming tutorials before that.

And yes, the giveaway has that kind of side effect ;).

Nice. This is a great tool.
I once tried to mod your splinterlands bot but really struggled because I am pretty much clueless on the inner workings of the Dapp.

Posted using Dapplr

Hi deniskj,

thanks. I hope you'll give it another try once the new framework is ready. I'm trying to make the whole framework as easy to use as possible. While maintaining most low level functions for more experienced programmers.

Btw, are you taking part in the giveaway, if so, is deniskj your splinterlands account ?

The issue has more to do with me than your code. I have more experience with php,css and javascript. I am learning Python at the moment while trying to work with your code. Some video tutorials would be very much welcome for visual learners like me.

Also, you can add me to the giveaway. I can't say no to a gold foil. :)

My programming skils (Cobol, Business BAsic) ended 30 years ago (: (no price for me!!!)

Hey goldrooster,

don't undersell yourself, at the company i work for we still maintain cobol backends because nobody wants to pay for overhauling them, and because of that cobol devs are still in demand.
Maybe you even helped write those systems, its a german bank ;)

Sssssst, that programm still transfers each rounded half a cent to my account on every transaction.

I wouldn't be surprised if that was hidden in there, and it probably still converts pfennig to cents ;)

Awesome, I was actually considering looking into this myself because an auto claim/stake tool for sps would be pretty awesome.

This is helpful!
!BEER !LUV

Awesome tutorial.

In the future, please add #technology tag to your tutorials like this, this way it shows up on STEMGeeks.net and you earn STEM tokens as well.

Hi @themarkymark,

thanks, will do.

Interesting, thank you.
I'm not familiar with Python yet, but these tutorials are a good way to learn it better

Hi @tehox,

you're welcome. Even if you don't do much work in python, it's still a quick way to prepare data/logfiles or do some manual tasks automatically.

For the giveaway: I guess your splinterlands account is also tehox ?

Yes, I'm tehox :)

This is great!! Thank you for sharing it! This is a great way for anyone to get their feet wet with python in a very practical way! Looking forward to part 2!!

Hi @cryptokungfu,

thanks and you're welcome. Hope you're also getting your feet wet with python ;)

Awesome. Thanks for this. I'm going to try it! ign: higherlow

!PIZZA

Hi @higherlow,

you're welcome. Hope you're getting some use out of it ;)

PIZZA!

PIZZA Holders sent $PIZZA tips in this post's comments:
@higherlow(1/7) tipped @bauloewe (x1)

Join us in Discord!

welcome back, how are the old projects going? splinterlands bot aicu curator

Hi @viniciotricolor,

thanks :), and regarding my old bots: right now they are both on hiatus. With the splinterlands bot I haven't decided what to do yet. I'll probably restart my low level bot aicu-chan and start another "from scratch" bot. But the champions league bot, I'm not certain yet.

But first I'm overhauling my public bot framework.

With the aicu curator bot I'm not quite sure, I'm currently trying to figure out if curation bots are still welcome or not. After i get a first sentiment on that I decide what to do.

For the giveaway: your splinterlands account is also viniciotricolor ?

thanks for the information yes, my account is viniciotricolor

Nicely written, I like the way you break down the steps in a way even an eejit like me can understand.

Hi @ammonite,

thanks, glad you like it.

Always remember: programming is teaching the stupidest thing on the planet how to do something. And you're the teacher ;)

btw, @ammonite is also your slinterlands account ?

I must quote you on that. I have many ideas I would like to do with programming and will follow you along to learn.

Yes, I am @ammonite in the Splinterlands

Awesome programming course. I was once a coder long ago and then cancer hit me and a stroke damaged my memory. I am studying python and javascript again. I found this post to be so inspirational that I am your new follower.

Hi @chris.topher,

thank you, I'm happy that you find the tutorial useful. Your response sounds really inspirational for me too.

Btw, out of curiosity what did you program/work on in your old job?

Nothing too technical just some web development using java, javascript, and HTML 😀

Alright, the seven days are over. Lets see who the lucky winners are:

1 GF Fire Elemental G4-161-AOADVCNCXC goes to @deniskj
1 GF Kobold Miner G1-3-63S0FR68MO goes to @ammonite

You should receive them within a couple minutes ;)

Wow! Just saw it. I am quite grateful for this, @bauloewe. It will really help to increase my SPS yield. :))
image.png

Wow @bauloewe, Thank you so much. I don't have the time to play Splinterlands myself but have been trying to build an account for my 6-year old for the day when he can finally get involved. I'll tell him in the morning about his first Christmas present. Thanks again.

I was making a bot and was stuck because I didn't know how to generate SIGs, so this was very helpful!

Hey @cs50x,

they are a bit tricky, took me while to figure it out. The last time i programmed something for splinterland you didn't need to login at all^^

What kind of bot are you making ?

Thanks for the great tutorial @bauloewe, I am a huge noob on Python and getting this error when trying to pip install beem : ERROR: Could not build wheels for scrypt, which is required to install pyproject.toml-based projects

Any one having the same issue ?

This is a very neat write up, I'm sorry I missed it when it was useful. I suspect though that I should be able to use this to claim LP pool rewards and SPS battle/chest rewards by changing the API link..
Thanks!