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.

The contract creator enters the description of the survey and the content of the four options. The general user sends their account number and the number he/she selected onto the contract. All users use the contract to view the results. 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.

csurvey : Enter a new survey add : Take part in survey result : View survey results

Table Configuration

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

Table with survey information and 4 options. Table that saves the survey participants and their selections. 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;

void csurvey()

{

print("your call to csurvey");

} /// @abi actionvoid csurvey()print("your call to csurvey");

void add()

{

print("your call to add");

} /// @abi actionvoid add()print("your call to add");

void result()

{

print("your call to result");

}

}; /// @abi actionvoid 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 std;

.

.

.

///

struct surveycon

{

account_name owner;

string question

string no1con;

string no2con;

string no3con;

string no4con; using namespace eosio;using namespace std;/// @abi table surveycon i64struct surveyconaccount_name owner;string questionstring 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.

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.



struct surveydetail

{

account_name who;

uint64_t selnum; /// @abi table surveydetail i64struct surveydetailaccount_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.

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.