Simple Vote with Choice

Hi all,

I am working on a modified voting architecture using Choice Coin on TestNet. My intention is to generate a simplified voting model, where a user inputs a Boolean value 1 or 0. If the user inputs a 1, then the vote function sends 1 Choice to a vote address, which aggregates all the votes. Below is the code block I am hacking with and here is a LINK to the full code on GitHub.

def vote():
    voter = input(str("Vote 0 for zero and vote 1 for one:"))
    params = algod_client.suggested_params()
    if voter is str('1'):
        # send one choice to address
        amount = 1
        txn = AssetTransferTxn(sender=creator_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
        signature = transaction.sign(voter_phrase)
        algod_client.send_transaction(signature)
        final = transaction.get_txid()
        return True, final
        print ("Thanks for voting for one.")
    else:
        # send zero choice to address
        print ("Thanks for voting for zero.")
vote()

Here, the function asks a voter for a 0 or 1 vote. If the vote is a 1, then it intends to send one Choice from the voter_address to the vote_address. But, I am consistently getting a network error, which is below.

Vote 0 for zero and vote 1 for one:1
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/algosdk/v2client/algod.py", line 82, in algod_request
    resp = urlopen(req)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Basic_Vote.py", line 44, in <module>
    vote()
  File "Basic_Vote.py", line 31, in vote
    params = algod_client.suggested_params()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/algosdk/v2client/algod.py", line 314, in suggested_params
    res = self.algod_request("GET", req, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/algosdk/v2client/algod.py", line 89, in algod_request
    raise error.AlgodHTTPError(e, code)
algosdk.error.AlgodHTTPError: invalid input: unable to parse base32 digest data 'txid': illegal base32 data at input byte 0

I’d appreciate any help or suggestions on hacking through this problem connecting to the network. I am using the PureStake API to connect, which has resolved similar urlopen errors for me in the past, but I am not sure this is primarily a urlopen error. The final line suggests a problem digesting the txid, but I am not sure how to account for this error output. Thanks in advance!

Hi bhaney, I’ve just answered this question in discord

1 Like

Hi @Bhanney Try using https://testnet-algorand.api.purestake.io/ps2 as your instead of the idx2 endpoint.

1 Like

This helped. I changed the API address and also the sender to the voter_address. The new error I am getting is below.

Vote 0 for zero and vote 1 for one:1
Traceback (most recent call last):
  File "Basic_Vote.py", line 44, in <module>
    vote()
  File "Basic_Vote.py", line 36, in vote
    signature = transaction.sign(voter_phrase)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/algosdk/future/transaction.py", line 141, in sign
    if not (self.sender == account.address_from_private_key(private_key)):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/algosdk/account.py", line 31, in address_from_private_key
    address = encoding.encode_address(pk)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/algosdk/encoding.py", line 196, in encode_address
    raise error.WrongKeyBytesLengthError
algosdk.error.WrongKeyBytesLengthError: key length in bytes must be 32
from algosdk import mnemonic
voter_phrase = mnemonic.to_private_key(phrase_sting_here)

@bhaney44

1 Like

This solved the error, but the functionality still is not working. So, the code runs clean, but it does not send 1 Choice when the user enters 1.

This says your phrase or private_key is an empty string “”>

Check the sender_address against the phrase to see if they match.

from algosdk.account import address_from_private_key

addr = address_from_private_key(mnemonic.to_private_key(phrase))
if address != voter_address:
    print("wrong phrase")

Then you have to check your amount

1 choice == 100

1 Like

change your amount to 100

You solved it with the last the comment. Thank you. Final code block.

def vote():
    voter = input(str("Vote 0 for zero and vote 1 for one:"))
    params = algod_client.suggested_params()
    if voter is str('1'):
        # send one choice to address
        amount = 1
        transaction = AssetTransferTxn(sender=voter_address, sp=params, receiver=vote_address, amt=amount, index=asset_id)
        signature = transaction.sign(voter_phrase)
        algod_client.send_transaction(signature)
        final = transaction.get_txid()
        print ("Thanks for voting for one.")
        print(final)
        return True, final
    else:
        # send zero choice to address
        print ("Thanks for voting for zero.")
vote()

TXID: DUBI5VFCBYKJ42UEADJUDIUTESM2CZXSTVU2I4KOUZGZB3W5PPHQ

1 Like

Yayyyy… still change the amount
to 100​:grin::+1:

1 Like

Done.
TXID: 5B4JC7YBRCDLAK6I2QLXOEYNRXNCHUHW5GAU7XXLMKJMN4G57OKQ

1 Like