| import streamlit as st |
| import web3 |
| import requests |
| import pandas |
|
|
| |
| w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"])) |
|
|
| |
| ABI = '[ { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "Store", "type": "event" }, { "inputs": [ { "internalType": "string", "name": "_IPFSHash", "type": "string" } ], "name": "storeHash", "outputs": [], "stateMutability": "nonpayable", "type": "function" } ]' |
|
|
| |
| st.set_page_config(page_title="Survey Data Retriever") |
| st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True) |
|
|
| |
| st.title('Survey Data Retriever') |
| st.write("# ") |
|
|
| |
| contract_address = st.text_input(r"$\textsf{\Large Smart Contract Address}$") |
|
|
| |
| button = st.button("Retrieve Survey Responses") |
|
|
| |
| if button: |
| my_bar = st.progress(0, text="Operation in progress. Please wait.") |
| df = pandas.DataFrame() |
|
|
| try: |
| contract = w3.eth.contract(address=w3.to_checksum_address(contract_address), abi=ABI) |
| logs = contract.events.Store.get_logs(from_block=4635673) |
| except Exception as e: |
| st.error("Error retrieving logs from the contract.") |
| st.stop() |
|
|
| st.write(r"$\textsf{\normalsize \textbf{IPFS Hashes}}$") |
|
|
| for i, log in enumerate(logs): |
| my_bar.progress((i+1)/len(logs), text="Retrieving responses...") |
|
|
| ipfs_hash = log.args.data |
| st.write(f"**Hash**: {ipfs_hash}; **Block number**: {log.blockNumber}") |
|
|
| |
| response = requests.get( |
| f"https://gateway.pinata.cloud/ipfs/{ipfs_hash}", |
| headers={"Authorization": f"Bearer {st.secrets['pinata_jwt']}"} |
| ) |
|
|
| if response.status_code == 200: |
| try: |
| json_to_row = pandas.json_normalize(response.json()) |
| df = pandas.concat([df, json_to_row], ignore_index=True) |
| except Exception as e: |
| st.error(f"Invalid JSON for hash: {ipfs_hash}") |
| st.text(f"Error: {e}\nRaw content:\n{response.text}") |
| else: |
| st.error(f"Failed to retrieve data from IPFS for hash: {ipfs_hash}") |
| st.text(f"Status Code: {response.status_code}\nContent: {response.text}") |
|
|
| st.write("# ") |
| st.write(r"$\textsf{\normalsize \textbf{Response Data}}$") |
| st.write(df) |
|
|
| st.download_button("Press to Download", |
| df.to_csv(index=False).encode('utf-8'), |
| "responses.csv", |
| "text/csv", |
| key='download-csv') |
|
|
| my_bar.empty() |
|
|