Python Web3与智能合约的互动

发布于 2025-02-13 07:23:04 字数 1433 浏览 0 评论 0原文

我正在使用web3.py进行智能合同方法。尽管已成功广播,但Etherscan认为它是“ ETH”转移而不是智能合同互动。

预期的方法是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/0xf6626aa3bbfeb67f3798ad1145ec899999995b0b0b9315e8888888cbf9b9b915f915f4610b4610b4610b8872 b8872

https://rinkeby.etherscan.io/tx/0x6b43cebf69bf69b82b82bcbf8cce654a24627688b4fe9ea7d43d43dd74b4b4b4b4b4fa4fa4fa4c1c11c1049a3c7

任何帮助将不胜感激。如果需要更多信息,请告诉我,谢谢!

I'm using web3.py to transact a smart contract method. Though broadcasted successfully, etherscan thinks its an "eth" transfer and not a smart contract interaction.

The method intended is mint().
Please find my code below:

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()}')

It should be like below (Done on etherscan manually):
https://rinkeby.etherscan.io/tx/0xf6626aa3bbfeb67f3798ad1145ec89c8995b0db9315e88cbf9b915f4610b8872

But as of now it's like below (From python):
https://rinkeby.etherscan.io/tx/0x6b43cebf69b82bcbf8cce654a24627688b4fe9ea7d43dd74b4fa4c1c1049a3c7

Am I missing a step before sending the transaction ?
Any help is greatly appreciated. If any more information is required, do let me know, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2025-02-20 07:23:04

您正在使用错误的合同地址。请注意: to:和与(to): eTherscan上的字段进行交互。

您有

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

地址是您的钱包地址。

您应该有

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

Contract_Address ='0xEB74D88306E09A78570795D7467F7467F729FFA786651'在您的情况下。

You are using the wrong address for your contract. Notice the To: and Interacted With (To): fields on etherscan.

You have

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

where address is your wallet address.

You should have

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

where contract_address = '0xeB74D88306e09A78570795d7467F729ffA786651' in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文