0

I am building a web application using django to register phone battery details and it interacts with a smart contract. I am trying to use the decode_log method but I keep getting this error:

event_data = w3.eth.abi.decode_log(EVENT_ABI, logs[0]) AttributeError: 'Eth' object has no attribute 'abi'

It is part of the view function:

            if logs:
            event_abi = contract.events.BatteryRegistered._get_event_abi()
            event_data = w3.eth.abi.decode_log(EVENT_ABI, logs[0])
            battery_id = event_data['batteryId'].hex()
            request.session['battery_id'] = battery_id
            return redirect('battery_registration_success')

this is the view funtion:

def battery_registration(request):
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=contract_abi)
if request.method == 'POST':
    form = BatteryRegistrationForm(request.POST)
    if form.is_valid():  
        manufacturer = request.user.manufacturer
        from_address = "0x6dcA0D94ED15473d0b92cb58D5beBbbFC62CB566"
        watt_hours = int(form.cleaned_data['watt_hours'])
        # Process form data and register battery
        transaction = contract.functions.registerBattery(
            form.cleaned_data['product_name'],
            form.cleaned_data['certification_mark'],
            form.cleaned_data['serial_number'],
            watt_hours,
        ).transact({'from': "0x6dcA0D94ED15473d0b92cb58D5beBbbFC62CB566"})
        transaction_receipt = w3.eth.get_transaction_receipt(transaction)
        logs = w3.eth.get_logs({
            'fromBlock': transaction_receipt['blockNumber'],
            'toBlock': transaction_receipt['blockNumber'],
            'address': CONTRACT_ADDRESS
        })
        if logs:
            event_abi = contract.events.BatteryRegistered._get_event_abi()
            event_data = w3.eth.abi.decode_log(EVENT_ABI, logs[0])
            battery_id = event_data['batteryId'].hex()
            request.session['battery_id'] = battery_id
            return redirect('battery_registration_success')
else:
    form = BatteryRegistrationForm()
return render(request, 'battery_registration.html', {'form': form})

I am building it on a local blockchain network, Ganache. It is my first smart contract and first time using django, so can you please help!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.