createSurvey(string question, string[] answers, uint256 expiration_time, uint256 response_cap, uint256 pooled_reward)
Creates a survey and adds the survey to the survey list. Returns true if the survey creation was successful and false otherwise.
register(String username)
Registers the caller if they are not already a registered user. If the username or address is already registered, return false. Returns true if the user was successfully registered.
surveyRespond(int surveyId, int answer)
People who wish to partipate in a survey do so using surveyRespond(). They input the surveyId of the survey they wish to respond to, as well as one of the numerical options they can respond with. The survey records the answer and increments the corresponding value in the survey's list of response counts. Each user can only respond to a survey once.
If someone successfully responds to a survey, return true. Else, if the user has already responded to the survey, the survey is not active, or the user did not input a valid response, return false.
getOwnSurveys()
As long as a registered user is logged in, they can call this function. getOwnSurveys() returns a list of survey IDs for each survey they own.
getActiveSurveys()
Gets a list of all survey IDs for currently active surveys.
getAnswerOptions(int surveyId)
Returns a string of all the answer options for the survey corresponding to the surveyId.
getSurveyQuestion(uint256 surveyId)
Returns the question of the survey that corresponds to the surveyId.
closeSurvey(int surveyId)
Closes survey, which is only allowed if the survey belongs to the caller and is not already closed, returns true if successfully closed. Return false if the survey does not successfully close.
addBalance()
Can only be called by a registered user. This function allows a user to deposit ETH from their personal balance into the global balance mapping of their address. This ETH is used to pay for creating surveys. The minimum deposit amount is 1 ether. Returns true if successful.
withdraw()
This withdraws all ETH from the global balance mapping of their address and deposits it back to their personal balance.
mapping(address => account): registeredUsers
Returns the account associated with the address of a registered user
mapping(uint256 => survey): surveyById
Returns the survey associated with its survey ID
mapping(address=>bool) isRegistered
Returns whether or not an address belongs to a registered user
mapping(string=>bool) isNameTaken
Returns whether or not a username is already taken
Survey[]: activeSurveys
The list of currently active surveys
uint256: HOST_CUT
The constant amount that the admin takes for each created survey.
We also added various checks in place to ensure that participants can not exploit the survey reward system by voting multiple times, voting in their own surveys, unauthorized withdraw of balances, and more. A more comprehensive depiction of our security measures can be found in our security test cases.
In order to run this application, the user must have Foundry installed, which can be done by following the steps here. The user can run the tests written for the application in a Linux environment by going to the directory that contains this program, then running
$forge test
When a user registers for an account, the application creates an Account data structure that contains their address, account name, and a list of IDs of all currently active surveys they have created.
Each survey is managed by its own survey data structure, which contains the address of the survey owner, the survey ID, its question and answer options, the maximum time and responses, and the pooled reward the survey owner has set. Each survey also keeps track of which blockchain addresses have responded, and how many votes each answer option has gotten.
Anyone with a blockchain address can vote in surveys, but without being a registered user, they can not create any. Survey participants will also receive a reward of ETH for participating in a survey. The amount they receive is determined by the survey owner. Registered users can create surveys as well as vote in them. Users can also determine how much ETH they intend to reward participants of their survey.