Web3.py: abi['name'] == name KeyError: 'name'

Created on 19 May 2020  路  8Comments  路  Source: ethereum/web3.py

  • Version: 5.9.0
  • Python: 3.7
  • OS: win
  • pip freeze output
aiodns==1.1.1
aiohttp==3.0.1
altgraph==0.16.1
appdirs==1.4.3
asn1crypto==0.24.0
async-generator==1.10
async-timeout==2.0.1
asyncio-run-in-process==0.1.0a4
attrdict==2.0.1
attrs==18.2.0
autobahn==19.1.1
Automat==0.7.0
base58==0.2.5
beautifulsoup4==4.8.2
bitmex==0.2.2
bitmex-ws==0.4.0
bitshares==0.3.2
bloom-filter==1.3
bravado==10.4.3
bravado-core==5.16.0
broker-client==1.0.0
bs4==0.0.1
ccxt==1.17.434
certifi==2018.11.29
cffi==1.11.5
chardet==3.0.4
Click==7.0
click-datetime==0.2
cloudpickle==1.3.0
constantly==15.1.0
cryptography==2.3
cycler==0.10.0
cytoolz==0.10.1
dateparser==0.7.0
dateutils==0.6.6
docopt==0.6.2
ecdsa==0.13.2
eth-abi==2.1.1
eth-account==0.4.0
eth-hash==0.2.0
eth-keyfile==0.5.1
eth-keys==0.2.4
eth-rlp==0.1.2
eth-typing==2.2.1
eth-utils==1.8.4
Events==0.3
future==0.17.1
graphenelib==1.1.18
hexbytes==0.2.0
hyperlink==18.0.0
idna==2.7
importlib-metadata==1.3.0
incremental==17.5.0
ipfshttpclient==0.4.12
jsonpointer==2.0
jsonref==0.2
jsonschema==3.2.0
jwt==0.6.1
kiwisolver==1.0.1
liquidtap==1.0.1
lru-dict==1.1.6
macholib==1.11
matplotlib==3.0.2
monotonic==1.5
more-itertools==8.0.2
msgpack-python==0.5.6
multiaddr==0.0.9
multidict==4.5.2
mypy-extensions==0.4.3
netaddr==0.7.19
numpy==1.16.0
oauthlib==3.0.0
pandas==0.25.3
parsimonious==0.8.1
pefile==2018.8.8
protobuf==3.11.3
pyasn1==0.4.5
pyasn1-modules==0.2.3
pyblake2==1.1.2
pycares==2.4.0
pycparser==2.19
pycryptodome==3.8.1
PyHamcrest==1.9.0
PyInstaller==3.4
PyJWT==1.7.1
pylibscrypt==1.8.0
pymultihash==0.8.2
pyOpenSSL==19.0.0
pyparsing==2.3.1
pypiwin32==223
pyqt-distutils==0.7.3
PyQt5==5.11.3
PyQt5-sip==4.19.13
pyrsistent==0.15.6
PySocks==1.6.8
python-axolotl-curve25519==0.4.1.post2
python-binance==0.7.1
python-dateutil==2.7.5
pytz==2018.9
PyWaves==0.8.20
pywin32==227
pywin32-ctypes==0.2.0
PyYAML==3.13
regex==2019.1.24
requests==2.21.0
requests-futures==1.0.0
requests-oauthlib==1.2.0
rfc3987==1.3.8
rlp==1.2.0
ruamel.yaml==0.15.87
scrypt==0.8.13
service-identity==18.1.0
simplejson==3.17.0
six==1.12.0
soupsieve==2.0
SQLAlchemy==1.2.16
ssz==0.2.3
strict-rfc3339==0.7
swagger-spec-validator==2.4.3
toolz==0.10.0
tweepy==3.7.0
twilio==6.35.5
Twisted==18.9.0
txaio==18.8.1
typing-extensions==3.7.4.1
tzlocal==1.5.1
urllib3==1.24.1
varint==1.0.2
web3==5.7.0
webcolors==1.10
websocket-client==0.57.0
websockets==8.1
xlrd==1.2.0
yarl==1.1.0
zipp==0.6.0
zope.interface==4.6.0

What was wrong?

from web3 import Web3
web3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/xx'))

routerV2ABI = json.loads('[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]')

routerV2Contract = web3.eth.contract(address='0xf164fC0Ec4E93095b804a4795bBe1e041497b92a', abi= routerV2ABI)

  • The code which produced the error
    routerV2Contract.functions.getAmountsOut(100000000000,['c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '2b591e99afe9f32eaa6214f7b7629768c40eeb39'])).call()

  • The full output of the error
    Traceback (most recent call last):
    File "C:/UniSwap/UniSwaper.py", line 72, in
    uniSwapV2PriceEthToHex()
    File "C:/UniSwap/UniSwaper.py", line 65, in uniSwapV2PriceEthToHex
    print(routerV2Contract.functions.getAmountsOut(100000000000,['c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '2b591e99afe9f32eaa6214f7b7629768c40eeb39'])).fallback.call()
    File "C:\PyCharm\environment\libsite-packages\web3\contract.py", line 824, in __call__
    clone._set_function_info()
    File "C:\PyCharm\environment\libsite-packages\web3\contract.py", line 834, in _set_function_info
    self.kwargs
    File "C:\PyCharm\environment\libsite-packages\web3_utils\contracts.py", line 120, in find_matching_fn_abi
    function_candidates = pipe(abi, name_filter, arg_count_filter, encoding_filter)
    File "cytoolz/functoolz.pyx", line 669, in cytoolz.functoolz.pipe
    return c_pipe(data, funcs)
    File "cytoolz/functoolz.pyx", line 644, in cytoolz.functoolz.c_pipe
    data = func(data)
    File "C:\PyCharm\environment\libsite-packages\web3_utils\abi.py", line 96, in filter_by_name
    in contract_abi
    File "C:\PyCharm\environment\libsite-packages\web3_utils\abi.py", line 99, in
    abi['name'] == name
    KeyError: 'name'

  • What type of node you were connecting to.
    Infura

How can it be fixed?

Any Function I call from this Contract gets this error. Other contracts works fine.

All 8 comments

@marcgarreau

  1. I think this is a bug that can be fixed reasonably simply. We've run into this type of thing before. Somewhere in the list of ABI definitions there is an entry that doesn't have a 'name' key. The fix could be as simple as changing the abi['name'] == name to be abi.get('name') == name.
  2. Since this isn't the first time that we've encountered this type of issue, either due to to changes in the way solidity produces the ABI, or just simply receiving malformed ABI definition, I'm thinking that we might want to add a new feature which validates and normalizes the provided ABI definition into an internal representation (maybe using NamedTuple types) so that we can eliminate (reduce) this type of error occuring at this stage and provide the user with a better error earlier (at the time of contract factory creation) with a better error message.

and it's worth looking into what if any of this functionality should/could live in eth-abi. I think it might be reasonable for the internal representation of the ABI to be part of that library

will poke at this today :eyes:

Actually, I wasn't able to reproduce the error with the getAmountsOut function.

The addresses used in the example code threw the Web3.py only accepts checksum addresses exception, but after applying a toChecksumAddress, Infura returned a successful response: [100000000000, 606728].

Is there something else in the environment that might explain the original issue?

Actually, I wasn't able to reproduce the error with the getAmountsOut function.

The addresses used in the example code threw the Web3.py only accepts checksum addresses exception, but after applying a toChecksumAddress, Infura returned a successful response: [100000000000, 606728].

Is there something else in the environment that might explain the original issue?

Here I tried again:
import json
from web3 import Web3

web3 = Web3(Web3.HTTPProvider('INFURA'))

routerV2ABI = json.loads('The same ABI as Above')
routerV2Contract = web3.eth.contract(address='0xf164fC0Ec4E93095b804a4795bBe1e041497b92a', abi= routerV2ABI)

print(routerV2Contract.functions.WETH())
print(routerV2Contract.functions.getAmountsOut(10000000000,['0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', '0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39'])).call()

I receive the same error for other functions too

Tried it in new environment. Its working now. Thanks guys!

Yeah, predictably, the other functions are working as expected on my end. I'm not sure of the best way forward; our package versions are aligned and I've tested in a script and within a jupyter notebook. @pipermerriam suggestions welcome on how to further debug.

If you're up for it @reinisb123, one thing you could try is to apply the fix Piper is suggesting in a locally edited version of web3 and see if that addresses your issue. Steps to do that:

  1. clone the web3 repo locally
  2. within web3/_utils/abi.py line 99, change abi['name'] == name to abi.get('name') == name
  3. install the cloned repo in your app, e.g., pip install -e ../path/to/cloned/web3.py
  4. run your example code again
  5. profit?

馃帀 ah, great. you just beat my message. 馃槅

Was this page helpful?
0 / 5 - 0 ratings