diff --git a/python/erc20_demurrage_token/demurrage.py b/python/erc20_demurrage_token/demurrage.py index 52c9d7f..7941ee2 100644 --- a/python/erc20_demurrage_token/demurrage.py +++ b/python/erc20_demurrage_token/demurrage.py @@ -43,6 +43,15 @@ class DemurrageCalculator: return adjusted_amount + + def amount_since_slow(self, amount, timestamp): + delta = datetime.datetime.utcnow() - datetime.datetime.fromtimestamp(timestamp) + remainder_minutes = math.floor(delta.total_seconds() / 60) + adjusted_amount = amount * ((1 - self.r_min) ** remainder_minutes) + logg.debug('adjusted for {} minutes {} -> {} delta {}'.format(remainder_minutes, amount, adjusted_amount, amount - adjusted_amount)) + + return adjusted_amount + @staticmethod def from_contract(rpc, chain_spec, contract_address, sender_address=ZERO_ADDRESS): diff --git a/python/tests/test_demurrage_ext.py b/python/tests/test_demurrage_ext.py index 14426fc..2525f59 100644 --- a/python/tests/test_demurrage_ext.py +++ b/python/tests/test_demurrage_ext.py @@ -16,13 +16,19 @@ from tests.base import TestDemurrage class TestEmulate(TestDemurrage): def test_amount_since(self): - d = datetime.datetime.utcnow() - datetime.timedelta(seconds=29, hours=5, minutes=3, days=4) c = DemurrageCalculator(0.00000050105908373373) a = c.amount_since(100, d.timestamp()) self.assert_within_lower(a, 99.69667, 0.1) + def test_amount_since_slow(self): + d = datetime.datetime.utcnow() - datetime.timedelta(seconds=29, hours=5, minutes=3, days=4) + c = DemurrageCalculator(0.00000050105908373373) + a = c.amount_since_slow(100, d.timestamp()) + self.assert_within_lower(a, 99.69667, 0.1) + + def test_from_contract(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)