Ethereum DApp on localhosted blockchain (part 1)

Install geth and solc

sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum solc
# test them:
geth version
solc --version

Creating the test blockchain

echo petshop > password.file
geth --datadir ~/.ethereum-test --password password.file account new
geth --datadir ~/.ethereum-test --password password.file account new
pal@PAL-T460:…/pet-shop-vanilla$ geth --datadir ~/.ethereum-testnet account listINFO [03–25|17:27:26] Maximum peer count ETH=25 LES=0 total=25Account #0: {25c9b3a4bab2b5716de1ff45dd6b3b1b27dfee51} keystore:///home/pal/.ethereum-testnet/keystore/UTC — 2018–03–14T14–13–58.408948700Z — 25c9b3a4bab2b5716de1ff45dd6b3b1b27dfee51Account #1: {39f9a7646debd14c6de402986b7db54ae78ed2b6} keystore:///home/pal/.ethereum-testnet/keystore/UTC — 2018–03–14T15–08–59.402901600Z — 39f9a7646debd14c6de402986b7db54ae78ed2b6
{
"genesis": {
"config": { ... }
[...]
}
}
became:{
"config": { ... }
[...]
}
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "0x200",
"gasLimit": "0x2100000",
"alloc": {
"25c9b3a4bab2b5716de1ff45dd6b3b1b27dfee51": {
"balance": "0x1337000000000000000000"
},
"39f9a7646debd14c6de402986b7db54ae78ed2b6": {
"balance": "0x1337000000000000000000"
}
}
}
geth --datadir ~/.ethereum-test --networkid 15 init genesis.json...INFO [03-25|19:59:03] Successfully wrote genesis state
geth --datadir ~/.ethereum-test console> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
23229320.729235784806170624
geth --rpc --rpcport 8545 --rpccorsdomain "*" \
--rpcapi "web3,eth,net,personal" \
--identity "MyTestNetNode" --nodiscover --networkid 15 \
--datadir ~/.ethereum-test \
--mine --etherbase 1 --minerthreads 1
geth --datadir ~/.ethereum-test --networkid 15 attach> // everything below is in the javascript consolepersonal.unlockAccount(eth.accounts[0], "petshop", 100);var txReceiptHex = eth.sendTransaction({from: eth.accounts[0], to:          eth.accounts[1], value: web3.toWei(0.05, "ether"), gas:21000});web3.eth.getTransactionReceipt(txReceiptHex); // may take few secs

Create the Contract

pragma solidity ^0.4.17;contract Adopters {
address[16] adopters; // internal by default
function adopt(uint dogId) public returns (uint) {
require(dogId >= 0 && dogId < 16);
adopters[dogId] = msg.sender;
return dogId;
}
function getAdopters() public view returns (address[16]) {
return adopters;
}
}
solc -o build/ --bin --abi --overwrite Adoption.sol
geth --datadir ~/.ethereum-test --networkid 15 attach> // everything below is within the JavaScript console
personal.unlockAccount(eth.accounts[0], "petshop", 100)
// instantiate contract object with ABI
var adoptionContract = web3.eth.contract([{"Place ABI array here": true}]);
// Deploy contract on chain
var adoption = adoptionContract.new({
from: web3.eth.accounts[0],
data: '0x place binary here',
gas: '4700000'
},
function (e, contract){
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: '
+ contract.address
+ ' transactionHash: '
+ contract.transactionHash);
}
else if (e !== null) {
console.log(e);
}
});
// unlock the account with index 0
personal.unlockAccount(eth.accounts[0], "petshop", 100);
// set the default account for the tx to be used from the wallet
web3.eth.defaultAccount = web3.eth.accounts[0];
// adopt the dog with dogId=0
adoption.adopt(0);
// after the mining placed the previous tx in a block
adoption.getAdopters();
geth --unlock <accIndex> --password password.file --datadir ~/.ethereum-test --networkid 15 attach

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store