erc20-demurrage-token

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

test_demurrage.py.old (4052B)


      1 # standard imports
      2 import datetime
      3 import unittest
      4 import logging
      5 import os
      6 
      7 # external imports
      8 from chainlib.eth.nonce import RPCNonceOracle
      9 from chainlib.eth.tx import receipt
     10 
     11 # local imports
     12 from erc20_demurrage_token import DemurrageToken
     13 from erc20_demurrage_token.demurrage import DemurrageCalculator
     14 
     15 # test imports
     16 from erc20_demurrage_token.unittest import TestDemurrageDefault
     17 
     18 logging.basicConfig(level=logging.INFO)
     19 logg = logging.getLogger()
     20 
     21 
     22 class TestDemurragePeriods(TestDemurrageDefault):
     23 
     24     # verify that tax level calculation is in ppm as expected
     25     def test_ppm(self):
     26         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     27         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     28 
     29         (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply)
     30         r = self.rpc.do(o)
     31 
     32         self.backend.time_travel(self.start_time + 60)
     33 
     34         (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
     35         self.rpc.do(o)
     36         o = receipt(tx_hash)
     37         r = self.rpc.do(o)
     38         self.assertEqual(r['status'], 1)
     39 
     40         o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0])
     41         r = self.rpc.do(o)
     42         bob_bal = c.parse_balance(r)
     43 
     44         o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0])
     45         r = self.rpc.do(o)
     46         sink_bal = c.parse_balance(r)
     47 
     48         o = c.total_supply(self.address, sender_address=self.accounts[0])
     49         r = self.rpc.do(o)
     50         new_supply = c.parse_total_supply(r)
     51 
     52         balance_delta = self.default_supply * ((2 / 1000000) / 1000) 
     53         self.assertEqual(bob_bal, self.default_supply - balance_delta)
     54 
     55 
     56 
     57     # verify balances and supply after multiple demurrage periods
     58     def test_over_time(self):
     59         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     60         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     61 
     62         (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply)
     63         r = self.rpc.do(o)
     64 
     65         o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0])
     66         r = self.rpc.do(o)
     67         bob_bal = c.parse_balance(r)
     68         prev_bob_bal = bob_bal
     69 
     70         nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc)
     71         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     72 
     73         iterations = 100
     74 
     75         for i in range(1, iterations + 1):
     76             self.backend.time_travel(self.start_time + (self.period_seconds * i))
     77 
     78             (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[1], prev_bob_bal - bob_bal)
     79             r = self.rpc.do(o)
     80             o = receipt(tx_hash)
     81             r = self.rpc.do(o)
     82             self.assertEqual(r['status'], 1)
     83 
     84             (tx_hash, o) = c.apply_demurrage(self.address, self.sink_address)
     85             self.rpc.do(o)
     86             o = receipt(tx_hash)
     87             r = self.rpc.do(o)
     88             self.assertEqual(r['status'], 1)
     89 
     90             o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0])
     91             r = self.rpc.do(o)
     92             bob_bal = c.parse_balance(r)
     93 
     94             o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0])
     95             r = self.rpc.do(o)
     96             sink_bal = c.parse_balance(r)
     97 
     98             o = c.total_supply(self.address, sender_address=self.accounts[0])
     99             r = self.rpc.do(o)
    100             new_supply = c.parse_total_supply(r)
    101 
    102             logg.info('round {} supply {} balance sink {} bob {}'.format(i, new_supply, sink_bal, bob_bal))
    103 
    104         sum_supply = sink_bal + bob_bal
    105 
    106         bob_delta = self.default_supply * ((2 / 1000000) / 100)
    107 
    108         self.assert_within_lower(sum_supply, new_supply, 0.00001)
    109         self.assert_within_greater(bob_bal, self.default_supply - bob_delta, 0.001)
    110         self.assert_within_lower(sink_bal, bob_delta, 1000)
    111         
    112 
    113 if __name__ == '__main__':
    114     unittest.main()