Add slow demurrage calc

This commit is contained in:
nolash 2021-07-04 14:52:12 +02:00
parent a6f53e7278
commit abe82949ea
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 16 additions and 1 deletions

View File

@ -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):

View File

@ -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)