script.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #infura_url_mainnet = "https://mainnet.infura.io/v3/88df052c847f4d80b839a6cdd00a515c"
  7. infura_kovan = "https://kovan.infura.io/v3/88df052c847f4d80b839a6cdd00a515c"
  8. # ganache_url = "http://127.0.0.1:7545"
  9. print("Connecting to IPFS...")
  10. ipfs_uri = "/dns/ipfs.infura.io/tcp/5001/https"
  11. client = ipfshttpclient.connect(ipfs_uri)
  12. result = client.add('Smart-Contract/Padra.txt')
  13. #print(result['Hash'])
  14. #print(type(result))
  15. result_hash = str(result['Hash'])
  16. print(f"Response saved on IPFS with this hash: {result_hash}")
  17. print("Connecting to Ethereum Test Network...")
  18. web3 = Web3(Web3.HTTPProvider(infura_kovan))
  19. # web3.eth.defaultAccount = web3.eth.accounts[0] #local
  20. web3.eth.defaultAccount = "0x92582427Bc16dEE757a20265F34460E13Fb05409"
  21. private_key = "c41170d8d93fb438f3a910d4a079d5e2140b55958c8f136b816c29280b2ed43d"
  22. print(f"Connected to Blockchain Test Network? {web3.isConnected()}")
  23. balance = web3.fromWei(web3.eth.getBalance(web3.eth.defaultAccount), 'gwei')
  24. #print(web3.eth.blockNumber)
  25. #print(web3.eth.getTransactionCount(web3.eth.defaultAccount))
  26. # sm_address_local = web3.toChecksumAddress("0x35341508Df8D36806171e4c62E2Df30004C8C774")
  27. sm_address = web3.toChecksumAddress("0x77161194E4d4B5af4Ec330CcE2f4F4A98a068d60")
  28. 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"}]')
  29. contract = web3.eth.contract(address = sm_address, abi= sm_abi)
  30. #print(contract)
  31. # local env
  32. # tx_hash = contract.functions.store(result_hash).transact()
  33. # print (tx_hash)
  34. # print(f"Access data from ipfs after writing to smart contract: https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}")
  35. # tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
  36. # print(tx_receipt)
  37. # Ethernet testnet
  38. gas_estimate = contract.functions.store(result_hash).estimateGas()
  39. print(f'Gas estimate to transact with store function: {gas_estimate}')
  40. web3.eth.setGasPriceStrategy(fast_gas_price_strategy)
  41. gasprice = web3.fromWei(web3.eth.generateGasPrice(), 'gwei')
  42. transaction_cost = gasprice * gas_estimate
  43. gas_limit = gas_estimate + (gas_estimate * 0.1)
  44. print(f'Transaction cost estimate: {transaction_cost}')
  45. if (balance > transaction_cost):
  46. print("Enough balance, processing...")
  47. print("Sending transaction to smart contract\n")
  48. transaction = contract.functions.store(result_hash).buildTransaction()
  49. transaction.update({ 'gas' : gas_estimate })
  50. transaction.update({ 'nonce' : web3.eth.getTransactionCount(web3.eth.defaultAccount) })
  51. signed_tx = web3.eth.account.signTransaction(transaction, private_key)
  52. #print(signed_tx)
  53. txn_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
  54. txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
  55. print("Transaction receipt mined:")
  56. pprint.pprint(dict(txn_receipt))
  57. if (txn_receipt["status"] == 1):
  58. print("Transaction was successful?")
  59. print(f"Access data from ipfs after writing to smart contract: https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}")
  60. else:
  61. print(f"transaction was not successful")
  62. else:
  63. print("Balance is not sufficent")