from web3 import Web3 from src.utils import load_abi class Scheduler(): def __init__(self, w3: Web3, address: str, abi_file: str): self.w3 = w3 abi = load_abi(abi_file) self.contract = self.w3.eth.contract(address=address, abi=abi) print(self.contract.functions) def signedAndSendTransaction(self, account, function): tx = function.build_transaction({ 'from': account.address, 'nonce': self.w3.eth.get_transaction_count(account.address) }) signed_tx = self.w3.eth.account.sign_transaction(tx, private_key=account.key) tx_hash = self.w3.eth.send_raw_transaction(signed_tx.rawTransaction) tx_receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash) _hash = tx_receipt['transactionHash'] return _hash def registerCluster(self, account, gpu_id: int, gpu_num: int): function = self.contract.functions.registerCluster(gpu_id, gpu_num) return self.signedAndSendTransaction(account, function) def registerTaskWithConditions(self, account, data_image: str, train_image: str, gpu_id: int, cluster_size: int): function = self.contract.functions.registerTaskWithConditions(data_image, train_image, gpu_id, cluster_size) return self.signedAndSendTransaction(account, function) def getClusters(self): return self.contract.functions.getClusters().call() def getTasks(self): return self.contract.functions.getTasks().call() def getStartRunEvent(self): event_filter = self.contract.events.StartRun.create_filter(fromBlock=0) events = event_filter.get_new_entries() print(events) def getRegisterClusterEvent(self): event_filter = self.contract.events.RegisterCluster.create_filter(fromBlock=0) events = event_filter.get_new_entries() print(events)