1

I want to encode data using abi.encodeWithSignature in Python, and send the output from this into a contract function parameter. Now the problem is I can't figure out how should I do it.

The function I want to encode is like abi.encodeWithSignature("Increment(uint256)",55)

2 Answers 2

1

We can find teh equivalent bytes of abi.encodeWithSignature in Python and eth-brownie by using encode_input.

In the above example I had to find the encoding of Increment() and with uint256 parameter value of 55. Suppose the contract is deployed in test and stored in variable ContractA.

To find abi.encodeWithSignature I will write:

value = ContractA.Increment.encode_input(55)

Somehow it took me alot of time to find this, therefore I am writing this answer.

0
  • I think there is some padding problem if we just do this ContractA.Increment.encode_input(55).
  • This is the correct way to add padding. Say there is contract which has a function methodName.
value = Contract.methodName.encode_input()
print(value)
# wrong
>> '0xa7a7e4e8' 
value = Contract.methodName.encode_input()
# add padding to make it 32 bytes
value = value + "0" * (66 - len(value))
print(value)
# correct
>> '0xa7a7e4e800000000000000000000000000000000000000000000000000000000'

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.