class ProtocolRegistryIntegration:
def __init__(self, w3, registry_address, abi):
self.w3 = w3
self.contract = w3.eth.contract(address=registry_address, abi=abi)
self.account = None
def set_account(self, private_key):
self.account = self.w3.eth.account.from_key(private_key)
def register_protocol(self, name, description, protocol_type, data_provider, weight):
if not self.account:
raise ValueError('Account not set')
from eth_abi import encode
metadata = encode(['string', 'string'], ['https://protocol.com', 'v1.0.0'])
tx = self.contract.functions.registerProtocol(
name, description, protocol_type, data_provider, weight, metadata
).buildTransaction({
'from': self.account.address,
'gas': 200000,
'gasPrice': self.w3.eth.gas_price,
'nonce': self.w3.eth.get_transaction_count(self.account.address)
})
signed_tx = self.w3.eth.account.sign_transaction(tx, self.account.key)
tx_hash = self.w3.eth.send_raw_transaction(signed_tx.rawTransaction)
receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)
event = self.contract.events.ProtocolRegistered().processReceipt(receipt)[0]
return {
'success': True,
'protocolId': str(event['args']['protocolId']),
'transactionHash': receipt.transactionHash.hex()
}