Files
txspector-new/extract_sc_addr.py
2024-06-15 11:27:29 +08:00

51 lines
1.9 KiB
Python

"""
Script for Adding sc_addr smart contract to address to the facts folder
To run the script, use the command: `python3 extract_sc_addr.py`
Author: Yangyi Zou
Date: Dec 2023
Usage: python3 extract_sc_addr.py
"""
import os
import requests
def fetch_transaction_details(transaction_hash: str):
url = 'http://localhost:8545'
headers = {'Content-Type': 'application/json'}
params = {
"jsonrpc": "2.0",
"method": "eth_getTransactionByHash",
"params": [transaction_hash],
"id": 1
}
try:
response = requests.post(url, json=params, headers=headers)
response_json = response.json()
if 'result' in response_json and response_json['result']:
return response_json['result']
else:
print(f"No result found for transaction hash: {transaction_hash}")
except requests.exceptions.RequestException as e:
print(f"Request failed for transaction hash: {transaction_hash}, error: {e}")
return None
def extract_to_address_from_facts(folder_path='facts'):
count = 0
for root, dirs, files in os.walk(folder_path):
for subfolder in dirs:
transaction_hash = subfolder.split('_')[-1]
transaction_details = fetch_transaction_details(transaction_hash)
if transaction_details and 'to' in transaction_details:
to_address = transaction_details['to']
file_path = os.path.join(root, subfolder, 'sc_addr.facts')
try:
with open(file_path, 'w') as file:
file.write(to_address)
count += 1
print(f"Processed {count}: {subfolder} -> sc_addr.facts")
except IOError as e:
print(f"Failed to write to {file_path}, error: {e}")
if __name__ == "__main__":
extract_to_address_from_facts()