Vote Contract Document Description

1. Vote Information Contract Template

Vote information contract realizes the function of vote information recording. Vote information is saved in Blockchain and cannot be tampered. All the users can view vote information, which ensures the features of vote data such as security, authenticity and fairness, etc.

1.1 Vote Contract Function

  • Vote Information Published on Chain
  • View vote details
  • View vote information list

1.2 Vote Contract Deployment

Deploy VoteRecord contract. The contract constructor parameter is the developer's account address of LinkToken Pocket.

1.3 Use Method

1.3.1 Vote Information Published on Chain

The user calls record method to publish vote information on chain

Interface definition:

  //Vote information published on chain
  function record(
    bytes32 name,                    //vote activity name
    string rule,                     //vote rules
    bytes32[] candidates,            //candidate list
    uint[] voteNums                  //vote number list
    )
    onlyOwner external
    returns(uint)

Note: In input parameter, users should ensure the candidate list in descending sort of vote number, and the contract does not sequence; the type of vote activity name, candidate name, and voter in the contract is bytes32. The length of the name input should not exceed 32 bytes. Or else, the contract will automatically cut it off.

Call getLastestVote method to inquire ID of vote activity published on chain. During subsequent view and update of the vote activity, vote activity ID is required.

Interface definition:

  //Inquire the latest vote information
  function getLastestVote()
    view external
    returns(
uint,    //vote activity ID
string   //vote activity name
    )

If voters are too many, recordVoter method can be called to publish voter information on chain in batches. The number of voter information on chain each time should not exceed 100.

Interface definition:

  // Voter information published on chain. The number of voter information on chain each time should not exceed 100.
  function recordVoter(
uint voteID,                //vote activity ID
bytes32[] voters,           //voter list
bytes32[] candidates,       //vote candidate list
uint[] voteNums             //vote number
    )
    onlyOwner external
    returns(bool)

Note: The type of voter is bytes32. The developer can use mobile No., user ID and user name to identify the voter, and ensure the uniqueness of voter identification.

1.3.2 View Vote Information

Call getVoteNum method to view contract vote information number

Interface definition:

//view vote information number
 function getVoteNum()
    view external
    returns (uint)

Call getVoteList method to view vote information list

Interface definition:

  //inquire vote information list
  function getVoteList(
uint startIndex,  //inquire vote information starting position, from 0. startIndex is maller than vote information number returned by getVoteNum interface
uint num          //inquire vote information number. Inquiry volume per time does not exceed 500

  )
    view external
    returns(
uint[],     //vote activity ID list
bytes32[],  //vote activity name list
bool        //whether there is more vote information
    )

The user can call getVoteInfo to view vote information

Interface definition:

  //view vote details
  function getVoteInfo(
uint voteID                 //vote activity ID
  )
    view external
    returns(
bytes32,                    //vote activity name
string,                     //vote rules
bytes32[],                  //candidate list
uint[],                     //vote number list
uint                        //voter information number
    )

Call getVoterInfo to view voter information in batch. The number of voter information per time does not exceed 100

Interface definition:

   //vew voter information
  function getVoterInfo(
uint voteID,        //vote activity ID
uint startIndex,  //inquire vote information starting position, from 0. startIndex is maller than vote information number returned by interface getVoteNum.
uint num            //view voter information number
    )
    view external
    returns(
bytes32[],        //voter list
bytes32[],        //vote candidate list
uint[],           //vote number
bool        //whether there is more vote information
    )

2. Vote System Contract Template

The contract realizes the main functions of vote system composed of two contracts, namely control contract VoteControl and data contract VoteData. Control contract provides the access to the contract, and processes data contract and saves relevant data.

2.1 Vote Contract Function

  • Create vote activity
  • Set and view vote rules
  • Vote
  • View vote information
  • View vote result
  • View voter information

2.2 Vote Contract Deployment

  1. Deploy VoteData contract. The construction parameter of the contract is the developer's account address of LinkToken Pocket.
  2. Deploy VoteControl contract. The construction parameter of the contract is the developer's account address of LinkToken Pocket and VoteData contract address.
  3. Deploy setVoteControlAddr of VoteData contract to set VoteControl contract address.

2.3 Use Method

The developer or user carries out contract interaction by calling VoteControl contract method.

2.3.1 Create Vote

The developer calls newVote method to create vote activity.

Interface definition:

function newVote(
  string name          //vote acitivity name
  )
  onlyOwner external
  returns (uint)

Call getLastestVote method to inquire ID of vote activity published on chain. During subsequent view and update of the vote activity, vote activity ID is required.

Interface definition:

  //Inquire the latest vote information
  function getLastestVote()
    view external
    returns(
uint,    //vote activity ID
string   //vote activity name
    )

After creation, call setVoteRule method to set activity rules. The developer can input parameters to set the rules according to the needs.

Interface definition:

  // Set vote activity rules
  function setVoteRule(
uint voteID,            //vote activity ID
uint startTime,         //Unix time stamp of vote starting time
uint endTime,           // Unix time stamp of vote ending time
bool dayTimeLimit,      //Whether to restrict daily vote time. True: yes. False: no
uint dayStartTime,      //daily vote starting time, unit of hourly time
uint dayEndTime,        // daily vote ending time, unit of hourly time
uint dayVoteLimit,      //restriction of daily vote number
uint voteLimitPerUser,  //restriction of vote number of each account 
address[] blacklist,    //vote blacklist
bytes32[] candidates    //vote candidate. Total candidate number does not exceed 300
    )

Note: The type of candidate name in the contract is bytes32. The length of the name input should not exceed 32 bytes. Or else, the contract will automatically cut it off.

After calling setVoteRule method, the activity automatically takes effect.

2.3.2 Vote

The user calls vote method to vote. If the vote rules are met, the interface returns true and the vote succeeds.

Interface definition:

  // vote
  function vote(
uint voteID,         //vote activity ID
uint candidateIndex  //candidate Index. Compute from 0 in candidate list
    )
    isHuman external
    returns(
int,     //returned code, 0: succeeded, 1: activity has not started, 2: activity has ended
                          3: not in daily vote time frame, 4: total daily vote times reached upper limit
                          5: the user's vote times reach upper limit 6: blacklist user 7: selected candidate does not exist
      string   //error code information
    )

2.3.3 View Vote Result

In vote process or after the vote ends, the user calls getVoteResult method to view vote result.

Interface definition:

  // View vote result
  function getVoteResult(uint voteID)
    view external
    returns(
bytes32[],  //candidate
uint[]      //vote number gained by the candidate
    )

Note: The contract does not sequence the returned vote number data of the candidates. The developer or user is required to sequence the vote number data.

2.3.4 View Vote Information

The user calls getVoteInfo to view vote information

Interface definition:

  // view vote information
  function getVoteInfo(uint voteID)
    view public
    returns(
string,  //vote activity name
uint,     //voter number
uint,     //total vote number
uint,     //vote activity status
uint      //voter information number
    )

2.3.5 View Voter Information

The user calls getVoteInfo to view voter information. Since there may be too many voters, voter information should be inquired in batches. The inquiry volume per time does not exceed 100.

Interface definition:

  // view voter information
  function getVoter(
uint voteID,     //vote activity ID
uint startIndex, //voter starting position Index,
startIndex is smaller than voter information number returned by interface getVoteInfo
uint num         //view voter information number
    )
    view external
    returns(
address[],  //voter account address
bytes32[],  //candidate name
uint[],    //vote number
bool        //whether there is more voter information
    )

Note: In returned data, the corresponding candidate and vote number of the voter is matched in array index.