ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.95k stars 1.69k forks source link

[FEAT] Add encodeFunctionData function #3294

Open ababyprogrammer opened 5 months ago

ababyprogrammer commented 5 months ago

ethers.js has a function called encodeFunctionDataand it encodes a function and it's arguments into bytes. This is helpful especially when working with proxies and low level delegate calls.

Here is an example.

I'd think the API would look something like:

from web3 import Web3

Web3.encode_function_data("functionName(uint256 parameter1, uint256 parameter2...)", parameter1, parameter2...)

The function could look something like:

def encode_function_data(function_string: string, *parameters):
        return Web3.toHex(Web3.toHex(functionFragment)[0:4] + Web3.toHex([*parameters])

(I think).

kclowes commented 5 months ago

Does contract.encodeABI work for your use case or would you rather have it on the Web3 class?