I am using web3.py and I want to get the binary (not the ABI) of a smart contract, given its address. Is it possible? If yes, how? If it isn't possible using web3.py, is there an alternative Python approach that I can use to get the binary of the smart contract?
2 Answers
You can do so by using the following method: web3.eth.get_code('0x6C8f2A135f6ed072DE4503Bd7C4999a1a17F824B')
Ref: https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.get_code
You need an API-key I am using Infura. Click the link and create an account. Their free tier covers 100000 API-calls per day so if you do not exceed this you are fine. https://app.infura.io
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/<you_own_infura_key>'))
bytecode = w3.eth.getCode(<your_address>)
If the it complains on checksum for the addres you provided use:
address2 = Web3.toChecksumAddress(addr)
Also if the format the bytecode is in is not pleasing (containing a lot of \x you can use the following function:
bytecode = w3.toHex(ugly_bytecode)