From 1c18726066590b2c799da4c51dfe8b66ac4d2297 Mon Sep 17 00:00:00 2001 From: sangge-redmi <2251250136@qq.com> Date: Mon, 30 Sep 2024 21:51:49 +0800 Subject: [PATCH] update eth --- src/eth_logger.py | 22 +++++++++++++--------- src/eth_logger_test.py | 8 ++++++++ src/logger.sol | 10 ++++++++++ src/node.py | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 src/eth_logger_test.py create mode 100644 src/logger.sol diff --git a/src/eth_logger.py b/src/eth_logger.py index 47d550c..93b75c4 100644 --- a/src/eth_logger.py +++ b/src/eth_logger.py @@ -4,7 +4,8 @@ import json rpc_url = "https://ethereum-holesky-rpc.publicnode.com" chain = Web3(Web3.HTTPProvider(rpc_url)) contract_address = "0x642C23F91bf8339044A00251BC09d1D98110C433" -contract_abi = json.loads('''[ +contract_abi = json.loads( + """[ { "anonymous": false, "inputs": [ @@ -37,23 +38,26 @@ contract_abi = json.loads('''[ "stateMutability": "nonpayable", "type": "function" } - ]''') + ]""" +) contract = chain.eth.contract(address=contract_address, abi=contract_abi) wallet_address = "0xe02666Cb63b3645E7B03C9082a24c4c1D7C9EFf6" pk = "ae66ae3711a69079efd3d3e9b55f599ce7514eb29dfe4f9551404d3f361438c6" def call_eth_logger(wallet_address, pk, message: str): - transaction = contract.functions.logmessage(message).build_transaction({ - 'chainId': 17000, - 'gas': 30000, - 'gasPrice': chain.to_wei('10', 'gwei'), - 'nonce': chain.eth.get_transaction_count(wallet_address, 'pending'), - }) + transaction = contract.functions.logmessage(message).build_transaction( + { + "chainId": 17000, + "gas": 30000, + "gasPrice": chain.to_wei("10", "gwei"), + "nonce": chain.eth.get_transaction_count(wallet_address, "pending"), + } + ) signed_tx = chain.eth.account.sign_transaction(transaction, private_key=pk) tx_hash = chain.eth.send_raw_transaction(signed_tx.raw_transaction) print(tx_hash) receipt = chain.eth.wait_for_transaction_receipt(tx_hash) transfer_event = contract.events.messageLog().process_receipt(receipt) for event in transfer_event: - print(event['args']['msg']) + print(event["args"]["msg"]) diff --git a/src/eth_logger_test.py b/src/eth_logger_test.py new file mode 100644 index 0000000..9cf9d1d --- /dev/null +++ b/src/eth_logger_test.py @@ -0,0 +1,8 @@ +from eth_logger import call_eth_logger + +wallet_address = ( + "0xe02666Cb63b3645E7B03C9082a24c4c1D7C9EFf6" # 修改成要使用的钱包地址/私钥 +) +wallet_pk = "ae66ae3711a69079efd3d3e9b55f599ce7514eb29dfe4f9551404d3f361438c6" + +call_eth_logger(wallet_address, wallet_pk, "hello World") diff --git a/src/logger.sol b/src/logger.sol new file mode 100644 index 0000000..385c23c --- /dev/null +++ b/src/logger.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract logger{ + event messageLog(string msg); + function logmessage(string memory text) public returns (bool){ + emit messageLog(text); + return true; + } +} \ No newline at end of file diff --git a/src/node.py b/src/node.py index 4925e07..e7a2c08 100644 --- a/src/node.py +++ b/src/node.py @@ -186,7 +186,7 @@ wallet_pk = "ae66ae3711a69079efd3d3e9b55f599ce7514eb29dfe4f9551404d3f361438c6" if __name__ == "__main__": - import uvicorn # pylint: disable=e0401 + import uvicorn threading.Thread(target=log_message).start()