Posts

How much SPORTS have I staked in last 30 days? - Hive-Engine API codes included.

avatar of @amr008
25
@amr008
·
·
0 views
·
2 min read

Good morning to one and all . I hope you are having a good day.

I don't usually post I staked 'x' token 'y' amount so I thought instead of posting daily I will gather the data for the past 30 days and post it . This post is about SPORTS token and I will also show how I got the data from Hive-Engine API .

First of all I have the hive-engine transaction data gathered from Hive-Engine API from December 21st onwards . Not only data related to my account but all the transactions . I have stored it in 'Shelve' which is persistent dictionary ( in layman term it means I have stored it in key,value pair)

import shelve  
import pandas as pd 
import json 
from datetime import datetime as dt 
 
 
s=shelve.open('Blocks\Blockchain') # This is where I have stored all transaction details 
df=pd.DataFrame.from_dict(s.items()) # I am transferring the shelve data to a dataframe 
 
df.columns=['Blocks','Transactions'] # Naming the columns 

This is how the table looks like -

for i in range(0,len(df)): 
    if(df['Transactions'][i]['Transaction']['contract']=='tokens'): 
        if(df['Transactions'][i]['Transaction']['action']=='stake'): 
            json_sports_stake=json.loads(df['Transactions'][i]['Transaction']['logs']) 
            if 'events' in json_sports_stake: 
                if(json_sports_stake['events'][0]['data']['symbol']=='SPORTS'): 
                    if(json_sports_stake['events'][0]['data']['account']=='amr008'): 
                        print(json_sports_stake['events'][0]['data'],df['Transactions'][i]['Date']) 

This will give me all the staking 'amr008' account has done for SPORTS token .

Last column is date and time of staking.

I can change this to any other token and I can even change the account name too . So if you need your data , feel free to ask but remember I have transaction details from only Dec 21st right now , I will try to download full data in couple of days.

list_sports_stake=[] 
for i in range(0,len(df)): 
    if(df['Transactions'][i]['Transaction']['contract']=='tokens'): 
        if(df['Transactions'][i]['Transaction']['action']=='stake'): 
            json_sports_stake=json.loads(df['Transactions'][i]['Transaction']['logs']) 
            if 'events' in json_sports_stake: 
                if(json_sports_stake['events'][0]['data']['symbol']=='SPORTS'): 
                    if(json_sports_stake['events'][0]['data']['account']=='amr008'): 
                        list_sports_stake.append([json_sports_stake['events'][0]['data']['quantity'],df['Transactions'][i]['Date']]) 
 

Here I have just stored the data into a list instead of printing . I am storing the quantity of SPORTS staked in the list .

Then I am using DataFrame again to get the table format -

df_stake=pd.DataFrame(list_sports_stake) 
df_stake.columns=['Amount','Date'] 

Now a simple sum function will give me the total value staked -

df_stake['Amount']=pd.to_numeric(df_stake['Amount'])# To convert string to numeric 
df_stake['Amount'].sum() 

Output -
100847.91300000002

So the answer to the question is - I have staked 100847 SPORTS in the past 30 days :)

The beauty of programming these is I need not code it again and again , I can use this script to get any account info by changing a few lines .

Note: The program I have written might not be the ideal way to gather info , I was just trying a few things and it came off right lol so you might fine tune this code to get results in a faster way


Regards,
MR.

Posted Using LeoFinance Beta