# Polling app
In this tutorial you will learn how to create a simple blockchain poll application. A user can sign in, create polls, cast votes and see voting results. Creating a poll will cost 200 tokens, voting is free, both actions will be available only for signed in users.
For this tutorial you will be using Starport (opens new window) v0.15.1, an easy to use tool for building blockchains. To install
/usr/local/bin, run the following command:
# Creating a blockchain
Run the following command to create a voter project:
app command will scaffold a project structure for your application in a
voter directory. Make sure to replace
username with your GitHub username.
voter directory you can see several files and directories:
appcontains files that connect all of the moving parts of your application.
cmdis responsible for the
voterddaemon, which respectively allow you to start your application and interact with it.
protocontains the protobuffer types of the blockchain application
vuecontains a web user interface for your app, reponsible for everything you see on the screenshot above.
xcontains the main building blocks of you app: modules. Right now we have only one:
The project's directory contains all the code required to build and launch a blockchain-based app. Try launching the app by running starport serve inside the project:
You should be able to see the following output - as well as any errors that might show up in your application.
starport serve --verbose to visualize detailed operations happening in the background
Congratulations! You now have a blockchain application running on your machine in just two commands. It doesn't do anything yet, add some transaction types next.
The voting applications has two types of entities: polls and votes. A poll is a type that has a
title and a list of
# Add a Poll Transaction
Open a new terminal in
voter directory and run the following:
This command generated code that handles the creation of
poll items. If you run
starport serve and visit http://localhost:8080 (opens new window) you will see a form for creating polls. It may take a short while to rebuild the app, so give it a couple of seconds.
Sign in with one of the passphrases printed in the console and try creating a poll. Click on
Access Wallet and then
Import existing wallet. Enter one of the passphrases that is in your console. Now you can give your wallet a name and password. The wallet can handle multiple accounts, give it a name in order to easier recognise wallets in the future. For this example, naming this wallet
voter would make sense.
You can find your newly created transaction type in the
Custom Type navigation point. Enter an example value for Title and Poll option to see the workflow.
You should see a new object created and displayed next to the form. You have successfully created an object and stored it on the blockchain!
This, however, does not look and work exactly like initially explained. You should be able to add more option fields (and store them as an array) and they should be displayed as interactive buttons.
Take a look at some of the files modified by the
starport type command.
# Modify the Protobuffer Types
To have multiple options, you need to change the value
string options in the Protobuffer definitions. Open the
proto/voter directory and look into the
Add the keyword
repeated before the options to allow passing an array of strings.
proto/voter/poll.proto file, modify the Poll message options field as follows:
proto/voter/tx.proto file you have the CRUD (Create, Read, Update and Delete) types for the poll transaction. Update the
options field for the messages
# Modify the Poll Transaction Message
Navigate to the file at
This file defines a message that creates a poll.
We need to make options to be stored as a list instead of a string. Replace
options string with
options string in the
To write anything to a blockchain or perform any other state transition a client (web app in our case) makes an HTTP POST request with a title and options to http://localhost:1317/voter/poll (opens new window) endpoint handler for which is defined in
x/voter/client/rest/txPoll.go. The handler creates an unsigned transaction which contains an array of messages. The client then signs the transaction and sends it to http://localhost:1317/txs (opens new window). The application processes the transaction by sending each message to a corresponding handler, in our case
x/voter/handler.go. A handler then calls a
CreatePoll function defined in
x/voter/keeper/poll.go which writes the poll data into the store.
# Modify the Poll Keeper
The keeper adds the polls to the blockchain database.
Navigate to the file at
x/voter/keeper/poll.go and change the
options parameter from
string in the
# Modify the REST Endpoint
The rest endpoint is defined in the file
Options string with
Options string in
Also further below in the
# Modify the CLI Transaction
A user will also be able to interact with our application through a command line interface.
The CLI definition is available at
This command will generate a transaction with "create poll" message, sign it using a private key of
alice (one of two users created by default) and broadcast it to the blockchain.
The modification we need to make is to change a line that reads arguments from the console.
In the function
msg is defined to read a string of argOptions, delete the stringification
We end up with the following function
Similar changes will need to be done for the function
And in the
CmdUpdatePoll we set
This will assume that all arguments after the first one represent a list of options.
You end up with the following function
In order for the app to recognize the changes that you have made, reset the application first before running it the next time
# Add the Votes
Up to now you have created a blockchain where users can create polls. Users will need to vote on the options of the poll. Create the code to cast votes on an existing poll.
A vote type transaction has the poll ID and an option - the string representation of the selected answer.
Now restart the application with
Remember, every time you reset the application state, you will have new passphrases. The reset restores all the data from your previously created state and you will receive new passphrases with new tokens. Mind to update your wallet accounts in the frontend once you reset the state of the blockchain.
Now that you have made all the necessary changes to the app, take a look at the client-side application.
# Front-end application
Starport has generated a basic front-end for the app. For convenience Vue.js (opens new window) framework is used with Vuex (opens new window) for state management, but since all features of the app are exposed through an HTTP API, clients can be built using any language or framework.
You will be mostly interested in
vue/src/views directory and the
vue/src/components directory. These directories contain the code for the page templates of our app.
vue/src/store/ handles sending transactions and receiving data from our blockchain and
@tendermint/vue (opens new window) directory, which contains components, like buttons and forms. It contains the generated protobuffer file definitions that were defined in the
vue/src/store/generated/username/voter/username.voter.voter/index.js you can see the generated transactions
MsgDeletePoll which use CosmJS (opens new window), a library for handling wallets, creating, signing and broadcasting transactions and define a Vuex store.
# Add your Module Component to the Frontend
Navigate to the views directory in
Since we don't need the default form component replace inside of
with two new components and a title
<script></script> tags below, import the component like this
Start creating the components.
# Create the PollForm Component
For the PollForm, create a new file
PollForm.vue in the
The component has a title and two buttons.
<script></script> tags below the javacsript code.
The Form manages the input of the user and broadcasts the transaction to the blockchain if the form gets submitted.
Refresh the page, sign in with a password and create a new poll. It takes a couple of seconds to process a transaction. Now, if you visit http://localhost:1317/voter/poll (opens new window) you should see a list of polls (this endpoint is defined in
# Create the Poll List Component
Create a new component
<script></script> tags below this:
PollList component lists every poll, including the options for that poll as buttons. Selecting an option triggers a
submit method that broadcasts a transaction with a "create vote" message and fetches data back from our application.
Two components are still missing from our App, to make it a bit better looking. Let's add
# Add the Options Component
<script></script> tag below this:
# Add the Poll List Text Component
<script></script> tags below this
Now in the
# Update the Frontend App
The App file handles the transactions of the components. Modify the script in
vue/src/App.vue to look the following
By now should be able to see the same UI as in the first screenshot. Try creating polls and casting votes. You may notice that it's possible to cast multiple votes for one poll. This is not what we want, so let's fix this behaviour.
# Access the API
To fix this issue you first have to understand how data is stored in our application.
Think of the data storage as a lexicographically ordered key value store. You can loop through the entries, filter by key prefix, add, update and delete entries. It is easier to visualize the store as JSON.
When you create a poll and cast on vote, this is the resulting JSON.
See the API and JSON output of your created Poll endpoint at http://localhost:1317/username/voter/voter/poll (opens new window)
For the votes you can go to the API page on http://localhost:1317/username/voter/voter/vote (opens new window)
The endpoint paths are defined by the username you use when bootstrapping the application with Starport, together with your module name.
Looking into this data, you can see the combination of
pollID is what we are looking for. Each account should only be allowed to have 1 vote per pollID.
# Limit to One Vote per User
The logic for access to a certain transaction should be in the
keeper directory. For the votes transaction logic, open the
msg_server_vote.go file at
x/voter/keeper/msg_server_vote.go and modify the
Now when you restart the app, in the frontend you should be able to only cast 1 vote per poll.
# Introducing a fee for creating polls
Add the logic for the transaction, that creating a poll costs 200 tokens.
We already require users to have accounts registered, and each user has tokens on balance. The only thing you need to do is to send coins from user's account to a module account before we create a poll.
# Add the Bank Keeper to the Module
First, load the
expected_keepers in the
This will define all the bank functions available in your module.
Second, add the keeper to the
Add it to the
type as well as the
NewKeeper function as follows:
Finally, add the bank module to the
app.go file in
Now you are ready to use all the bank functions that you added to the expected keepers file above. Next you will define how the transaction will require the funds in order to exectue the transaction.
# Modify the Message with the price
Modify the msg at
The fee payment happens before
k.AppendPoll. This way, if a user does not have enough tokens, the application will raise an error and will not proceed to creating a poll. Make sure to have
"github.com/tendermint/tendermint/crypto" added to the import statement (if your text editor didn't do that for you).
Now, restart the app and try creating several polls to see how this affects your token balance.
Congratulations, you have built a blockchain voting application.