Hi stackeoverflow community !
I've an issue using web3.py library.
I'm trying to interact with a deployed smart contract on the Polygon (layer2) network.
Here is my python code :
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
I don't understand because acct.address is a string (my public address from my privatekey) 20 bytes long (without "Ox" as prefix).
There is my solidity code :
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;
}
}
And there is the full error :
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
I'm using the 5.21.0 version of Web3.py.
I will not provide my ABI here because it's a very long json file. But let me know if you need it.
I could try to call the solidity function with a RawTransaction, but i don't know how to pass parameters inside. If you have examples...
If you have any idea, please share and I will try it.
Thanks :)