Skip to content

Latest commit

 

History

History
319 lines (245 loc) · 42.1 KB

tutorial.md

File metadata and controls

319 lines (245 loc) · 42.1 KB

Bamboo 0.0.01 Tutorial

This document covers

  • installation of the Bamboo compiler
  • compilation and deployment of a Bamboo program
  • interaction with the deployed Ethereum contract

Installing the Bamboo compiler

Make sure OCaml 4.04 is installed

$ ocaml --version
The OCaml toplevel, version 4.04.2

If a newer version is installed get the old one here

The Bamboo compiler can be installed with the OCaml package manager opam.

If you don't have it yet, get opam by

After installing opam, run opam init and follow the instructions.

When opam is ready, opam install bamboo gets you the Bamboo compiler.

$ bamboo --version
0.0.01

If anything goes wrong, poke @pirapira on the Gitter channel.

Compiling Bamboo code

Let's deploy this ERC20 contract on the Ropsten test network.

$ wget https://raw.githubusercontent.com/pirapira/bamboo/master/src/parse/examples/01b_erc20better.bbo

will get you a file 01b_erc20better.bbo. You can have a look now, but I'll explain the contents when we interact with the contract.

Now,

$ bamboo < 01b_erc20better.bbo > compiled.hex

should store something in compiled.hex file. You can have a look. If you use it for anything serious, you should investigate compiled.hex here. Since we are just playing on the test network, we go ahead deploying the compiled code.

Also,

$ bamboo --abi < 01b_erc20better.bbo > abi.json

will get you a JSON file abi.json that describes the interface of this Ethereum contract.

Deploying on the Ropsten test network

The Ropsten network is a test network for Ethereum. The balances and accounts are cleared sometimes. In this tutorial we deploy the code to Ropsten.

Connecting go-ethereum to the Ropsten network

We use go-ethereum. Install go-ethereum somehow and run

$ build/bin/geth console --testnet --fast --bootnodes "enode://20c9ad97c081d63397d7b685a412227a40e23c8bdc6688c6f37e97cfbc22d2b4d1db1510d8f61e6a8866ad7f0e17c02b14182d37ea7c3c8b9c2683aeb6b733a1@52.169.14.227:30303,enode://6ce05930c72abc632c58e2e4324f7c7ea478cec0ed4fa2528982cf34483094e9cbc9216e7aa349691242576d552a2a56aaeae426c5303ded677ce455ba1acd9d@13.84.180.240:30303"

When you are still seeing something like

INFO [08-02|14:00:28] Imported new chain segment               blocks=119 txs=324 mgas=130.952 elapsed=8.008s    mgasps=16.352 number=1375400 hash=cd3c3f…a39ed3

with blocks=119 or some big numbers, go-ethereum has not synced yet.

When it's synced, you will be seeing blocks=1

INFO [08-02|14:22:16] Imported new chain segment               blocks=1    txs=0    mgas=0.000   elapsed=13.958ms  mgasps=0.000   number=1417137 hash=8603a5…1de724

Creating an account

For deploying code on the Ropsten network, you need an account. If you don't have one, you can create it by typing

> personal.newAccount()

into the geth console.

Earning some Ropsten test Ether

For deploying code on the Ropsten network, you also need Ropsten Ether. You can either mine it, or get it from somebody else.

You can check your first account's balance by

> eth.getBalance(eth.accounts[0])
2991644430772416863

in the geth console. If it shows zero, there are ways to earn Ropsten Ether.

Option 1. mining

You can start mining Ropsten Ether

> miner.start(2)

Usually you will get some Ropsten Ether within some hours.

When you have some non-zero number, you can stop the mining by

> miner.stop()

on the geth console.

Option 2. asking

If you know somebody with Ropsten Ether, you can tell them your address created above, and ask them to send you some Ropsten Ether.

You can find your address by typing

> eth.accounts[0]

Preparing the code and the ABI

Now you can give a name to the hex code in compiled.hex.

> var code = "0x60606040527f0000000000000000000000000000000000000000000000000000000000000020806040805180920190528180380382397f0000000000000000000000000000000000000000000000000000000000003dd13814156002577f00000000000000000000000000000000000000000000000000000000000000025b821563000000fc57815181557f0000000000000000000000000000000000000000000000000000000000000020909203917f000000000000000000000000000000000000000000000000000000000000000101907f00000000000000000000000000000000000000000000000000000000000000200190630000007e565b505050600154630000010d57600180555b6300000003630000014857600154806300000003557f0000000000000000000000000000000000000000000000000000000000000001016001555b6300000004630000018357600154806300000004557f0000000000000000000000000000000000000000000000000000000000000001016001555b7f00000000000000000000000000000000000000000000000000000000000000237f0000000000000000000000000000000000000000000000000000000000000000557f0000000000000000000000000000000000000000000000000000000000003b9a80604080518092019052817f00000000000000000000000000000000000000000000000000000000000002178239f37f000000000000000000000000000000000000000000000000000000000000000054565b60606040527f00000000000000000000000000000000000000000000000000000000000000003560e060020a9004507f0000000000000000000000000000000000000000000000000000000000000075565b7f000000000000000000000000000000000000000000000000000000000000000254307f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000012c57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020557f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000020f57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000002a357507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455005b60606040527f00000000000000000000000000000000000000000000000000000000000000003560e060020a9004806318160ddd147f00000000000000000000000000000000000000000000000000000000000004a857806370a08231147f00000000000000000000000000000000000000000000000000000000000006da578063a9059cbb147f0000000000000000000000000000000000000000000000000000000000000997578063095ea7b3147f00000000000000000000000000000000000000000000000000000000000012e8578063dd62ed3e147f000000000000000000000000000000000000000000000000000000000000178e57806323b872dd147f0000000000000000000000000000000000000000000000000000000000001b45578063d96a094a147f0000000000000000000000000000000000000000000000000000000000002867578063d79875eb147f000000000000000000000000000000000000000000000000000000000000301057506002565b63000000043614156000577f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000058a57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000061e57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f0000000000000000000000000000000000000000000000000000000000000004557f00000000000000000000000000000000000000000000000000000000000000025460208060408051809201905291825290f35b63000000243614156000577f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f00000000000000000000000000000000000000000000000000000000000007bc57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000085057507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000097957507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205460208060408051809201905291825290f35b6300000044361415600057630000002435337f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000a3d57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205410156300000a55576002565b630000001035606060020a90043314156300000c9857333363ddf252ad630000002435602080604080518092019052918252909060006000500190a37f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000000b6857507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000bfc57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f35b630000002435337f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000d3357507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205403337f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000dd557507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000e8257507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054630000002435630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000f3557507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020540110156300000f4e576002565b630000002435630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000000ff557507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205401630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000010a357507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000001035606060020a90043363ddf252ad630000002435602080604080518092019052918252909060006000500190a37f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f00000000000000000000000000000000000000000000000000000000000011b857507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000124c57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f35b6300000044361415600057630000002435337f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000138e57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054101563000013a6576002565b630000001035606060020a900433141563000013c0576002565b630000002435630000001035606060020a9004337f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000146857507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020548063000015495750337f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000151157507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000001035606060020a900433638c5be1e5630000002435602080604080518092019052918252909060006000500190a37f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000165e57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000016f257507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f35b63000000443614156000577f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000187057507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000190457507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455630000003035606060020a9004630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000001a3a57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054806300001b275750630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000001aef57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205460208060408051809201905291825290f35b6300000064361415600057630000004435630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000001bf757507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205410156300001c0f576002565b63000000443533630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000001cb757507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054806300001da45750630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000001d6c57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205410156300001dbc576002565b630000003035606060020a9004630000001035606060020a90041415630000202357630000003035606060020a9004630000001035606060020a900463ddf252ad630000004435602080604080518092019052918252909060006000500190a37f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000001ef357507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000001f8757507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f35b630000004435630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000020ca57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205403630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000217857507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205563000000443533630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000222c57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020548063000023195750630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f00000000000000000000000000000000000000000000000000000000000022e157507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020540333630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f00000000000000000000000000000000000000000000000000000000000023c857507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020548063000024b55750630000001035606060020a90047f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000247d57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000004435630000003035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000256857507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205401630000003035606060020a90047f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000261657507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000003035606060020a9004630000001035606060020a900463ddf252ad630000004435602080604080518092019052918252909060006000500190a37f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000273757507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000027cb57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f35b6300000024361415600057630000000435307f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000290d57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205410156300002925576002565b337f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000029ba57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054630000000435337f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002a6157507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020540110156300002a7a576002565b3430310334630000000435307f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002b1a57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054030263000000043530310211156300002b3d576002565b630000000435307f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002bd857507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205403307f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002c7a57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000000435337f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002d2157507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205401337f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002dc357507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205533631cbc5ab1630000000435602080604080518092019052918252909034602080604080518092019052918252909060006000500190500190a27f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f0000000000000000000000000000000000000000000000000000000000002ee057507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f0000000000000000000000000000000000000000000000000000000000002f7457507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f35b630000004436141560005763000000243530311015630000302f576002565b630000000435337f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000030ca57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054101563000030e2576002565b307f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000317757507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054630000000435307f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000321e57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020540110156300003237576002565b341515156300003245576002565b3031630000002435303103630000000435307f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000032eb57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002054016300000024358102630000000435830210156300003314576002565b80307f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000033aa57507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b600052602052604060002055630000000435337f000000000000000000000000000000000000000000000000000000000000000354807f000000000000000000000000000000000000000000000000000000000000345157507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b60005260205260406000205403337f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000034f357507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b6000526020526040600020553363ed7a144f6300000004356020806040805180920190529182529090630000002435602080604080518092019052918252909060006000500190500190a26000546000805560008081818181630000002435336300000bb85a03f1156000579160005550507f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f000000000000000000000000000000000000000000000000000000000000000454807f000000000000000000000000000000000000000000000000000000000000363c57507f0000000000000000000000000000000000000000000000000000000000000004600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f000000000000000000000000000000000000000000000000000000000000000354807f00000000000000000000000000000000000000000000000000000000000036d057507f0000000000000000000000000000000000000000000000000000000000000003600154807f0000000000000000000000000000000000000000000000000000000000000001016001558091555b7f0000000000000000000000000000000000000000000000000000000000000002547f0000000000000000000000000000000000000000000000000000000000000002557f0000000000000000000000000000000000000000000000000000000000000003557f000000000000000000000000000000000000000000000000000000000000000455600160208060408051809201905291825290f360606040527f0000000000000000000000000000000000000000000000000000000000000020806040805180920190528180380382397f0000000000000000000000000000000000000000000000000000000000003dd13814156002577f00000000000000000000000000000000000000000000000000000000000000025b821563000000fc57815181557f0000000000000000000000000000000000000000000000000000000000000020909203917f000000000000000000000000000000000000000000000000000000000000000101907f00000000000000000000000000000000000000000000000000000000000000200190630000007e565b505050600154630000010d57600180555b6300000003630000014857600154806300000003557f0000000000000000000000000000000000000000000000000000000000000001016001555b6300000004630000018357600154806300000004557f0000000000000000000000000000000000000000000000000000000000000001016001555b7f00000000000000000000000000000000000000000000000000000000000000237f0000000000000000000000000000000000000000000000000000000000000000557f0000000000000000000000000000000000000000000000000000000000003b9a80604080518092019052817f00000000000000000000000000000000000000000000000000000000000002178239f360606040527f0000000000000000000000000000000000000000000000000000000000000020806040805180920190528180380382397f0000000000000000000000000000000000000000000000000000000000003dd13814156002577f00000000000000000000000000000000000000000000000000000000000000025b821563000000fc57815181557f0000000000000000000000000000000000000000000000000000000000000020909203917f000000000000000000000000000000000000000000000000000000000000000101907f00000000000000000000000000000000000000000000000000000000000000200190630000007e565b505050600154630000010d57600180555b6300000003630000014857600154806300000003557f0000000000000000000000000000000000000000000000000000000000000001016001555b6300000004630000018357600154806300000004557f0000000000000000000000000000000000000000000000000000000000000001016001555b7f000000000000000000000000000000000000000000000000000000000000032d7f0000000000000000000000000000000000000000000000000000000000000000557f0000000000000000000000000000000000000000000000000000000000003b9a80604080518092019052817f00000000000000000000000000000000000000000000000000000000000002178239f3"

Now console session remembers the code.

> code

should show the same string.

(If you don't want to copy-and-paste strings, probably you should look at the web3 library.)

Also, we can store the abi in abi.json.

var abi = [{"type": "constructor", "inputs":[{"name": "totalSupply", "type": "uint256"}], "name": "PreToken", "outputs":[], "payable": true},{"type":"fallback","inputs": [],"outputs": [],"payable": true},{"type":"event","inputs":[{"name":"_from","type":"address","indexed":true},{"name":"_to","type":"address","indexed":true},{"name":"_amount","type":"uint256","indexed":false}],"name":"Transfer"},{"type":"event","inputs":[{"name":"_buyer","type":"address","indexed":true},{"name":"_amount","type":"uint256","indexed":false},{"name":"_value","type":"uint256","indexed":false}],"name":"Buy"},{"type":"event","inputs":[{"name":"_buyer","type":"address","indexed":true},{"name":"_amount","type":"uint256","indexed":false},{"name":"_value","type":"uint256","indexed":false}],"name":"Sell"},{"type":"event","inputs":[{"name":"_owner","type":"address","indexed":true},{"name":"_spender","type":"address","indexed":true},{"name":"_value","type":"uint256","indexed":false}],"name":"Approval"},{"type":"function","name":"totalSupply","inputs": [],"outputs": [{"name": "", "type": "uint256"}],"payable": true},{"type":"function","name":"balanceOf","inputs": [{"name": "a", "type": "address"}],"outputs": [{"name": "", "type": "uint256"}],"payable": true},{"type":"function","name":"transfer","inputs": [{"name": "_to", "type": "address"},{"name": "_amount", "type": "uint256"}],"outputs": [{"name": "", "type": "bool"}],"payable": true},{"type":"function","name":"approve","inputs": [{"name": "_spender", "type": "address"},{"name": "_amount", "type": "uint256"}],"outputs": [{"name": "", "type": "bool"}],"payable": true},{"type":"function","name":"allowance","inputs": [{"name": "_owner", "type": "address"},{"name": "_spender", "type": "address"}],"outputs": [{"name": "", "type": "uint256"}],"payable": true},{"type":"function","name":"transferFrom","inputs": [{"name": "_from", "type": "address"},{"name": "_to", "type": "address"},{"name": "_amount", "type": "uint256"}],"outputs": [{"name": "", "type": "bool"}],"payable": true},{"type":"function","name":"buy","inputs": [{"name": "_amount", "type": "uint256"}],"outputs": [{"name": "", "type": "bool"}],"payable": true},{"type":"function","name":"sell","inputs": [{"name": "_amount", "type": "uint256"},{"name": "_value", "type": "uint256"}],"outputs": [{"name": "", "type": "bool"}],"payable": true}]

Preparing the initial arguments

When you deploy the code, the contract first becomes the PreToken contract.

contract PreToken
(uint256 totalSupply
,address => uint256 balances
,address => address => uint256 allowances
)
{
    <snip>
}

The PreToken contract has three arguments totalSupply, balances, and allowances. However, when you deploy the code, you can only specify totalSupply because the other arguments are mappings (TODO: document mappings and add a link here).

Let's say we want the totalSupply to be 2^30. The argument needs to be in hex (without 0x) and 64 characters (representing 32 bytes = 1 word in Ethereum).

var totalSupply = "0000000000000000000000000000000000000000000000000000000040000000"

You can check the length of totalSupply like this:

> totalSupply.length
64

If you have a different number, the contract will not be deployed.

When we concatenate the code and the argument, we get the initialization data.

var initdata = code + totalSupply

Unlocking the Account

In order to deploy code, you need an unlocked account with positive balance.

> personal.unlockAccount(eth.accounts[0])

Deploying the contract

> var tx = eth.sendTransaction({from: eth.accounts[0], data: initdata, gas: 4000000})

For the beginning, while the transaction is not included in the blockchain yet, you see

> eth.getTransactionReceipt(tx)
null

After a while, you will see something like

> eth.getTransactionReceipt(tx)
{
  blockHash: "0x00b34e48533b402b212b8278a06cd8455779af02eb213ca125e2cb0a67176d0c",
  blockNumber: 1417246,
  contractAddress: "0x624b4eab5c2dadc2e6db2e3517b0623d3bb15a68",
  cumulativeGasUsed: 3486372,
  from: "0xe64ae430b97ff403a194e214175c4144a82969f4",
  gasUsed: 3486372,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0xfeeace094af32de1843304a2e50942a20e86275e97566b42da82aa5cc0d56f91",
  to: null,
  transactionHash: "0x3a8bb995cfd33af80d3a65d29f5c6906bb6759cde65824988fea3bb5794caaf4",
  transactionIndex: 0
}

Now the Ethereum contract has been deployed on the Ropsten network!

Interacting with the Ethereum contract

The Ethereum contract is deployed at a certain address.

var contractAddress = eth.getTransactionReceipt(tx).contractAddress

You can get an object representing the Ethereum contract.

var contract = eth.contract(abi).at(contractAddress)

Now remember that the contract is currently a PreToken:

contract PreToken
(uint256 totalSupply
,address => uint256 balances
,address => address => uint256 allowances
)
{
    default
    {
        balances[this] = totalSupply;
        return then become Token(totalSupply, balances, allowances);
    }
}

As the source code reads, PreToken becomes a Token contract when it is called with enough gas.

Now let's just try to do that.

> eth.sendTransaction({from: eth.accounts[0], to: contractAddress, gas:3000000})

You might need to unlock the account.

After that, the contract becomes a Token contract.

contract Token
(uint256 totalSupply
,address => uint256 balances
,address => address => uint256 allowances
)
{
    case(uint256 totalSupply())
    {
        <snip>
    }
    case(uint256 balanceOf(address a))
    {
        <snip>
    }

    case(bool transfer(address _to, uint256 _amount))
    {
        <snip>
    }

    case(bool approve(address _spender, uint256 _amount))
    {
        <snip>
    }
    case(uint256 allowance(address _owner, address _spender))
    {
        <snip>
    }
    case(bool transferFrom(address _from, address _to, uint256 _amount))
    {
        <snip>
    }

    case(bool buy(uint256 _amount))
    {
        <snip>
    }
    case (bool sell(uint256 _amount, uint256 _value))
    {
        <snip>
    }
}

You don't have a balance in this ERC20 contract yet, so you can only buy the ERC20 token you have just created.

Say you want to buy 100 tokens with 500 wei.

> contract.buy(100, {from: eth.accounts[0], value: 500})

After a while, you will see the balance

> contract.balanceOf.call(eth.accounts[0])
100

Now you can use the other methods as well. Try transfer() perhaps.