-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
190 lines (168 loc) · 7.84 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Leilão</title>
<link href='https://fonts.googleapis.com/css?family=Open Sans:400,700' rel='stylesheet' type='text/css'>
<link href='bootstrap.min.css' rel='stylesheet' type='text/css'>
<script language="javascript" type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript"
src="https://cdn.jsdelivr.net/npm/web3-min-js@1.0.0/web3.min.js"></script>
<script language="javascript" type="text/javascript" src="js/abi.js"></script>
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<h2>Leilão</h2>
</div>
<div class="row">
<div class="col-md order-md mb">
<h4 class="d-flex justify-content-between align-items-center mb-3">
<span class="text-muted">Leilão</span>
</h4>
<form class="card p-2">
<div class="input-group">
<input type="text" class="form-control" id="item_titulo" placeholder="Titulo do item">
</div>
<br />
<div class="input-group">
<textarea class="form-control" rows="4" cols="20" id="item_descricao" placeholder="Descrição do item"></textarea>
</div>
<br />
<div class="input-group">
<input type="text" class="form-control" id="item_valor_inicial" placeholder="Valor inicial do item">
</div>
<br />
<div class="input-group">
<input type="text" class="form-control" id="item_valor_incremento" placeholder="Valor de incremento do item">
</div>
<br />
<div class="input-group">
<input type="text" class="form-control" id="item_data_expiracao" placeholder="Duração do leilão">
</div>
<br />
<button type="button" onclick="adicionar_item()" class="btn btn-secondary">Adicionar Item</button>
</form>
<br />
<form class="card p-2">
<div class="input-group">
<input type="text" class="form-control" id="item_id" placeholder="Número do item">
</div>
<br />
<div class="input-group">
<button type="button" onclick="mostrar_itens_disponiveis()" class="btn btn-secondary">Mostrar Itens</button>
</div>
<div class="input-group" id="itens">
</div>
</form>
<form class="card p-2">
<div class="input-group">
<input type="text" class="form-control" id="item_id" placeholder="Número correspondente ao item">
</div>
<div class="input-group">
<input type="text" class="form-control" id="item_lance_atual" placeholder="Valor do lance">
</div>
<br />
<div class="input-group">
<button type="button" onclick="dar_lance()" class="btn btn-secondary">Dar Lance</button>
</div>
</form>
</div>
</div>
<footer class="my-5 pt-5 text-muted text-center text-small">
<p id="contractAddress" class="text-center"></p>
<p class="mb-1">IMD - IMD0729</p>
</footer>
</div>
<script>
var leilao;
var userAccount;
var actualAccount;
function startApp(){
var contractAddress = '0x20f0FD75DC62967fB1D0E3e6cf0C9c21E79A1C00';
leilao = new web3.eth.Contract(abi, contractAddress);
var accountInterval = setInterval(function() {
web3.eth.getAccounts().then(function (result) {
actualAccount = result[0];
})
// Check if account has changed
if (actualAccount !== userAccount) {
userAccount = actualAccount;
// Call some function to update the UI with the new account
// $("#contractAddress").text("Your Account: " + userAccount);
atualizarDados();
}
}, 100);
}
function dar_lance(id){
var id = document.getElementById("item_id").value;
var lance_atual = document.getElementById("item_lance_atual").value;
var valor = 1000000000000000000*lance_atual;
leilao.methods.dar_lance(id).send({ from: userAccount, value: valor });
return false;
}
function mostrar_itens_disponiveis() {
var ids = document.getElementById("item_id").value;
$("#itens").empty();
for (id of ids) {
itens_disponiveis(id)
.then((result) => {
// Using ES6's "template literals" to inject variables into the HTML.
const new_item = `
<div class="zombie">
<ul>
<li>Titulo: ${result.titulo}</li>
<li>Descrição: ${result.descricao}</li>
<li>Valor atual: ${result.lance_atual}</li>
</ul>
</div>
`;
document.getElementById("itens").innerHTML = new_item;
});
}
}
function itens_disponiveis(id){
return leilao.methods.itens_disponiveis(id).call();
}
function atualizarDados(){
document.getElementById("contractAddress").innerHTML = "Your Account: " + userAccount;
}
function adicionar_item(){
var titulo = document.getElementById("item_titulo").value;
var descricao = document.getElementById("item_descricao").value;
var valor_inicial = document.getElementById("item_valor_inicial").value;
var valor_incremento = document.getElementById("item_valor_incremento").value;
var data_expiracao = document.getElementById("item_data_expiracao").value;
leilao.methods.adicionar_item(titulo, descricao, valor_inicial, valor_incremento, data_expiracao).send({ from: userAccount });
return false;
}
// Padrão para detectar um web3 injetado.
window.addEventListener('load', function () {
web3Provider = null;
// Modern dapp browsers...
if (window.ethereum) {
web3Provider = window.ethereum;
try {
// Request account access
window.ethereum.enable();
} catch (error) {
// User denied account access...
console.error("User denied account access")
}
}
// Legacy dapp browsers...
else if (window.web3) {
web3Provider = window.web3.currentProvider;
}
// If no injected web3 instance is detected, fall back to Ganache
else {
console.log('No web3? You should consider trying MetaMask!')
web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
}
web3 = new Web3(web3Provider);
startApp()
})
</script>
</body>
</html>