Browse Source

Added files

Mo Masoumi 3 years ago
parent
commit
4dae23d938

BIN
.DS_Store


+ 3 - 0
.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "python.pythonPath": "/Users/me/opt/anaconda3/envs/padraV2/bin/python"
+}

+ 1 - 0
Smart-Contract/Padra.txt

@@ -0,0 +1 @@
+Hello From Mo and Padra :

+ 5 - 0
Smart-Contract/requirements.txt

@@ -0,0 +1,5 @@
+web3==5.12.1
+infura==0.2.1
+# infura user: zzj58582@cuoly.com
+# infura pass: qwertz123456
+ipfs-api ==0.2.3

+ 76 - 0
Smart-Contract/script.py

@@ -0,0 +1,76 @@
+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")

+ 26 - 0
Smart-Contract/storage.sol

@@ -0,0 +1,26 @@
+pragma solidity >=0.4.22 <0.7.0; //compiled with 0.4.22
+
+/**
+ * @title Storage
+ * @dev Store & retrieve value in a variable
+ */
+contract Storage {
+
+    string hash_url;
+
+    /**
+     * @dev Store value in variable
+     * @param hash value to store
+     */
+    function store(string hash) public {
+        hash_url = hash;
+    }
+
+    /**
+     * @dev Return value 
+     * @return value of 'hash_url'
+     */
+    function retrieve() public view returns (string){
+        return hash_url;
+    }
+}

+ 40 - 0
Smart-Contract/test.py

@@ -0,0 +1,40 @@
+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 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), 'ether')
+balance2 = web3.fromWei(web3.eth.getBalance(web3.eth.defaultAccount), 'gwei')
+print(balance)
+print(balance2)
+# web3.eth.setGasPriceStrategy(fast_gas_price_strategy)
+# gasprice = web3.eth.generateGasPrice()
+# gasprice2 = web3.fromWei(gasprice, 'gwei')
+# print(" gas price in wei", gasprice)
+# print(" gas price in gwei", gasprice2)
+
+gas_estimate = 25000
+gas_limit = gas_estimate + (gas_estimate * 0.1)
+print (gas_limit)
+# print(f'Gas estimate to transact with store function: {gas_estimate}')
+# transaction_cost = gasprice2 * gas_estimate
+# print(f'Transaction cost estimate: {transaction_cost} gwei')
+# if (balance2 > transaction_cost):
+#     print("transaction can be done")
+# else:
+#     print("not enough balance")