-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleStorage
135 lines (90 loc) · 3.61 KB
/
sampleStorage
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!--dApp (simpleStorage)
1.스마트계약에 저장되어 있는 데이타 불러오기
2.사용자가 입력한 정수값을 입력받고 저장하기
-->
<!DOCTYPE html>
<html>
<title> simple Storage page </title>
<head>
<meta charset="UTF-8">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.2.3/milligram.min.css">
<title> Simple Storage </title>
<style>
body {margin-left:50px;}
#storedData {font-size:300%; margin-right:10px;}
#newValue {width: 200px; margin-right:10px; text-align:right;}
</style>
</head>
<body>
<h3> Simple Storage </h3>
<ul>
<li>스마트계약 주소 : <span id="contract_addr"></span></li>
<li>마이계정 주소 : <span id="account_addr"></span></li>
<li>스마트계약에 저장된 값 : <span id="contract_value"></span>
<button onclick="getValue()">새로고침</button>(현재블록: <span id="lastBlock"></span>)</li>
<li><input id="newValue" type="text"><button onclick="setValue()">새로운 값으로 저장</button>
<div id="result"></div></li>
<li>새 값을 저장후에 팬딩 트랜잭션이 블록에 포함되면 자동으로 페이지가 업데이트됨.</li>
</ul>
</body>
<script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
<script>
var contract_address = "0x63445a644C3c0E5c0F6136732C6839f99469eDd6";
var abi = [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
var simpleStorageContract;
var simpleStorage;
window.addEventListener('load',function(){
//checking if web3 has been injected by the browsers (mist / metamask)
if(typeof web3 !== 'undefined'){
//use mist/metamask provider
window.Web3 = new Web3(Web3.currentProvider);
}else{
console.log('no web3? you should consider trying metamask!')
window.Web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log('window.web3 connecct successfully!!');
}
startApp();
});
function startApp(){
simpleStorageContract = Web3.eth.contract(abi);
simpleStorage = simpleStorageContract.at(contract_address);
document.getElementById('contract_addr').innerHTML = getLink(contract_address);
Web3.eth.getAccounts(function(e,r){
document.getElementById('account_addr').innerHTML = getLink(r[0]);
});
getValue();
}
function getLink(addr) {
return '<a target="_blank" href="http://testnet.etherscan.io/address/'+addr+'>'+addr+'</a>';
// body...
}
function getValue(){
simpleStorage.get(function(e,r){
//document.getElementById('storedData').innerHTML = r.toNumber();
});
Web3.eth.getBlockNumber(function(e,r){
document.getElementById('lastBlock').innerHTML = r;
});
}
function setValue(){
var newValue = document.getElementById('newValue').value;
var txid;
simpleStorage.set(newValue,function(e,r){
document.getElementById('result').innerHTML = 'Transaction id: '+r+'<span id="pending" style="color:red;">(Pending)</span>';
txid = r;
});
var filter = Web3.eth.filter('lastest');
filter.watch(function(e,r){
getValue();
Web3.eth.getTransaction(txid,function(e,r){
if(r != null && r.blockNumber > 0){
document.getElementById('pending').style.cssText='color:green;';
document.getElementById('storedData').style.cssText='color:green;font-size:300%;';
filter.stopWatching();
}
});
});
}
</script>
</html>