Coinmonks
Published in

Coinmonks

How to Make a Survey with an EOS Smart Contract (1)

Making a sample survey using a smart contract.

Greetings, this is Jaehyun Sim of ITAM Games. In this post, I will be giving an example of making a survey using a smart contract.

Keep in mind, the sample was made to maintain a level of simplicity and therefore may not be practical for real use, but the fixed requirements are all included.

To Start

First, let’s draw it out in our heads.

  1. The contract creator enters the description of the survey and the content of the four options.
  2. The general user sends their account number and the number he/she selected onto the contract.
  3. All users use the contract to view the results.
  4. General users will not be able to modify the results of the survey once they have selected.

The requirements have been structured while organizing, and it seems that there needs to be 3 in order to structure simply. Meaning 2~3 Tables.

Action Configuration

Action was configured as below.

  1. csurvey : Enter a new survey
  2. add : Take part in survey
  3. result : View survey results

Table Configuration

The table has also been configured into 3 parts as below.

  1. Table with survey information and 4 options.
  2. Table that saves the survey participants and their selections.
  3. Table that gathers and saves the results.

I don’t think there should be any problems if you mix 1 and 3, but to make sure the components are visible, I’ll separate in this example.

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;class survey : public eosio::contract
{
public:
using contract::contract;
/// @abi action
void csurvey()
{
print("your call to csurvey");
}
/// @abi action
void add()
{
print("your call to add");
}
/// @abi action
void result()
{
print("your call to result");
}
};
EOSIO_ABI( survey, (csurvey) (add) (result) )

Make a simple skeleton, and we will input a test code print(“your call to csurvey”) into each action. Compile the source and test after publishing. (Please refer to previous post for methods on compiling, publishing, and executing a contract)

You can see that each action is working without any problems.

Implementation

First, make a table that’ll be used on csurvey action

using namespace eosio;
using namespace std;
.
.
.
/// @abi table surveycon i64
struct surveycon
{
account_name owner;
string question
string no1con;
string no2con;
string no3con;
string no4con;
uint64_t primary_key() const {return owner;}EOSLIB_SERIALIZE(surveycon,(owner)(question)(no1con)(no2con)(no3con)(no4con))
};
typedef multi_index<N(surveycon),surveycon> _survey;

In order to use a string variable, first use the std namespace (using namespace std;). Then define the tables needed by the smart contract as structs. The tables must have a primary key for search. The primary key must not be a duplicate or empty. In this example, because we are going to do just one survey, use the primary key as the account of the smart contract, and the remaining data of survey, answer1, answer2, answer3, answer4 has been declared as string type.

Now implement the action csurvey feature.

/// @abi action
void csurvey(string question,string no1con,string no2con,string no3con,string no4con)
{
_survey survey(_self,_self);

survey.emplace(_self,[&](auto& surveycon)
{
surveycon.owner=_self;
surveycon.question=question;
surveycon.no1con=no1con;
surveycon.no2con=no2con;
surveycon.no3con=no3con;
surveycon.no4con=no4con;
});
return;
}

First, define the data to pass to action. We got the string data of the question that’s going to be inputted onto the table, and the answers to 1~4. Now enter the data from csurvey onto the table. For the sake of simple explanation, we did not go over the problem of key duplication.

Next, create the table users would save the voting information onto.

/// @abi table surveydetail i64
struct surveydetail
{
account_name who;
uint64_t selnum;
uint64_t primary_key() const {return who;}EOSLIB_SERIALIZE(surveydetail,(who)(selnum))
};
typedef multi_index<N(surveydetail),surveydetail> _items;

To store who saved what number, define the who variable and the selected number, selnum. I have used who (the account) as the primary key, and _items as the type.

Now implement the add action.

/// @abi action
void add(account_name user,uint64_t selnum)
{
require_auth(user);
_items item(_self,_self);item.emplace(_self,[&](auto& surveydetail){
surveydetail.who=user;
surveydetail.selnum=selnum;
});
return;
}

This code is no different than the csurvey action above. It may be strange because all we did was store onto the table that there was survey participation, but we’ll add the functionality in the following code. The require_auth (user) at the top of the code compares to verify if the execution rights of the contract are in line with the user. Because eosio allows you to call actions to the execution rights of multiple accounts, it is difficult to distinguish between the right to execute a contract and the right to execute an action. Therefore, we use the function to check whether the contract was executed with the necessary account execution rights after putting the name of the necessary account in the data of the action.

This content will continue on the next post due to its length.

ITAM Games is a blockchain platform for a transparent gaming ecosystem

Subscribe to ITAM Games and receive the latest info.

Visit the ITAM Games Telegram to communicate regarding ITAM Games and Blockchain. Join by clicking the link below! 👫

Website: https://itam.games
Telegram: https://t.me/itamgames

--

--

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