erc20-demurrage-token

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

test_redistribution_single.py (8189B)


      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 from chainlib.eth.block import (
     12         block_latest,
     13         block_by_number,
     14         )
     15 from chainlib.eth.address import to_checksum_address
     16 from hexathon import (
     17         strip_0x,
     18         add_0x,
     19         same as hex_same,
     20         )
     21 from dexif import from_fixed
     22 
     23 # local imports
     24 from erc20_demurrage_token import DemurrageToken
     25 
     26 # test imports
     27 from erc20_demurrage_token.unittest import TestDemurrageDefault
     28 
     29 logging.basicConfig(level=logging.INFO)
     30 logg = logging.getLogger()
     31 
     32 testdir = os.path.dirname(__file__)
     33 
     34 class TestRedistribution(TestDemurrageDefault):
     35 
     36 
     37     def test_redistribution_periods(self):
     38         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     39         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     40 
     41         demurrage = (1 - (self.tax_level / 1000000)) * (10**28)
     42         supply = self.default_supply
     43 
     44         (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], supply)
     45         self.rpc.do(o)
     46 
     47         for i in range(1, 100):
     48             logg.info('execute time travel to period {}'.format(i))
     49             self.backend.time_travel(self.start_time + (self.period_seconds * i))
     50             (tx_hash, o) = c.change_period(self.address, self.accounts[0])
     51             self.rpc.do(o)
     52             o = receipt(tx_hash)
     53             r = self.rpc.do(o)
     54             self.assertEqual(r['status'], 1)
     55 
     56             redistribution_value = 0
     57             for lg in r['logs']:
     58                 if hex_same(lg['topics'][0], '0x9a2a887706623ad3ff7fc85652deeceabe9fe1e00466c597972079ee91ea40d3'):
     59                     redistribution_value = int(strip_0x(lg['data']), 16)
     60 
     61             o = c.redistributions(self.address, i, sender_address=self.accounts[0])
     62             redistribution = self.rpc.do(o)
     63 
     64             o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0])
     65             r = self.rpc.do(o)
     66             demurrage = from_fixed(r)
     67 
     68             o = c.redistributions(self.address, i-1, sender_address=self.accounts[0])
     69             redistribution = self.rpc.do(o)
     70 
     71             o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0])
     72             r = self.rpc.do(o)
     73             demurrage_previous = from_fixed(r)
     74 
     75             o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
     76             r = self.rpc.do(o)
     77             balance_sink = c.parse_balance(r)
     78 
     79             o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
     80             r = self.rpc.do(o)
     81             balance_minter = c.parse_balance(r)
     82 
     83             logg.info('testing redistro {} sink {} mint {} adds up to {} supply {} with demurrage between {} and {}'.format(redistribution_value, balance_sink, balance_minter, balance_sink + balance_minter, supply, demurrage_previous, demurrage))
     84 
     85             self.assert_within(balance_minter + balance_sink, supply, 10)
     86 
     87 
     88     def test_redistribution_catchup_periods(self):
     89         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     90         c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     91 
     92         demurrage = (1 - (self.tax_level / 1000000)) * (10**28)
     93         supply = self.default_supply
     94 
     95         (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], supply)
     96         self.rpc.do(o)
     97 
     98         self.backend.time_travel(self.start_time + (self.period_seconds * 100))
     99 
    100         balance_minter = None
    101         balance_sink = None
    102         real_supply = None
    103 
    104         for i in range(1, 101):
    105             (tx_hash, o) = c.change_period(self.address, self.accounts[0])
    106             self.rpc.do(o)
    107             o = receipt(tx_hash)
    108             r = self.rpc.do(o)
    109             self.assertEqual(r['status'], 1)
    110 
    111             o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
    112             r = self.rpc.do(o)
    113             balance_sink = c.parse_balance(r)
    114 
    115             o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
    116             r = self.rpc.do(o)
    117             balance_minter = c.parse_balance(r)
    118             
    119             real_supply = balance_sink + balance_minter
    120             logg.info('period {} testing sink {} mint {} adds up to supply {} of original {} (delta {})'.format(i, balance_sink, balance_minter, real_supply, supply, supply - real_supply))
    121 
    122         i = 100
    123         o = c.redistributions(self.address, i, sender_address=self.accounts[0])
    124         redistribution = self.rpc.do(o)
    125 
    126         o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0])
    127         r = self.rpc.do(o)
    128         demurrage = c.parse_to_redistribution_item(r)
    129 
    130         o = c.redistributions(self.address, i-1, sender_address=self.accounts[0])
    131         redistribution = self.rpc.do(o)
    132 
    133         o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0])
    134         r = self.rpc.do(o)
    135         demurrage_previous = c.parse_to_redistribution_item(r)
    136 
    137         o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
    138         r = self.rpc.do(o)
    139         balance_sink = c.parse_balance(r)
    140 
    141         o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
    142         r = self.rpc.do(o)
    143         balance_minter = c.parse_balance(r)
    144 
    145         logg.debug('testing sink {} mint {} adds up to supply {} with demurrage between {} and {}'.format(balance_sink, balance_minter, real_supply, demurrage_previous, demurrage))
    146 
    147         self.assert_within_lower(balance_minter + balance_sink, supply, 0.1)
    148 
    149 
    150 #    def test_redistribution_boundaries(self):
    151 #        nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
    152 #        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    153 #
    154 #        demurrage = (1 - (self.tax_level / 1000000)) * (10**28)
    155 #        supply = self.default_supply
    156 #
    157 #        (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], supply)
    158 #        self.rpc.do(o)
    159 #
    160 #        o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
    161 #        r = self.rpc.do(o)
    162 #        balance = c.parse_balance(r)
    163 #        logg.debug('balance before {} supply {}'.format(balance, supply))
    164 #
    165 #        self.backend.time_travel(self.start_time + self.period_seconds)
    166 #        (tx_hash, o) = c.change_period(self.address, self.accounts[0])
    167 #        r = self.rpc.do(o)
    168 #
    169 #        o = receipt(tx_hash)
    170 #        r = self.rpc.do(o)
    171 #        self.assertEqual(r['status'], 1)
    172 #
    173 #        o = c.redistributions(self.address, 1, sender_address=self.accounts[0])
    174 #        r = self.rpc.do(o)
    175 #        oo = c.to_redistribution_supply(self.address, r, sender_address=self.accounts[0])
    176 #        rr = self.rpc.do(oo)
    177 #        oo = c.to_redistribution_demurrage_modifier(self.address, r, sender_address=self.accounts[0])
    178 #        rr = self.rpc.do(oo)
    179 #
    180 #        o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
    181 #        r = self.rpc.do(o)
    182 #        balance = c.parse_balance(r)
    183 #
    184 #        self.backend.time_travel(self.start_time + self.period_seconds * 2 + 1)
    185 #        (tx_hash, o) = c.change_period(self.address, self.accounts[0])
    186 #        r = self.rpc.do(o)
    187 #
    188 #        o = receipt(tx_hash)
    189 #        r = self.rpc.do(o)
    190 #        self.assertEqual(r['status'], 1)
    191 #
    192 #        o = c.redistributions(self.address, 2, sender_address=self.accounts[0])
    193 #        r = self.rpc.do(o)
    194 #        oo = c.to_redistribution_supply(self.address, r, sender_address=self.accounts[0])
    195 #        rr = self.rpc.do(oo)
    196 #        oo = c.to_redistribution_demurrage_modifier(self.address, r, sender_address=self.accounts[0])
    197 #        rr = self.rpc.do(oo)
    198 #
    199 #        o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
    200 #        r = self.rpc.do(o)
    201 #        balance = c.parse_balance(r)
    202 
    203 
    204 if __name__ == '__main__':
    205     unittest.main()