Create demurrage calc from contract

This commit is contained in:
nolash 2021-07-04 12:10:01 +02:00
parent 00bb87e3ec
commit 98c460dc2f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
17 changed files with 103 additions and 13 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,14 @@
# # standard imports
import logging import logging
import datetime import datetime
import math import math
# eternal imports
from chainlib.eth.constant import ZERO_ADDRESS
# local imports
from .token import DemurrageToken
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
@ -36,3 +42,18 @@ class DemurrageCalculator:
logg.debug('adjusted for {} hours {} -> {} delta {}'.format(remainder_minutes, amount, adjusted_amount, adjusted_delta)) logg.debug('adjusted for {} hours {} -> {} delta {}'.format(remainder_minutes, amount, adjusted_amount, adjusted_delta))
return adjusted_amount return adjusted_amount
@staticmethod
def from_contract(rpc, chain_spec, contract_address, sender_address=ZERO_ADDRESS):
c = DemurrageToken(chain_spec)
o = c.tax_level(contract_address, sender_address=sender_address)
r = rpc.do(o)
taxlevel_i = c.parse_tax_level(r)
o = c.resolution_factor(contract_address, sender_address=sender_address)
r = rpc.do(o)
divider = c.parse_resolution_factor(r)
logg.debug('taxlevel {} f {}'.format(taxlevel_i, divider))
taxlevel_f = taxlevel_i / divider
return DemurrageCalculator(taxlevel_f)

View File

@ -343,7 +343,15 @@ class DemurrageToken(ERC20):
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
tx = self.finalize(tx, tx_format) tx = self.finalize(tx, tx_format)
return tx return tx
def tax_level(self, contract_address, sender_address=ZERO_ADDRESS):
return self.call_noarg('taxLevel', contract_address, sender_address=sender_address)
def resolution_factor(self, contract_address, sender_address=ZERO_ADDRESS):
return self.call_noarg('resolutionFactor', contract_address, sender_address=sender_address)
def actual_period(self, contract_address, sender_address=ZERO_ADDRESS): def actual_period(self, contract_address, sender_address=ZERO_ADDRESS):
return self.call_noarg('actualPeriod', contract_address, sender_address=sender_address) return self.call_noarg('actualPeriod', contract_address, sender_address=sender_address)
@ -507,3 +515,13 @@ class DemurrageToken(ERC20):
@classmethod @classmethod
def parse_get_distribution(self, v): def parse_get_distribution(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_tax_level(self, v):
return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_resolution_factor(self, v):
return abi_decode_single(ABIContractType.UINT256, v)

33
python/run_tests.sh Normal file
View File

@ -0,0 +1,33 @@
#!/bin/bash
set -e
export PYTHONPATH=.
#modes=(MultiNocap MultiCap SingleCap SingleNocap)
modes=(SingleCap SingleNocap) # other contracts need to be updted
for m in ${modes[@]}; do
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_basic.py
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_growth.py
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_amounts.py
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_single.py
done
modes=(SingleCap) # other contracts need to be updted
for m in ${modes[@]}; do
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_period.py
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_redistribution_unit.py
done
modes=(MultiCap SingleCap)
for m in ${modes[@]}; do
ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_cap.py
done
#modes=(MultiCap MultiNocap)
#for m in ${modes[@]}; do
# ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_remainder.py
# ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_redistribution.py
#done
set +e

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = erc20-demurrage-token name = erc20-demurrage-token
version = 0.0.2a1 version = 0.0.2a2
description = ERC20 token with redistributed continual demurrage description = ERC20 token with redistributed continual demurrage
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no

View File

@ -2,7 +2,11 @@
import datetime import datetime
import unittest import unittest
# external imports
from chainlib.eth.nonce import RPCNonceOracle
# local imports # local imports
from erc20_demurrage_token import DemurrageToken
from erc20_demurrage_token.demurrage import DemurrageCalculator from erc20_demurrage_token.demurrage import DemurrageCalculator
# test imports # test imports
@ -19,5 +23,13 @@ class TestEmulate(TestDemurrage):
self.assert_within_lower(a, 99.69667, 0.1) 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)
self.deploy(c, 'SingleNocap')
dc = DemurrageCalculator.from_contract(self.rpc, self.chain_spec, self.address, sender_address=self.accounts[0])
self.assertEqual(dc.r_min, 0.02)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -61,6 +61,9 @@ contract DemurrageTokenMultiCap {
// (this constant x 1000000 is contained within 128 bits) // (this constant x 1000000 is contained within 128 bits)
uint256 constant ppmDivider = 100000000000000000000000000000000; uint256 constant ppmDivider = 100000000000000000000000000000000;
// demurrage decimal width; 38 places
uint256 public immutable resolutionFactor = ppmDivider * 1000000;
// Timestamp of start of periods (time which contract constructor was called) // Timestamp of start of periods (time which contract constructor was called)
uint256 public immutable periodStart; uint256 public immutable periodStart;

View File

@ -56,6 +56,9 @@ contract DemurrageTokenMultiNocap {
// (this constant x 1000000 is contained within 128 bits) // (this constant x 1000000 is contained within 128 bits)
uint256 constant ppmDivider = 100000000000000000000000000000000; uint256 constant ppmDivider = 100000000000000000000000000000000;
// demurrage decimal width; 38 places
uint256 public immutable resolutionFactor = ppmDivider * 1000000;
// Timestamp of start of periods (time which contract constructor was called) // Timestamp of start of periods (time which contract constructor was called)
uint256 public immutable periodStart; uint256 public immutable periodStart;

View File

@ -56,7 +56,7 @@ contract DemurrageTokenSingleCap {
uint256 constant growthResolutionFactor = 1000000000000; uint256 constant growthResolutionFactor = 1000000000000;
// demurrage decimal width; 38 places // demurrage decimal width; 38 places
uint256 immutable resolutionFactor = nanoDivider * growthResolutionFactor; uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor;
// Timestamp of start of periods (time which contract constructor was called) // Timestamp of start of periods (time which contract constructor was called)
uint256 public immutable periodStart; uint256 public immutable periodStart;

View File

@ -53,7 +53,7 @@ contract DemurrageTokenSingleCap {
uint256 constant growthResolutionFactor = 1000000000000; uint256 constant growthResolutionFactor = 1000000000000;
// demurrage decimal width; 38 places // demurrage decimal width; 38 places
uint256 immutable resolutionFactor = nanoDivider * growthResolutionFactor; uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor;
// Timestamp of start of periods (time which contract constructor was called) // Timestamp of start of periods (time which contract constructor was called)
uint256 public immutable periodStart; uint256 public immutable periodStart;