1

I'm trying to get the to and from address from the logs in a transaction. In my example, the log entry is for a Transfer(address,address,uint256).

The resulting topics[1] and [2] return the addresses, but in a long format: 0x0000000000000000000000002dbbbebef9915110dac11a89ca986ba6051d0972

How do I convert this to a regular address? Eg: 0x2dbbbebef9915110dac11a89ca986ba6051d0972

0

1 Answer 1

0

Let's assume that address stores the given address.


Option #1 - purely in Python:

Since an Ethereum address is 40 hexadecimal characters, you can simply use

x = '0x' + address[-40:]

Option #2 - with web3.py:

Using the utility function toChecksumAddress, you can simply use

x = Web3.toChecksumAddress('0x' + address[-40:])

The second option is better because it gives you the result in standard (EIP55) form, which means that you can compare your addresses (strings) without worrying about case-sensitive differences.

2
  • Thank you. Option 2, whilst I agree would be preferable, gives me an error:ValueError: Unknown format 0x00000000000000000000000...... Option 1 works great. I guess I could do option 1 and then option 2 to vefiy the address.
    – Twiddles
    Commented Nov 15, 2020 at 5:04
  • @Julian: Please see updated answer. Commented Nov 15, 2020 at 5:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.