Skip to content

Instantly share code, notes, and snippets.

@davaymne
Created November 28, 2022 20:19
Show Gist options
  • Save davaymne/2ef5c3c8cefbb9347e9d47a6d1ee5f50 to your computer and use it in GitHub Desktop.
Save davaymne/2ef5c3c8cefbb9347e9d47a6d1ee5f50 to your computer and use it in GitHub Desktop.
import pprint
import dateutil.relativedelta
from datetime import datetime
import glob
import os
import sys
from web3 import Web3
from datetime import datetime
from python_graphql_client import GraphqlClient
import json
import csv
from time import gmtime
import time
import datetime as dt
from datetime import datetime
import requests
import json
from prettytable import PrettyTable
import base58
ethereum_node = <ETHEREUM>
w3 = Web3(Web3.HTTPProvider(ethereum_node))
w3.isConnected()
def get_allocations(indexer):
query = """
{
allocations(where: { indexer: "%s", status: "Active"}){
id
createdAt
allocatedTokens
subgraphDeployment{
originalName
id
signalledTokens
stakedTokens
versions (orderBy: createdAt, orderDirection:desc, first:1){
subgraph{
displayName
}
}
}
}
}
""" % (indexer)
data = client.execute(query=query)
allocations = data['data']['allocations']
return allocations
def get_rewards(alloc):
alloc = w3.toChecksumAddress(alloc)
return contract.functions.getRewards(alloc).call() / TOKEN
def import_from_file(filename):
data = ''
if os.path.getsize(filename) > 0:
with open(filename,'r') as f:
data = json.load(f)
return data
def export_to_file(filename, output):
with open(filename, 'w') as fp:
json.dump(output, fp, indent=4)
client = GraphqlClient(endpoint="https://gateway.network.thegraph.com/network")
TOKEN = 1000000000000000000
contract_address = w3.toChecksumAddress('0x9Ac758AB77733b4150A901ebd659cbF8cB93ED66')
abi = """
[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"param","type":"string"}],"name":"ParameterUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType
":"address","name":"indexer","type":"address"},{"indexed":true,"internalType":"address","name":"allocationID","type":"address"},{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"},{
"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"RewardsAssigned","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"indexer",
"type":"address"},{"indexed":true,"internalType":"address","name":"allocationID","type":"address"},{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"RewardsDenied","type"
:"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"subgraphDeploymentID","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"sinceBlock","type":"uint25
6"}],"name":"RewardsDenylistUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"controller","type":"address"}],"name":"SetController","type":"event"},{"
inputs":[],"name":"accRewardsPerSignal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"accRewardsPerSignalLastBlockUpdat
ed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IGraphProxy","name":"_proxy","type":"address"}],"name
":"acceptProxy","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IGraphProxy","name":"_proxy","type":"address"},{"internalType":"bytes","name":"_data","t
ype":"bytes"}],"name":"acceptProxyAndCall","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"addressCache","outputs":
[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"controller","outputs":[{"internalType":"contract IController","name":"","type":"add
ress"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"denylist","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"s
tateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_subgraphDeploymentID","type":"bytes32"}],"name":"getAccRewardsForSubgraph","outputs":[{"internalType":"uint256","nam
e":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_subgraphDeploymentID","type":"bytes32"}],"name":"getAccRewardsPerAllocatedToken","outputs
":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getAccRewardsPerSignal","ou
tputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getNewRewardsPerSignal","outputs":[{"internalType":"uint256","name":"","type
":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_allocationID","type":"address"}],"name":"getRewards","outputs":[{"internalType":"uint256","name":"",
"type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_controller","type":"address"},{"internalType":"uint256","name":"_issuanceRate","type":"uint256"
}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_subgraphDeploymentID","type":"bytes32"}],"name":"isDenied","outputs":[{"
internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"issuanceRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutabi
lity":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_subgraphDeploymentID","type":"bytes32"}],"name":"onSubgraphAllocationUpdate","outputs":[{"internalType":"uint256","name":"","t
ype":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_subgraphDeploymentID","type":"bytes32"}],"name":"onSubgraphSignalUpdate","outputs":[{"inter
nalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_controller","type":"address"}],"name":"setController","outpu
ts":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_subgraphDeploymentID","type":"bytes32"},{"internalType":"bool","name":"_deny","type":"bool"}],"name":
"setDenied","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32[]","name":"_subgraphDeploymentID","type":"bytes32[]"},{"internalType":"bool[]","name":"_deny"
,"type":"bool[]"}],"name":"setDeniedMany","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_issuanceRate","type":"uint256"}],"name":"setIssuanceR
ate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_subgraphAvailabilityOracle","type":"address"}],"name":"setSubgraphAvailabilityOracle","out
puts":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"subgraphAvailabilityOracle","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type
":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"subgraphs","outputs":[{"internalType":"uint256","name":"accRewardsForSubgraph","type":"uint256"},{"internalType":"ui
nt256","name":"accRewardsForSubgraphSnapshot","type":"uint256"},{"internalType":"uint256","name":"accRewardsPerSignalSnapshot","type":"uint256"},{"internalType":"uint256","name":"accRewardsPerAllocatedTok
en","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_allocationID","type":"address"}],"name":"takeRewards","outputs":[{"internalType":"uint256",
"name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"updateAccRewardsPerSignal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutab
ility":"nonpayable","type":"function"}]
"""
contract = w3.eth.contract(address=contract_address, abi=abi)
indexer_list = [['indexer', <Rewards Cut>]]
def getIPFS(id):
return base58.b58encode(bytes.fromhex("1220"+id[2:])).decode('utf-8')
def telegram_bot_sendtext(bot_msg):
bot_token = <TOKEN>
bot_chatID = <bot_chatID>
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_msg
response = requests.get(send_text)
return response.json()
currentTime = int(time.time())
filename = glob.glob("/root/Scripts/ExportCurrentRewards*.csv")[0]
newfilename = '/root/Scripts/ExportCurrentRewards{}.csv'.format(str(currentTime))
prevRecord = import_from_file(filename)
prevTime = int(filename[-14:-4])
print(prevTime)
to_save = []
now = datetime.now()
t = now.strftime("%Y-%m-%d-%H:%M:%S")
recordIndexer = {}
for indexer in indexer_list:
print('Indexer: %s delta: %s sec' % (indexer[0], str(currentTime-prevTime)))
allocations = get_allocations(indexer[0])
t = PrettyTable(['Name', 'K', 'ID', 'Amount', 'Duration', 'Rewards Pending', 'Rate GRT/HOUR', 'Rate GRT/HOUR/1M'])
rewards = 0
total_rph = 0
recordAllocation = {}
for allocation in allocations:
aid = allocation['id']
name = allocation['subgraphDeployment']['versions'][0]['subgraph']['displayName']
sub_id = getIPFS(allocation['subgraphDeployment']['id'])
amount = int(allocation['allocatedTokens'])/TOKEN
createdAt = allocation['createdAt']
signalledTokens = int(allocation['subgraphDeployment']['signalledTokens'])
stakedTokens = int(allocation['subgraphDeployment']['stakedTokens'])
k = round(signalledTokens/stakedTokens*1000,2)
if k < 0.5:
telegram_bot_sendtext('Signal Mon: Signal on sub: {} is too Low: {}.'.format(name, k))
d = dateutil.relativedelta.relativedelta(datetime.fromtimestamp(currentTime),datetime.fromtimestamp(createdAt))
duration = '{}d {}h {}m {}s'.format(d.days, d.hours, d.minutes, d.seconds)
currentRewards = get_rewards(allocation['id'])
if prevRecord:
prevRewardsAllocation = prevRecord[indexer[0]].get(aid, 0)
if prevRewardsAllocation:
prevRewards = prevRewardsAllocation.get('rewards', 0)
else:
prevRewards = 0
else:
prevRewards = 0
rate_rps = (currentRewards - prevRewards)/(currentTime-prevTime)
rate_rph = ((currentRewards - prevRewards)/(currentTime-prevTime))*3600
rate_rps_per1m = ((currentRewards - prevRewards)/(currentTime-prevTime)/amount)*(1000000)
rate_rph_per1m = (((currentRewards - prevRewards)/(currentTime-prevTime))/amount)*1000000*3600
t.add_row([name, k, amount, sub_id, duration, round(currentRewards,2), round(rate_rph,2), round(rate_rph_per1m,2)])
rewards = rewards + round(currentRewards,2)
total_rph = rate_rph + total_rph
recordAllocation[aid] = {'name': name, 'amount': amount, 'rewards': currentRewards}
recordIndexer[indexer[0]] = recordAllocation
t.align="r"
print(t)
print('Rewards (pending) {}%: {} GRT'.format(indexer[1]*100, round(rewards*indexer[1]),0))
print('Rewards per hr: {} GRT/HOUR \n'.format(round(total_rph,2)))
os.remove(filename)
export_to_file(newfilename, recordIndexer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment