evm-tokenvote

Voting machine using ERC20 tokens as votes.
Info | Log | Files | Refs | README

test_token.py (5665B)


      1 # standard imports
      2 import unittest
      3 import logging
      4 import os
      5 from chainlib.eth.nonce import RPCNonceOracle
      6 from chainlib.eth.tx import receipt
      7 from chainlib.eth.block import block_latest
      8 from hexathon import same as same_hex
      9 from eth_erc20 import ERC20
     10 from giftable_erc20_token import GiftableToken
     11 
     12 # local imports
     13 from evm_tokenvote.unittest import TestEvmVoteProposal
     14 from evm_tokenvote.unittest.base import hash_of_foo
     15 from evm_tokenvote import Voter
     16 from evm_tokenvote import ProposalState
     17 
     18 
     19 logging.basicConfig(level=logging.DEBUG)
     20 logg = logging.getLogger()
     21 
     22 
     23 class TestVoteToken(TestEvmVoteProposal):
     24 
     25     def test_withdraw(self):
     26         c = Voter(self.chain_spec)
     27         o = c.get_proposal(self.voter_address, 0, sender_address=self.accounts[0])
     28         r = self.rpc.do(o)
     29         proposal = c.parse_proposal(r)
     30 
     31         half_supply = self.initial_supply / 2
     32         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
     33         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     34         (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.alice, half_supply)
     35         self.rpc.do(o)
     36 
     37         nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn)
     38         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     39         (tx_hash, o) = c.approve(self.address, self.alice, self.voter_address, half_supply)
     40         self.rpc.do(o)
     41 
     42         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     43         (tx_hash, o) = c.vote(self.voter_address, self.alice, half_supply)
     44         self.rpc.do(o)
     45 
     46         c = ERC20(self.chain_spec)
     47         o = c.balance_of(self.voter_address, self.alice, sender_address=self.accounts[0])
     48         r = self.rpc.do(o)
     49         balance = int(r, 16)
     50         self.assertEqual(balance, half_supply)
     51 
     52         o = c.balance_of(self.address, self.alice, sender_address=self.accounts[0])
     53         r = self.rpc.do(o)
     54         balance = int(r, 16)
     55         self.assertEqual(balance, 0)
     56 
     57         nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn)
     58         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     59         (tx_hash, o) = c.withdraw(self.voter_address, self.alice)
     60         self.rpc.do(o)
     61         o = receipt(tx_hash)
     62         r = self.rpc.do(o)
     63         self.assertEqual(r['status'], 0)
     64 
     65         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     66         o = c.balance_of(self.voter_address, self.alice, sender_address=self.accounts[0])
     67         r = self.rpc.do(o)
     68         balance = int(r, 16)
     69         self.assertEqual(balance, half_supply)
     70 
     71         o = c.balance_of(self.address, self.alice, sender_address=self.accounts[0])
     72         r = self.rpc.do(o)
     73         balance = int(r, 16)
     74         self.assertEqual(balance, 0)
     75 
     76         c = Voter(self.chain_spec)
     77         o = c.get_proposal(self.voter_address, 0, sender_address=self.alice)
     78         r = self.rpc.do(o)
     79         proposal = c.parse_proposal(r)
     80         self.assertEqual(proposal.total, half_supply)
     81 
     82         o = block_latest()
     83         now_block_height = self.rpc.do(o)
     84         need_blocks = self.proposal_block_height + 100 - now_block_height + 1
     85         self.backend.mine_blocks(need_blocks)
     86 
     87         # after deadline withdraw is locked
     88         nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn)
     89         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     90         (tx_hash, o) = c.withdraw(self.voter_address, self.alice)
     91         self.rpc.do(o)
     92         o = receipt(tx_hash)
     93         r = self.rpc.do(o)
     94         self.assertEqual(r['status'], 0)
     95 
     96         o = c.get_proposal(self.voter_address, 0, sender_address=self.accounts[0])
     97         r = self.rpc.do(o)
     98         proposal = c.parse_proposal(r)
     99         self.assertEqual(proposal.total, half_supply)
    100 
    101         c = ERC20(self.chain_spec)
    102         o = c.balance_of(self.voter_address, self.alice, sender_address=self.accounts[0])
    103         r = self.rpc.do(o)
    104         balance = int(r, 16)
    105         self.assertEqual(balance, half_supply)
    106 
    107         o = c.balance_of(self.address, self.alice, sender_address=self.accounts[0])
    108         r = self.rpc.do(o)
    109         balance = int(r, 16)
    110         self.assertEqual(balance, 0)
    111 
    112         # scan + finalize unlocks tokens
    113         nonce_oracle = RPCNonceOracle(self.trent, conn=self.conn)
    114         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    115         (tx_hash, o) = c.scan(self.voter_address, self.trent, 0, 0)
    116         self.rpc.do(o)
    117 
    118         (tx_hash, o) = c.finalize_vote(self.voter_address, self.trent)
    119         self.rpc.do(o)
    120         o = receipt(tx_hash)
    121         r = self.rpc.do(o)
    122         self.assertEqual(r['status'], 1)
    123 
    124         nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn)
    125         c = Voter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    126         o = c.get_proposal(self.voter_address, 0, sender_address=self.accounts[0])
    127         r = self.rpc.do(o)
    128         proposal = c.parse_proposal(r)
    129         self.assertEqual(proposal.total, half_supply)
    130 
    131         (tx_hash, o) = c.withdraw(self.voter_address, self.alice)
    132         self.rpc.do(o)
    133         o = receipt(tx_hash)
    134         r = self.rpc.do(o)
    135         self.assertEqual(r['status'], 1)
    136 
    137         c = ERC20(self.chain_spec)
    138         o = c.balance_of(self.address, self.alice, sender_address=self.accounts[0])
    139         r = self.rpc.do(o)
    140         balance = int(r, 16)
    141         self.assertEqual(balance, half_supply)
    142 
    143         o = c.balance_of(self.voter_address, self.alice, sender_address=self.accounts[0])
    144         r = self.rpc.do(o)
    145         balance = int(r, 16)
    146         self.assertEqual(balance, 0)
    147 
    148 
    149 
    150 if __name__ == '__main__':
    151     unittest.main()