Ethereum: Web3.pyでAddress has an invalid EIP checksumのエラーが吐かれる場合


Web3.pyを利用していて、以下のようなコードで表題のエラーが吐かれる場合があります。


token_address = '0xbbfce3c05aef921b35c7264fff21bb08f490a821'
token_abi = open("AlisToken.abi", "r")
token = web3.eth.contract(
    address=token_address,
    abi=token_abi.read(),
)

結論

この場合、 web3.toChecksumAddress を用いることで解決できます。

token_address = web3.toChecksumAddress('0xbbfce3c05aef921b35c7264fff21bb08f490a821')
token_abi = open("AlisToken.abi", "r")
token = web3.eth.contract(
    address=token_address,
    abi=token_abi.read(),
)

環境

  • AWS Lambda python 3.6
  • Web3.py 4.2.0

吐かれたエラー

Traceback (most recent call last):
  File "/var/task/lambda_base.py", line 21, in main
    result = self.exec_main_proc()
  File "/var/task/token_distributer.py", line 36, in exec_main_proc
    abi=alis_token_abi.read(),
  File "/var/task/web3/eth.py", line 346, in contract
    return ContractFactory(address)
  File "/var/task/web3/contract.py", line 206, in __init__
    self.address = normalize_address(self.web3.ens, address)
  File "/var/task/web3/utils/normalizers.py", line 160, in normalize_address
    validate_address(address)
  File "/var/task/web3/utils/validation.py", line 116, in validate_address
    raise InvalidAddress("Address has an invalid EIP checksum", value)
web3.exceptions.InvalidAddress: ('Address has an invalid EIP checksum', '0xbbfce3c05aef921b35c7264fff21bb08f490a821')
('Address has an invalid EIP checksum', '0xbbfce3c05aef921b35c7264fff21bb08f490a821'): InvalidAddress
Traceback (most recent call last):
  File "/var/task/handler.py", line 10, in lambda_handler
    return token_distributer.main()
  File "/var/task/lambda_base.py", line 40, in main
    raise err
  File "/var/task/lambda_base.py", line 21, in main
    result = self.exec_main_proc()
  File "/var/task/token_distributer.py", line 36, in exec_main_proc
    abi=alis_token_abi.read(),
  File "/var/task/web3/eth.py", line 346, in contract
    return ContractFactory(address)
  File "/var/task/web3/contract.py", line 206, in __init__
    self.address = normalize_address(self.web3.ens, address)
  File "/var/task/web3/utils/normalizers.py", line 160, in normalize_address
    validate_address(address)
  File "/var/task/web3/utils/validation.py", line 116, in validate_address
    raise InvalidAddress("Address has an invalid EIP checksum", value)
web3.exceptions.InvalidAddress: ('Address has an invalid EIP checksum', '0xbbfce3c05aef921b35c7264fff21bb08f490a821')