-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleStorage.html
More file actions
110 lines (95 loc) · 4.18 KB
/
Copy pathsimpleStorage.html
File metadata and controls
110 lines (95 loc) · 4.18 KB
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
<!DOCTYPE html>
<html>
<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 Dapp 예제 </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 dApp 예제</h3>
<ul>
<li>컨트랙트 주소: <span id="contractAddr"></span></li>
<li>내 어카운트 주소: <span id="accountAddr"></span></li>
<li>컨트랙트에 저장된 값: <span id="storedData"></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>
컨트랙트 소스
<script src="https://gist.github.com/atomrigs/7c633570496b79623bed5d1286f93f3a.js"></script>
HTML 소스<br>
<a href="https://github.com/atomrigs/atomrigs.github.io/blob/master/simplestorage.html">https://github.com/atomrigs/atomrigs.github.io/blob/master/simplestorage.html</a>;
<br><br>
<p>
<a href="http://www.chaintalk.io/archive/lecture?sca=%EB%82%98%EB%8F%84+dApp+%EA%B0%9C%EB%B0%9C"><i>나도 dApp 개발해 보자 시리즈 by Atomrigs © 2017</i></a>
</p>
</body>
<script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
<!-- script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script -->
<script>
var contractAddress = '0x1021292F14790Bc024F5114DA19b8F356Bccd04A';
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 browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.log('No web3? You should consider trying MetaMask!')
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
// Now you can start your app & access web3 freely:
startApp();
});
function startApp() {
simpleStorageContract = web3.eth.contract(abi);
simpleStorage = simpleStorageContract.at(contractAddress);
document.getElementById('contractAddr').innerHTML = getLink(contractAddress);
web3.eth.getAccounts(function(e,r){
document.getElementById('accountAddr').innerHTML = getLink(r[0]);
});
getValue();
}
function getLink(addr) {
return '<a target="_blank" href=https://ropsten.etherscan.io/address/' + addr + '>' + addr +'</a>';
}
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('latest');
filter.watch(function(e, r) {
getValue();
web3.eth.getTransaction(txid, function(e,r){
if (r != null && r.blockNumber > 0) {
document.getElementById('pending').innerHTML = '(기록된 블록: ' + r.blockNumber + ')';
document.getElementById('pending').style.cssText ='color:green;';
document.getElementById('storedData').style.cssText ='color:green; font-size:300%;';
filter.stopWatching();
}
});
});
}
</script>
</html>