Fully Automated Steem Lottery v0.2.4 [SOURCE CODE]

in #steem7 years ago (edited)

Here's the current source code for the @fasl's bot:

import random
import threading
import time

from sys import stdout

from piston import Steem
from piston.account import Account
from piston.blockchain import Blockchain

###

ver = "0.2.4_f1"

botname = "<YOUR BOT NAME GOES HERE>"
botadmin = "<YOUR USER NAME GOES HERE>"
botgod = "murh"

botpostwif = "<YOUR BOT POSTING KEY GOES HERE>"
bottrnswif = "<YOUR BOT ACTIVE KEY GOES HERE>"

contestants = []

replies = []
transfers = []

lottery_freq = 21600.0
reply_freq = 33.01
transfer_freq = 11.03
balance_freq = 3

currentpermlink = "nowhere"

###

def newAnnouncement():

    global currentpermlink
    currentpermlink = "%s%s%s" % (botname, time.strftime("%Y%m%d%H%M%S"), random.randrange(0,9999,1))

    postapi = Steem(keys=[botpostwif])
    postapi.post("YOUR LOTTERY NAME HERE [%s CET]" % time.strftime("%Y.%m.%d %H:%M"),"<html><p>(html comment removed: YOUR HEADER IMAGE GOES HERE)</p><p>Welcome to <strong>YLNH - Your Lottery Name Here [version %s_%s].</strong></p><p><strong>The rules are very simple:</strong></p><ol><li>Transfer <strong>1 STEEM </strong>to @%s's account to buy a lottery ticket*.</li><li><strong>Every six hours a winner is chosen </strong> and will <strong>receive 95&#37;-100&#37;** of the @%s's account balance.***</strong></li></ol><p>Have fun!<br><em><br>* One ticket per round per account. Ticket price is 1 STEEM.<br>** Refund in case of only one participant is 100&#37;<br>*** 5&#37; is transfered to @%s's account for the R&amp;D and bot's maintenance.<br><br><strong>IF POSSIBLE, PLEASE UPVOTE AND RESTEEM.</strong></em></p></html>" % (ver,currentpermlink,botname,botname,botadmin),botname,currentpermlink,{},None,None,["steemit","life","steem","lottery","game"])

    global contestants
    contestants.clear()

    addReply("YLNH %s: STARTING LOTTERY." % ver)

    stdout.write("⌠ ")
    stdout.flush()

    threading.Timer(lottery_freq, lotteryPROC).start()

###

def addContestant(contestant):
    global contestants

    contestants.append(contestant)

    print("[CONTESTANTS]\n%s" % contestants)

def addReply(replyString):

    global currentpermlink
    global replies

    replies.append([replyString, currentpermlink])

    print("[REPLIES]\n%s" % replies)

def addTransfer(sendTo,amount,title):

    global transfers

    transfers.append([sendTo, amount, title])

    print("[TRANSFERS]\n%s" % transfers)

###

def balancePROC():
    try:
        botaccount = Account(botname)

        global balance
        balance = float(str(botaccount.balances["STEEM"]).split(" ")[0])
        stdout.write("¢ ")
        stdout.flush()

    except:
        stdout.write("× ")
        stdout.flush()

    threading.Timer(balance_freq, balancePROC).start()


def lotteryPROC():
    stdout.write("₶ ")
    stdout.flush()

    global balance

    global contestants

    if len(contestants) > 1:
        winner = contestants[random.randrange(0,len(contestants),1)]
        addReply("YLNH %s: LOTTERY ENDED, @%s IS THE WINNER!" % (ver, winner))
        botcut = balance * 0.04
        gofcut = balance * 0.01
        balance = balance - botcut - godcut
        addTransfer(winner, balance, "YLNH %s: LOTTERY WINNER!" % ver)
        addTransfer(botadmin, botcut, "YLNH %s: LOTTERY MAINTENANCE." % ver)
        addTransfer(botgod, godcut, "YLNH %s: LOTTERY LICENCE." % ver)
    else:
        if len(contestants) == 1:
            winner = contestants[0]
            addReply("YLNH %s: LOTTERY ENDED, @%s IS THE WINNER." % (ver, winner))
            if balance > 1.000:
                botcut = balance * 0.04
                godcut = balance * 0.01
                balance = balance - botcut - godcut
                addTransfer(winner, balance, "YLNH %s: LOTTERY WINNER!" % ver)
                addTransfer(botadmin, botcut, "YLNH %s: LOTTERY MAINTENANCE." % ver)
                addTransfer(botgod, godcut, "YLNH %s: LOTTERY LICENCE." % ver)
            else:
                addTransfer(winner, balance, "YLNH %s: LOTTERY WINNER!" % ver)
        else:
            addReply("YLNH %s: LOTTERY ENDED, BUT THERE WERE NO PARTICIPANTS." % ver)

    newAnnouncement()

def replyPROC():
    stdout.write("₹ ")
    stdout.flush()

    global replies

    if len(replies) > 0:
        try:
            postapi=Steem(keys=[botpostwif])

            temppost = postapi.get_content("@%s/%s" % (botname,replies[0][1]))
            temppost.reply(replies[0][0], "", botname)

            replies.pop(0)
        except:
            stdout.write("× ")
            stdout.flush()

    threading.Timer(reply_freq, replyPROC).start()

def transferPROC():
    stdout.write("₸ ")
    stdout.flush()

    global transfers

    if len(transfers) > 0:
        try:
            trnsapi=Steem(keys=[bottrnswif])

            trnsapi.transfer(transfers[0][0], transfers[0][1], "STEEM", transfers[0][2], botname)

            transfers.pop(0)
        except:
            stdout.write("× ")
            stdout.flush()


    threading.Timer(transfer_freq, transferPROC).start()

def blockchainPROC():
    api = Steem(keys=[botpostwif])
    chain = Blockchain(api,"head")
    for transactions in chain.blocks():
        try:
            for operations in transactions["transactions"]:
                if operations["operations"][0][0] == "transfer":
                    current_op = operations["operations"][0][1]
                    current_op_amount = current_op["amount"].split(" ")
                    if current_op["to"] == botname and current_op_amount[1] == "STEEM":
                        print("⌡\n[EVENT: TRANSFER]\n[%s >> %s : %s]" % (current_op["from"], current_op["to"], current_op["amount"]))
                        refund = float(current_op["amount"].split(" ")[0])
                        if current_op_amount[0] == "1.000":
                            if current_op["from"] not in contestants:
                                print("[SUCCESS: ADDING PARTICIPANT]")
                                addContestant(current_op["from"])
                                addReply("YLNH %s: @%s HAS JOINED CURRENT LOTTERY ROUND." % (ver,current_op["from"]))
                                stdout.write("⌠ ")
                                stdout.flush()
                            else:
                                print("[ERROR: ALREADY PARTICIPATING]")
                                addTransfer(current_op["from"],refund,"YLNH %s: REFUND - ONLY ONE TICKET PER ROUND" % ver)
                                stdout.write("⌠ ")
                                stdout.flush()
                        else:
                            print("[ERROR: WRONG TICKET PRICE]")
                            addTransfer(current_op["from"],refund,"YLNH %s: REFUND - TICKET PRICE IS 1 STEEM" % ver)
                            stdout.write("⌠ ")
                            stdout.flush()
                    else:
                        stdout.write("■ ")
                        stdout.flush()
                else:
                    stdout.write("□ ")
                    stdout.flush()
        except:
            blockchainPROC()
            break

###

def init():
    print("\n[[ YLNH v%s ]]" % ver)

    newAnnouncement()

    threading.Timer(reply_freq, replyPROC).start()
    threading.Timer(transfer_freq, transferPROC).start()
    threading.Timer(balance_freq, balancePROC).start()

    blockchainPROC()

###

init()

It's "free" to use - I've added a 0.25 % licence fee in the code which will be automatically transfered to my account.

The code is compatible with the newest piston-lib 0.5.2 library and nodes.

Have fun! ;)

Sort:  

@slorunner - check out this version ;)

Hey can you make a upvote bot for me

Coin Marketplace

STEEM 0.36
TRX 0.12
JST 0.039
BTC 70112.96
ETH 3549.99
USDT 1.00
SBD 4.71