Skip to content

Decentralized voting system based on NAS smart contract

Notifications You must be signed in to change notification settings

kun368/vote-dapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vote-Dapp: 去中心化投票系统

Build Status Language Language

基于NAS智能合约的去中心化投票系统, 构建去中心、公平、公开、公正的区块链投票系统

简介

该智能合约解决了投票场景中的不信任痛点。

Vote-Dapp不依赖特定的投票机构,它以利用特定的智能合约技术,使用整个P2P网络中众多节点构成的分布式数据库来确认并记录所有的投票行为,并使用密码学的设计来确投票的各个环节安全性。

其去中心化特性与算法本身可以确保防范作弊投票的现象,确保一人一票。

Snapshot

合约

"use strict";


var OptionVotes = function (text) {
  if (text) {
    var obj = JSON.parse(text);
    this.optionId = obj.optionId;
    this.votes = obj.votes;
  } else {
    this.optionId = '';
    this.votes = [];
  }
};

OptionVotes.prototype = {
  toString: function () {
    return JSON.stringify(this);
  }
};

var SuperDictionary = function () {
  LocalContractStorage.defineMapProperty(this, "repo", {
    parse: function (text) {
      return new OptionVotes(text);
    },
    stringify: function (o) {
      return o.toString();
    }
  });
};

SuperDictionary.prototype = {
  init: function () {
    // todo
  },

  voteFor: function (optionId) {
    var from = Blockchain.transaction.from;
    var value = Blockchain.transaction.value;


    var item = this.repo.get(optionId);
    if (!item) {
      item = new OptionVotes();
      item.optionId = optionId;
    }
    item.votes.push({
      from: from,
      value: value
    });
    this.repo.put(optionId, item);
  },

  queryOption: function (optionId) {
    var item = this.repo.get(optionId);
    if (!item) {
      throw new Error("deposit not exist.");
    }
    return item;
  },

  queryOptionsSize: function (optionIds) {
    var ids = optionIds.split(',');
    return ids.map((item, i) => {
      var res = this.repo.get(item);
      if (!res) {
        return 0;
      }
      return res.votes.length;
    })
  }
};
module.exports = SuperDictionary;

About

Decentralized voting system based on NAS smart contract

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published