python web3与智能合约交互



我正在使用web3.py进行智能合约交易方法。虽然成功地广播了,但以太坊认为它是一个"以太"。合同转让和不是一个聪明的互动。

使用的方法是mint()。

infura_url = "https://rinkeby.infura.io/v3/<Infura code here>"
w3 = Web3(Web3.HTTPProvider(infura_url))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
address = w3.toChecksumAddress(<My wallet address>)
abi = json.loads(<ABI here>)
contract=w3.eth.contract(address=address, abi=abi)
mint_tx = contract.functions.mint(address,1).buildTransaction()
mint_tx['nonce'] = w3.eth.getTransactionCount(address)
signed_txn = w3.eth.account.sign_transaction(mint_tx, private_key=os.environ['private_key'])
txhash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f'Transaction hash: {txhash.hex()}')

应该如下所示(在etherscan上手动完成):
https://rinkeby.etherscan.io/tx/0xf6626aa3bbfeb67f3798ad1145ec89c8995b0db9315e88cbf9b915f4610b8872

但是现在它就像下面(来自python):
https://rinkeby.etherscan.io/tx/0x6b43cebf69b82bcbf8cce654a24627688b4fe9ea7d43dd74b4fa4c1c1049a3c7

在发送交易之前我是否错过了一个步骤?
任何帮助都是非常感谢的。如果需要更多的信息,请告诉我,谢谢!

你的合同地址用错了。注意etherscan上的To:Interacted With (To):字段。

你有

contract = w3.eth.contract(address=address, abi=abi)

其中address是您的钱包地址。

你应该有

contract = w3.eth.contract(address=contract_address, abi=abi)

wherecontract_address = '0xeB74D88306e09A78570795d7467F729ffA786651'in your case

最新更新