I am trying to convert Javascript code to Python and believe the equivalent "web3.eth.abi.encodeFunctionSignature" function in JS to encode ABI in Python is with the "encode_abi" function from the "eth-abi" library (https://eth-abi.readthedocs.io/en/latest/encoding.html).

However, I am completely stuck on the correct syntax to encode the below "_maxTxAmount" function call with "encode_abi". Any help/explanation would be greatly appreciated.

    let sig = web3.eth.abi.encodeFunctionSignature({ name: '_maxTxAmount', type: 'function', inputs: [] });
d = {
    to: address,
    from: '0x8894e0a0c962cb723c1976a4421c95949be2d4e3',
    value: 0,
    gas: 15000000,
    data: sig,
try {
    let val = await web3.eth.call(d);
    maxTXAmount = web3.utils.toBN(val);

The above excerpt is from a anti-honeypot script I found on Github and want to use this function call to enquire the above function with non-verified contracts e.g. I cannot make a "regular" contract call.

1 Answer 1


For this it is important to understand how function calls are encoded.

The first part is to calculate the function selector. This is a 4byte identifier of the method that starts the calldata. (See https://docs.soliditylang.org/en/v0.8.11/abi-spec.html?highlight=Function%20id#function-selector). For you example the function selector would be the keccak hash of _maxTxAmount.

This is then appended by the encoded parameters for the function. For this you can use the library you mentioned. For your example you don't need this as the function doesn't have any parameters.

The easiest way to interact with contracts using python is web3py. Here is an example for calling a contract: https://web3py.readthedocs.io/en/stable/contracts.html

  • Many thanks for taking the time to answer. I managed to do it with web3.py's native encodeABI() function now, but would still be good to understand how to do it with eth-abi. Will look into your first point. Re encoding the parameters, can this be encoded as a single string? e.g. encode_abi(['string'], ['[{"type":"function","inputs":[]}]']) ? (understand in this instance not required but as an example).
    – gino
    Commented Jan 8, 2022 at 18:01
  • eth-abi will only help you encode the function parameters, for the function selector you need to use a lib that provides the keccak256 hash functions. To encode then the parameters you can use encode_abi(paramer_types, parameter_values). So in you example you would encode the string [{"type":"function","inputs":[]}] as a parameter input.
    – Richard
    Commented Jan 8, 2022 at 21:56
  • 1
    excellent, many thanks - I also managed to calculate the function selector with keccak256 and successfully called the function. Thanks again for your help.
    – gino
    Commented Jan 8, 2022 at 22:31

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.