Web3.py TypeError:必须以字符串形式提供地址<object>

2024-06-10 04:29:53 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,stackeoverflow社区

我在使用web3.py库时遇到问题。 我正在尝试与多边形(layer2)网络上部署的智能合约进行交互

以下是我的python代码:

def mintNFT(metaCID):
    polygon_url = "https://rpc-mainnet.maticvigil.com"
    web3 = Web3(Web3.HTTPProvider(polygon_url))
    print ("BlockchainIsConnected :",web3.isConnected())

    my_account = "MY_PRIVATE_KEY"
    
    acct = web3.eth.account.from_key(my_account)
    web3.eth.defaultAccount = acct
    
    print (type(acct.address), type(acct.key)) #Returns str and str

    abi_file = open('/Users/aa/Desktop/minty/data/abi/Minty.json','r')
    abi = json.load(abi_file)
    minty_contract_address = web3.toChecksumAddress("CONTRACT_ADDRESS")
    contract = web3.eth.contract(address=minty_contract_address,abi=abi)
    tx_hash = contract.functions.mintToken(acct.address,metaCID).transact()
    print ("tx_hash =",web3.toHex(tx_hash))
    web3.eth.waitForTransactionReceipt(tx_hash) #Waiting for transaction to be mined

我不明白,因为acct.address是一个20字节长的字符串(我的私钥中的公共地址)(没有“Ox”作为前缀)。

这是我的可靠度代码:

pragma solidity ^0.7.0;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract Minty is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("TOKENNAME", "SYMBOL") {}

    function mintToken(address owner, string memory metadataURI)
    public
    returns (uint256)
    {
        _tokenIds.increment();

        uint256 id = _tokenIds.current();
        _safeMint(owner, id);
        _setTokenURI(id, metadataURI);

        return id;
    }
}

还有一个完全的错误:

Traceback (most recent call last):
  File "/Users/aa/Desktop/minty/scripts/test.py", line 80, in <module>
    mintNFT(metaCID)
  File "/Users/aa/Desktop/minty/scripts/test.py", line 76, in mintNFT
    tx_hash = contract.functions.mintToken(acct.address,metaCID).transact()
  File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 997, in transact
    return transact_with_contract_function(
  File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 1590, in transact_with_contract_function
    txn_hash = web3.eth.send_transaction(transact_transaction)
  File "/usr/local/lib/python3.9/site-packages/web3/eth.py", line 577, in send_transaction
    return self._send_transaction(transaction)
  File "/usr/local/lib/python3.9/site-packages/web3/module.py", line 53, in caller
    (method_str, params), response_formatters = method.process_params(module, *args, **kwargs)  # noqa: E501
  File "/usr/local/lib/python3.9/site-packages/web3/method.py", line 201, in process_params
    _apply_request_formatters(params, self.request_formatters(method)))
  File "/usr/local/lib/python3.9/site-packages/eth_utils/functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
  File "/usr/local/lib/python3.9/site-packages/web3/method.py", line 51, in _apply_request_formatters
    formatted_params = pipe(params, request_formatters)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
  File "cytoolz/functoolz.pyx", line 505, in cytoolz.functoolz.Compose.__call__
  File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "/usr/local/lib/python3.9/site-packages/eth_utils/decorators.py", line 91, in wrapper
    return ReturnType(result)  # type: ignore
  File "/usr/local/lib/python3.9/site-packages/eth_utils/applicators.py", line 22, in apply_formatter_at_index
    yield formatter(item)
  File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/rpc_abi.py", line 212, in apply_abi_formatters_to_dict
    formatted_values = map_abi_data(
  File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/abi.py", line 799, in map_abi_data
    return pipe(data, *pipeline)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
  File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/abi.py", line 833, in data_tree_map
    return recursive_map(map_to_typed_data, data_tree)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
    wrapped_val = to_wrap(*args)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 89, in recursive_map
    items_mapped = map_collection(recurse, data)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 76, in map_collection
    return datatype(map(func, collection))
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 88, in recurse
    return recursive_map(func, item)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
    wrapped_val = to_wrap(*args)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 90, in recursive_map
    return func(items_mapped)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/abi.py", line 830, in map_to_typed_data
    return ABITypedData(func(*elements))
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/normalizers.py", line 78, in wrapper
    modified = to_wrap(type_str, data)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/normalizers.py", line 196, in abi_address_to_hex
    validate_address(data)
  File "/usr/local/lib/python3.9/site-packages/web3/_utils/validation.py", line 177, in validate_address
    raise TypeError('Address {} must be provided as a string'.format(value))
TypeError: Address <eth_account.signers.local.LocalAccount object at 0x105ddb7c0> must be provided as a string

我使用的是Web3.py的5.21.0版本

我不会在这里提供我的ABI,因为它是一个很长的json文件。但是如果你需要,请告诉我

我可以尝试用RawTransaction调用solidity函数,但我不知道如何在内部传递参数。如果你有例子

如果你有任何想法,请分享,我会试试。 谢谢:)


Tags: inpylibpackagesusrlocallinesite