Playing With Ethereum

I’ve been skirting around Ethereum for awhile. Now and then I see a post about it in hacker news, or people circle jerking over the DAO. But it is pretty confusing. I decided to dive in yesterday and see what you can do with Ethereum. Smart contracts are only starting to make sense now that I’ve been playing with them.

This was my favorite intro article:
http://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#a-simple-smart-contract

This is the online IDE I used:
https://ethereum.github.io/browser-solidity/#version=soljson-latest.js

And I used the Wallet from the main website:
https://www.ethereum.org/

Ethereum

So if I understand it correctly. A bunch of people (miners) setup Ethereum nodes which are just computational machines running all over the world.

Normal people can then issue jobs to these machines and the world can see the results. It cost money (Ether/Gas) to run these jobs.

In my limited understanding there’s three different jobs.

  1. send money (Ether) to someone else, like bitcoin
  2. create smart contracts. These are bundles of codes that are “public” to everyone that can later be interacted with publicly
  3. interact with smart contracts (hey smart contract XXXX, go send 5 eth to this other contract)

Ether

To perform any actions, you need Ether. Which was a pain to get. I used coinbase to get some bitcoins, and then used the Shifty widget built into the Ethereum wallet. Probably not the most cost efficient way.

Contracts

This doc has some real contracts to help get you started. http://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#a-simple-smart-contract

If you don’t want to read the docs, here’s two simple contracts I created that do nothing important:

contract Stack {

// push(x): add an item on the top
// pop: remove the item at the top
// peek: return the item at the top (without removing it)
// size: return the number of items in the stack
// isEmpty: return whether the stack has no items

  string[] public stack;
  uint  _size;
  address public owner;

  function Stack(uint swag) {
    owner = msg.sender;
    _size = 0;
  }

  function push(string key) public {
    stack.push(key);
    _size += 1;
  }

  function pop() returns (string) {
    string val = stack[_size-1];
    delete stack[_size-1];
    _size -= 1;
    return val;
  }

  function peek() constant returns (string value) {
    return stack[_size-1];
  }

  function size() constant returns (uint) {
      return _size;
  }

  function isEmpty() constant returns (bool) {
      return (_size == 0);
  }

  function die() public {
      if (msg.sender == owner) {
          suicide(owner);
      }
  }
}

This is simply a stack data structure. Once you send it off to the blockchain, anyone can interact with the contract. My crappy code now exist all over the world!

This is what it looks like to deploy code to the blockchain:

73cba30eb880f29a51e71a921fb61c00.gif

And now I (and others) can call methods on this Stack implementation I just created. Here’s me calling “pop()” on the stack:

0ede134819d712ff389658b5d396ae04.gif

And this is all public. Everyone can now inspect the block chain and see that I called pop.

Interacting With Other Contracts

Contracts can also interact with each other. Here’s a simple “guestbook” that takes the address of the previous stack implementation, and allows people to push messages.

I added an admin function to remove the most previous entry off the stack (even though anyone can call the stack directly and call pop(), but you get the idea). You also need to include the code from the other contract so Solidity (the Ethereum programming language), knows how to interact with the remote address.


contract Stack{function Stack(uint256 swag);function die();function push(string key);function peek()constant returns(string value);function isEmpty()constant returns(bool );function owner()constant returns(address );function size()constant returns(uint256 );function pop()returns(string );function stack(uint256 )constant returns(string );}

contract Guestbook {
  address public ownerAddr;
  address public stackAddr;

  function Guestbook(address _stack) {
    ownerAddr = msg.sender;
    stackAddr = _stack;
  }

  function signGuestBook(string message) {
    Stack s = Stack(stackAddr);
    s.push(message);
  }

  function removeRecentEntry() {
    if (msg.sender == ownerAddr) {
      Stack s = Stack(stackAddr);
      s.pop();
    }
  }

  function die() public {
    if (msg.sender == ownerAddr) {
      suicide(ownerAddr);
    }
  }
}
//Previous Stack Contract:
// 0x700de3d287A6857D1653A0A804b838e131AB41E7

After we deploy this code we can call “Sign Guest Book” with the value of “Hawllo Wourld”:

Screen Shot 2016-06-22 at 8.25.21 PM.png


contract Stack{function Stack(uint256 swag);function die();function push(string key);function peek()constant returns(string value);function isEmpty()constant returns(bool );function owner()constant returns(address );function size()constant returns(uint256 );function pop()returns(string );function stack(uint256 )constant returns(string );}
contract Guestbook {
address public ownerAddr;
address public stackAddr;
function Guestbook(address _stack) {
ownerAddr = msg.sender;
stackAddr = _stack;
}
function signGuestBook(string message) {
Stack s = Stack(stackAddr);
s.push(message);
}
function removeRecentEntry() {
if (msg.sender == ownerAddr) {
Stack s = Stack(stackAddr);
s.pop();
}
}
function die() public {
if (msg.sender == ownerAddr) {
suicide(ownerAddr);
}
}
}
// Stack address 0x700de3d287A6857D1653A0A804b838e131AB41E7

view raw

guestbook.col

hosted with ❤ by GitHub

And then once this executes, we can go back to our stack contract, and see that it’s been updated!

Screen Shot 2016-06-22 at 8.25.56 PM.png

Magic!

Conclusion

The real uses of Ethereum actually do something useful, like provable odds in gambling, fair voting, kick starters with refunds.

Do I think Ethereum has a future? ¯\_(ツ)_/¯ #yolo