Rehabilitate redistribution unit
This commit is contained in:
		
							parent
							
								
									555b0b1724
								
							
						
					
					
						commit
						34af3b1b30
					
				@ -1,4 +1,5 @@
 | 
			
		||||
from .token import (
 | 
			
		||||
        DemurrageToken,
 | 
			
		||||
        DemurrageTokenSettings,
 | 
			
		||||
        DemurrageRedistribution,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@ -306,18 +306,18 @@ class DemurrageToken(ERC20):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def to_redistribution(self, contract_address, participants, demurrage_modifier_ppm, value, period, sender_address=ZERO_ADDRESS, id_generator=None):
 | 
			
		||||
    def to_redistribution(self, contract_address, participants, demurrage_modifier, value, period, sender_address=ZERO_ADDRESS, id_generator=None):
 | 
			
		||||
        j = JSONRPCRequest(id_generator)
 | 
			
		||||
        o = j.template()
 | 
			
		||||
        o['method'] = 'eth_call'
 | 
			
		||||
        enc = ABIContractEncoder()
 | 
			
		||||
        enc.method('toRedistribution')
 | 
			
		||||
        enc.typ(ABIContractType.UINT256)
 | 
			
		||||
        enc.typ(ABIContractType.UINT256)
 | 
			
		||||
        enc.typ_literal('int128')
 | 
			
		||||
        enc.typ(ABIContractType.UINT256)
 | 
			
		||||
        enc.typ(ABIContractType.UINT256)
 | 
			
		||||
        enc.uint256(participants)
 | 
			
		||||
        enc.uint256(demurrage_modifier_ppm)
 | 
			
		||||
        enc.uint256(demurrage_modifier)
 | 
			
		||||
        enc.uint256(value)
 | 
			
		||||
        enc.uint256(period)
 | 
			
		||||
        data = add_0x(enc.get())
 | 
			
		||||
@ -536,7 +536,7 @@ class DemurrageToken(ERC20):
 | 
			
		||||
        enc = ABIContractEncoder()
 | 
			
		||||
        enc.method('getDistribution')
 | 
			
		||||
        enc.typ(ABIContractType.UINT256)
 | 
			
		||||
        enc.typ(ABIContractType.UINT256)
 | 
			
		||||
        enc.typ_literal('int128')
 | 
			
		||||
        enc.uint256(supply)
 | 
			
		||||
        enc.uint256(demurrage_amount)
 | 
			
		||||
        data = add_0x(enc.get())
 | 
			
		||||
@ -555,7 +555,7 @@ class DemurrageToken(ERC20):
 | 
			
		||||
        enc = ABIContractEncoder()
 | 
			
		||||
        enc.method('getDistributionFromRedistribution')
 | 
			
		||||
        v = strip_0x(redistribution)
 | 
			
		||||
        enc.typ_literal('(uint32,uint72,uint104)')
 | 
			
		||||
        enc.typ_literal('(uint32,uint72,uint64)')
 | 
			
		||||
        enc.bytes32(v[:64])
 | 
			
		||||
        enc.bytes32(v[64:128])
 | 
			
		||||
        enc.bytes32(v[128:192])
 | 
			
		||||
 | 
			
		||||
@ -114,6 +114,17 @@ class TestDemurrage(EthTesterCase):
 | 
			
		||||
        logg.debug('contract address {} start block {} start time {}'.format(self.address, self.start_block, self.start_time))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def assert_within(self, v, target, tolerance_ppm):
 | 
			
		||||
        lower_target = target - (target * (tolerance_ppm / 1000000))
 | 
			
		||||
        higher_target = target + (target * (tolerance_ppm / 1000000))
 | 
			
		||||
        #self.assertGreaterEqual(v, lower_target)
 | 
			
		||||
        #self.assertLessEqual(v, higher_target)
 | 
			
		||||
        if v >= lower_target and v <= higher_target:
 | 
			
		||||
            logg.debug('asserted within {} <= {} <= {}'.format(lower_target, v, higher_target))
 | 
			
		||||
            return
 | 
			
		||||
        raise AssertionError('{} not within lower {} and higher {}'.format(v, lower_target, higher_target))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def assert_within_lower(self, v, target, tolerance_ppm):
 | 
			
		||||
        lower_target = target - (target * (tolerance_ppm / 1000000))
 | 
			
		||||
        self.assertGreaterEqual(v, lower_target)
 | 
			
		||||
 | 
			
		||||
@ -19,9 +19,11 @@ from hexathon import (
 | 
			
		||||
 | 
			
		||||
# local imports
 | 
			
		||||
from erc20_demurrage_token import DemurrageToken
 | 
			
		||||
from erc20_demurrage_token.fixed import to_fixed
 | 
			
		||||
from erc20_demurrage_token import DemurrageRedistribution
 | 
			
		||||
 | 
			
		||||
# test imports
 | 
			
		||||
from erc20_demurrage_token.unittest.base import TestDemurrageUnit
 | 
			
		||||
from erc20_demurrage_token.unittest import TestDemurrageDefault
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(level=logging.DEBUG)
 | 
			
		||||
logg = logging.getLogger()
 | 
			
		||||
@ -29,7 +31,7 @@ logg = logging.getLogger()
 | 
			
		||||
testdir = os.path.dirname(__file__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestRedistribution(TestDemurrageUnit):
 | 
			
		||||
class TestRedistribution(TestDemurrageDefault):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # TODO: move to "pure" test file when getdistribution is implemented in all contracts
 | 
			
		||||
@ -40,11 +42,12 @@ class TestRedistribution(TestDemurrageUnit):
 | 
			
		||||
        demurrage = (1 - (self.tax_level / 1000000)) * (10**28)
 | 
			
		||||
        supply = self.default_supply
 | 
			
		||||
 | 
			
		||||
        o = c.get_distribution(self.address, supply, demurrage, sender_address=self.accounts[0])
 | 
			
		||||
        #o = c.get_distribution(self.address, supply, demurrage, sender_address=self.accounts[0])
 | 
			
		||||
        o = c.get_distribution(self.address, supply, to_fixed(self.tax_level / 1000000), sender_address=self.accounts[0])
 | 
			
		||||
        r = self.rpc.do(o)
 | 
			
		||||
        distribution = c.parse_get_distribution(r)
 | 
			
		||||
        expected_distribution = self.default_supply * (self.tax_level / 1000000)
 | 
			
		||||
        self.assert_within_lower(distribution, expected_distribution, 1000)
 | 
			
		||||
        self.assert_within(distribution, expected_distribution, 100)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def test_distribution_from_redistribution(self):
 | 
			
		||||
@ -53,18 +56,17 @@ class TestRedistribution(TestDemurrageUnit):
 | 
			
		||||
 | 
			
		||||
        demurrage = (1 - (self.tax_level / 100000)) * (10**28)
 | 
			
		||||
 | 
			
		||||
        logg.debug('demurrage {}'.format(demurrage))
 | 
			
		||||
        supply = self.default_supply
 | 
			
		||||
 | 
			
		||||
        o = c.to_redistribution(self.address, 0, demurrage, supply, 2, sender_address=self.accounts[0])
 | 
			
		||||
        o = c.to_redistribution(self.address, 0, to_fixed(self.tax_level / 1000000), supply, 2, sender_address=self.accounts[0])
 | 
			
		||||
        redistribution = self.rpc.do(o)
 | 
			
		||||
 | 
			
		||||
        o = c.get_distribution_from_redistribution(self.address, redistribution, self.accounts[0])
 | 
			
		||||
        r = self.rpc.do(o)
 | 
			
		||||
        distribution = c.parse_get_distribution(r)
 | 
			
		||||
        expected_distribution = (self.default_supply * self.tax_level) / 100000
 | 
			
		||||
        expected_distribution = (self.default_supply * (self.tax_level / 1000000))
 | 
			
		||||
        logg.debug('distribution {} supply {}'.format(distribution, self.default_supply))
 | 
			
		||||
        self.assert_within_lower(distribution, expected_distribution, 1000)
 | 
			
		||||
        self.assert_within(distribution, expected_distribution, 1000)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def test_single_step_basic(self):
 | 
			
		||||
@ -124,7 +126,7 @@ class TestRedistribution(TestDemurrageUnit):
 | 
			
		||||
        nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
 | 
			
		||||
        c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
 | 
			
		||||
 | 
			
		||||
        mint_amount = 100000000
 | 
			
		||||
        mint_amount = self.default_supply
 | 
			
		||||
        half_mint_amount = int(mint_amount / 2)
 | 
			
		||||
 | 
			
		||||
        (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount)
 | 
			
		||||
@ -156,7 +158,7 @@ class TestRedistribution(TestDemurrageUnit):
 | 
			
		||||
        o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
 | 
			
		||||
        r = self.rpc.do(o)
 | 
			
		||||
        balance = c.parse_balance(r)
 | 
			
		||||
        self.assertEqual(balance, expected_balance)
 | 
			
		||||
        self.assert_within(balance, expected_balance, 10)
 | 
			
		||||
 | 
			
		||||
        half_demurrage_amount = int((self.tax_level / 1000000) * half_mint_amount)
 | 
			
		||||
 | 
			
		||||
@ -177,14 +179,15 @@ class TestRedistribution(TestDemurrageUnit):
 | 
			
		||||
 | 
			
		||||
        o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
 | 
			
		||||
        redistribution = self.rpc.do(o)
 | 
			
		||||
        logg.debug('redistribution {}'.format(redistribution))
 | 
			
		||||
        o = c.to_redistribution_supply(self.address, redistribution, sender_address=self.accounts[0])
 | 
			
		||||
        r = self.rpc.do(o)
 | 
			
		||||
        supply = c.parse_to_redistribution_item(r)
 | 
			
		||||
        o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0])
 | 
			
		||||
        r = self.rpc.do(o)
 | 
			
		||||
        demurrage = c.parse_to_redistribution_item(r)
 | 
			
		||||
        logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply))
 | 
			
		||||
        #demurrage = c.parse_to_redistribution_item(r)
 | 
			
		||||
        #logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply))
 | 
			
		||||
        redistro_item = DemurrageRedistribution(redistribution)
 | 
			
		||||
        logg.debug('redistribution {}'.format(redistro_item))
 | 
			
		||||
 | 
			
		||||
        expected_balance = int(supply * (self.tax_level / 1000000))
 | 
			
		||||
        expected_balance_tolerance = 1
 | 
			
		||||
 | 
			
		||||
@ -298,14 +298,11 @@ contract DemurrageTokenSingleCap {
 | 
			
		||||
		return lastRedistribution;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	function getDistribution(uint256 _supply, int128 _demurrageAmount) public pure returns (uint256) {
 | 
			
		||||
	function getDistribution(uint256 _supply, int128 _demurrageAmount) public view returns (uint256) {
 | 
			
		||||
		int128 difference;
 | 
			
		||||
 | 
			
		||||
//		difference = _supply * (resolutionFactor - (_demurrageAmount * 10000000000));
 | 
			
		||||
//		return difference / resolutionFactor;
 | 
			
		||||
		difference = ABDKMath64x64.mul(ABDKMath64x64.fromUInt(_supply), _demurrageAmount);
 | 
			
		||||
		return ABDKMath64x64.toUInt(difference);
 | 
			
		||||
		//return _supply;
 | 
			
		||||
		difference = ABDKMath64x64.mul(ABDKMath64x64.fromUInt(_supply), ABDKMath64x64.sub(ABDKMath64x64.fromUInt(1), _demurrageAmount));
 | 
			
		||||
		return _supply - ABDKMath64x64.toUInt(difference);
 | 
			
		||||
			
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user