Ethereum DApp on localhosted blockchain (part 1)

  • Set up a test blockchain with geth
  • Create a contract and compile it with solc
  • Deploy the contract via geth javascript console
  • Test the contract

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

Create some new accounts

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

Create the genesis block

{
"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

Start mining!

geth --rpc --rpcport 8545 --rpccorsdomain "*" \
--rpcapi "web3,eth,net,personal" \
--identity "MyTestNetNode" --nodiscover --networkid 15 \
--datadir ~/.ethereum-test \
--mine --etherbase 1 --minerthreads 1

Test our blockchain

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;
}
}

Compile the contract

solc -o build/ --bin --abi --overwrite Adoption.sol

Deploy the contract

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);
}
});

Test the contract

// 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
  • installed geth and solc
  • created a couple of user accounts in geth’s own wallet
  • created a genesis block
  • started mining process with geth
  • tested our blockchain by transferring some ether
  • created a contract called Adoption.sol
  • compiled this contract to get it’s abi and evm bytecode
  • deployed this contract via geth’s JavaScript console
  • interacted with the contract

--

--

--

Software engineer, technical lead. Interested in distributed systems and soft skills. I don’t have to prented to be always right, I just am.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

For me the #SUDWA project isn’t about money or fame.

Hello all of you.

The Lazy Lawyer’s Guide to Smart Contract Technology: Part 2

We have partnered with VEGAS CITY to build BONE CITY in DECENTRALND

Summary of the first AMA in Discord

Moonshot Hunter AMA

Issuing certificates with the EW Origin SDK (Part II)

EOS Alliance — Dispute Resolution & Arbitration Working Group — Aug 27–31 Summary & Plans for Sept…

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
Pal Konyves

Pal Konyves

Software engineer, technical lead. Interested in distributed systems and soft skills. I don’t have to prented to be always right, I just am.

More from Medium

Blockchain bridges

https://www.blockchainx.tech/bridge-smart-contract-development

How did I manage to create a smart contract in web3?

Reflections on Chainlink Oracle Olympics #1

Thoughts while Learning Solidity