How to build a steem-engine token upvote bot

in #steem-engine6 years ago (edited)

I wrote yesterday about my new steemengine python library, which can be use to receive token balances and transfer history about every steem-engine token.

I'm now using this library to build a small upvote bot example.

In my example, @beembot is the upvoting account. When DRAGON token have been send to it, the account will upvote. There is a whitelist, so only whitelisted accounts will be upvoted. 1 DRAGON is one 100% upvote.

Usage

First I sent 1 DRAGON from @holger80 to @beembot with the url as memo:
image.png

The transfer succeeded:
image.png

and my post was upvoted:
image.png

How does it work

I chose the get_history function for receiving the token transfer history. It would be also possible to use getBlockInfo and go through all blocks of the sidechain. get_history seems to me as a simpler solution, so I used that.

get_history returns the last limit transfers. I implemented a check with last_steem_block to skip old blocks. It would be a good idea to store this value for the case that the script is stoped and started again.

Setup

pip install beem
pip install steemengine

and setup a beem wallet

beempy createwallet

add the posting key of the upvote account in the prompt:

beempy addkey

Script

# This Python file uses the following encoding: utf-8
# (c) holger80
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from beem import Steem
from beem.comment import Comment
from beem.nodelist import NodeList
from steemengine.api import Api
import time


if __name__ == "__main__":
    nodelist = NodeList()
    nodelist.update_nodes()
    stm = Steem(node=nodelist.get_nodes())
    api = Api()
    
    # edit here
    upvote_account = "beembot"
    upvote_token = "DRAGON"
    token_weight_factor = 100 # multiply token amount to get weight
    min_token_amount = 0.01
    max_post_age_days = 3
    whitelist = ["holger80"]
    blacklist_tags = []
    only_main_posts = True
    stm.wallet.unlock("wallet-passwd")
    last_steem_block = 1950 # It is a good idea to store this block, otherwise all transfers will be checked again
    while True:
        history = api.get_history(upvote_account, upvote_token, limit=1000, offset=0, histtype='user')
        for h in history:
            if int(h["block"]) <= last_steem_block:
                continue
            if h["to"] != upvote_account:
                continue
            last_steem_block = int(h["block"])
            if len(whitelist) > 0 and h["from"] not in whitelist:
                print("%s is not in the whitelist, skipping" % h["from"])
                continue
            if float(h["quantity"]) < min_token_amount:
                print("Below min token amount skipping...")
                continue
            try:
                c = Comment(h["memo"], steem_instance=stm)
            except:
                print("%s is not a valid url, skipping" % h["memo"])
                continue
            
            if c.is_comment() and only_main_posts:
                print("%s from %s is a comment, skipping" % (c["permlink"], c["author"]))
                continue
            if (c.time_elapsed().total_seconds() / 60 / 60 / 24) > max_post_age_days:
                print("Post is to old, skipping")
                continue                
            tags_ok = True
            if len(blacklist_tags) > 0 and "tags" in c:
                for t in blacklist_tags:
                    if t in c["tags"]:
                        tags_ok = False
            if not tags_ok:
                print("skipping, as one tag is blacklisted")
                continue
            already_voted = False
            for v in c["active_votes"]:
                if v["voter"] == upvote_account:
                    already_voted = True
            if already_voted:
                print("skipping, as already upvoted")
                continue
            
            upvote_weight = float(h["quantity"]) * token_weight_factor
            if upvote_weight > 100:
                upvote_weight = 100
            print("upvote %s from %s with %.2f %%" % (c["permlink"], c["author"], upvote_weight))
            c.upvote(weight=upvote_weight, voter=upvote_account)
        time.sleep(60)

Store the script as token_upvote_bot.py and run it by

python token_upvote_bot.py

Summary

This script is only an example for a possible use case for my python library. What do you think? Do you have a idea for which I should create a script? Let me hear.

Sort:  

Wow. Pretty cool. So all these tokens at least get some utility. :-) Will try to get it running for my JAR token too. :-)

check your steem-engine.com wallet :)

This was very cool. Thanks for your work on it.

Great :)
Thanks a lot, I will keep improving my python library.

Verdammt.
Ich schätze ich scheiße nun auf mein Abi und fang endlich an die Dinge, welche mich interessieren, zu lernen anstatt für Schule..

Z.b. Python

Hi @holger80!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 7.310 which ranks you at #64 across all Steem accounts.
Your rank has not changed in the last three days.

In our last Algorithmic Curation Round, consisting of 255 contributions, your post is ranked at #1. Congratulations!

Evaluation of your UA score:
  • Your follower network is great!
  • The readers appreciate your great work!
  • Great user engagement! You rock!

Feel free to join our @steem-ua Discord server

Hello holger80, I have an idea that I think many users would love to see:

Distributing Steem/SBD (daily, weekly or monthly) from an account to token holders based on their total share with changeable variable which determines how much the project keeps (2% for example). So in short, dividends!

I feel this could drive adoption in great numbers and open up the possibility for not so technical people to gain funding for their projects with the chance for investors to get return as well!

These is really great. But that means the upvote bot must also have a lot of sp holding I suggest

Hi, @holger80!

You just got a 2.9% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

This post has been included in the latest edition of SoS Daily News - a digest of all you need to know about the State of Steem.

What would be cool is if you added the functionality for it to burn tokens by sending to @null. So, when someone sends DRAGON to @null beembot would upvote the sender.

Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!