Splinterlands Api/Bot Learning: 2. How to write a program to realize automatic card rental by using python

image.png

As we all know, renting cards in splinterlands, especially the cards you want, is a very time-consuming thing. So, how to solve this problem?

There's one thing we need to know, this game is a blockchain game. And what we do in the game, like renting a card, is essentially sending a request message to the blockchain.

So we can write a program to send a request message to the blockchain instead of renting cards by our hands.

Step 1:
Install python on your computer. Go to python's official website to download and install it. https://www.python.org/getit/

Step 2:
Create a new python file, such as rental.py
And open it.
image.png

Step 3:
Start Writing.
Do some prep work

import requests
from beem import Hive

API2 = "https://api2.splinterlands.com"

# your username
username = 'username'

# your posting keyword and active keyword
passwords = ['aaa','bbb']

hive = Hive(keys=passwords)

class Card:
    market_id = ''
    uid = ''
    detail_id = 0
    price = 0

    def __init__(self, m, u, d, p):
        self.market_id = m
        self.uid = u
        self.detail_id = d
        self.price = p

    def __lt__(self, other):
        return self.price < other.price

Step 4:
Write main codes

  1. get rental list of the card we want
    for example:
    image.png
    This card's card_id is 336, gold is False, edition is 3, xp is 10.
    The price indicates the maximum price you can afford for this card, unit is DEC
def get_rent_cards_xp(card_id: int, edition: int, gold: bool, xp: int, price: float) -> list:
    url = API2 + "/market/for_rent_by_card"
    request: dict = {"card_detail_id": card_id,
                     "gold": gold,
                     "edition": edition}
    rent_cards = requests.get(url, params=request).json()
    cards = [card for card in rent_cards if card.get("xp") >= xp and float(card.get("buy_price")) <= price]
    v_cards = []
    for c in cards:
        v_cards.append(Card(c.get('market_id'), c.get('uid'), c.get('card_detail_id'), float(c.get('buy_price'))))
    v_cards.sort()
    return v_cards

2.Verify if the card is available for rent

def verify(market_id: str, uid: str, card_detail_id: int) -> bool:
    url = API2 + "/market/validateListing"
    request: dict = {"card_detail_id": card_detail_id,
                     "uid": uid,
                     "market_id": market_id}
    return requests.get(url, params=request).json().get('isValid')

3.Rent a card

def rent_card(player: str, card_ids: list[str], days: int, currency: str):
    data: dict = {"items": card_ids,
                  "currency": currency,
                  "days": days,
                  "app": "splinterlands/0.7.139"}
    hive.custom_json("sm_market_rent", data, required_auths=[player], required_posting_auths=[])
    print(player, 'rent cards success')

4.Start rent card

cards_sorted = get_rent_cards_xp(335, 3, False, 1, 3)
for card_sorted in cards_sorted:
    if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
        rent_card(username, [card_sorted.market_id], 1, 'DEC')
        exit()
print('no available cards')

Step 5:
run this program
open cmd.exe, and input

pip install requests
pip install beem
python rental.py

All the work is over, you have successfully rented this card

Rent the card Venari Seedsmith as a demonstration:
image.png

image.png

The complete code is here, you can directly copy the past to use
But you need change the username and passords to your own in this file

import requests
from beem import Hive

API2 = "https://api2.splinterlands.com"

# 👇👇👇👇👇👇👇👇👇👇👇 your username 
username = 'username'

# 👇👇👇👇👇👇👇👇👇👇👇 your posting keyword and active keyword
passwords = ['aaa', 'bbb']

hive = Hive(keys=passwords)

class Card:
    market_id = ''
    uid = ''
    detail_id = 0
    price = 0

    def __init__(self, m, u, d, p):
        self.market_id = m
        self.uid = u
        self.detail_id = d
        self.price = p

    def __lt__(self, other):
        return self.price < other.price


def get_rent_cards_xp(card_id: int, edition: int, gold: bool, xp: int, price: float) -> list:
    url = API2 + "/market/for_rent_by_card"
    request: dict = {"card_detail_id": card_id,
                     "gold": gold,
                     "edition": edition}
    rent_cards = requests.get(url, params=request).json()
    cards = [card for card in rent_cards if card.get("xp") >= xp and float(card.get("buy_price")) <= price]
    v_cards = []
    for c in cards:
        v_cards.append(Card(c.get('market_id'), c.get('uid'), c.get('card_detail_id'), float(c.get('buy_price'))))
    v_cards.sort()
    return v_cards

def verify(market_id: str, uid: str, card_detail_id: int) -> bool:
    url = API2 + "/market/validateListing"
    request: dict = {"card_detail_id": card_detail_id,
                     "uid": uid,
                     "market_id": market_id}
    return requests.get(url, params=request).json().get('isValid')


def rent_card(player: str, card_ids: list[str], days: int, currency: str):
    data: dict = {"items": card_ids,
                  "currency": currency,
                  "days": days,
                  "app": "splinterlands/0.7.139"}
    hive.custom_json("sm_market_rent", data, required_auths=[player], required_posting_auths=[])
    print(player, 'rent cards success')
    
    
cards_sorted = get_rent_cards_xp(335, 3, False, 1, 3)
for card_sorted in cards_sorted:
    if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
        rent_card(username, [card_sorted.market_id], 1, 'DEC')
        exit()
print('no available cards')

In the next installment, I will teach you how to rent cards for multiple accounts, or rent multiple cards.

If you have any questions, please leave a message in the comment area

Sort:  

Impressive and usefull!

Thank you for your compliment

Hi can you make a script to imitate peakmonsters CP renting thanks

Thank you for this, but can u please help me I'm having this kind of error

def rent_card(player: str, card_ids: list[str], days: int, currency: str):
TypeError: 'type' object is not subscriptable

how to resolve this, thank you.

Thanks for this! Time to get on board learning Python😃 because renting cards is a terrible life experience lol. This will save me so much time. Is there a way to make bids for cards too?

this doesn't work. As in, the transaction is posted, but the card is not rented.