import streamlit as st import web3 import requests import pandas # Connect to the Sepolia Ethereum blockchain w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"])) # Variables 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" } ]' # Changing the App title st.set_page_config(page_title="Survey Data Retriever",) # The following code centralizes all the buttons st.markdown("", unsafe_allow_html=True) # Title st.title('Survey Data Retriever') st.write("# ") # Text field contract_address = st.text_input(r"$\textsf{\Large Smart Contract Address}$", '0x42b76d8c32f914630627bf924bd1e06055673cf8') # Button button = st.button("Retrieve Survey Responses") # Button is pressed if button: # Starts the progression bar my_bar = st.progress(0, text= "Operation in progress. Please wait.") # data frame with the data df = pandas.DataFrame() # Getting logs from ETH contract contract = w3.eth.contract( address = contract_address, abi = ABI) logs = contract.events.Store.get_logs(fromBlock= 4635673) # Writing a label st.write(r"$\textsf{\normalsize \textbf{IPFS Hashes}}$") for i, log in enumerate(logs): # Adjusting progression bard my_bar.progress((i+1)/len(logs), text= "Operation in progress. Please wait.") # Writing the hashes st.write(f"**Hash**: {log.args.data}; **Block number**: {log.blockNumber}") # Requesting data from IPFS using INFURA API params = (('arg', log.args.data),) response = requests.post('https://ipfs.infura.io:5001/api/v0/cat', params=params, auth=(st.secrets["username"], st.secrets["password"])) # create a row for the values associated with the 'data' key json_to_row = pandas.json_normalize(response.json()) # append data to empty data frame df = pandas.concat([df,json_to_row], ignore_index=True) # Printing df st.write("# ") st.write(r" $\textsf{\normalsize \textbf{Response Data}}$") st.write(df) # Download button st.download_button("Press to Download", df.to_csv(index=False).encode('utf-8'), "responses.csv", "text/csv", key='download-csv') # Clearing progression bar my_bar.empty()