script.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from web3 import Web3
  2. import json
  3. import ipfshttpclient
  4. import pprint
  5. from web3.gas_strategies.time_based import medium_gas_price_strategy, fast_gas_price_strategy
  6. from Utils import *
  7. def main(file_path):
  8. #infura_url_mainnet = "https://mainnet.infura.io/v3/88df052c847f4d80b839a6cdd00a515c"
  9. infura_kovan = "https://kovan.infura.io/v3/88df052c847f4d80b839a6cdd00a515c"
  10. # ganache_url = "http://127.0.0.1:7545"
  11. simplePrint("Connecting "," IPFS...")
  12. ipfs_uri = "/dns/ipfs.infura.io/tcp/5001/https"
  13. client = ipfshttpclient.connect(ipfs_uri)
  14. result = client.add(file_path)
  15. #print(result['Hash'])
  16. #print(type(result))
  17. result_hash = str(result['Hash'])
  18. simplePrint("Response saved on IPFS with this hash", result_hash)
  19. simplePrint("Connecting "," Ethereum Test Network...")
  20. web3 = Web3(Web3.HTTPProvider(infura_kovan))
  21. # web3.eth.defaultAccount = web3.eth.accounts[0] #local
  22. web3.eth.defaultAccount = "0x92582427Bc16dEE757a20265F34460E13Fb05409"
  23. private_key = "c41170d8d93fb438f3a910d4a079d5e2140b55958c8f136b816c29280b2ed43d"
  24. bPrint("Connected to Blockchain Test Network?", web3.isConnected())
  25. balance = web3.fromWei(web3.eth.getBalance(web3.eth.defaultAccount), 'gwei')
  26. #print(web3.eth.blockNumber)
  27. #print(web3.eth.getTransactionCount(web3.eth.defaultAccount))
  28. # sm_address_local = web3.toChecksumAddress("0x35341508Df8D36806171e4c62E2Df30004C8C774")
  29. sm_address = web3.toChecksumAddress("0x77161194E4d4B5af4Ec330CcE2f4F4A98a068d60")
  30. sm_abi = json.loads('[{"constant":false,"inputs":[{"name":"hash","type":"string"}],"name":"store","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"retrieve","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]')
  31. contract = web3.eth.contract(address = sm_address, abi= sm_abi)
  32. #print(contract)
  33. # local env
  34. # tx_hash = contract.functions.store(result_hash).transact()
  35. # print (tx_hash)
  36. # print(f"Access data from ipfs after writing to smart contract: https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}")
  37. # tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
  38. # print(tx_receipt)
  39. # Ethernet testnet
  40. gas_estimate = contract.functions.store(result_hash).estimateGas()
  41. bPrint('Gas estimate to transact with store function',gas_estimate)
  42. web3.eth.setGasPriceStrategy(fast_gas_price_strategy)
  43. gasprice = web3.fromWei(web3.eth.generateGasPrice(), 'gwei')
  44. transaction_cost = gasprice * gas_estimate
  45. gas_limit = gas_estimate + (gas_estimate * 0.1)
  46. bBackPrint('Transaction cost estimate',transaction_cost)
  47. if (balance > transaction_cost):
  48. gBackPrint("Enough balance"," processing...")
  49. gPrint("Sending transaction"," to smart contract")
  50. transaction = contract.functions.store(result_hash).buildTransaction()
  51. transaction.update({ 'gas' : gas_estimate })
  52. transaction.update({ 'nonce' : web3.eth.getTransactionCount(web3.eth.defaultAccount) })
  53. signed_tx = web3.eth.account.signTransaction(transaction, private_key)
  54. #print(signed_tx)
  55. txn_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
  56. txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
  57. gBackPrint("Transaction receipt mined:","")
  58. pprint.pprint(dict(txn_receipt))
  59. if (txn_receipt["status"] == 1):
  60. gBackPrint("","Transaction was successful?")
  61. gBackPrint("Access data from ipfs after writing to smart contract: ",f"https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}")
  62. else:
  63. rBackPrint(f"transaction was not successful")
  64. return None
  65. return f"https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}"
  66. else:
  67. yBackPrint("Balance is not sufficent")
  68. return None