File size: 2,756 Bytes
582c2f7
 
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
582c2f7
 
38f2380
582c2f7
 
38f2380
 
 
582c2f7
 
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
 
582c2f7
38f2380
 
582c2f7
38f2380
582c2f7
38f2380
 
582c2f7
38f2380
 
582c2f7
38f2380
 
 
 
 
 
 
 
582c2f7
38f2380
 
582c2f7
38f2380
 
 
 
582c2f7
38f2380
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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("<style>.row-widget.stButton {text-align: center;}</style>", 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 = w3.to_checksum_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()