3

Constructor takes byte32 value. How send32 value, using web3.py or only python?

2 Answers 2

2

In web3.py v4+, you can send either a hex string, or native Python bytes, like:

contract.constructor('0xf005ba11').transact()
contract.constructor(b'\xf0\x05\xba\x11').transact()
2
  • 1
    It doesn't work because I get "Could not identify the intended function with name"
    – 0x3bfc
    Jun 11, 2018 at 10:24
  • @aabdulwahed I'm guessing that you're using ConciseContract. Try it again without ConciseContract. If that doesn't solve your problem, please start a new question.
    – carver
    Jun 12, 2018 at 22:12
2

How to convert/encode string to bytes32 in Python for Solidity bytes32 arguments via web3.py

zBytes = "YAY!! Less than 32"
#zBytes = "YAY!! We Are Going To Make Ethereum & Python Great! Go Crypto and Blockchain and DAG, etc...!!!"
len1 = len(zBytes)
if len1 > 32:
    print('input string length: '+ str(len1)+ ' is too long')
    zBytes32 = zBytes[:32]
else:
    print('input string length: '+ str(len1)+ ' is too short')
    print('More characters needed: '+ str(32-len1))
    zBytes32 = zBytes.ljust(32, '0')
print('zBytes32 = '+ str(zBytes32)+ ' and its length: '+ str(len(zBytes32)))
xBytes32 = bytes(zBytes32, 'utf-8')
print('xBytes32 = '+ str(xBytes32))
contractInstance.functions.yourFunctionName(xBytes32).buildTransaction()

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.