DragandDropGroup commited on
Commit
ad5b707
·
verified ·
1 Parent(s): 1942526

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +15 -8
src/streamlit_app.py CHANGED
@@ -2,6 +2,13 @@ import streamlit as st
2
  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
@@ -16,7 +23,7 @@ def upload_json_to_ipfs(data):
16
  st.write("No JWT secret found, please add your JWT in a secret titled \"PinataJWT\"")
17
  return
18
 
19
- jwt_token = st.secrets["PinataJWT"].strip()
20
  headers = {
21
  "Authorization": f"Bearer {jwt_token}",
22
  "Content-Type": "application/json"
@@ -39,18 +46,18 @@ def upload_json_to_ipfs(data):
39
  # function that uploads to blockchain
40
  def upload_to_blockchain(hash):
41
  print("starting blockchain upload")
42
- w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
43
 
44
  # create an instance of our contract
45
- contract = w3.eth.contract(address=st.secrets["ContractAddress"], abi = abi)
46
 
47
  # Call your function: 11155111 is Sepolia's id
48
  call_function = contract.functions.storeHash(hash).build_transaction({"chainId": 11155111,
49
- "from": st.secrets["EthWallet"],
50
- "nonce": w3.eth.get_transaction_count(st.secrets["EthWallet"])})
51
 
52
  # Sign transaction
53
- signed_tx = w3.eth.account.sign_transaction(call_function, private_key=st.secrets["pk"])
54
 
55
  # Send transaction
56
  send_tx = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
@@ -64,10 +71,10 @@ def upload_to_blockchain(hash):
64
  return tx_receipt.logs[0].transactionHash.hex()
65
 
66
  def get_ipfs_hashes():
67
- w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
68
 
69
  # Create an instance of the contract
70
- contract = w3.eth.contract(address=st.secrets["ContractAddress"], abi=abi)
71
 
72
  # Call the getHashes function
73
  try:
 
2
  import requests
3
  import json
4
  import web3
5
+ import os
6
+
7
+ PINATA_JWT = os.environ.get("PinataJWT")
8
+ INFURA_URL = os.environ.get("infura")
9
+ CONTRACT_ADDRESS = os.environ.get("ContractAddress")
10
+ ETH_WALLET = os.environ.get("EthWallet")
11
+ PRIVATE_KEY = os.environ.get("pk")
12
 
13
  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"}]
14
  # function that uploads the results to ipfs
 
23
  st.write("No JWT secret found, please add your JWT in a secret titled \"PinataJWT\"")
24
  return
25
 
26
+ jwt_token = PINATA_JWT.strip()
27
  headers = {
28
  "Authorization": f"Bearer {jwt_token}",
29
  "Content-Type": "application/json"
 
46
  # function that uploads to blockchain
47
  def upload_to_blockchain(hash):
48
  print("starting blockchain upload")
49
+ w3 = web3.Web3(web3.HTTPProvider(INFURA_URL))
50
 
51
  # create an instance of our contract
52
+ contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi = abi)
53
 
54
  # Call your function: 11155111 is Sepolia's id
55
  call_function = contract.functions.storeHash(hash).build_transaction({"chainId": 11155111,
56
+ "from": ETH_WALLET,
57
+ "nonce": w3.eth.get_transaction_count(ETH_WALLET)})
58
 
59
  # Sign transaction
60
+ signed_tx = w3.eth.account.sign_transaction(call_function, private_key=PRIVATE_KEY)
61
 
62
  # Send transaction
63
  send_tx = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
 
71
  return tx_receipt.logs[0].transactionHash.hex()
72
 
73
  def get_ipfs_hashes():
74
+ w3 = web3.Web3(web3.HTTPProvider(INFURA_URL))
75
 
76
  # Create an instance of the contract
77
+ contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=abi)
78
 
79
  # Call the getHashes function
80
  try: