Web3 错误:Geth 无法与/调用合约函数进行交易

Posted

技术标签:

【中文标题】Web3 错误:Geth 无法与/调用合约函数进行交易【英文标题】:Web3 Error: Geth Could not transact with/call contract function 【发布时间】:2021-10-06 04:56:09 【问题描述】:

我决定运行我自己的 ethereum/bsc 全节点。我下载了 geth-linux-amd64-1.1.0 并像这样运行它: geth --syncmode "full" --http --http.port 8545 --http.corsdomain "*" --http.addr "127.0.0.1" --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --cache 18000 --maxpeers 500 --datadir /someplace

getth attach 和 eth.syncing 给我看

currentBlock: 9606766, highestBlock: 9606883, knownStates: 345833179, pulledStates: 345782057, startingBlock: 9604320

所以我想我已经同步了。但是,当我运行 w3.eth.getBalance(some_address) 时,它返回 0。当我使用 Metamask 或 infura 时,我得到不同的值。

我也无法在以太坊(或任何其他代币)上与 Tether 交互:

ERC20_ABI = [..] tether_contract = w3.eth.contract('0xdAC17F958D2ee523a2206206994597C13D831ec7', abi=ERC20_ABI) tether_contract.functions.balanceOf(that_address).call()

结果:

Traceback (most recent call last):
      File "/usr/local/lib/python3.8/dist-packages/web3/contract.py", line 1513, in call_contract_function
        output_data = web3.codec.decode_abi(output_types, return_data)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/codec.py", line 181, in decode_abi
        return decoder(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 127, in __call__
        return self.decode(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_utils/functional.py", line 45, in inner
        return callback(fn(*args, **kwargs))
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 173, in decode
        yield decoder(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 127, in __call__
        return self.decode(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 142, in decode
        start_pos = decode_uint_256(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 127, in __call__
        return self.decode(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 198, in decode
        raw_data = self.read_data_from_stream(stream)
      File "/usr/local/lib/python3.8/dist-packages/eth_abi/decoding.py", line 305, in read_data_from_stream
        raise InsufficientDataBytes(
    eth_abi.exceptions.InsufficientDataBytes: Tried to read 32 bytes.  Only got 0 bytes
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.8/dist-packages/web3/contract.py", line 957, in call
        return call_contract_function(
      File "/usr/local/lib/python3.8/dist-packages/web3/contract.py", line 1530, in call_contract_function
        raise BadFunctionCallOutput(msg) from e
    web3.exceptions.BadFunctionCallOutput: Could not transact with/call contract function, is contract deployed correctly and chain synced?

我做错了什么?

编辑: 很长一段时间,它总是落后于网络约 64 个块。 (即使使用 NVMe)

【问题讨论】:

【参考方案1】:

currentBlock:9606766,highestBlock:9606883,knownStates:345833179,pullStates:345782057,startingBlock:9604320

所以我想我已经同步了

其实这意味着你的节点还在同步,eth_syncing结束后会返回一个空结果。

https://eth.wiki/json-rpc/API#eth_syncing

不同步时具有同步状态数据或 FALSE 的对象

【讨论】:

以上是关于Web3 错误:Geth 无法与/调用合约函数进行交易的主要内容,如果未能解决你的问题,请参考以下文章

调用 web3.js,连接 infura 节点进行合约转账

无法以大数字作为参数从 web3 调用合约函数

如何获取以太坊合约和账户信息(使用geth和web3.py)

Geth 控制台使用及 Web3.js 使用实战

使用Nodejs部署智能合约

关于web3.js中与交易发送交易签名智能合约函数调用相关api的理解