test_amounts.py (5408B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 7 # external imports 8 from chainlib.eth.constant import ZERO_ADDRESS 9 from chainlib.eth.nonce import RPCNonceOracle 10 from chainlib.eth.tx import receipt 11 12 # local imports 13 from erc20_demurrage_token import DemurrageToken 14 15 # test imports 16 from erc20_demurrage_token.unittest import TestDemurrageDefault 17 18 logging.basicConfig(level=logging.DEBUG) 19 logg = logging.getLogger() 20 21 testdir = os.path.dirname(__file__) 22 23 class TestAmounts(TestDemurrageDefault): 24 25 def test_mints(self): 26 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 27 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 28 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000) 29 r = self.rpc.do(o) 30 o = receipt(tx_hash) 31 r = self.rpc.do(o) 32 self.assertEqual(r['status'], 1) 33 34 self.backend.time_travel(self.start_time + self.period_seconds) 35 36 (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) 37 r = self.rpc.do(o) 38 39 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 40 r = self.rpc.do(o) 41 balance = c.parse_balance(r) 42 self.assertEqual(balance, 980) 43 44 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000) 45 r = self.rpc.do(o) 46 47 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 48 r = self.rpc.do(o) 49 balance = c.parse_balance(r) 50 self.assert_within_lower(balance, 1980, 750) 51 52 self.backend.time_travel(self.start_time + self.period_seconds * 2) 53 54 (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) 55 r = self.rpc.do(o) 56 57 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 58 r = self.rpc.do(o) 59 balance = c.parse_balance(r) 60 61 expected_balance = ((1 - self.tax_level / 1000000) ** 10) * 1000 62 expected_balance += ((1 - self.tax_level / 1000000) ** 20) * 1000 63 #self.assert_within_lower(balance, expected_balance, 500) 64 self.assertEqual(balance, 1940) 65 66 def test_transfers(self): 67 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 68 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 69 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 2000) 70 r = self.rpc.do(o) 71 72 self.backend.time_travel(self.start_time + self.period_seconds) 73 74 (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) 75 r = self.rpc.do(o) 76 77 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 78 r = self.rpc.do(o) 79 balance = c.parse_balance(r) 80 self.assertEqual(balance, 1960) 81 82 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 83 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 84 (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[2], 500) 85 r = self.rpc.do(o) 86 87 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 88 r = self.rpc.do(o) 89 balance = c.parse_balance(r) 90 self.assertEqual(balance, 1460) 91 92 o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0]) 93 r = self.rpc.do(o) 94 balance = c.parse_balance(r) 95 self.assert_within_lower(balance, 500, 2000) 96 97 98 def test_dynamic_amount(self): 99 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 100 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 101 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 2000) 102 r = self.rpc.do(o) 103 104 cases = [ 105 (60, 1960), 106 (120, 1920), 107 (180, 1882), 108 (240, 1844), 109 (300, 1807), 110 (360, 1771), 111 (420, 1736), 112 (480, 1701), 113 (540, 1667), 114 (600, 1634), 115 ] 116 for case in cases: 117 self.backend.time_travel(self.start_time + int(case[0] * (self.period_seconds / 60))) 118 119 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 120 r = self.rpc.do(o) 121 balance = c.parse_balance(r) 122 self.assert_within_lower(balance, case[1], 10000) 123 124 125 def test_sweep(self): 126 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 127 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 128 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], 2000) 129 r = self.rpc.do(o) 130 131 (tx_hash, o) = c.sweep(self.address, self.accounts[0], self.accounts[1]) 132 self.rpc.do(o) 133 o = receipt(tx_hash) 134 r = self.rpc.do(o) 135 self.assertEqual(r['status'], 1) 136 137 o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0]) 138 r = self.rpc.do(o) 139 self.assertEqual(c.parse_balance(r), 0) 140 141 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 142 r = self.rpc.do(o) 143 self.assert_within(c.parse_balance(r), 2000, 1) 144 145 146 if __name__ == '__main__': 147 unittest.main()