erc20-demurrage-token

ERC20 token with redistributed continual demurrage
Info | Log | Files | Refs | README

test_single.py (3644B)


      1 import os
      2 import unittest
      3 import json
      4 import logging
      5 
      6 # external imports
      7 from chainlib.eth.constant import ZERO_ADDRESS
      8 from chainlib.eth.nonce import RPCNonceOracle
      9 from chainlib.eth.tx import receipt
     10 from chainlib.eth.block import block_latest
     11 from chainlib.eth.address import to_checksum_address
     12 from hexathon import (
     13         strip_0x,
     14         add_0x,
     15         )
     16 from dexif import to_fixed
     17 
     18 # local imports
     19 from erc20_demurrage_token import DemurrageToken
     20 
     21 # test imports
     22 from erc20_demurrage_token.unittest import TestDemurrageDefault
     23 
     24 logging.basicConfig(level=logging.DEBUG)
     25 logg = logging.getLogger()
     26 
     27 testdir = os.path.dirname(__file__)
     28 
     29 
     30 class TestRedistributionSingle(TestDemurrageDefault):
     31 
     32     def test_single_even_if_multiple(self):
     33         mint_amount = 100000000
     34 
     35         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     36         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     37 
     38         for i in range(3):
     39             (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount)
     40             r = self.rpc.do(o)
     41 
     42         external_address = to_checksum_address('0x' + os.urandom(20).hex())
     43         nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
     44         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     45         (tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, int(mint_amount) * 0.1)
     46         r = self.rpc.do(o)
     47 
     48         nonce_oracle = RPCNonceOracle(self.accounts[3], self.rpc)
     49         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     50         (tx_hash, o) = c.transfer(self.address, self.accounts[3], external_address, int(mint_amount) * 0.2)
     51         r = self.rpc.do(o)
     52 
     53         self.backend.time_travel(self.start_time + self.period_seconds + 1)
     54         (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[3])
     55         self.rpc.do(o)
     56         o = receipt(tx_hash)
     57         rcpt = self.rpc.do(o)
     58         self.assertEqual(rcpt['status'], 1)
     59 
     60         (tx_hash, o) = c.change_period(self.address, self.accounts[3])
     61         self.rpc.do(o)
     62         o = receipt(tx_hash)
     63         r = self.rpc.do(o)
     64         self.assertEqual(r['status'], 1)
     65 
     66         tax_modifier = 0.98
     67         o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
     68         r = self.rpc.do(o)
     69         balance = c.parse_balance(r)
     70         self.assertEqual(balance, int(mint_amount * tax_modifier))
     71 
     72         o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
     73         r = self.rpc.do(o)
     74         balance = c.parse_balance(r)
     75         base_amount = mint_amount - int(mint_amount * 0.1)
     76         self.assertEqual(balance, int(base_amount * tax_modifier)) #(base_amount - (base_amount * (self.tax_level / 1000000))))
     77 
     78         o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0])
     79         r = self.rpc.do(o)
     80         balance = c.parse_balance(r)
     81         base_amount = mint_amount - int(mint_amount * 0.2)
     82         self.assertEqual(balance, int(base_amount * tax_modifier)) #(base_amount - (base_amount * (self.tax_level / 1000000))))
     83 
     84         o = c.total_supply(self.address, sender_address=self.accounts[0])
     85         r = self.rpc.do(o)
     86         new_supply = c.parse_total_supply(r)
     87 
     88         o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
     89         r = self.rpc.do(o)
     90         balance = c.parse_balance(r)
     91         expected_balance = new_supply - (new_supply * tax_modifier)
     92         self.assert_within_lower(balance, expected_balance, 1)
     93 
     94 
     95 if __name__ == '__main__':
     96     unittest.main()