DragandDropGroup commited on
Commit
deb7a7c
·
verified ·
1 Parent(s): 040dc65

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -11
app.py CHANGED
@@ -3,7 +3,48 @@ import requests
3
  import json
4
  import web3
5
 
6
- abi = [{"anonymous":False,"inputs":[{"indexed":False,"internalType":"string","name":"ipfsHash","type":"string"}],"name":"Store","type":"event"},{"inputs":[],"name":"getHashes","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"ipfsHash","type":"string"}],"name":"storeHash","outputs":[],"stateMutability":"nonpayable","type":"function"}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # function that uploads the results to ipfs
8
  def upload_json_to_ipfs(data):
9
  try:
@@ -48,7 +89,8 @@ def upload_to_blockchain(ipfs_hash):
48
  contract_address = st.secrets["ContractAddress"]
49
  checksum_address = w3.to_checksum_address(contract_address)
50
  except Exception as e:
51
- raise ValueError(f"Invalid contract address: {contract_address}\nError: {e}")
 
52
 
53
  try:
54
  assert isinstance(abi, list), "ABI must be a list"
@@ -57,11 +99,6 @@ def upload_to_blockchain(ipfs_hash):
57
 
58
  contract = w3.eth.contract(address=checksum_address, abi=abi)
59
 
60
- print("Sending from:", st.secrets["EthWallet"])
61
- print("To contract:", checksum_address)
62
- print("Function input (IPFS hash):", ipfs_hash)
63
- print("Nonce:", w3.eth.get_transaction_count(st.secrets["EthWallet"]))
64
-
65
  # Build transaction
66
  call_function = contract.functions.storeHash(ipfs_hash).build_transaction({
67
  "chainId": 11155111,
@@ -85,7 +122,6 @@ def upload_to_blockchain(ipfs_hash):
85
 
86
  return tx_receipt.transactionHash.hex()
87
 
88
-
89
  def get_ipfs_hashes():
90
  w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
91
 
@@ -124,6 +160,7 @@ def submission(survey_data):
124
  print("IPFS Upload Successful")
125
  print(ipfs_hash)
126
  upload_to_blockchain(ipfs_hash)
 
127
  total_number_pages = 4
128
  placeholder_buttons = None
129
 
@@ -167,7 +204,7 @@ if "current_page" not in st.session_state:
167
  # Page 1; Video
168
  if st.session_state["current_page"] == 1:
169
 
170
- st.markdown("""<p class="big-font">This is a test survey</p>""", unsafe_allow_html=True)
171
 
172
  st.radio(label = "What is your favorite food?",
173
  options = Q1_radio_options,
@@ -297,8 +334,8 @@ elif st.session_state["current_page"] == 4: # Last Page
297
  response = {
298
  "Q1": Q1_radio_options[st.session_state["Q1"]],
299
  "Q2": st.session_state["Q2"],
300
- "Q3": st.session_state["Q3"],
301
- "Q4": st.session_state["Q4"],
302
  "Q5": st.session_state["Q5"],
303
  }
304
  submission(response)
@@ -306,6 +343,8 @@ elif st.session_state["current_page"] == 4: # Last Page
306
  print(e)
307
  st.error(f'An error ocurred. Here is the error message: {e}', icon="🚨")
308
 
 
 
309
  if st.button('Back'):
310
  st.session_state["current_page"] -= 1
311
  st.rerun()
 
3
  import json
4
  import web3
5
 
6
+ abi = [
7
+ {
8
+ "anonymous": False,
9
+ "inputs": [
10
+ {
11
+ "indexed": False,
12
+ "internalType": "string",
13
+ "name": "ipfsHash",
14
+ "type": "string"
15
+ }
16
+ ],
17
+ "name": "Store",
18
+ "type": "event"
19
+ },
20
+ {
21
+ "inputs": [],
22
+ "name": "getHashes",
23
+ "outputs": [
24
+ {
25
+ "internalType": "string[]",
26
+ "name": "",
27
+ "type": "string[]"
28
+ }
29
+ ],
30
+ "stateMutability": "view",
31
+ "type": "function"
32
+ },
33
+ {
34
+ "inputs": [
35
+ {
36
+ "internalType": "string",
37
+ "name": "ipfsHash",
38
+ "type": "string"
39
+ }
40
+ ],
41
+ "name": "storeHash",
42
+ "outputs": [],
43
+ "stateMutability": "nonpayable",
44
+ "type": "function"
45
+ }
46
+ ]
47
+
48
  # function that uploads the results to ipfs
49
  def upload_json_to_ipfs(data):
50
  try:
 
89
  contract_address = st.secrets["ContractAddress"]
90
  checksum_address = w3.to_checksum_address(contract_address)
91
  except Exception as e:
92
+ raise ValueError(f"Invalid contract address: {contract_address}
93
+ Error: {e}")
94
 
95
  try:
96
  assert isinstance(abi, list), "ABI must be a list"
 
99
 
100
  contract = w3.eth.contract(address=checksum_address, abi=abi)
101
 
 
 
 
 
 
102
  # Build transaction
103
  call_function = contract.functions.storeHash(ipfs_hash).build_transaction({
104
  "chainId": 11155111,
 
122
 
123
  return tx_receipt.transactionHash.hex()
124
 
 
125
  def get_ipfs_hashes():
126
  w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
127
 
 
160
  print("IPFS Upload Successful")
161
  print(ipfs_hash)
162
  upload_to_blockchain(ipfs_hash)
163
+
164
  total_number_pages = 4
165
  placeholder_buttons = None
166
 
 
204
  # Page 1; Video
205
  if st.session_state["current_page"] == 1:
206
 
207
+ st.markdown("""<p style='font-size:18px ;'></p>""", unsafe_allow_html=True)
208
 
209
  st.radio(label = "What is your favorite food?",
210
  options = Q1_radio_options,
 
334
  response = {
335
  "Q1": Q1_radio_options[st.session_state["Q1"]],
336
  "Q2": st.session_state["Q2"],
337
+ "Q3": Q3_radio_options[st.session_state["Q3"]],
338
+ "Q4": Q4_radio_options[st.session_state["Q4"]],
339
  "Q5": st.session_state["Q5"],
340
  }
341
  submission(response)
 
343
  print(e)
344
  st.error(f'An error ocurred. Here is the error message: {e}', icon="🚨")
345
 
346
+ print("Success!")
347
+
348
  if st.button('Back'):
349
  st.session_state["current_page"] -= 1
350
  st.rerun()