Commercializing Steemnova Project with automated Dark Matter distribution

image.png
It is not online! It is only tool shared with all of you. All changes will be anounced by admins in official update. Follow @steemnova account for all important informations.

dark_matter.py - what it is all about

It's a piece of code that adds Dark Matter to the payer account.
For example I transfer 10 SBD to the @steemnova account with given memo (atm. 'ZGFya21hdHRlcgo') and in game resource called Dark Matter will be automatically added in no time!
No manual administrator action required!

Techical Description

Dependencies

Code is written in python3.6

The most needed dependency is steem_dshot for integration with Steem blockchain.

Rest of unusual dependencies are:

  • MySQLdb
  • csv

Code Explantaion

There are 6 funcions:

1- init() - it connects to the DB and interacts with Steem blockchain

 _database = MySQLdb.connect(host="localhost", user="unova", passwd="password", db="NOVADB") # creates global _database object variable 

---cut---

try:
        _json = s.get_account_history('steemnova', -1, limit=300)   # getting steem history for account steemnova from newest one with limit 300 so "grab last 300 actions from steemnova acc" and put it in global variable
except :
        raise NameError("init() failed at downloading history. \tData response:\n\t "+data) # if failed then inform system about it

2- get_transfers() - it is taking only STEEM/SBD transactions from all of the account activity (upvotes, flags, posted posts, comments) from global var. and loging it into file for future payments and archiving. This function also handles calculating amount of DM added into players account. It saves searched tx's into CSV file with given headers for easy data handling.

function;timestamp;player;recived;darkmatter

3- pay_users(String __user,int __amount,String __timestamp,DB_object _database)

var types only for visualization

This one does the main job. It technically handles the money.
It takes username, amount to pay, timestamp to log and database object at which it will make SQL query.

_cursor = _database.cursor()
__query = "UPDATE {0} SET darkmatter=(darkmatter+{1}) WHERE username='{2}'".format(_table_name, __amount, __user)
_cursor.execute(__query);

4- rotate() - I called it so in a surge of creative inspiration ッIt does act as brain for the whole thing. Almost...

rotate() opens txt file in which it have saved timestamp for last payed tx in format: YYYY-MM-DDTHH:mm:ss
and then pays everyone that have send TX after saved timestamp (info from *.csv file).
Magic two lines (if row[timestamp] is later than lastpaid timestamp):

if row['timestamp'] > __lastpaid_date:                      
                    pay_users(row['player'], row['darkmatter'], row['timestamp'], _database)

5- log(__line) - it takes given string __line and appends it to the log file which is our CSV file.

6- cleanup() - closes the DB connection

So that's all from technical point of view. Whole code is available at github (link below). Please comment suggestions after seeing "TO DO" paragraph.

To Do Essentials

  • [ ] init() - give it file handling capabilities - to check if logfiles are existing and make them as needed
  • [ ] get_transfers() - add SBD and STEEM recognition and also validate taken data to dodge nasty injections
  • [ ] in general - make some exception handling, make it userproof, add some asci arts :)

Done

It has been tested and works just as it was supposed to work.

Author Comments

All suggestions will be happily accepted and positively considered but only construtive criticism will be taken in mind.

Feel free to ask, comment & play with us!

LINKS



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now