#!/usr/bin/env python3 import json from os import getenv, path from secrets import token_urlsafe from datetime import datetime from hashlib import sha256 from random import choices from web3 import Web3 from dotenv import load_dotenv load_dotenv() def get_eth_contract(_ca, _rp): compiled_contract_path = path.abspath(_rp) with open(compiled_contract_path) as file: contract_json = json.load(file) contract_abi = contract_json['abi'] return w3.eth.contract(address=w3.toChecksumAddress(_ca), abi=contract_abi) def sendit(w3, t, nonce): t['from'] = w3.eth.defaultAccount t['nonce'] = nonce s = w3.eth.account.sign_transaction(t, private_key=getenv(f'{net}_KEY')) return w3.eth.send_raw_transaction(s.rawTransaction) if __name__ == '__main__': net = getenv('NET').upper() if not getenv(f'{net}_CONTRACT') or not getenv(f'{net}_KEY') or not getenv(f'{net}_RPC'): print('invalid env vars set') exit() # web3 setup WEB3_PROVIDER_URI = getenv(f'{net}_RPC') w3 = Web3(Web3.HTTPProvider(WEB3_PROVIDER_URI)) w3.eth.defaultAccount = w3.eth.account.from_key(getenv(f'{net}_KEY')).address contract = get_eth_contract(getenv(f'{net}_CONTRACT'), './out/Lottery.sol/Lottery.json') nonce = w3.eth.get_transaction_count(w3.eth.defaultAccount) # Calculate gas if getenv('HASH'): tokens = choices(range(0, 5000), k=10) phrase = f'{datetime.utcnow()},{token_urlsafe(12).replace(",", "")},{" ".join([str(i) for i in tokens])}' hashed = sha256(phrase.encode('utf-8')).hexdigest() r = sendit(w3, contract.functions.setWinningPhrase(hashed).build_transaction(), nonce) print(f'sent tx {r.hex()} with nonce {nonce} to update the winning phrase') with open('output.txt', 'w') as f: f.write(f'{phrase}\n{hashed}') if getenv('TOKENS'): msg = '' with open('output.txt', 'r') as f: msg = f.readlines()[0] _tokens = msg.split(',')[2] tokens = [int(i) for i in _tokens.split()] r = sendit(w3, contract.functions.setWinningTokens(tokens).build_transaction(), nonce) print(f'sent tx {r.hex()} with nonce {nonce} to update the winning tokens')