Smart Contracts: The Working Parts

codingwithgas
Jul 25, 2016 · 14 min read
Image for post
Image for post
B&H Tools, Mountain View, CA, 1969 —© and permission to use granted by: Mountain View Public Library History Center

Tooling Up to write smart contracts — 2016 Edition

Table of Contents

Status

brew tap ethereum/ethereum
brew install ethereum
Image for post
Image for post
Ethereum CLI tools install for Mac Users
> geth --fast --cache=1024 --testnet --rpc --rpccorsdomain '*' console --testnet means "run on the testnet"
--rpc means "listen for rpc commands that come from outside"
--rpccorsdomain '*' "allow cross origin requests from anywhere (like the solidity compiler running on github.io)"
--fast "download big ass chunks of blockchain to go faster"
--cache=1024 "use tons of RAM to go faster"
'console' means launch an interactive geth console
> geth attach
Fatal: Unable to attach to remote geth: dial unix /Users/you/Library/Ethereum/geth.ipc: connect: no such file or directory
geth attach ipc:/Users/you/Library/Ethereum/testnet/geth.ip
geth --fast --cache=1024 --testnet --rpc --rpccorsdomain '*' console 2>> geth.log
bash-3.2$ echo "geth --testnet --rpc --rpccorsdomain '*' console" > ~/bin/rungeth_testnet.sh
bash-3.2$ chmod +x ~/bin/rungeth_testnet.sh
personal.newAccount()
Passphrase:
"0x1aefd704facfda6069270ab69ef73e847aa897cc"
eth.accounts
personal.unlockAccount("0x9bcd2d4f8dddea8f4ff303fe2052fc134b655c8a", "your_password")
var me = eth.accounts[0];
undefined
me
"0x9bcd2d4f8dddea8f4ff303fe2052fc134b655c8a"
> personal. <hit tab>
personal.listAccounts personal.newAccount personal.unlockAccount
cd ~/Library/Ethereum/keystore/
ls -la
tar cvzf oldkeys.tgz *
openssl aes-256-cbc -a -salt -in oldkeys.tgz -out oldkeys.tgz.enc
ls
rm UTC*
rm oldkeys.tgz
mv oldkeys.tgz.enc ~/place_I_store_encrypted_stuff_that_gets_backed_up
~/Library/Ethereum/testnet/keystore/
> I0802 22:01:40.381033    5591 downloader.go:288] Block synchronisation started
I0802 22:01:44.057910 5591 blockchain.go:889] imported 192 header(s) (0 ignored) in 1.148585631s. #192 [4be46f27… / e46144f5…]
I0718 18:57:19.859391 43671 blockchain.go:1251] imported 1 block(s) (0 queued 0 ignored) including 0 txs in 4.389402ms. #1333027 [2f0e3825 / 2f0e3825]
net.listening
true
net.peerCount
10
admin.nodeInfo
ports: {
discovery: 30303,
listener: 30303
},
miner.start()
I0718 19:27:02.221854 45253 ethash.go:252] Generating DAG: 96%
I0718 19:27:08.514340 45253 ethash.go:252] Generating DAG: 97%
I0718 19:27:14.770954 45253 ethash.go:252] Generating DAG: 98%
I0718 19:38:53.697866 45253 worker.go:569] commit new work on block 1333201 with 1 txs &amp; 0 uncles. Took 44.675404ms
I0718 19:46:48.537178 39568 worker.go:348] :hammer: Mined block (#1333236 / 065dff46). Wait 5 blocks for confirmation
Image for post
Image for post
Cool…unicode has a pickaxe!
> eth.getBalance(me)
70757845080000109920
> personal.unlockAccount(me)
Unlock account 9bcd2d4f8dddea8f4ff303fe2052fc134b655c8a
Passphrase:
true
contract HelloWorld{
string public message;
function setMessage(string newMessage){
message = newMessage;
}
}
Image for post
Image for post
Pasting into the online solidity compiler
Image for post
Image for post
Solidity Environment Tab
Image for post
Image for post
Successful contract creation
var helloWorld = eth.contract(YOUR INTERFACE STRING).at(YOUR CONTRACT ADDRESS)
Image for post
Image for post
Where to find the interface string for your contract.
[{"constant":false,"inputs":[{"name":"newMessage","type":"string"}],"name":"setMessage","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"message","outputs":[{"name":"","type":"string"}],"type":"function"}]
Image for post
Image for post
Finding the contract address string
var helloWorld = eth.contract([{"constant":false,"inputs":[{"name":"newMessage","type":"string"}],"name":"setMessage","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"message","outputs":[{"name":"","type":"string"}],"type":"function"}]
).at("0x9bcd2d4f8dddea8f4ff303fe2052fc134b655c8a")
> helloWorld
{
address: "0xafebc7b4b1d0d8bef87c20d9ed6b1739589a6a69",
allEvents: function(),
message: function(),
setMessage: function()
}
helloWorld.message.call()
helloWorld.setMessage.sendTransaction("Greetings Blockchain", {from: eth.accounts[0]})
helloWorld.message.call()
Image for post
Image for post
The online solidity compiler is awesome.
Image for post
Image for post
Viewing your contract and it’s transactions in a blockchain browser

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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