12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- from web3 import Web3
- import json
- import ipfshttpclient
- import pprint
- from web3.gas_strategies.time_based import medium_gas_price_strategy, fast_gas_price_strategy
- #infura_url_mainnet = "https://mainnet.infura.io/v3/88df052c847f4d80b839a6cdd00a515c"
- infura_kovan = "https://kovan.infura.io/v3/88df052c847f4d80b839a6cdd00a515c"
- # ganache_url = "http://127.0.0.1:7545"
- print("Connecting to IPFS...")
- ipfs_uri = "/dns/ipfs.infura.io/tcp/5001/https"
- client = ipfshttpclient.connect(ipfs_uri)
- result = client.add('Smart-Contract/Padra.txt')
- #print(result['Hash'])
- #print(type(result))
- result_hash = str(result['Hash'])
- print(f"Response saved on IPFS with this hash: {result_hash}")
- print("Connecting to Ethereum Test Network...")
- web3 = Web3(Web3.HTTPProvider(infura_kovan))
- # web3.eth.defaultAccount = web3.eth.accounts[0] #local
- web3.eth.defaultAccount = "0x92582427Bc16dEE757a20265F34460E13Fb05409"
- private_key = "c41170d8d93fb438f3a910d4a079d5e2140b55958c8f136b816c29280b2ed43d"
- print(f"Connected to Blockchain Test Network? {web3.isConnected()}")
- balance = web3.fromWei(web3.eth.getBalance(web3.eth.defaultAccount), 'gwei')
- #print(web3.eth.blockNumber)
- #print(web3.eth.getTransactionCount(web3.eth.defaultAccount))
- # sm_address_local = web3.toChecksumAddress("0x35341508Df8D36806171e4c62E2Df30004C8C774")
- sm_address = web3.toChecksumAddress("0x77161194E4d4B5af4Ec330CcE2f4F4A98a068d60")
- 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"}]')
- contract = web3.eth.contract(address = sm_address, abi= sm_abi)
- #print(contract)
- # local env
- # tx_hash = contract.functions.store(result_hash).transact()
- # print (tx_hash)
- # print(f"Access data from ipfs after writing to smart contract: https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}")
- # tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
- # print(tx_receipt)
- # Ethernet testnet
- gas_estimate = contract.functions.store(result_hash).estimateGas()
- print(f'Gas estimate to transact with store function: {gas_estimate}')
- web3.eth.setGasPriceStrategy(fast_gas_price_strategy)
- gasprice = web3.fromWei(web3.eth.generateGasPrice(), 'gwei')
- transaction_cost = gasprice * gas_estimate
- gas_limit = gas_estimate + (gas_estimate * 0.1)
- print(f'Transaction cost estimate: {transaction_cost}')
- if (balance > transaction_cost):
- print("Enough balance, processing...")
- print("Sending transaction to smart contract\n")
- transaction = contract.functions.store(result_hash).buildTransaction()
- transaction.update({ 'gas' : gas_estimate })
- transaction.update({ 'nonce' : web3.eth.getTransactionCount(web3.eth.defaultAccount) })
- signed_tx = web3.eth.account.signTransaction(transaction, private_key)
- #print(signed_tx)
- txn_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
- txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
- print("Transaction receipt mined:")
- pprint.pprint(dict(txn_receipt))
- if (txn_receipt["status"] == 1):
- print("Transaction was successful?")
- print(f"Access data from ipfs after writing to smart contract: https://ipfs.infura.io/ipfs/{contract.functions.retrieve().call()}")
- else:
- print(f"transaction was not successful")
- else:
- print("Balance is not sufficent")
|