Skip to content

Instantly share code, notes, and snippets.

@falehenrique
Created November 13, 2017 13:28
Show Gist options
  • Save falehenrique/15a392b256e919d808e6c713936e580e to your computer and use it in GitHub Desktop.
Save falehenrique/15a392b256e919d808e6c713936e580e to your computer and use it in GitHub Desktop.
Meetup Blockchain e Chatbot 11/11/2017 - node.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var bodyParser = require('body-parser')
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
extended: true
}));
var http = require('http')
var port = 3000
var Web3 = require('web3');
var web3 = null;
var urlNode = 'http://localhost:8545/';
web3 = new Web3(new Web3.providers.HttpProvider(urlNode));
function checkWeb3(){
if (web3 && web3.isConnected()) {
console.info('Connected');
} else {
console.info('Not Connected');
}
}
app.post('/enviarEther', function (req, res) {
sendEther(
req.body.accountUnlock,
req.body.password,
req.body.accountUnlock,
req.body.receiver,
req.body.amount
);
var json = JSON.stringify({
messages: [{
"text": "ok",
},
{
"text": "transação efetuada",
}
]
});
res.end(json);
});
function sendEther(accountUnlock, password, sender, receiver, amount) {
web3.personal.unlockAccount(accountUnlock, password, function(error, result) {
if(error){
console.info(error);
} else {
if(result){
console.info('Account unlock');
web3.eth.sendTransaction({
"from": sender,
"to": receiver,
"value": web3.toWei(amount, 'ether'),
"gas": 300000},
function(error, result) {
if(error){
console.info(error);
} else {
if(result){
console.info("transaction send" + result);
} else {
console.info("It wasn't possible to send transaction.");
}
}
});
} else {
console.info("It wasn't possible to unlock the account.");
}
}
});
}
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
Exercicios
1- Donwload Geth
https://geth.ethereum.org/downloads/
2- Variaveis de ambiente
- Mac vi ~/.bash_profile
#GETH ETHEREUM
export GETH_HOME=/Users/hgleite/Documents/ferramentas/geth
export PATH=$PATH:$GETH_HOME
para salvar ESC :wq
Feche o bash_profile e salve
Execute
source ~/.bash_profile
- Windows adicionar geth no PATH
3- Subir com console para criar uma conta
geth --dev console
personal.newAccount("12345678")
Criem mais duas contas para as ongs
//verificar saldo da conta
eth.getBalance("sua conta")
4- Subir o geth para minerar
geth --dev --mine
5- Acessar o console do nó
Abrir outro terminal e executar geth attach ipc:\\.\pipe\geth.ipc
6- listar contas
Acessar o console
personal.listAccounts(
*Libs(Debug; eth ; personal ; web3 ;)
7- Exercicio
- Enviar ether de uma conta para outraa
> personal.unlockAccount("0xeb519618e013a95e0e87f3d3ef0ec8a048a333d1", "12345678" )
true
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(0.1, "ether")})
"0x990d9fca0024d0bbc231c256fdce76aa7a049bf37c75bdcc109204941255f72d"
> eth.getBalance(eth.accounts[1])
100000000000000000
> eth.getBalance(eth.accounts[0])
4.8119e+21
>
8- Baixar ethereum wallet
https://github.com/ethereum/mist/releases
Instalar
9- Ir até o endereço do executável e executar via comando
-- EthereumWallet.exe --rpc http://localhost:8545
10- liberar rpc
geth --dev --rpc --rpcaddr "localhost" --rpcport "8545" --rpcapi "web3,eth,net,personal" --mine
geth --DIR --rpc --rpcaddr "localhost" --rpcport "8545" --rpcapi "web3,eth,net,personal" --mine
11- Estudar o projeto de crowd funding do site da Ethereum
12- Acessar remix para testar o contrato
https://remix.ethereum.org/
13- Compilar e realizar o contrato no mist
post
accountUnlock, password, sender, receiver, amount
https://ethereum.stackexchange.com/questions/20874/payable-function-in-solidity
function deposit() payable {
amountRaised += msg.value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment