-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
51 lines (38 loc) · 1.47 KB
/
product.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import time
from web3 import Web3, HTTPProvider,TestRPCProvider
from solc import compile_source
from web3.contract import ConciseContract
# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.0;
contract History {
string public name;
string public time;
string public product;
function setLog(string a, string b, string c) public constant returns(string, string,string ) {
name = a;
time = b;
product = c;
}
function getLog() constant returns (string, string, string) {
return (name, time, product);
}
}
'''
compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:History']
provider = HTTPProvider('http://0.0.0.0:9945')
w3 = Web3(provider)
contract = w3.eth.contract(contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = contract.deploy(transaction={'from': w3.eth.coinbase, 'gas':1000000})
print ('tx_hash : ' + tx_hash)
while w3.eth.getTransactionReceipt(tx_hash) is None:
time. sleep(0.1)
contract_address = w3.eth.getTransactionReceipt(tx_hash).contractAddress
print ('contract_address' + contract_address)
contract_instance = w3.eth.contract(contract_interface['abi'], contract_address, ContractFactoryClass=ConciseContract)
contract_instance.setLog("test", "123", "pro",transact={'from': w3.eth.coinbase,'gas':3000000})
time.sleep(3)
print (contract_instance.getLog())
print('Contract value: {}'.format(contract_instance.getLog()))