Code
The Code for Decentralized Decisions provides several software structures for voting and autonomous governance.
Python
The main programming language used for Decentralized Decisions development is Python. Python is a general purpose interpreted programming language. However, other languages such as JavaScript may also be used for variations of the software.
Hash Function
1
# Hash function to secure data using quantum secure hash.
2
def hash(item):
3
# Assumes the default UTF-8.
4
# This encodes the string with the SHA-512 scheme.
5
hash_object = hashlib.sha512(item.encode())
6
# This returns the hexadecimal encode as a string.
7
item = hash_object.hexdigest()
8
return item
Copied!
The hash function converts a string to a secure code using the SHA-512 cryptographic scheme. SHA-512 is a post-quantum cryptographic scheme, thus ensuring that private information is made secure from malicious attackers.
Vote Functions
1
# The voting function allows the decision to happen.
2
def choice_vote(sender, key, receiver,amount,comment):
3
parameters = algod_client.suggested_params() # Sets suggested parameters
4
transaction = AssetTransferTxn(sender, parameters, receiver, amount, choice_id,note=comment)
5
# Defines an inital transaction for Choice Coin
6
signature = transaction.sign(key)
7
# Signs the transaction with the senders private key
8
algod_client.send_transaction(signature)
9
# Sends the transaction with the signature
10
final = transaction.get_txid()
11
return True, final
Copied!
The choice_vote function defines a stateless smart contract on the Algorand Network. It sends Choice Coin to the appropriate destination address based on user input.
1
def vote():
2
voter = input(str("Vote 0 for zero and vote 1 for one:"))
3
params = algod_client.suggested_params()
4
if voter is str('1'):
5
# send one choice to address
6
amount = 100
7
transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
8
signature = transaction.sign(voter_phrase)
9
algod_client.send_transaction(signature)
10
final = transaction.get_txid()
11
print ("Thanks for voting for one.")
12
print(final)
13
else:
14
# do not send one choice to address
15
print ("Thanks for voting for zero.")
Copied!
Here, the vote function uses a simple binary to send one Choice to an address with a positive vote, or takes no action with a negative vote. The amount is set to 100 to account for the two decimals places associate with the Choice asset. Below is another vote function, which uses two addresses instead of one.
1
def vote():
2
voter = input(str("Vote 0 for zero and vote 1 for one:"))
3
params = algod_client.suggested_params()
4
if voter is str('1'):
5
# send one choice to address
6
amount = 100
7
vote_address = ""
8
transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
9
signature = transaction.sign(voter_phrase)
10
algod_client.send_transaction(signature)
11
final = transaction.get_txid()
12
print ("Thanks for voting for one.")
13
print(final)
14
else:
15
amount = 100
16
vote_address = ""
17
transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
18
signature = transaction.sign(voter_phrase)
19
algod_client.send_transaction(signature)
20
final = transaction.get_txid()
21
print ("Thanks for voting for zero.")
22
print(final)
Copied!
Here, each option has an associated address. This allows for a simple count of each address to determine the total number of votes.
Results Functions
1
# The count_votes funciton counts the votes to determine the winner.
2
def count_votes():
3
yes_count = count(decision_one)
4
no_count = count(decision_two)
5
show_results(yes_count,no_count)
6
if yes_count > no_count:
7
return "The Voting Process has ended. Choice One had the most votes!"
8
else no_count > yes_count:
9
return "The Voting Process has ended. Choice Two had the most votes!"
Copied!
This function counts the total number of votes to return a statement regarding which candidate has won. This model may apply to both corporate or electoral voting.
1
def check_results():
2
asset_id = 42771692
3
address = ""
4
account_info = client.account_info(address)
5
assets = account_info.get("assets")
6
for asset in assets:
7
if asset['asset-id'] == asset_id:
8
amount = asset.get("amount")
9
print("Account {} has {}.".format(address, balance_formatter(amount, asset_id, client)))
10
return
11
print("Account {} must opt-in to Asset ID {}.".format(address, asset_id))
12
check_results()
Copied!
Here, the check_results function checks the amount the asset in the account, to determine the total number of votes for the particular address.
Links
Decentralized Decisions GitHub: https://github.com/ChoiceCoin/Voting
Decentralized Decisions White Paper: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3913316
Python GitHub: https://github.com/python
Last modified 26d ago
Copy link