Beginner’s guide to blockchain/Ethereum (1) — Build a smart contract on a private network

Overview & Incentive

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Start coding!

cd “path of the file you just downloaded”
// in Mac, you can simply drag the folder to console to get the path
make geth
echo "alias geth159='your_folder_path/build/bin/geth'" >> ~/.bashrc
source ~/.bashrc
echo "source ~/.bashrc" >> ~/.profile
brew install solidity
mkdir privchain
geth159 --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --dev --datadir privchain
geth159 attach ipc://Users/tina/privchain/geth.ipc 
// use your own ipc url here!
personal.listAccounts
personal.newAccount('') 
// you can enter your password like newAccount(‘password_ya’)
personal.listAccounts 
// just to make sure you hv made the account successfully
web3.eth.coinbase 
// This property is read only and returns the coinbase address where the mining rewards go to.
source = "contract test { function multiply(uint a) returns(uint d){return a * 7;}}"
web3.eth.getCompilers() // the output should be ["Solidity"]
code = web3.eth.compile.solidity(source)
{<stdin>:test: {    code: "0x60606040523415600b57fe5b5b60918061001a6000396000f300606060405263ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663c6888fa181146039575bfe5b3415604057fe5b6049600435605b565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a723058206a8cbef13138690601a279c73e208f9dcf42e4678d860038475fe555599593d60029",    info: {        abiDefinition: [{...}],
compilerOptions: "--combined-json bin,abi,userdoc,devdoc -- add-std --optimize",
compilerVersion: "0.4.11", developerDoc: { methods: {} }, language: "Solidity", languageVersion: "0.4.11", source: "contract test { function multiply(uint a) returns(uint d){return a * 7;}}", userDoc: { methods: {} } }}}
MyContract = web3.eth.contract(code["<stdin>:test"].info.abiDefinition)
account1 = web3.eth.coinbase
web3.eth.getBalance(account1)
miner.start()
miner.stop()
web3.eth.getBalance(account1)
personal.unlockAccount(account1, '') 
// you can replace '' with the password you set previously
bytecode = code["<stdin>:test"].code
web3.eth.estimateGas({data: bytecode})
contractInstance = MyContract.new({data: bytecode gas: 1000000, from: account1}, function(e, contract){if(!e){if(!contract.address){console.log("Contract transaction send: Transaction Hash: "+contract.transactionHash+" waiting to be mined...");}else{console.log("Contract mined! Address: "+contract.address);console.log(contract);}}else{console.log(e)}})
miner.start()
miner.stop()
eth.getCode(contractInstance.address) // the output should be the same as your bytecode
contractInstance.multiply.call(6) // replace 6 with any unsigned int

Learn more through…

Wrap up

--

--

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
李婷婷 Lee Ting Ting

Founder of Z Institute | Blockchain dev & security audit | Berkeley Blockchain Lab | HKUST | IG: tinaaaaalee | Github: tina1998612