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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -17
app.py CHANGED
@@ -37,39 +37,54 @@ def upload_json_to_ipfs(data):
37
  st.write(f"Error uploading to Pinata: {e}")
38
 
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
  print("Sending from:", st.secrets["EthWallet"])
48
- print("To contract:", st.secrets["ContractAddress"])
49
- print("Function input (IPFS hash):", hash)
50
  print("Nonce:", w3.eth.get_transaction_count(st.secrets["EthWallet"]))
51
 
52
- # Call your function: 11155111 is Sepolia's id
53
- call_function = contract.functions.storeHash(hash).build_transaction({"chainId": 11155111,
54
- "from": st.secrets["EthWallet"],
55
- "nonce": w3.eth.get_transaction_count(st.secrets["EthWallet"])})
 
 
 
 
56
 
57
  # Sign transaction
58
  signed_tx = w3.eth.account.sign_transaction(call_function, private_key=st.secrets["pk"])
59
 
60
-
61
-
62
-
63
  # Send transaction
64
- send_tx = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
65
 
66
  # Wait for transaction receipt
67
- tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
 
 
 
68
 
69
- print("ETH Hash:")
70
- print(tx_receipt.logs[0].transactionHash.hex())
71
 
72
- return tx_receipt.logs[0].transactionHash.hex()
73
 
74
  def get_ipfs_hashes():
75
  w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
 
37
  st.write(f"Error uploading to Pinata: {e}")
38
 
39
  # function that uploads to blockchain
40
+ def upload_to_blockchain(ipfs_hash):
41
  print("starting blockchain upload")
42
+
43
  w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
44
+ if not w3.is_connected():
45
+ raise RuntimeError("Failed to connect to Ethereum network")
46
+
47
+ try:
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"
55
+ except Exception as e:
56
+ raise ValueError(f"ABI format error: {e}")
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,
68
+ "from": st.secrets["EthWallet"],
69
+ "nonce": w3.eth.get_transaction_count(st.secrets["EthWallet"]),
70
+ "gas": 300000,
71
+ "gasPrice": w3.to_wei("10", "gwei")
72
+ })
73
 
74
  # Sign transaction
75
  signed_tx = w3.eth.account.sign_transaction(call_function, private_key=st.secrets["pk"])
76
 
 
 
 
77
  # Send transaction
78
+ tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
79
 
80
  # Wait for transaction receipt
81
+ tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
82
+
83
+ print("Transaction successful!")
84
+ print("ETH Tx Hash:", tx_receipt.transactionHash.hex())
85
 
86
+ return tx_receipt.transactionHash.hex()
 
87
 
 
88
 
89
  def get_ipfs_hashes():
90
  w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))