-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb3.html
378 lines (258 loc) · 9.02 KB
/
web3.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h1, h2, h3 {
font-weight: 400;
margin-bottom: 0;
}
.remark-slide-content h1 {
font-size: 3em;
}
.remark-slide-content h2 {
font-size: 2em;
}
.remark-slide-content h3 {
font-size: 1.6em;
}
.remark-slide-content {
background: #272822;
color: #f3f3f3;
}
.footnote {
position: absolute;
bottom: 3em;
}
li p {
line-height: 1.25em;
}
.red {
color: #fa0000;
}
.large {
font-size: 2em;
}
a, a>code {
color: #9fa28c;
text-decoration: none;
}
code {
background: none repeat scroll 0 0 #272822;
border: 1px solid #DEDEDE;
border-radius: 3px;
padding: 0 0.2em;
}
.remark-code, .remark-inline-code {
font-family: "Bitstream Vera Sans Mono", "Courier", monospace;
}
.remark-code-line-highlighted {
background-color: #373832;
}
.pull-left {
float: left;
width: 47%;
}
.pull-right {
float: right;
width: 47%;
}
.pull-right~p {
clear: both;
}
#slideshow .slide .content code {
font-size: 0.8em;
}
#slideshow .slide .content pre code {
font-size: 0.9em;
padding: 15px;
}
.main-title, .title {
background: #272822;
color: #777872;
text-shadow: 0 0 20px #333;
}
.title h1, .title h2, .main-title h1, .main-title h2 {
color: #f3f3f3;
line-height: 0.8em;
}
.code{
}
/* Custom */
.remark-code {
display: block;
padding: 0.5em;
}
</style>
</head>
<body>
<textarea id="source">
#Blockchain for devs
## Sesión 3: Introducción al desarrollo de Smart Contracts: Web3
### Carlos Buendía ([@buendiadas](https://twitter.com/buendiadas))
---
# Agenda
1. Introducción
2. Clientes
- Introducción
- Geth
- Instalación
- Inicialización de parámetros
- Creación de una blockchain privada
- Comienzos con la blockchain pública
- testRPC
3. `Web3`
- Funciones principales
- Realización de una transacción
- Compilando un contrato
4. Solidity
5. Ejemplos Smart Contracts
-DAO
-Crowdfunding
-Token
6. Ejercicio con Token
---
# Arquitectura de una Dapp
![image](img/arch-dapp.png)
---
# Web3.js
- Principal interfaz para interactuación con la blockchain
- Ofrece, a través de javascript una [librería](https://github.com/ethereum/wiki/wiki/JavaScript-API) de herramientas completa para interactuar y desarrollar con la blockchain.
- Interactuación con otros servicios adicionales de Ethereum (keystore, whisper, swarm)
### Ejemplos:
- Herramienta externa: Encriptación (web3.sha3())
- Utilización de la blockchain: web3.eth.sendTransaction(object)
- Utilización de servicios extra Ethereum: shh
---
# Web3.js
- Instalación:
`npm install web3`
- Abre Geth
$ geth --networkid 03032017 --rpccorsdomain "*" --datadir "/home/satoshi/chain" --port "30303" "db,eth,net,web3"
- Ejecuta el siguiente código: `node web3-test.js`
```sh
var Web3 = require('web3');
var web3 = new Web3(
new Web3.providers.HttpProvider("http://localhost:8545"));
web3.eth.getBlock(0, function(error, result){
if(!error)
console.log(result)
else
console.error(error);
});
```
---
# Utilizando Web3:
### Realización de una [transacción](https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction)
Previo: ¡Para poder realizar una transacción **necesitarás Ether**! En tu consola geth:
>web3.personal.newAccount() // Crea una nueva transacción
>web3.eth.accounts // Lista el array de cuentas
>web3.miner.setEtherbase (eth.accounts[0]) //Selecciona la cuenta 0 como cuenta principal para el minado
>web3.miner.start()
Una vez que hayas comenzado a minar, verás que el balance de la cuenta 0 deberá haber aumentado
> web3.eth.getBalance(web3. eth. accounts[0]);
---
# Utilizando Web3:
### Realización de una [transacción](https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction)
Previo: ¡Para poder realizar una transacción **necesitarás Ether**! En tu consola geth:
> web3.personal.newAccount() // Crea una nueva transacción
> web3.eth.accounts // Lista el array de cuentas
> web3.miner.setEtherbase (eth.accounts[0]) //Selecciona la cuenta 0 como cuenta principal para el minado
> web3.miner.start()
---
# Utilizando Web3:
### Realización de una [transacción](https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction)
Recordamos el objeto transaction:
*from, *to, *value, *data, *nonce
Transacción entre cuentas externas --> Campo value > 0, no data
> web3.eth.sendTransaction(tx_object)
> web3.eth.sendTransaction ({from:web3.eth.accounts[0],
to: web3.eth.accounts[1],
value: web3.toWei(1, "ether")})
> web3.eth.getBalance(web3.eth.accounts[0])
> web3.eth.getBalance(web3.eth.accounts[1])
---
# Utilizando Web3:
### Recuperando [la transacción](https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction)
Recupera al campo txhash
> var transaction = web3.eth.getTransaction(blockNumber, indexOfTransaction);
> console.log(transaction);
---
# Utilizando Web3:
### Encriptando con [KECCAK-256](http://keccak.noekeon.org)
- En la consola de geth ejecuta
> web3.sha3("Hello Ethereum Course")
---
# Utilizando Web3
###Compilando un contrato
Crea el siguiente archivo (compilecontract.js):
```Javascript
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
var source = 'pragma solidity ^0.4.6; contract demo {string public name = "buendiadas"; function changeName(string _newName){name = _newName; } }';
var compiled = web3.eth.compile.solidity(source);
console.log(compiled);
console.log("ABI")
console.log( compiled.info.abiDefinition);
```
Ejecuta:
```sh
$ node compilecontract.js
```
---
# Utilizando Web3
### Subiendo el contrato
Copia las anteriores lineas en un nuevo fichero(e.g. contractupload.js), y añade las siguientes:
```Javascript
var nameContract= web3.eth.contract(compiled.info.abiDefinition);
var uploadedContract = nameContract.new({from:web3.eth.accounts[0], data: compiled.code, gas: 1000000}, function(e, contract){
if(!e) {
if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else {
console.log("Contract mined! Address: " + contract.address);
console.log(contract);
}
}
});
```
En terminal:
```sh
$ node contractupload.js
```
---
# Utilizando Web3
### Llamando a un contrato
Recupera los valores ABI, y contract Address del valor anterior
Con estos, sustituye en el siguiente script, y crea un nuevo fichero (getname.js)
```Javascript
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
var ABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newName","typ$
var contractAddress = "0xbbfd34ccf4bd62ec0d33d8b25c687c34888941af";
var contract = web3.eth.contract(ABI).at(contractAddress);
contract.name.call(function(data){
console.log(data)
});
```
---
# Utilizando Web3
### Conectando con solidity
Recupera los valores ABI, y contract Address del valor anterior
Accede a el IDE oficial : http://ethereum.github.io/browser-solidity/#version=soljson-v0.4.9+commit.364da425.js
---
</textarea>
<a href="ethereum2.html">
<script src="https://gnab.github.io/remark/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>