python - *** web3.exceptions.BadFunctionCallOutput : Could not transact with/call contract function, 合约部署正确且链同步了吗?

标签 python web3py

我在本地运行 ganache 并使用 brownie console 将我的合约部署到它上面

❯ brownie console
Brownie v1.17.1 - Python development framework for Ethereum

BeautyEthereumProject is the active project.
Brownie environment is ready.
>>> ERC721
[<ERC721 Contract '0x6c83A98d16A80B1f88404563DBD5e57f5C7C7266'>]

web3 doc 中,我必须提供合约地址和合约 abi。

我知道合约地址,因为我记下了。

Transaction sent: 0x25a1576a66dd33135928c2d91b2f7cd6612c2cd4e70e44b831599cbb6b8c9705
  Gas price: 20.0 gwei   Gas limit: 620775   Nonce: 0
  ERC721.constructor confirmed   Block: 1   Gas used: 564341 (90.91%)
  ERC721 deployed at: 0x6c83A98d16A80B1f88404563DBD5e57f5C7C7266

<ERC721 Contract '0x6c83A98d16A80B1f88404563DBD5e57f5C7C7266'>

获取 ABI vyper -f abi filename.vy doc

main.py

from web3 import Web3, HTTPProvider

human_0 = '0x6A48A50A53462A22D2c30F5138c4970a3020b30a'
human_address_1 = "0x92e320aE601BC8235D605d38D03142C3FE28Ba92"

def main():
    w3 = Web3(HTTPProvider('http://localhost:8545'))  # web3 must be called locally
    assert True is w3.isConnected()
    print('Network connected')
    address = "0x6c83A98d16A80B1f88404563DBD5e57f5C7C7266"
    # To get ABI
    # vyper -f abi contracts/ERC721.vy
    abi = '[{"name": "Transfer", "inputs": [{"name": "sender", "type": "address", "indexed": true}, {"name": "receiver", "type": "address", "indexed": true}, {"name": "tokenId", "type": "uint256", "indexed": true}], "anonymous": false, "type": "event"}, {"name": "Approval", "inputs": [{"name": "owner", "type": "address", "indexed": true}, {"name": "approved", "type": "address", "indexed": true}, {"name": "tokenId", "type": "uint256", "indexed": true}], "anonymous": false, "type": "event"}, {"name": "ApprovalForAll", "inputs": [{"name": "owner", "type": "address", "indexed": true}, {"name": "operator", "type": "address", "indexed": true}, {"name": "approved", "type": "bool", "indexed": false}], "anonymous": false, "type": "event"}, {"stateMutability": "nonpayable", "type": "constructor", "inputs": [], "outputs": []}, {"stateMutability": "view", "type": "function", "name": "supportsInterface", "inputs": [{"name": "_interfaceID", "type": "bytes32"}], "outputs": [{"name": "", "type": "bool"}], "gas": 2641}, {"stateMutability": "view", "type": "function", "name": "balanceOf", "inputs": [{"name": "_owner", "type": "address"}], "outputs": [{"name": "", "type": "uint256"}], "gas": 2925}, {"stateMutability": "view", "type": "function", "name": "ownerOf", "inputs": [{"name": "_tokenId", "type": "uint256"}], "outputs": [{"name": "", "type": "address"}], "gas": 2813}, {"stateMutability": "view", "type": "function", "name": "getApproved", "inputs": [{"name": "_tokenId", "type": "uint256"}], "outputs": [{"name": "", "type": "address"}], "gas": 5040}, {"stateMutability": "view", "type": "function", "name": "isApprovedForAll", "inputs": [{"name": "_owner", "type": "address"}, {"name": "_operator", "type": "address"}], "outputs": [{"name": "", "type": "bool"}], "gas": 3190}, {"stateMutability": "nonpayable", "type": "function", "name": "transferFrom", "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 173314}, {"stateMutability": "nonpayable", "type": "function", "name": "safeTransferFrom", "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 190624}, {"stateMutability": "nonpayable", "type": "function", "name": "safeTransferFrom", "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}, {"name": "_data", "type": "bytes"}], "outputs": [], "gas": 190624}, {"stateMutability": "nonpayable", "type": "function", "name": "approve", "inputs": [{"name": "_approved", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 46741}, {"stateMutability": "nonpayable", "type": "function", "name": "setApprovalForAll", "inputs": [{"name": "_operator", "type": "address"}, {"name": "_approved", "type": "bool"}], "outputs": [], "gas": 39516}, {"stateMutability": "nonpayable", "type": "function", "name": "mint", "inputs": [{"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [{"name": "", "type": "bool"}], "gas": 82269}, {"stateMutability": "nonpayable", "type": "function", "name": "burn", "inputs": [{"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 99632}]'
    contract_instance = w3.eth.contract(address=address, abi=abi)
    # Both raise error
    balance = contract_instance.functions.balanceOf(human_0).call()
    result = contract_instance.functions.mint(human_address_1, 111).call()

这个函数调用 contract.functions.method.call() 运行合约doc

我遇到了错误

*** web3.exceptions.BadFunctionCallOutput: Could not transact with/call contract function, is contract deployed correctly and chain synced?

问题:
如何使用 web3 python 调用现有方法?

最佳答案

合约调用(.call())和交易(.transact())的完整代码。

我添加了 sleep() 来等待矿工提交我的交易。否则断言将失败

from web3 import Web3, HTTPProvider

human_0 = '0x6A48A50A53462A22D2c30F5138c4970a3020b30a'
human_address_1 = "0x92e320aE601BC8235D605d38D03142C3FE28Ba92"
foggy = '0xa456c84CC005100B277D4637896456F99a59A290'
sarit = '0xB795518Ee574c2a55B513D2C1319e8e6e40F6c04'


def main():
    w3 = Web3(HTTPProvider('http://localhost:8545'))  # web3 must be called locally
    assert True is w3.isConnected()
    print('Network connected')
    address = "0x89829891FBB78bf1142dd58952f8C62CC2222D6B"
    # To get ABI
    # vyper -f abi contracts/ERC721.vy
    abi = '[{"name": "Transfer", "inputs": [{"name": "sender", "type": "address", "indexed": true}, {"name": "receiver", "type": "address", "indexed": true}, {"name": "tokenId", "type": "uint256", "indexed": true}], "anonymous": false, "type": "event"}, {"name": "Approval", "inputs": [{"name": "owner", "type": "address", "indexed": true}, {"name": "approved", "type": "address", "indexed": true}, {"name": "tokenId", "type": "uint256", "indexed": true}], "anonymous": false, "type": "event"}, {"name": "ApprovalForAll", "inputs": [{"name": "owner", "type": "address", "indexed": true}, {"name": "operator", "type": "address", "indexed": true}, {"name": "approved", "type": "bool", "indexed": false}], "anonymous": false, "type": "event"}, {"stateMutability": "nonpayable", "type": "constructor", "inputs": [], "outputs": []}, {"stateMutability": "view", "type": "function", "name": "supportsInterface", "inputs": [{"name": "_interfaceID", "type": "bytes32"}], "outputs": [{"name": "", "type": "bool"}], "gas": 2641}, {"stateMutability": "view", "type": "function", "name": "balanceOf", "inputs": [{"name": "_owner", "type": "address"}], "outputs": [{"name": "", "type": "uint256"}], "gas": 2925}, {"stateMutability": "view", "type": "function", "name": "ownerOf", "inputs": [{"name": "_tokenId", "type": "uint256"}], "outputs": [{"name": "", "type": "address"}], "gas": 2813}, {"stateMutability": "view", "type": "function", "name": "getApproved", "inputs": [{"name": "_tokenId", "type": "uint256"}], "outputs": [{"name": "", "type": "address"}], "gas": 5040}, {"stateMutability": "view", "type": "function", "name": "isApprovedForAll", "inputs": [{"name": "_owner", "type": "address"}, {"name": "_operator", "type": "address"}], "outputs": [{"name": "", "type": "bool"}], "gas": 3190}, {"stateMutability": "nonpayable", "type": "function", "name": "transferFrom", "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 173314}, {"stateMutability": "nonpayable", "type": "function", "name": "safeTransferFrom", "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 190624}, {"stateMutability": "nonpayable", "type": "function", "name": "safeTransferFrom", "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}, {"name": "_data", "type": "bytes"}], "outputs": [], "gas": 190624}, {"stateMutability": "nonpayable", "type": "function", "name": "approve", "inputs": [{"name": "_approved", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 46741}, {"stateMutability": "nonpayable", "type": "function", "name": "setApprovalForAll", "inputs": [{"name": "_operator", "type": "address"}, {"name": "_approved", "type": "bool"}], "outputs": [], "gas": 39516}, {"stateMutability": "nonpayable", "type": "function", "name": "mint", "inputs": [{"name": "_to", "type": "address"}, {"name": "_tokenId", "type": "uint256"}], "outputs": [{"name": "", "type": "bool"}], "gas": 82269}, {"stateMutability": "nonpayable", "type": "function", "name": "burn", "inputs": [{"name": "_tokenId", "type": "uint256"}], "outputs": [], "gas": 99632}]'
    contract_instance = w3.eth.contract(address=address, abi=abi)
    # Both raise error
    human_0_balance = contract_instance.functions.balanceOf(human_0).call({'from': foggy})
    # Change tokenId to avoid error
    token_id = 15
    result = contract_instance.functions.mint(human_0, token_id).transact({'from': human_0})

    import time
    time.sleep(60)
    new_human_0_balance = contract_instance.functions.balanceOf(human_0).call({'from': foggy})
    assert new_human_0_balance == human_0_balance + 1

关于python - *** web3.exceptions.BadFunctionCallOutput : Could not transact with/call contract function, 合约部署正确且链同步了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70002495/

相关文章:

python - 2D循环卷积Vs卷积FFT [Matlab/Octave/Python]

python - 如何使用 microsoft graph api 选择来自特定电子邮件地址的所有电子邮件

python - 分配任务给人们

Python 以太坊区 block 链交易

python - 使用Python-Flask自动创建路由和html文件

python - 在 macOS (Sierra) 上安装 PyQt5(包括 Qt Designer)

python - pip install web3 未安装

python - 如何使用geth在钱包之间转移代币

python-3.x - Python Web3 通过用户名和密码连接 Ankr Binance 智能链 API

python - 导入错误 : No module named solcx