eth-brownie / brownie

A Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.
https://eth-brownie.readthedocs.io
MIT License
2.64k stars 550 forks source link

JSONDecodeError when importing contract object from BSCscan #1082

Closed sabotagebeats closed 3 years ago

sabotagebeats commented 3 years ago

Environment information

What was wrong?

when I try to import a contract with Contract.from_explorer(contract_address) I get JSONDecodeError: Expecting value: line 2 column 1 (char 1)

this same command works fine on ethereum mainnet for example:

>>> uniswap = Contract.from_explorer('0x1f9840a85d5af5bf1d1762f925bdaddc4201f984')
Fetching source of 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 from api.etherscan.io...
>>>

How can it be fixed?

most likely an issue with the json formatting response from bscscan

Fill this in if you know how the bug could be fixed.

sabotagebeats commented 3 years ago

had a similar error when deploying to bsc, although the deployment succeeds, the variable does not get assigned with the required json to interact with the contract.


  File "<console>", line 1, in <module>
  File "brownie/network/contract.py", line 593, in __call__
    return tx["from"].deploy(
  File "brownie/network/account.py", line 512, in deploy
    contract.publish_source(deployed_contract, silent=silent)
  File "brownie/network/contract.py", line 452, in publish_source
    data = response.json()
  File "requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 2 column 1 (char 1)```
sabotagebeats commented 3 years ago

I am still able to interact with the deployed contract if I load it from abi with Contract.from_abi

iamdefinitelyahuman commented 3 years ago

I'm unable to replicate - guessing it was an API issue with bscscan

Quaid-Doug commented 3 years ago

I'm unable to replicate - guessing it was an API issue with bscscan

Yes - that happens from times to times. Maybe we could add the mention "the problem may be on the explorer side" or something alike ?

sabotagebeats commented 3 years ago

I'll give it another shot and see if it works for me now. Thank you!

sabotagebeats commented 3 years ago

Looks like I'm still having the issue.

File "<console>", line 1, in <module>
  File "brownie/network/contract.py", line 452, in publish_source
    data = response.json()
  File "requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 2 column 1 (char 1)
poocoin = Contract.from_explorer('0xb27adaffb9fea1801459a1a81b17218288c097cc')
Fetching source of 0xB27ADAfFB9fEa1801459a1a81B17218288c097cc from bscscan.com...
  File "<console>", line 1, in <module>
  File "brownie/network/contract.py", line 1090, in from_explorer
    data = _fetch_from_explorer(address, "getsourcecode", silent)
  File "brownie/network/contract.py", line 1891, in _fetch_from_explorer
    data = response.json()
  File "requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 2 column 1 (char 1)
sabotagebeats commented 3 years ago

This fixed the issue for me:

brownie networks modify binance explorer="https://api.bscscan.com/api"

previously I was using the wrong explorer address https://bscscan.com/