test_redistribution_unit.py (7895B)
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 ( 11 block_latest, 12 block_by_number, 13 ) 14 from chainlib.eth.address import to_checksum_address 15 from hexathon import ( 16 strip_0x, 17 add_0x, 18 ) 19 from dexif import to_fixed 20 21 # local imports 22 from erc20_demurrage_token import DemurrageToken 23 from erc20_demurrage_token import DemurrageRedistribution 24 25 # test imports 26 from erc20_demurrage_token.unittest import TestDemurrageDefault 27 28 logging.basicConfig(level=logging.DEBUG) 29 logg = logging.getLogger() 30 31 testdir = os.path.dirname(__file__) 32 33 34 class TestRedistribution(TestDemurrageDefault): 35 36 37 38 # TODO: move to "pure" test file when getdistribution is implemented in all contracts 39 def test_distribution_direct(self): 40 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 41 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 42 43 demurrage = (1 - (self.tax_level / 1000000)) * (10**28) 44 supply = self.default_supply 45 46 #o = c.get_distribution(self.address, supply, demurrage, sender_address=self.accounts[0]) 47 o = c.get_distribution(self.address, supply, to_fixed(self.tax_level / 1000000), sender_address=self.accounts[0]) 48 r = self.rpc.do(o) 49 distribution = c.parse_get_distribution(r) 50 expected_distribution = self.default_supply * (self.tax_level / 1000000) 51 self.assert_within(distribution, expected_distribution, 100) 52 53 54 def test_distribution_from_redistribution(self): 55 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 56 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 57 58 demurrage = (1 - (self.tax_level / 100000)) * (10**28) 59 60 supply = self.default_supply 61 62 o = c.to_redistribution(self.address, 0, to_fixed(self.tax_level / 1000000), supply, 2, sender_address=self.accounts[0]) 63 redistribution = self.rpc.do(o) 64 65 o = c.get_distribution_from_redistribution(self.address, redistribution, self.accounts[0]) 66 r = self.rpc.do(o) 67 distribution = c.parse_get_distribution(r) 68 expected_distribution = (self.default_supply * (self.tax_level / 1000000)) 69 logg.debug('distribution {} supply {}'.format(distribution, self.default_supply)) 70 self.assert_within(distribution, expected_distribution, 1000) 71 72 73 def test_single_step_basic(self): 74 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 75 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 76 77 mint_amount = 100000000 78 79 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount) 80 self.rpc.do(o) 81 82 self.backend.time_travel(self.start_time + self.period_seconds) 83 84 (tx_hash, o) = c.change_period(self.address, self.accounts[0]) 85 self.rpc.do(o) 86 87 expected_balance = int(mint_amount - ((self.tax_level / 1000000) * mint_amount)) 88 89 o = c.balance_of(self.address, ZERO_ADDRESS, sender_address=self.accounts[0]) 90 r = self.rpc.do(o) 91 balance = c.parse_balance(r) 92 93 logg.debug('balance {}'.format(balance)) 94 95 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 96 r = self.rpc.do(o) 97 balance = c.parse_balance(r) 98 99 self.assertEqual(balance, expected_balance) 100 101 102 def test_single_step_multi(self): 103 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 104 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 105 106 mint_amount = 100000000 107 108 for i in range(3): 109 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount) 110 self.rpc.do(o) 111 112 self.backend.time_travel(self.start_time + self.period_seconds) 113 114 (tx_hash, o) = c.change_period(self.address, self.accounts[0]) 115 self.rpc.do(o) 116 117 expected_balance = int(mint_amount - ((self.tax_level / 1000000) * mint_amount)) 118 119 for i in range(3): 120 o = c.balance_of(self.address, self.accounts[i+1], sender_address=self.accounts[0]) 121 r = self.rpc.do(o) 122 balance = c.parse_balance(r) 123 self.assertEqual(balance, expected_balance) 124 125 126 def test_single_step_transfer(self): 127 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 128 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 129 130 mint_amount = self.default_supply 131 half_mint_amount = int(mint_amount / 2) 132 133 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount) 134 self.rpc.do(o) 135 136 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], mint_amount) 137 self.rpc.do(o) 138 139 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 140 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 141 (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[3], half_mint_amount) 142 self.rpc.do(o) 143 144 self.backend.time_travel(self.start_time + self.period_seconds) 145 146 (tx_hash, o) = c.change_period(self.address, self.accounts[1]) 147 self.rpc.do(o) 148 o = receipt(tx_hash) 149 r = self.rpc.do(o) 150 self.assertEqual(r['status'], 1) 151 152 # check that we have crossed into new period, this will throw if not 153 o = c.redistributions(self.address, 1, sender_address=self.accounts[0]) 154 self.rpc.do(o) 155 156 demurrage_amount = int((self.tax_level / 1000000) * mint_amount) 157 158 expected_balance = mint_amount - demurrage_amount 159 o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0]) 160 r = self.rpc.do(o) 161 balance = c.parse_balance(r) 162 self.assert_within(balance, expected_balance, 10) 163 164 half_demurrage_amount = int((self.tax_level / 1000000) * half_mint_amount) 165 166 expected_balance = half_mint_amount - half_demurrage_amount 167 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 168 r = self.rpc.do(o) 169 balance = c.parse_balance(r) 170 self.assertEqual(balance, expected_balance) 171 172 o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0]) 173 r = self.rpc.do(o) 174 balance = c.parse_balance(r) 175 self.assertEqual(balance, expected_balance) 176 177 o = c.total_supply(self.address, sender_address=self.accounts[0]) 178 r = self.rpc.do(o) 179 supply = c.parse_total_supply(r) 180 181 o = c.redistributions(self.address, 0, sender_address=self.accounts[0]) 182 redistribution = self.rpc.do(o) 183 o = c.to_redistribution_supply(self.address, redistribution, sender_address=self.accounts[0]) 184 r = self.rpc.do(o) 185 supply = c.parse_to_redistribution_item(r) 186 o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0]) 187 r = self.rpc.do(o) 188 #demurrage = c.parse_to_redistribution_item(r) 189 #logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply)) 190 redistro_item = DemurrageRedistribution(redistribution) 191 logg.debug('redistribution {}'.format(redistro_item)) 192 193 expected_balance = int(supply * (self.tax_level / 1000000)) 194 expected_balance_tolerance = 1 195 196 o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) 197 r = self.rpc.do(o) 198 balance = c.parse_balance(r) 199 self.assert_within_lower(balance, expected_balance, 1000) 200 201 202 if __name__ == '__main__': 203 unittest.main()