Ethereum Blog

Ethereum in practice part 2: how to build a better democracy in under a 100 lines of code

Introduction

user

Alex Van de Sande

Alex Van de Sande is an UX designer at the Ethereum Foundation and lead of the Mist team.


LATEST POSTS

How to build server less applications for Mist 12th July, 2016

Ethereum in practice part 3: how to build your own transparent bank on the blockchain 07th December, 2015

release

Ethereum in practice part 2: how to build a better democracy in under a 100 lines of code

Posted on .

This is a second post on a series on how to use the current Ethereum tools to build smart contracts without needing programming experience. Download the latest version of the Ethereum Wallet, read the first part and let’s have fun!

Creating a token is fun, but what is the value of a token that doesn’t do anything new? We are now going to create a new contract that uses the tokens we just created. The contract will be a Democratic organization that lives on the blockchain and that anyone holding a share token will be able to vote on proposals.

So let’s go back to “Contracts” and then “Deploy Contract” and paste the DAO source code on the “Solidity Source” field. Choose the contract “Democracy” on the Picker and then select these parameters:

  • On the amount field you can add any ether amount you want your DAO to start with. Since you can send ether to it at any time in the future, if this is the first time you’ve been doing this then keep the amount at 0 and send the money later.
  • On the sharesAddress field, paste the address of the token contract you just created. Pay attention to the icon and color of the little circle that appears by the side of the address. If it doesn’t match exactly the one for the contract you created previously, then there’s an error.
  • On minimumSharesForVoting pick what is the minimum quorum of shareholders that need to vote on an issue before it passes. Here you must put the integer number of the minimum token possible, so if you created a token with 2 decimal places, putting 500 here will mean that in order for a proposal to be executed then the number of votes must be more than 5% of the total shares of the company.
  • minutesForDebating: this is the minimum time a proposal must be discussed and voted on before the results can be tallied up. Put a small number like 10 minutes if you want just to create something for testing, but put something like 20,000 if you want to store large amounts of ether, so all proposals must stay there for at least two weeks.

 

Your contract should be looking something like this:

Ethereum Wallet Screenshot 2015-12-03 at 3.50.36 PM 16

 

After a few seconds you’ll be redirected to a the dashboard where you’ll see your new contract being created:

Ethereum Wallet Screenshot 2015-12-03 at 3.50.36 PM 13

You can also see the link to the new contract on the “Contracts” tab. If it’s not there (for example: if you are adding a contract that you didn’t create) then you’ll have to add it manually. To add that you’ll need the contract’s address and it’s JSON interface, a string of text that explains to the wallet how to interact with the code. You can pick any name you want and press OK.

Ethereum Wallet Screen Shot 2015-12-03 at 9.57.34 AM

Click your new contract box and you’ll see a contract page. If you haven’t already deposited some ether on that contract, click on deposit and put some small amount of ether so you can test how this particular democracy works. If the contract already has some funds, click the “show contract info” button.

On the “read from contract” you can see all the functions you can execute for free on the contract, as they are just reading information from the blockchain. Here you can see, for instance, that the “sharesTokenAddress” is indeed the correct address or that there are currently 0 proposals on the contract. Let’s change that.

On the “Write to contract” you have a list of all the functions that will attempt to do some computation that saves data to the blockchain, and therefore will cost ether. Select “newProposal” and it will show all the options options for that function.

Ethereum Wallet Screen Shot 2015-12-01 at 6.10.32 PM

 

For “beneficiary” add the address of someone you want to send ether to, then put how many ethers you want on the “etherAmount” (must be an integer) and finally some text describing the reason you want to do this. Leave transactionByteCode blank for now. Click execute and type your password. After a few seconds the numProposals will increase to 1 and the first proposal, number 0, will appear on the left column. As you add more proposals, you can see any of them by simply putting the proposal number on the “proposals” field and you can read all about it.

Voting on a proposal is also very simple. Choose “vote” on the function picker. Type the proposal Number on the first box and check the “Yes” box if you agree with it (or leave it blank to vote against it). Click “execute” to send your vote.

 

Screen Shot 2015-12-01 at 6.18.22 PM

When the minimum voting time has passed, you can select “executeProposal”. If the proposal was simply sending ether, then you can also leave the “transactionBytecode” field blank. After pressing “execute” but before typing your password, pay attention to the screen that appears. If there is a warning on the “estimated fee consumption” field, then this means that for some reason the function called will not execute and will be abruptly terminated. It can mean many things, but in the context of this contract this warning will show up whenever you try to execute a contract before its deadline has passed, or if the user is trying to send a different bytecode data than the original proposal had. For security reasons if any of these things happens, the contract execution is abruptly terminated and the user that attempted the illegal transaction will lose the all the ether he sent to pay transaction fees.


Ethereum Wallet Screen Shot 2015-12-01 at 6.21.30 PM

If everything went well you should be able to see the results of the vote in a few seconds. The “openToVote” parameter on the first box will turn to false while the proposalPassed will reflect if the proposal has been accepted or not. You should also be able to see that the Ether balance of the contract will go down and the equivalent ether will be sent to the beneficiary of the ether you wanted to send.

Now take a moment to let that in: you just created an organization that only exists on the blockchain, that obeys votes based on completely digital tokens, but yet it can move real value around and create a very real impact on the world. Also notice that the organization is not under your control anymore: it will execute only the exact code you used to create it, forever. You can’t bribe it, you can’t earmark it and the same rules apply either you are moving 0.01 or 1,000,000 ethers.

Can it get any better than this? Actually, it can. On our next post we will explore how you can use “transactionBytecode” to allow the DAO to execute any kind of ethereum transaction, even owning or creating other contracts. We’ll also modify the token code to allow the DAO to control the amount of a token that exists on circulation and how to send it forward.

profile

Alex Van de Sande

Alex Van de Sande is an UX designer at the Ethereum Foundation and lead of the Mist team.

Comments
user

Author Nina Kilbride

Posted at 12:54 pm December 4, 2015.

Thanks again Alex – I’m going to try this today. I see how Ethereum is appropriate for broad-based machine arbitration and it starts with making high quality contracts. http://www.cryptolawyer.net

Reply
user

Author bruno cecchini

Posted at 10:05 pm December 4, 2015.

Cool stuff, the picture became clearer days by days !

Reply
user

Author Tommy Economics

Posted at 7:55 am December 5, 2015.

Hey Alex, thanks for providing this, it’s absolutely awesome. One thing however, I tried this numerous times and I can get everything to work but the last step — I am having trouble getting the contract to finish, the “opentovote” never turns to false, and and “proposalpassed” never turns to true. When I execute the contract, it’s also not giving me any errors or anything, so its acting like it executes.. it just never takes effect.

I did ensure I had a plentiful share of the token in my posession, as well as doing both amounts of “500” and “1” for the minimum amount required to pass the vote.

Any ideas what might fix it?

Thanks again, this is awesome.

Reply
user

Author Edmund John

Posted at 10:29 am December 5, 2015.

Another great tutorial and obviously this has many different real world applications.

“On our next post we will explore how you can use “transactionBytecode” to allow the DAO to execute any kind of ethereum transaction, even owning or creating other contracts. We’ll also modify the token code to allow the DAO to control the amount of a token that exists on circulation and how to send it forward.”

Looking forward to it Alex! Please keep writing these as they are extremely helpful.

Cheers,

Edmund John

http://Flagtheory.com
http://Incorporations.io
http://Passports.io

Reply
user

Author Joshua Davis

Posted at 2:07 pm December 5, 2015.

thank you again for these insights

Reply
user

Author BitcoinTalkRadio

Posted at 4:24 am December 7, 2015.

Very interesting. Thanks again Alex

Reply
user

Author Chris Römer

Posted at 3:03 pm December 9, 2015.

Alex, thanks for Part 2. Quick suggestion/feedback. On the “Wallets” tab, where the transactions are listed, it only always says “Contract Execution”. For the voting exercise, we do 3 steps: “newProposal”, “vote”, “executeProposal”. In this transaction list, I think it would be helpful to be able to see the specific function in order to follow the flow and logic and what has been done. Either right in the Header, next to where it currently just says “Contract Execution” or in the pop-up window when I click on a transaction (because the specific function is not shown there, either).

Reply
    user

    Author Alexandre Van de Sande

    Posted at 2:26 pm December 14, 2015.

    Yeah the whole transaction logging still leaves a lot to be desired.. We’ll improve on that.

    Reply
user

Author Arvind Tiwari

Posted at 10:45 pm December 11, 2015.

Really cool stuff Alex, thanks for posting it. Is there any way to buy some Ether? I would like to test it on main network as testnet is not working for me; I am unable to post the contract.

Thanks

Reply
    user

    Author Alexandre Van de Sande

    Posted at 2:25 pm December 14, 2015.

    We can’t really endorse exchanges but Kraken, Poloniex and Shapeshift do the bitcoin to ether thing. Personally I’m looking forward to btcrelay.org

    Reply
user

Author condensa42

Posted at 8:30 pm December 12, 2015.
    user

    Author Alexandre Van de Sande

    Posted at 2:24 pm December 14, 2015.

    What did you change? What wasn’t working before exactly?

    Thanks!

    Reply
      user

      Author condensa42

      Posted at 5:32 pm December 14, 2015.

      What did you change?
      add variables for debugging:

      struct Proposal {
      ..
      uint quorum;
      uint yea;
      uint nay;
      int result;
      ..
      }

      rewrite function executeProposal(uint proposalNumber, bytes transactionBytecode) returns (int result) {

      assigned the value (1,2,3) to a variable “result”.
      add last “else” in “if else if”
      assign the value to “global” variable “quorum”,”yea”,”nay” (only for debug)

      What wasn’t working before exactly?
      Probably because of you call ProposalTallied(proposalNumber, result, quorum, p.openToVote); with undefined variable “result”.

      logical (maybe) error:

      if (quorum > minimumQuorum && yea > nay ) {
      // has quorum and was approved
      p.recipient.call.value(p.amount*1000000000000000000)(transactionBytecode);
      p.openToVote = false;
      p.proposalPassed = true;
      } else if (quorum > minimumQuorum && nay > yea) {
      p.openToVote = false;
      p.proposalPassed = false;
      }
      state undefined for “quorum < minimumQuorum”.

      if (condition1) {
      block of code to be executed if condition1 is true
      } else if (condition2) {
      block of code to be executed if the condition1 is false and condition2 is true
      } else {
      block of code to be executed if the condition1 is false and condition2 is false
      }

      In the old code, in this case, nothing will happen.

      Reply
        user

        Author Felipe Aranguiz

        Posted at 6:12 am December 18, 2015.

        Thanks! I was trying to figure it out myself…

        How do I delete (stop seeing) the old contracts that didnt workout?

        Reply
        user

        Author Felipe Aranguiz

        Posted at 6:12 am December 18, 2015.

        Thanks! I was trying to figure it out myself…

        How do I delete (stop seeing) the old contracts that didnt workout?

        Reply
        user

        Author Felipe Aranguiz

        Posted at 6:28 am December 18, 2015.

        Also I would add to the vote function:

        if (now >= p.votingDeadline) throw;

        Reply
user

Author sherod

Posted at 10:03 am December 21, 2015.

I’m knew to this but as I understand it the fact that I voted for this proposal will be irreversibly recorded with my cryptographically unique ID?

Reply
user

Author Artem Shelukha

Posted at 11:31 pm January 21, 2016.

Hi Alex!
My wallet is crashing on startup. I tried 0.3.7 and 0.3.8 versions. Do the app write any logs?

Reply
user

Author GCM

Posted at 9:31 am January 25, 2016.

I sent 0.1 to the contract and now I can’t get it back? Why can you only transfer full integers?

Reply
    user

    Author Alexandre Van de Sande

    Posted at 2:18 pm January 26, 2016.

    I made this to simplify the contract, you can modify it to deal with wei which is a 1/10^18 of a single ether, but it might be harder to count the zeros on the wallet. You can always send another 0.9 to the contract and then get 1 back!

    Reply
      user

      Author J T

      Posted at 11:57 am February 27, 2016.

      This. I had less than one ether and then fought until I read this comment 🙂 Added some ether and saved what was “trapped”

      Reply
user

Author Udi Shapiro

Posted at 8:50 pm April 21, 2016.

Hi I have a more general question on online democracy. I have presented a vision on of internet democracy in Davos WEF 2016. (http://youtu.be/GgS9myPsGUw) and also wrote about it in the Financial Times (http://on.ft.com/1oeG0JM). But The main thing that bothers me with the vision I presented is the dependence on government-issued eIDs. This is not good for supporting freedom of association/assembly in non-democratic countries, and also not ideal in democratic countries that feel free to spy on any local organization/movement (presumably most democratic countries).

I hope that the Ethereum blockchain system may support a non-centralized alternative for verifiable biometric IDs based on bottom-up “networks of trust”. It may entail people vouching with money for the identities of other people. Anyone thinking about this?

Otherwise how can we create an online democracy?

Thanks,
Udi

Reply
user

Author Ross Perkins

Posted at 5:38 am May 5, 2016.

I’m a little late to the party but I’m trying to follow along. I took your advice “if this is the first time you’ve been doing this then keep the amount at 0 and send the money later” but then I forgot to add the money later.

The result is that after trying to vote, all of democracy 19f9 seems to have crumbled! 🙂

In case others get confused, I suggest perhaps making an edit saying “if you didn’t give your democracy any ether, do it now before you try to transfer ether out of it.”

Is it expected that the contract is greyed out and inaccessible now? As a non-technical user I would be confused why it’s in that state, having seen no error message or anything else. It just seems like now my democracy is gone.

PS- Thanks so much for this awesome post. I am enjoying playing with Ethereum and following your examples.

Reply
user

Author Ross Perkins

Posted at 6:15 pm May 5, 2016.

Hi Alex, one suggestion for making this easier for new users to follow.

After these sentences: When the minimum voting time has passed, you can select “executeProposal”. If the proposal was simply sending ether, then you can also leave the “transactionBytecode” field blank.

I suggest adding a line: If you are executing the new coin minting proposal, copy the bytecode from the coin minting contract into the “transactionBytecode” field.

Also further down in that paragraph, you may suggest to people that if they are indeed seeing the “not enough gas” error, to try various combinations of recipient and bytecode farther down the page in the “Check proposal code” area until they find a combination that says “Code checks out: YES”

Thanks again for the excellent example!

Reply
user

Author Amen

Posted at 10:30 am September 21, 2016.

Hey I am getting this error can you explain why this happen

Return value of low-level calls not used.
p.recipient.call.value(p.amount*1000000000000000000)(transactionBytecode);
^———————————————————————–^

Reply

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

View Comments (38) ...
Navigation