forked from chuanqin3/Udacity-Blockchain-Ethereum-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·174 lines (144 loc) · 6.89 KB
/
index.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Star Notary</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<script src="StarNotaryABI.js"></script>
</head>
<body>
<div class="container">
<h1>Star Notary</h1>
<div class="row">
<label class="bold">Star Name:</label>
<input type="text" id="star-name-input"></label>
</div>
<div class="row">
<label class="bold">Dec:</label>
<input type="text" id="star-dec-input"></label>
</div>
<div class="row">
<label class="bold">Mag:</label>
<input type="text" id="star-mag-input"></label>
</div>
<div class="row">
<label class="bold">Ra:</label>
<input type="text" id="star-ra-input"></label>
</div>
<div class="row">
<label class="bold">Star Story:</label>
<input type="text" id="star-story-input"></label>
</div>
<div class="row">
<label class="bold">Star Token Id (numbers only):</label>
<input type="text" id="star-tokenid-input"></label>
</div>
<!-- If star is created successfully, it will show up -->
<div id="create-star-status"></div>
<button id="claim-button" onclick="claimButtonClicked()">Claim Star</button>
</div>
<div class="container">
<h1>Find a Star</h1>
<div class="row">
<label class="bold">Star Token ID:</label>
<input type="text" id="star-tokenid-input">
</div>
<!-- If star is found successfully, it will show up -->
<div id="star-lookup-result"></div>
<button id="star-lookup" onclick="starLookupButtonClicked()">Look up a star by Token ID</button>
</div>
<div class="container">
<h1>Sell a Star</h1>
<div class="row">
<label class="bold">Star Token ID:</label>
<input type="text" id="sale-star-tokenid-input">
</div>
<div class="row">
<label class="bold">Price for sale:</label>
<input type="text" id="star-price-input">
</div>
<!-- If star is found successfully, it will show up -->
<div id="star-sale-result"></div>
<button id="star-sale" onclick="starSaleButtonClicked()">Sell my star</button>
</div>
<script>
if(typeof web3 != 'undefined') {
web3 = new Web3(web3.currentProvider) // what Metamask injected
} else {
// Instantiate and set Ganache as your provider
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
// The default (top) wallet account from a list of test accounts
web3.eth.defaultAccount = web3.eth.accounts[0];
// The interface definition for your smart contract (the ABI)
// This address is from Metamask Rinkeby Test network
let StarNotary = web3.eth.contract(starNotaryABI).at('0xd56eccb93995c35159921044a64683531d85c0d2');
// Enable claim button being clicked
function claimButtonClicked() {
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error)
return
}
var account = accounts[0]
console.log("Account: " + account)
// Get the star information from user's input
const starName = document.getElementById("star-name-input").value
const starRa = document.getElementById("star-ra-input").value
const starDec = document.getElementById("star-dec-input").value
const starMag = document.getElementById("star-mag-input").value
const starStory = document.getElementById("star-story-input").value
const tokenId = document.getElementById("star-tokenid-input").value
// create star using smart contracts
StarNotary.createStar(starName, starStory, starRa, starDec, starMag, tokenId, { "from": account, "gas": 4950000, "gasPrice": '10000000000'}, function (error, result) {
if (!error) {
document.getElementById('create-star-status').innerText = "Star registered successfully!"
} else {
console.log(error);
}
})
})
}
// Look up Star by tokenId
function starLookupButtonClicked() {
// Get the token ID from input
const tokenId = document.getElementById("star-tokenid-input").value
// look up star using smart contracts
StarNotary.tokenIDToStarInfo(tokenId, function (error, result) {
if (!error) {
console.log(result)
document.getElementById('star-lookup-result').innerText = result
} else {
console.log(error)
}
})
}
// Put a star for sale
function starSaleButtonClicked() {
// Get the token ID from input
const tokenId = document.getElementById("sale-star-tokenid-input").value
// Get the price from input
const price = document.getElementById("star-price-input").value
// get account information and then put star for sale
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error)
return
}
var account = accounts[0]
console.log("Account: " + account)
// create star using smart contracts
StarNotary.putStarUpForSale(tokenId, price, { "from": account, "gas": 4950000, "gasPrice": '10000000000'}, function (error, result) {
if (!error) {
document.getElementById('star-sale-result').innerText = "Put star for sale successfully!"
} else {
console.log(error);
}
})
})
}
</script>
</body>
</html>