test_expiry.py (3190B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 import datetime 7 8 # external imports 9 from chainlib.eth.constant import ZERO_ADDRESS 10 from chainlib.eth.nonce import RPCNonceOracle 11 from chainlib.eth.tx import receipt 12 from chainlib.eth.block import ( 13 block_latest, 14 block_by_number, 15 ) 16 17 # local imports 18 from erc20_demurrage_token import DemurrageToken 19 20 # test imports 21 from erc20_demurrage_token.unittest import TestDemurrageDefault 22 23 logging.basicConfig(level=logging.DEBUG) 24 logg = logging.getLogger() 25 26 testdir = os.path.dirname(__file__) 27 28 29 class TestExpire(TestDemurrageDefault): 30 31 def test_expires(self): 32 mint_amount = self.default_supply 33 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 34 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 35 36 for i in range(3): 37 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount) 38 r = self.rpc.do(o) 39 40 (tx_hash, o) = c.set_expire_period(self.address, self.accounts[0], 2) 41 r = self.rpc.do(o) 42 o = receipt(tx_hash) 43 r = self.rpc.do(o) 44 self.assertEqual(r['status'], 1) 45 46 o = c.expires(self.address, sender_address=self.accounts[0]) 47 r = self.rpc.do(o) 48 expiry_time = c.parse_expires(r) 49 50 self.backend.time_travel(expiry_time + 60) 51 o = block_latest() 52 r = self.rpc.do(o) 53 o = block_by_number(r) 54 r = self.rpc.do(o) 55 self.assertGreaterEqual(r['timestamp'], expiry_time) 56 57 nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc) 58 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 59 (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[2], 1) 60 r = self.rpc.do(o) 61 o = receipt(tx_hash) 62 r = self.rpc.do(o) 63 self.assertEqual(r['status'], 1) 64 65 (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[2], 1) 66 r = self.rpc.do(o) 67 o = receipt(tx_hash) 68 r = self.rpc.do(o) 69 self.assertEqual(r['status'], 0) 70 71 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 72 r = self.rpc.do(o) 73 balance = c.parse_balance(r) 74 self.assert_within(balance, 0.9604 * mint_amount, 1) 75 76 o = c.total_supply(self.address, sender_address=self.accounts[0]) 77 r = self.rpc.do(o) 78 supply = c.parse_balance(r) 79 80 (tx_hash, o) = c.change_period(self.address, self.sink_address) 81 r = self.rpc.do(o) 82 o = receipt(tx_hash) 83 r = self.rpc.do(o) 84 self.assertEqual(r['status'], 1) 85 86 o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) 87 r = self.rpc.do(o) 88 balance = c.parse_balance(r) 89 90 o = c.decay_by(self.address, supply, int((expiry_time - self.start_time) / 60), sender_address=self.sink_address) 91 r = self.rpc.do(o) 92 target_balance = c.parse_balance(r) 93 94 self.assert_within_lower(balance, supply - target_balance, 0.0001) 95 96 97 if __name__ == '__main__': 98 unittest.main()